mcpField
A
azure-monitor-opentelemetry-exporter-java

azure-monitor-opentelemetry-exporter-java

github.com/sickn33クラウド・DevOps48.7k54 回インストール今日更新
リポジトリ

azure-monitor-opentelemetry-exporter-java について

Java の OpenTelemetry データを Azure Monitor と Application Insights に送るためのスキルです。エクスポーターの設定やスパン、メトリクスの記録を扱い、このパッケージが非推奨で `azure-monitor-opentelemetry-autoconfigure` への移行が推奨される点も示します。

主な機能

  1. 01OpenTelemetry データを Azure Monitor にエクスポート
  2. 02接続文字列による設定
  3. 03スパンの作成とカスタマイズ
  4. 04メトリクスと例外の記録

活用シーン

  1. 01Java アプリのテレメトリを Application Insights に送信
  2. 02トレースにスパンと属性を追加
  3. 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 とのより優れた統合
移行手順
  1. 依存関係を置き換えます。

    
    <dependency>
        <groupId>com.azure</groupId>
        <artifactId>azure-monitor-opentelemetry-exporter</artifactId>
    </dependency>
    
    
    <dependency>
        <groupId>com.azure</groupId>
        <artifactId>azure-monitor-opentelemetry-autoconfigure</artifactId>
    </dependency>
    
  2. 移行ガイドに従って初期化コードを更新します。

ベストプラクティス

  1. autoconfigure を使用する — azure-monitor-opentelemetry-autoconfigure に移行します
  2. 意味のあるスパン名を設定する — 操作の内容がわかる名前を使用します
  3. 関連する属性を追加する — デバッグに役立つコンテキストデータを含めます
  4. 例外を処理する — 必ずスパンに例外を記録します
  5. セマンティック規約を使用する — OpenTelemetry のセマンティック規約に従います
  6. finally でスパンを終了する — スパンが必ず終了するようにします
  7. try-with-resources を使用する — try-with-resources パターンでスコープを管理します

参考リンク

リソースURL
Maven パッケージhttps://central.sonatype.com/artifact/com.azure/azure-monitor-opentelemetry-exporter
GitHubhttps://github.com/Azure/azure-sdk-for-java/tree/main/sdk/monitor/azure-monitor-opentelemetry-exporter
移行ガイドhttps://github.com/Azure/azure-sdk-for-java/blob/main/sdk/monitor/azure-monitor-opentelemetry-exporter/MIGRATION.md
Autoconfigure パッケージhttps://central.sonatype.com/artifact/com.azure/azure-monitor-opentelemetry-autoconfigure
OpenTelemetry Javahttps://opentelemetry.io/docs/languages/java/
Application Insightshttps://learn.microsoft.com/azure/azure-monitor/app/app-insights-overview

使用する場合

このスキルは、概要に記載されているワークフローまたはアクションを実行する場合に適用できます。

制限事項

  • このスキルは、タスクが上記の説明の範囲に明確に該当する場合にのみ使用してください。
  • 出力を、環境固有の検証、テスト、または専門家によるレビューの代わりとして扱わないでください。
  • 必要な入力、権限、安全上の境界、または成功基準が不足している場合は、作業を中止して確認してください。

コメント