Docs » Instrument serverless functions for 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:

Define the environment variables πŸ”—

Set the required environment variables in your function’s settings:

  1. Select your function in Function App.

  2. Go to Settings, then Configuration.

  3. Select New application setting to add the following settings:

    Name

    Value

    SPLUNK_ACCESS_TOKEN

    Your Splunk access token. To obtain an access token, see Retrieve and manage user API access tokens using Splunk Observability Cloud.

    SPLUNK_REALM

    Your Splunk Observability Cloud realm, for example us0. To find your Splunk realm, see Note about realms.

    NODE_OPTIONS

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

  4. Add any other settings you might need.

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).

Instrument the code to send spans πŸ”—

Next, instrument your code using OpenTelemetry. Use the following examples as a starting point to instrument your code. See https://learn.microsoft.com/en-us/azure/azure-functions/functions-how-to-use-azure-function-app-settings in Microsoft Azure documentation for steps to add environment variables to an Azure function.

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)
});

Check that data is coming in πŸ”—

Run your function and search for its spans in Splunk APM. See View and filter for spans within a trace for more information.

Troubleshooting πŸ”—

If you are a Splunk Observability Cloud customer and are not able to see your data in Splunk Observability Cloud, you can get help in the following ways.

Available to Splunk Observability Cloud customers

Available to prospective customers and free trial users

  • Ask a question and get answers through community support at Splunk Answers .

  • Join the Splunk #observability user group Slack channel to communicate with customers, partners, and Splunk employees worldwide. To join, see Chat groups in the Get Started with Splunk Community manual.

This page was last updated on Dec 09, 2024.