次の方法で共有


Azure Functions で OpenTelemetry を使用する

重要

OpenTelemetry の Azure Functions のサポートは現在プレビュー段階です。 まだ一般公開されていない Azure 機能に適用される法的条件については、 Microsoft Azure プレビューの補足使用条件 を参照してください。

この記事では、OpenTelemetry 形式でログおよびトレース データをエクスポートするように関数アプリを構成する方法について説明します。 Azure Functions は、Functions ホスト プロセスと、関数コードを実行する言語固有のワーカー プロセスの両方から、関数の実行に関するテレメトリ データを生成します。 既定では、このテレメトリ データは Application Insights SDK を使用して Application Insights に送信されます。 ただし、OpenTelemetry セマンティクスを使用してこのデータをエクスポートすることもできます。 OpenTelemetry 形式を使用して Application Insights にデータを送信することはできますが、同じデータを他の OpenTelemetry 準拠のエンドポイントにエクスポートすることもできます。

ヒント

この記事は選択された開発言語を対象としているため、記事の上部で適切な言語を必ず選択してください。

OpenTelemetry は現在、 C# インプロセス アプリではサポートされていません。

関数アプリで OpenTelemetry を有効にすると、次の利点が得られます。

  • ホストとアプリケーション コードの両方で生成されるトレースとログ間でデータを関連付けます。
  • エクスポート可能なテレメトリ データの一貫性のある標準ベースの生成を可能にします。
  • OpenTelemetry 準拠のデータを使用できる他のプロバイダーとの統合。

OpenTelemetry は、ホスト構成 (host.json) とコード プロジェクトの両方で、関数アプリ レベルで有効になります。 関数は、言語固有のワーカー プロセスで実行されている関数コードから OpenTelemetry データをエクスポートするためのクライアント最適化エクスペリエンスも提供します。

Functions ホストで OpenTelemetry を有効にする

関数アプリの host.json ファイルで OpenTelemetry 出力を有効にすると、アプリで使用されている言語スタックに関わらず、ホストによって OpenTelemetry 出力がエクスポートされます。

Functions ホストからの OpenTelemetry 出力を有効にするには、コード プロジェクトの host.json ファイルを更新して、ルート コレクションに "telemetryMode": "OpenTelemetry" 要素を追加します。 OpenTelemetry を有効にすると、host.json ファイルは次のようになります。

{
    "version": "2.0",
    "telemetryMode": "OpenTelemetry",
    ...
}

アプリケーション設定の構成

host.json ファイルで OpenTelemetry が有効になっている場合、データの送信先のエンドポイントは、アプリの環境変数で使用できる OpenTelemetry でサポートされるアプリケーション設定に基づいて決定されます。

OpenTelemetry の出力先に基づいて、関数アプリで特定のアプリケーション設定を作成します。 Application Insights と OpenTelemetry プロトコル (OTLP) エクスポーターの両方に接続設定が指定されている場合、OpenTelemetry データは両方のエンドポイントに送信されます。

APPLICATIONINSIGHTS_CONNECTION_STRING: Application Insights ワークスペースの接続文字列。 この設定が存在すると、OpenTelemetry データがそのワークスペースに送信されます。 この設定は、OpenTelemetry を有効にせずに Application Insights に接続するために使用される設定と同じです。 アプリにこの設定がまだない場合は、Application Insights の統合を有効にする必要がある場合があります。

JAVA_APPLICATIONINSIGHTS_ENABLE_TELEMETRY: Functions ホストが Java ワーカー プロセスで OpenTelemetry ログを直接ストリーミングできるように、 true に設定すると、ホスト レベルのエントリが重複しないようにします。

PYTHON_APPLICATIONINSIGHTS_ENABLE_TELEMETRY: Functions ホストが OpenTelemetry ログを直接ストリーミングできるように、Functions ホストが true に設定すると、ホスト レベルのエントリの重複を防ぐことができます。

アプリで OpenTelemetry を有効にする

OpenTelemetry を使用するように Functions ホストが構成されている場合は、OpenTelemetry データを出力するようにアプリケーション コードを更新する必要もあります。 ホストとアプリケーション コードの両方で OpenTelemetry を有効にすると、Functions ホスト プロセスと言語ワーカー プロセスの両方から出力されるトレースとログ間の相関関係が向上します。

OpenTelemetry を使用するようにアプリケーションをインストルメント化する方法は、ターゲットの OpenTelemetry エンドポイントによって異なります。

この記事の例では、アプリがバージョン 2.x 以降のIHostApplicationBuilder で利用可能なを使用していることを前提としています。 詳細については、C# 分離ワーカー モデル ガイドの バージョン 2.x を参照してください。

  1. 次のコマンドを実行して、必要なアセンブリをアプリにインストールします。

    dotnet add package Microsoft.Azure.Functions.Worker.OpenTelemetry --version  1.1.0-preview6
    dotnet add package OpenTelemetry.Extensions.Hosting 
    dotnet add package Azure.Monitor.OpenTelemetry.Exporter  
    
  2. Program.cs プロジェクト ファイルに、次の using ステートメントを追加します。

    using Azure.Monitor.OpenTelemetry.Exporter; 
    
  3. OpenTelemetry を構成する方法は、.NET 分離 worker モデル拡張機能の v2.x で導入された IHostBuilder または IHostApplicationBuilderをプロジェクトのスタートアップで使用するかどうかによって異なります。

    program.csで、次のコード行をConfigureFunctionsWebApplication後に追加します。

    builder.Services.AddOpenTelemetry()
        .UseFunctionsWorkerDefaults()
        .UseAzureMonitorExporter();
    

    同じアプリから両方の OpenTelemetry エンドポイントにエクスポートできます。

  1. 必要なライブラリをアプリに追加します。 ライブラリを追加する方法は、Maven と Kotlin のどちらを使用してデプロイするか、Application Insights にもデータを送信するかどうかによって異なります。

    <dependency>
      <groupId>com.microsoft.azure.functions</groupId>
      <artifactId>azure-functions-java-opentelemetry</artifactId>
      <version>1.0.0</version>
    </dependency>
    <dependency>
      <groupId>com.azure</groupId>
      <artifactId>azure-monitor-opentelemetry-autoconfigure</artifactId>
      <version>1.2.0</version>
    </dependency>
    
  2. 必要に応じて、次のコードを追加してカスタム スパンを作成できます。

    import com.microsoft.azure.functions.opentelemetry.FunctionsOpenTelemetry;
    import io.opentelemetry.api.trace.Span;
    import io.opentelemetry.api.trace.SpanKind;
    import io.opentelemetry.context.Scope;
    
    Span span = FunctionsOpenTelemetry.startSpan(
            "com.contoso.PaymentFunction",  // tracer name
            "validateCharge",               // span name
            null,                           // parent = current context
            SpanKind.INTERNAL);
    
    try (Scope ignored = span.makeCurrent()) {
        // business logic here
    } finally {
        span.end();
    }
    
  1. プロジェクト ディレクトリに次の npm パッケージをインストールします。

    npm install @opentelemetry/api 
    npm install @opentelemetry/auto-instrumentations-node 
    npm install @azure/monitor-opentelemetry-exporter 
    npm install @azure/functions-opentelemetry-instrumentation
    
  1. プロジェクトにコード ファイルを作成し、この新しいファイルに次のコードをコピーして貼り付け、ファイルを src/index.js として保存します。

    const { AzureFunctionsInstrumentation } = require('@azure/functions-opentelemetry-instrumentation');
    const { AzureMonitorLogExporter, AzureMonitorTraceExporter } = require('@azure/monitor-opentelemetry-exporter');
    const { getNodeAutoInstrumentations, getResourceDetectors } = require('@opentelemetry/auto-instrumentations-node');
    const { registerInstrumentations } = require('@opentelemetry/instrumentation');
    const { detectResourcesSync } = require('@opentelemetry/resources');
    const { LoggerProvider, SimpleLogRecordProcessor } = require('@opentelemetry/sdk-logs');
    const { NodeTracerProvider, SimpleSpanProcessor } = require('@opentelemetry/sdk-trace-node');
    
    const resource = detectResourcesSync({ detectors: getResourceDetectors() });
    
    const tracerProvider = new NodeTracerProvider({ resource });
    tracerProvider.addSpanProcessor(new SimpleSpanProcessor(new AzureMonitorTraceExporter()));
    tracerProvider.register();
    
    const loggerProvider = new LoggerProvider({ resource });
    loggerProvider.addLogRecordProcessor(new SimpleLogRecordProcessor(new AzureMonitorLogExporter()));
    
    registerInstrumentations({
        tracerProvider,
        loggerProvider,
        instrumentations: [getNodeAutoInstrumentations(), new AzureFunctionsInstrumentation()],
    });
    
  2. package.json ファイルの main フィールドを更新して、この新しい src/index.js ファイルを含めます。これは次のようになります。

    "main": "src/{index.js,functions/*.js}"
    
  1. プロジェクトにコード ファイルを作成し、この新しいファイルに次のコードをコピーして貼り付け、ファイルを src/index.ts として保存します。

    import { AzureFunctionsInstrumentation } from '@azure/functions-opentelemetry-instrumentation';
    import { AzureMonitorLogExporter, AzureMonitorTraceExporter } from '@azure/monitor-opentelemetry-exporter';
    import { getNodeAutoInstrumentations, getResourceDetectors } from '@opentelemetry/auto-instrumentations-node';
    import { registerInstrumentations } from '@opentelemetry/instrumentation';
    import { detectResourcesSync } from '@opentelemetry/resources';
    import { LoggerProvider, SimpleLogRecordProcessor } from '@opentelemetry/sdk-logs';
    import { NodeTracerProvider, SimpleSpanProcessor } from '@opentelemetry/sdk-trace-node';
    
    const resource = detectResourcesSync({ detectors: getResourceDetectors() });
    
    const tracerProvider = new NodeTracerProvider({ resource });
    tracerProvider.addSpanProcessor(new SimpleSpanProcessor(new AzureMonitorTraceExporter()));
    tracerProvider.register();
    
    const loggerProvider = new LoggerProvider({ resource });
    loggerProvider.addLogRecordProcessor(new SimpleLogRecordProcessor(new AzureMonitorLogExporter()));
    
    registerInstrumentations({
        tracerProvider,
        loggerProvider,
        instrumentations: [getNodeAutoInstrumentations(), new AzureFunctionsInstrumentation()],
    });
    
  2. package.json ファイルの main フィールドを更新して、この新しい src/index.ts ファイルの出力を含めます。これは、次のようになります。

    "main": "dist/src/{index.js,functions/*.js}"
    

重要

言語ワーカーからの Application Insights への OpenTelemetry 出力は、PowerShell アプリでは現在サポートされていません。 代わりに、OTLP エクスポーター エンドポイントを使用することもできます。 ホストが Application Insights への OpenTelemetry 出力用に構成されている場合、PowerShell ワーカー プロセスによって生成されたログは引き続き転送されますが、現在、分散トレースはサポートされていません。

これらの手順は、OTLP エクスポーターにのみ適用されます。

  1. 値が OTEL_FUNCTIONS_WORKER_ENABLEDTrue という名前のアプリケーション設定を追加します。

  2. アプリのルートに アプリレベルの Modules フォルダーを作成し、次のコマンドを実行します。

    Save-Module -Name AzureFunctions.PowerShell.OpenTelemetry.SDK
    

    これにより、必要な AzureFunctions.PowerShell.OpenTelemetry.SDK モジュールがアプリに直接インストールされます。 requirements.psd1 ファイルを使用してこの依存関係を自動的にインストールすることはできません。マネージド依存関係 は、Flex 従量課金プラン プレビューでは現在サポートされていないためです。

  3. 次のコードを profile.ps1 ファイルに追加します。

    Import-Module AzureFunctions.PowerShell.OpenTelemetry.SDK -Force -ErrorAction Stop 
    Initialize-FunctionsOpenTelemetry 
    
  1. コメントを解除するか、自分を追加するかに関係なく、これらのライブラリが requirements.txt ファイル内にあることを確認します。

    azure-monitor-opentelemetry
    
  2. function_app.py メイン エントリ ポイント ファイルに次のコードを追加します。

    アプリケーション設定に PYTHON_APPLICATIONINSIGHTS_ENABLE_TELEMETRY=true を既に追加している場合は、この手順をスキップできます。 自動インストルメンテーションなしで Application Insights コレクションを手動で有効にするには、次のコードをアプリに追加します。

    from azure.monitor.opentelemetry import configure_azure_monitor 
    configure_azure_monitor() 
    
  3. SDK をさらに構成する方法のオプションについては、 Azure Monitor Distro の使用状況 に関するドキュメントを参照してください。

OpenTelemetry に関する考慮事項

OpenTelemetry を使用してデータをエクスポートする場合は、これらの現在の考慮事項に留意してください。

  • 現時点では、OpenTelemetry 出力では HTTP、Service Bus、Event Hubs トリガーのみがサポートされています。

  • ホストが OpenTelemetry を使用するように構成されている場合、Azure portal ではログ ストリーミングや最近の関数呼び出しトレースはサポートされません。

  • カスタム スパンには、すべてのリソース属性が自動的に含まれており、アプリで構成されたエクスポーターが使用されます。

  • ローカル開発中を含め、アプリが Azure の外部で実行されている場合、リソース検出機能によって、 service.name 属性が既定で java-function-app に設定されます。

  • 単体テスト中にローカルで実行するときにテレメトリを無音にするには、次の Java 仮想マシン (JVM) フラグを使用します。

    • -Dotel.traces.exporter=none
    • -Dotel.metrics.exporter=none
    • -Dotel.logs.exporter=none
  • ミドルウェアを手動で登録する必要はありません。Java ワーカー自動検出 OpenTelemetryInvocationMiddleware
  • Azure portal の Azure Functions 診断は、潜在的な監視関連の問題を検出して診断するのに役立つリソースです。

アプリで診断にアクセスするには:

  1. Azure portal で、関数アプリ リソースに移動します。

  2. 左側のウィンドウで、[問題の診断と解決] を選択し、機能アプリのテレメトリが不足している場合の Application Insights または OpenTelemetry ワークフローを検索します。

  3. このワークフローを選択し、インジェスト方法を選択して、[ 次へ] を選択します。

  4. トラブルシューティング ツールによって提供されるガイドラインと推奨事項を確認します。

Azure Functions の監視Flex 従量課金プラン