azure-monitor-opentelemetry-exporter-java について
Java の OpenTelemetry データを Azure Monitor と Application Insights に送るためのスキルです。エクスポーターの設定やスパン、メトリクスの記録を扱い、このパッケージが非推奨で `azure-monitor-opentelemetry-autoconfigure` への移行が推奨される点も示します。
主な機能
- 01OpenTelemetry データを Azure Monitor にエクスポート
- 02接続文字列による設定
- 03スパンの作成とカスタマイズ
- 04メトリクスと例外の記録
活用シーン
- 01Java アプリのテレメトリを Application Insights に送信
- 02トレースにスパンと属性を追加
- 03計測対象の処理でエラーを記録
インストール
コマンドをコピーしてターミナルで実行します。
npx skills add https:/ / github.com/ sickn33/ agentic-awesome-skills --skill azure-monitor-opentelemetry-exporter-javaインストール後、Claude Code で/azure-monitor-opentelemetry-exporter-javaを入力して読み込まれたか確認します。
詳細
Java 用 Azure Monitor OpenTelemetry エクスポーター
⚠️ 非推奨のお知らせ:このパッケージは非推奨です。
azure-monitor-opentelemetry-autoconfigureに移行してください。詳細な手順については、移行ガイドを参照してください。
OpenTelemetry のテレメトリデータを Azure Monitor/Application Insights にエクスポートします。
インストール(非推奨)
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-monitor-opentelemetry-exporter</artifactId>
<version>1.0.0-beta.x</version>
</dependency>
推奨:代わりに Autoconfigure を使用する
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-monitor-opentelemetry-autoconfigure</artifactId>
<version>REPLACE_WITH_REVIEWED_COMPATIBLE_VERSION</version>
</dependency>
環境変数
APPLICATIONINSIGHTS_CONNECTION_STRING=InstrumentationKey=xxx;IngestionEndpoint=https://xxx.in.applicationinsights.azure.com/
バージョンとテレメトリのスコープ
リンク先の公式移行ガイド/パッケージドキュメントから、実際に互換性のあるバージョンを確認してください。プレースホルダーのバージョンは、ビルド可能な依存関係ではありません。エクスポーターを設定しても、すべてのアプリケーションライブラリが計装されるとは限りません。固定したインテグレーションでトレース/メトリクス/ログがエクスポートされることを確認し、SDK を重複して初期化しないでください。機密性の高い属性や例外情報をマスキングし、承認済みのテレメトリ送信先を使用してください。また、アプリケーションのライフサイクルを通じて SDK のシャットダウン/フラッシュを管理してください。
Autoconfigure を使用した基本設定(推奨)
環境変数を使用する場合
import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk;
import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdkBuilder;
import io.opentelemetry.api.OpenTelemetry;
import com.azure.monitor.opentelemetry.exporter.AzureMonitorExporter;
// Connection string from APPLICATIONINSIGHTS_CONNECTION_STRING env var
AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder();
AzureMonitorExporter.customize(sdkBuilder);
OpenTelemetry openTelemetry = sdkBuilder.build().getOpenTelemetrySdk();
接続文字列を明示的に指定する場合
AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder();
AzureMonitorExporter.customize(sdkBuilder, "{connection-string}");
OpenTelemetry openTelemetry = sdkBuilder.build().getOpenTelemetrySdk();
スパンの作成
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.context.Scope;
// Get tracer
Tracer tracer = openTelemetry.getTracer("com.example.myapp");
// Create span
Span span = tracer.spanBuilder("myOperation").startSpan();
try (Scope scope = span.makeCurrent()) {
// Your application logic
doWork();
} catch (Throwable t) {
span.recordException(t);
throw t;
} finally {
span.end();
}
スパン属性の追加
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
Span span = tracer.spanBuilder("processOrder")
.setAttribute("order.id", "12345")
.setAttribute("customer.tier", "premium")
.startSpan();
try (Scope scope = span.makeCurrent()) {
// Add attributes during execution
span.setAttribute("items.count", 3);
span.setAttribute("total.amount", 99.99);
processOrder();
} finally {
span.end();
}
カスタム SpanProcessor
import io.opentelemetry.sdk.trace.SpanProcessor;
import io.opentelemetry.sdk.trace.ReadWriteSpan;
import io.opentelemetry.sdk.trace.ReadableSpan;
import io.opentelemetry.context.Context;
private static final AttributeKey<String> CUSTOM_ATTR = AttributeKey.stringKey("custom.attribute");
SpanProcessor customProcessor = new SpanProcessor() {
@Override
public void onStart(Context context, ReadWriteSpan span) {
// Add custom attribute to every span
span.setAttribute(CUSTOM_ATTR, "customValue");
}
@Override
public boolean isStartRequired() {
return true;
}
@Override
public void onEnd(ReadableSpan span) {
// Post-processing if needed
}
@Override
public boolean isEndRequired() {
return false;
}
};
// Register processor
AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = AutoConfiguredOpenTelemetrySdk.builder();
AzureMonitorExporter.customize(sdkBuilder);
sdkBuilder.addTracerProviderCustomizer(
(sdkTracerProviderBuilder, configProperties) ->
sdkTracerProviderBuilder.addSpanProcessor(customProcessor)
);
OpenTelemetry openTelemetry = sdkBuilder.build().getOpenTelemetrySdk();
ネストされたスパン
public void parentOperation() {
Span parentSpan = tracer.spanBuilder("parentOperation").startSpan();
try (Scope scope = parentSpan.makeCurrent()) {
childOperation();
} finally {
parentSpan.end();
}
}
public void childOperation() {
// Automatically links to parent via Context
Span childSpan = tracer.spanBuilder("childOperation").startSpan();
try (Scope scope = childSpan.makeCurrent()) {
// Child work
} finally {
childSpan.end();
}
}
例外の記録
Span span = tracer.spanBuilder("riskyOperation").startSpan();
try (Scope scope = span.makeCurrent()) {
performRiskyWork();
} catch (Exception e) {
span.recordException(e);
span.setStatus(StatusCode.ERROR, e.getMessage());
throw e;
} finally {
span.end();
}
メトリクス(OpenTelemetry 経由)
import io.opentelemetry.api.metrics.Meter;
import io.opentelemetry.api.metrics.LongCounter;
import io.opentelemetry.api.metrics.LongHistogram;
Meter meter = openTelemetry.getMeter("com.example.myapp");
// Counter
LongCounter requestCounter = meter.counterBuilder("http.requests")
.setDescription("Total HTTP requests")
.setUnit("requests")
.build();
requestCounter.add(1, Attributes.of(
AttributeKey.stringKey("http.method"), "GET",
AttributeKey.longKey("http.status_code"), 200L
));
// Histogram
LongHistogram latencyHistogram = meter.histogramBuilder("http.latency")
.setDescription("Request latency")
.setUnit("ms")
.ofLongs()
.build();
latencyHistogram.record(150, Attributes.of(
AttributeKey.stringKey("http.route"), "/api/users"
));
主な概念
| 概念 | 説明 |
|---|---|
| 接続文字列 | インストルメンテーションキーを含む Application Insights の接続文字列 |
| トレーサー | 分散トレーシング用のスパンを作成します |
| スパン | 時間情報と属性を持つ作業単位を表します |
| SpanProcessor | カスタマイズのためにスパンのライフサイクルをインターセプトします |
| エクスポーター | テレメトリを Azure Monitor に送信します |
Autoconfigure への移行
azure-monitor-opentelemetry-autoconfigure パッケージには、次の機能があります。
- 一般的なライブラリの自動計装
- 簡素化された設定
- OpenTelemetry SDK とのより優れた統合
移行手順
-
依存関係を置き換えます。
<dependency> <groupId>com.azure</groupId> <artifactId>azure-monitor-opentelemetry-exporter</artifactId> </dependency> <dependency> <groupId>com.azure</groupId> <artifactId>azure-monitor-opentelemetry-autoconfigure</artifactId> </dependency> -
移行ガイドに従って初期化コードを更新します。
ベストプラクティス
- autoconfigure を使用する —
azure-monitor-opentelemetry-autoconfigureに移行します - 意味のあるスパン名を設定する — 操作の内容がわかる名前を使用します
- 関連する属性を追加する — デバッグに役立つコンテキストデータを含めます
- 例外を処理する — 必ずスパンに例外を記録します
- セマンティック規約を使用する — OpenTelemetry のセマンティック規約に従います
- finally でスパンを終了する — スパンが必ず終了するようにします
- try-with-resources を使用する — try-with-resources パターンでスコープを管理します
参考リンク
使用する場合
このスキルは、概要に記載されているワークフローまたはアクションを実行する場合に適用できます。
制限事項
- このスキルは、タスクが上記の説明の範囲に明確に該当する場合にのみ使用してください。
- 出力を、環境固有の検証、テスト、または専門家によるレビューの代わりとして扱わないでください。
- 必要な入力、権限、安全上の境界、または成功基準が不足している場合は、作業を中止して確認してください。