0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ECS上で実行されているGolangアプリケーションを監視する

Last updated at Posted at 2025-02-13

本記事はこちらのブログを参考にしています。
翻訳にはアリババクラウドのModelStudio(Qwen)を使用しております。

Golangアプリケーションの監視と評価の重要性

Golang開発者として、アプリケーションのパフォーマンスを追跡し評価することの価値を理解しています。分散システムやマイクロサービスアーキテクチャの人気が高まる中、強力な観測可能性ソリューションの実装は、Golangアプリケーションの安定性と全体的な健全性に依存する重要な要素です。以下では、OpenTelemetryを使用してアプリケーションを監視する手順を説明します。

OpenTelemetryとは?

OpenTelemetryは、観測可能性に関連するすべての基盤技術です。

ステップ1: Dockerコンテナの作成

Docker Composeファイルを作成してDockerコンテナを構築します。

コマンド:bash
mkdir golang
cd golang

docker-compose.yaml ファイルを作成します。yaml
version: '3'
services:
otel-collector:
image: otel/opentelemetry-collector-dev:latest
ports:
- "4317:4317"
- "55680:55680"
app:
build: .
ports:
- "8080:8080"

次に、Dockerfileを作成します。dockerfile
FROM golang:latest
WORKDIR /app
COPY . .
RUN go build -o main .
CMD ["./main"]

コンテナを実行してアプリケーションを起動します。bash
docker-compose up

ステップ2: GoアプリケーションでOpenTelemetryを実装する

以下のGoパッケージをインポートします。go
import (
"context"
"fmt"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/exporters/otlp"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp"
"go.opentelemetry.io/otel/sdk/resource"
"go.opentelemetry.io/otel/trace"
)

エクスポーターを適切に設定してデータを簡単に取得できるようにします。go
exporter, err := otlp.NewExporter(context.TODO(),
otlp.WithInsecure(),
otlp.WithEndpoint("http://otel-collector:4317"),
otlp.WithHTTPClient(otlptracehttp.NewClient()),
)
if err != nil {
log.Fatalf("Failed to create exporter: %v", err)
}
defer exporter.Shutdown(context.Background())

次に、トレーサーを初期化します。go
exporter, err := otlp.NewExporter(context.TODO(),
otlp.WithInsecure(),
otlp.WithEndpoint("http://otel-collector:4317"),
otlp.WithHTTPClient(otlptracehttp.NewClient()))
if err != nil {
log.Fatalf("Failed to create exporter: %v", err)
}
defer exporter.Shutdown(context.Background())

provider := otel.GetTracerProvider()
tracer := provider.Tracer("example")

トレースプロバイダーを作成し、エクスポーターを登録します。go
otel.SetTracerProvider(otel.NewTracerProvider(
otel.TracerProviderOptions{
Resource: resource.NewWithAttributes(
resource.Attributes{
"service.name": "my-service",
},
),
BatchExporter: exporter,
},
))

これで、スパンを作成した後に出力を確認できるようになります。go
ctx, span := otel.Tracer("my-component").Start(context.Background(), "my-operation")
defer span.End()

最も優れたGolang監視ツール

Golang監視におけるベストプラクティス

  • コードのインストルメンテーションを行う
  • 効果的なログ記録を実装する
  • 適切な監視ツールを選択する
  • 主要なメトリクスに基づくアラート設定を行う
  • 監視を自動化する

免責事項: 本記事で述べられている見解は参考用であり、必ずしもAlibaba Cloudの公式見解を表すものではありません。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?