Monitor AWS Lambda Node.js with OpenTelemetry

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?

OpenTelemetryopen in new window is an open source observability framework hosted by Cloud Native Computing Foundation. It is a merger of OpenCensus and OpenTracing projects.

OpenTelemetry aims to provide a single standard across all types of observability signals such as OpenTemetry logsopen in new window, distributed tracingopen in new window, and metricsopen in new window.

OpenTelemetry specifies how to collect and send telemetry data to backend platforms. With OpenTelemetry, you can instrument your application once and then add or change vendors without changing the instrumentation.

AWS Lambda instrumentation

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

// 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: '{{ dsn }}',
  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:

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 configureopen in new window NODE_OPTIONS environment variable.

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

OpenTelemetry Lambda

Alternatively, you can add opentelemetry-lambdaopen in new window as a Lambda layer, but it does not offer any advantages at the moment. See the exampleopen in new window for details.

What is Uptrace?

Uptrace is a OpenTelemetry backendopen in new window 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 demoopen in new window (no login required) or running it locally with Dockeropen in new window. The source code is available on GitHubopen in new window.

What's next?

Next, instrument more operations to get a more detailed picture. Try to prioritize network calls, disk operations, database queries, error and logs.

You can also create your own instrumentations using OpenTelemetry JS Tracing APIopen in new window.

Last Updated: 7/25/2024, 12:36:08 PM