OpenTelemetry Node.js AWS Lambda

Vladimir Mihailenco
September 25, 2024
3 min read

AWS Lambda simplifies serverless application development by abstracting away the infrastructure management, allowing developers to focus on writing code and responding to events.

By leveraging OpenTelemetry with AWS Lambda, you can gain deeper visibility into the execution and interactions of your serverless functions, making it easier to identify and troubleshoot performance bottlenecks or issues within your application.

What is OpenTelemetry?

OpenTelemetry is an open-source observability framework that aims to standardize and simplify the collection, processing, and export of telemetry data from applications and systems.

OpenTelemetry supports multiple programming languages and platforms, making it suitable for a wide range of applications and environments.

OpenTelemetry enables developers to instrument their code and collect telemetry data, which can then be exported to various OpenTelemetry backends or observability platforms for analysis and visualization. In containerized environments like Kubernetes, the OpenTelemetry Operator provides auto-instrumentation capabilities and simplifies collector configuration. Combine your serverless traces with the OpenTelemetry Kubernetes guide when functions interact with workloads inside a cluster.

AWS Lambda instrumentation

To instrument your Node.js lambda function, create a separate file called otel.js and put the OpenTelemetry initialization code there:

js
// The very first import must be Uptrace/OpenTelemetry.
const uptrace = require('@uptrace/node')
const otel = require('@opentelemetry/api')
const {
  getNodeAutoInstrumentations,
} = require('@opentelemetry/auto-instrumentations-node')
const {
  AwsLambdaInstrumentation,
} = require('@opentelemetry/instrumentation-aws-lambda')

// Start OpenTelemetry SDK and invoke instrumentations to patch the code.
uptrace.configureOpentelemetry({
  // copy your project DSN here or use UPTRACE_DSN env var
  //dsn: '<FIXME>',
  serviceName: 'myservice',
  serviceVersion: '1.0.0',
  deploymentEnvironment: 'production',
  instrumentations: [
    getNodeAutoInstrumentations(),
    new AwsLambdaInstrumentation({
      // Disable reading the X-Ray context headers.
      disableAwsContextPropagation: true,
    }),
  ],
})

Next, we need to configure AWS Lambda to load the OpenTelemetry code before the application code using the --require flag.

If you use serverless, here is the serverless.yml file with the relevant NODE_OPTIONS env variable:

yaml
service: lambda-otel-example
frameworkVersion: '2'
provider:
  name: aws
  runtime: nodejs12.x
  lambdaHashingVersion: 20201221
  environment:
    NODE_OPTIONS: --require otel
  region: eu-west-2
functions:
  otel-lambda-example:
    handler: handler.hello

Otherwise, you can use AWS Console or CLI to configure NODE_OPTIONS environment variable.

shell
aws lambda update-function-configuration --function-name otel-lambda-example \
    --environment "Variables={NODE_OPTIONS=--require otel}"

OpenTelemetry Lambda

Alternatively, you can add opentelemetry-lambda as a Lambda layer, but it does not offer any advantages at the moment. See the example for details.

What is Uptrace?

Uptrace is a OpenTelemetry APM that supports distributed tracing, metrics, and logs. You can use it to monitor applications and troubleshoot issues.

Uptrace Overview

Uptrace comes with an intuitive query builder, rich dashboards, alerting rules with notifications, and integrations for most languages and frameworks.

Uptrace can process billions of spans and metrics on a single server and allows you to monitor your applications at 10x lower cost.

In just a few minutes, you can try Uptrace by visiting the cloud demo (no login required) or running it locally with Docker. The source code is available on GitHub.

What's next?

Node.js Lambda functions now have full OpenTelemetry tracing for serverless monitoring. Track invocations, cold starts, and downstream dependencies. For traditional Node.js applications, see Express instrumentation, or compare with Go Lambda for alternative runtime options.