Docs » Splunk Observability Cloud 用のサーバーレス関数を実装する » Instrument NodeJS Azure functions for Splunk Observability Cloud

Instrument NodeJS Azure functions for Splunk Observability Cloud 🔗

By instrumenting NodeJS Azure functions you can send spans to Splunk Observability Cloud every time your functions run.

To instrument your NodeJS Azure function with OpenTelemetry to send telemetry to Splunk Observability Cloud, follow these high-level steps:

環境変数の定義 🔗

関数の設定で必要な環境変数を設定します:

  1. 関数アプリで関数を選択します。

  2. Settings にアクセスし、次に Configuration にアクセスします。

  3. New application setting を選択し、以下の設定を追加します:

    Name

    SPLUNK_ACCESS_TOKEN

    Splunk アクセストークン。アクセストークンを取得するには、Splunk Observability Cloudを使用したユーザー APIアクセストークンの取得と管理 を参照してください。

    SPLUNK_REALM

    Splunk Observability Cloud レルム、たとえば us0 です。Splunk レルムを見つけるには、レルムに関する注意 を参照してください。

    NODE_OPTIONS

    Specify NodeJS options to preload instrumentation module: -r @splunk/otel/instrument

  4. その他必要な設定を追加します。

Add the required libraries using NPM 🔗

Install the latest version of @splunk/otel and match the @opentelemetry/api version used in the @splunk/otel (see package.json).

スパンを送信するコードをインストルメンテーションする 🔗

次に、OpenTelemetry を使ってコードをインストルメンテーションします。コードをインストルメンテーションするための出発点として、以下の例を使用してください。Azure 関数に環境変数を追加する手順については、Microsoft Azure ドキュメントの https://learn.microsoft.com/en-us/azure/azure-functions/functions-how-to-use-azure-function-app-settings を参照してください。

The following example shows how to instrument a function using the instrumentationWrapper helper:

import { app, HttpRequest, HttpResponseInit, InvocationContext } from "@azure/functions";
import { trace, Span } from "@opentelemetry/api";

const tracer = trace.getTracer('splunk-example-azure', '0.1.0');

export async function myhttptrigger(request: HttpRequest, context: InvocationContext): Promise<HttpResponseInit> {
   context.log(`Http function processed request for url "${request.url}"`);

   const response = // run your function logic here.

   return { body: `Hello, ${response}!` };
};

// Universal wrapper method that helps to generate root span for Azure Functions
export const instrumentationWrapper = <T extends (...args: Parameters<T>) => Promise<Awaited<ReturnType<T>>>>(func: T) =>
   async (...args: Parameters<T>): Promise<Awaited<ReturnType<T>>> => {

      let result: Promise<Awaited<ReturnType<T>>>;
      let functionName = func.name;

      await tracer.startActiveSpan(functionName, async (span: Span) => {

            // setup custom attributes for root span, specific to your Azure Functions.
            span.setAttribute("foo", 1);
            span.setAttribute("bar", "Hello World!");
            span.setAttribute("baz", [1, 2, 3])

            result = await func(...args)

            span.end();
         });

      return result;
   }

app.http('myhttptrigger', {
   methods: ['GET', 'POST'],
   authLevel: 'anonymous',
   handler: instrumentationWrapper(myhttptrigger)
});

データが入力されていることを確認する 🔗

関数を実行し、そのスパンを Splunk APM で検索します。詳しくは トレース内でのスパンの表示およびフィルタリング を参照してください。

トラブルシューティング 🔗

Splunk Observability Cloudをご利用のお客様で、Splunk Observability Cloudでデータを確認できない場合は、以下の方法でサポートを受けることができます。

Splunk Observability Cloudをご利用のお客様

見込み客および無料トライアルユーザー様

  • Splunk Answers のコミュニティサポートで質問し、回答を得る

  • Splunk #observability ユーザーグループの Slack チャンネルに参加して、世界中の顧客、パートナー、Splunk 社員とのコミュニケーションを図る。参加するには、Get Started with Splunk Community マニュアルの チャットグループ を参照してください。

This page was last updated on 2024年05月29日.