.NET AWS Lambda 関数を Splunk Observability Cloud にインストルメンテーションする

以下の手順に従って、OpenTelemetry を使用して AWS の .NET AWS Lambda 関数をインストルメンテーションし、Splunk Observability Cloud にトレースを送信します。

以下の OpenTelemetry テンプレートを使用して、.NET AWS Lambda 関数をインストルメンテーションし、Splunk Observability Cloud にトレースを送信できます。テンプレートは次のパッケージを使用します。

AWS Lambda for Splunk APM で .NET 関数をインストルメンテーションするには、以下の手順に従います:

  1. 既存の AWS Lambda を以下のテンプレートと統合するか、テンプレートを使用して新しい関数を開始します:

    using Amazon.Lambda.Core;
    using OpenTelemetry;
    using OpenTelemetry.Exporter;
    using OpenTelemetry.Instrumentation.AWSLambda;
    using OpenTelemetry.Resources;
    using OpenTelemetry.Trace;
    using System.Diagnostics;
    
    namespace DotNetInstrumentedLambdaExample;
    
    public class Function
    {
       public static readonly TracerProvider TracerProvider;
    
       static Function()
       {
          TracerProvider = ConfigureSplunkTelemetry()!;
       }
    
       // Note: Do not forget to point function handler to here.
       public string TracingFunctionHandler(string input, ILambdaContext context)
          => AWSLambdaWrapper.Trace(TracerProvider, FunctionHandler, input, context);
    
       public string FunctionHandler(string input, ILambdaContext context)
       {
          // TODO: Your function handler code here
       }
    
       private static TracerProvider ConfigureSplunkTelemetry()
       {
          var serviceName = Environment.GetEnvironmentVariable("AWS_LAMBDA_FUNCTION_NAME") ?? "Unknown";
          var accessToken = Environment.GetEnvironmentVariable("SPLUNK_ACCESS_TOKEN")?.Trim();
          var realm = Environment.GetEnvironmentVariable("SPLUNK_REALM")?.Trim();
    
          ArgumentNullException.ThrowIfNull(accessToken, "SPLUNK_ACCESS_TOKEN");
          ArgumentNullException.ThrowIfNull(realm, "SPLUNK_REALM");
    
          var builder = Sdk.CreateTracerProviderBuilder()
                // Use Add[instrumentation-name]Instrumentation to instrument missing services
                // Use Nuget to find different instrumentation libraries
                .AddHttpClientInstrumentation()
                .AddAWSInstrumentation()
                // Use AddSource to add your custom DiagnosticSource source names
                //.AddSource("My.Source.Name")
                .SetSampler(new AlwaysOnSampler())
                .AddAWSLambdaConfigurations(opts => opts.DisableAwsXRayContextExtraction = true)
                .ConfigureResource(configure => configure
                      .AddService(serviceName, serviceVersion: "1.0.0")
                      // Different resource detectors can be found at
                      // https://github.com/open-telemetry/opentelemetry-dotnet-contrib/tree/main/src/OpenTelemetry.Resources.AWS#usage
                      .AddAWSEBSDetector())
                .AddOtlpExporter(opts =>
                {
                   opts.Endpoint = new Uri($"https://ingest.{realm}.signalfx.com/v2/trace/otlp");
                   opts.Protocol = OtlpExportProtocol.HttpProtobuf;
                   opts.Headers = $"X-SF-TOKEN={accessToken}";
                });
    
          return builder.Build()!;
       }
    }
  2. function-handler ファイルの aws-lambda-tools-defaults.json フィールドを <project-name>::<class-namespace-with-class-name>::TracingFunctionHandler に更新して、メインエントリポイントが TracingFunctionHandler に設定されていることを確認します。AWS Web コンソールを使用し、Runtime settings のハンドラを変更して、これを行うこともできます。

    以下は、関数ハンドラが TracingFunctionHandler に設定された aws-lambda-tools-defaults.json ファイルの例です。例の内容をファイルに貼り付けないでください。そのほとんどが使用環境と一致しません。一致する必要がある部分は TracingFunctionHandler です。

    {
        "Information": [
            "This file provides default values for the deployment wizard inside Visual Studio and the AWS Lambda commands added to the .NET Core CLI.",
            "To learn more about the Lambda commands with the .NET Core CLI execute the following command at the command line in the project root directory.",
            "dotnet lambda help",
            "All the command line options for the Lambda command can be specified in this file."
        ],
        "profile": "default",
        "region": "us-west-2",
        "configuration": "Release",
        "function-architecture": "x86_64",
        "function-runtime": "dotnet8",
        "function-memory-size": 512,
        "function-timeout": 30,
        "function-handler": "AWSLambdaSample::AWSLambdaSample.Function::TracingFunctionHandler"
    }
  3. テンプレートは以下の環境変数を想定しています:

    • AWS_LAMBDA_FUNCTION_NAME: AWS Lambda関数の名前

    • SPLUNK_ACCESS_TOKEN:Splunk インジェストアクセストークン

    • SPLUNK_REALM:Splunk インジェスト領域。例: us0

  4. また、このテンプレートには ConfigureSplunkTelemetry() に以下のカスタマイズポイントが含まれています:

    • 他のサードパーティライブラリをサポートするためのカスタム インストルメンテーション ライブラリを追加します。NuGet および OpenTelemetry.Instrumentation. で始まる文字列を使用して、ライブラリを検索できます。

    • ライブラリの中には、すでに System.Diagnostics.DiagnosticSource モジュールが組み込まれているものもあります。カスタム DiagnosticSource名を含めるには、.AddSource() メソッドを使用してください。

    • AWS パッケージには、環境を記述するのに役立つ複数の ResourceDetectors 要素が含まれています。各自のユースケースに応じたディテクタを選択してください。

  5. デフォルトの AWS テンプレートが期待するように、FunctionHandler 関数にコードを追加します。