mcpField
A
azure-monitor-opentelemetry-exporter-py

azure-monitor-opentelemetry-exporter-py

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

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

カスタム OpenTelemetry パイプラインや詳細なテレメトリ制御が必要な Python 開発者向けの低レベルエクスポーターです。トレース、メトリクス、ログを Application Insights に送信し、オフラインストレージ、サンプリング、Azure AD 認証を設定できます。

主な機能

  1. 01トレース、メトリクス、ログのエクスポート
  2. 02カスタム OpenTelemetry パイプラインへの対応
  3. 03オフライン保存と再試行
  4. 04サンプリングと Azure AD 認証

活用シーン

  1. 01Python サービスのテレメトリを Application Insights に送信
  2. 02カスタムテレメトリパイプラインの構築
  3. 03通信中断に備えたテレメトリの一時保存

インストール

コマンドをコピーしてターミナルで実行します。

$npx skills add https://github.com/sickn33/agentic-awesome-skills --skill azure-monitor-opentelemetry-exporter-py

インストール後、Claude Code で/azure-monitor-opentelemetry-exporter-pyを入力して読み込まれたか確認します。

詳細

Azure Monitor OpenTelemetry Exporter for Python

Application Insights に OpenTelemetry のトレース、メトリクス、ログを送信するための低レベルエクスポーターです。

インストール

pip install azure-monitor-opentelemetry-exporter

環境変数

export APPLICATIONINSIGHTS_CONNECTION_STRING='InstrumentationKey=xxx;IngestionEndpoint=https://xxx.in.applicationinsights.azure.com/'

使用する場面

シナリオ使用するもの
手早くセットアップして自動計装するazure-monitor-opentelemetry(ディストリビューション)
カスタムの OpenTelemetry パイプラインazure-monitor-opentelemetry-exporter(このパッケージ)
テレメトリをきめ細かく制御するazure-monitor-opentelemetry-exporter(このパッケージ)

エクスポートとプライバシーの境界

テレメトリは Azure に送信され、オフラインストレージにローカル保存される場合もあります。フィールド、エンドポイント、認証情報、保持期間、コストを確認し、エクスポート前にシークレットと個人データをマスキングしてください。グローバルプロバイダーを繰り返しリセットするのではなく、既存のプロバイダーを再利用し、プロセス終了時にプロバイダーをフラッシュしてシャットダウンしてください。以下の個別のスニペットはそれぞれ代替手段であり、まとめて実行する初期化スクリプトではありません。

トレースエクスポーター

from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
from azure.monitor.opentelemetry.exporter import AzureMonitorTraceExporter

# Create exporter
exporter = AzureMonitorTraceExporter(
    connection_string="InstrumentationKey=xxx;..."
)

# Configure tracer provider
trace.set_tracer_provider(TracerProvider())
trace.get_tracer_provider().add_span_processor(
    BatchSpanProcessor(exporter)
)

# Use tracer
tracer = trace.get_tracer(__name__)
with tracer.start_as_current_span("my-span"):
    print("Hello, World!")

メトリクスエクスポーター

from opentelemetry import metrics
from opentelemetry.sdk.metrics import MeterProvider
from opentelemetry.sdk.metrics.export import PeriodicExportingMetricReader
from azure.monitor.opentelemetry.exporter import AzureMonitorMetricExporter

# Create exporter
exporter = AzureMonitorMetricExporter(
    connection_string="InstrumentationKey=xxx;..."
)

# Configure meter provider
reader = PeriodicExportingMetricReader(exporter, export_interval_millis=60000)
metrics.set_meter_provider(MeterProvider(metric_readers=[reader]))

# Use meter
meter = metrics.get_meter(__name__)
counter = meter.create_counter("requests_total")
counter.add(1, {"route": "/api/users"})

ログエクスポーター

import logging
from opentelemetry._logs import set_logger_provider
from opentelemetry.sdk._logs import LoggerProvider, LoggingHandler
from opentelemetry.sdk._logs.export import BatchLogRecordProcessor
from azure.monitor.opentelemetry.exporter import AzureMonitorLogExporter

# Create exporter
exporter = AzureMonitorLogExporter(
    connection_string="InstrumentationKey=xxx;..."
)

# Configure logger provider
logger_provider = LoggerProvider()
logger_provider.add_log_record_processor(BatchLogRecordProcessor(exporter))
set_logger_provider(logger_provider)

# Add handler to Python logging
handler = LoggingHandler(level=logging.INFO, logger_provider=logger_provider)
logging.getLogger().addHandler(handler)

# Use logging
logger = logging.getLogger(__name__)
logger.info("This will be sent to Application Insights")

環境変数から取得

エクスポーターは APPLICATIONINSIGHTS_CONNECTION_STRING を自動的に読み取ります。

from azure.monitor.opentelemetry.exporter import AzureMonitorTraceExporter

# Connection string from environment
exporter = AzureMonitorTraceExporter()

Azure AD 認証

from azure.identity import DefaultAzureCredential
from azure.monitor.opentelemetry.exporter import AzureMonitorTraceExporter

exporter = AzureMonitorTraceExporter(
    credential=DefaultAzureCredential()
)

サンプリング

一貫したサンプリングには ApplicationInsightsSampler を使用します。

from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.sampling import ParentBasedTraceIdRatio
from azure.monitor.opentelemetry.exporter import ApplicationInsightsSampler

# Sample 10% of traces
sampler = ApplicationInsightsSampler(sampling_ratio=0.1)

trace.set_tracer_provider(TracerProvider(sampler=sampler))

オフラインストレージ

再試行のためにオフラインストレージを構成します。

from azure.monitor.opentelemetry.exporter import AzureMonitorTraceExporter

exporter = AzureMonitorTraceExporter(
    connection_string="...",
    storage_directory="/path/to/storage",  # Custom storage path
    disable_offline_storage=False  # Enable retry (default)
)

オフラインストレージを無効化

exporter = AzureMonitorTraceExporter(
    connection_string="...",
    disable_offline_storage=True  # Disable disk persistence; transport retry behavior is separate
)

ソブリンクラウド

from azure.identity import AzureAuthorityHosts, DefaultAzureCredential
from azure.monitor.opentelemetry.exporter import AzureMonitorTraceExporter

# Azure Government
credential = DefaultAzureCredential(authority=AzureAuthorityHosts.AZURE_GOVERNMENT)
exporter = AzureMonitorTraceExporter(
    connection_string="InstrumentationKey=xxx;IngestionEndpoint=https://xxx.in.applicationinsights.azure.us/",
    credential=credential
)

エクスポーターの種類

エクスポーターテレメトリの種類Application Insights のテーブル
AzureMonitorTraceExporterトレース/スパンrequests、dependencies、exceptions
AzureMonitorMetricExporterメトリクスcustomMetrics、performanceCounters
AzureMonitorLogExporterログtraces、customEvents

構成オプション

パラメーター説明デフォルト
connection_stringApplication Insights の接続文字列環境変数から取得
credentialAAD 認証用の Azure 認証情報なし
disable_offline_storage再試行用ストレージを無効化False
storage_directoryカスタムストレージのパス一時ディレクトリ

ベストプラクティス

  1. 本番環境では BatchSpanProcessor を使用します(SimpleSpanProcessor ではなく)。
  2. サービス間で一貫したサンプリングを行うには ApplicationInsightsSampler を使用します。
  3. 本番環境での信頼性を高めるため、オフラインストレージを有効化します。
  4. インストルメンテーションキーではなく、AAD 認証を使用します。
  5. ワークロードに適した エクスポート間隔を設定します。
  6. カスタムパイプラインが必要な場合を除き、ディストリビューション(azure-monitor-opentelemetry)を使用します。

制限事項

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

コメント