Splunk Observability Cloud 用に NodeJS Azure 関数をインストルメンテーションする
スパンをSplunk Observability CloudにエクスポートするためにNodeJS Azure関数をインストルメンテーションする方法を学習します。
NodeJS Azure関数をインストルメンテーションすることで、関数が実行されるたびにスパンをSplunk Observability Cloudに送信できます。
NodeJSのAzure関数をOpenTelemetryで計測し、Splunk Observability Cloudにテレメトリを送信するには、以下の概要の手順に従ってください:
環境変数の定義
関数の設定で必要な環境変数を設定します。
-
関数アプリで関数を選択します。
-
Settings にアクセスし、次に Configuration にアクセスします。
-
New application setting を選択し、以下の設定を追加します。
Name
値
SPLUNK_ACCESS_TOKENSplunk アクセストークン。アクセストークンを取得するには、「Splunk Observability Cloud を使用したユーザー API アクセストークンの取得と管理」を参照してください。
SPLUNK_REALMSplunk Observability Cloud のレルム(
us0など)。Splunk レルムを見つけるには、「Note about realms」を参照してください。NODE_OPTIONSインストルメンテーションモジュールをプリロードするための NodeJS オプションを指定します:
-r @splunk/otel/instrument -
その他必要な設定を追加します。
NPMを使って必要なライブラリを追加する
@splunk/otel の最新バージョンをインストールし、@splunk/otel で使用されている@opentelemetry/api のバージョンと一致させます( package.json を参照)。
スパンを送信するコードをインストルメンテーションする
次に、OpenTelemetry を使用してコードをインストルメンテーションします。コードをインストルメンテーションする出発点として、以下の例を使ってください。Azure 関数に環境変数を追加する手順については、Microsoft Azure ドキュメント(https://learn.microsoft.com/en-us/azure/azure-functions/functions-how-to-use-azure-function-app-settings)を参照してください。
次の例では、instrumentationWrapper ヘルパーを使った関数をインストルメンテーションする方法を示しています:
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 で検索します。詳細については、「トレース内でのスパンの表示およびフィルタリング」を参照してください。
トラブルシューティング
__ ___ ___ _ ______ _____________ _____ ________ ___ ___ ___ ____ __ ___ ____ ____ __ ______ _____________ ______ ___ ___ ___ ____ __ ___ _________ _____
_________ __ ______ _____________ _____ _________
-
______ _ ____ __ ___ ______ _______ _______
-
_______ ______ ________
_________ __ ___________ _________ ___ ____ _____ _____
-
___ _ ________ ___ ___ _______ _______ _________ _______ __ ______ ________
-
____ ___ ______ ______________ ____ _____ _____ _______ __ ___________ ____ __________ _________ ___ ______ _________ __________ __ _____ ___ ____ _______