Send traces and logs from AWS Lambda Node.js functions to SigNoz

You can auto instrument your Node.js Lambda function to send traces and logs to SigNoz. Follow the steps below.

Configure OpenTelemetry for AWS Lambda Functions

Add Required Dependencies

Add the OpenTelemetry API dependency to your NodeJS application

yarn add @opentelemetry/api

Set Up Your Lambda Function

Use OpenTelemetry APIs to trace spans and log events within your Lambda function.

Below is an index.js file of a sample app. This example demonstrates how to create a trace span, emit a custom log entry, and ensure logs are captured by explicitly flushing the LoggerProvider at the end of the execution. It is important to flush to avoid losing any log data during the function's lifecycle.

index.js
const { trace } = require("@opentelemetry/api");
const logsAPI = require("@opentelemetry/api-logs");

const provider = logsAPI.logs.getLoggerProvider();
const logger = provider.getLogger("default", "1.0.0");
const { flush } = require("./instrumentation");

const tracer = trace.getTracer("test", "0.1");

exports.handler = async (event) => {
  const parentSpan = tracer.startSpan("main");
  tracer.startActiveSpan("testSpan", (span) => {
    logger.emit({
      severityText: "info",
      body: "this is a log body example",
      attributes: { "log.type": "custom" },
    });
    span.end();
  });

  const response = {
    statusCode: 200,
    body: JSON.stringify("Hello from Lambda!"),
  };

  provider.forceFlush();
  return response;
};

Zip the Folder for Deployment

Run the below command in the root directory of your NodeJS project. The command recursively (-r) compresses all files and folders in the current directory (./) into a zip file named deploy.zip.

zip -r deploy.zip ./

Upload to AWS Lambda

  • Navigate to the Code section in AWS Lambda.
  • Select Upload from .zip file and upload deploy.zip.

Configure Environment Variables

Go to Configuration > Environment Variables and add the following:

AWS_LAMBDA_EXEC_WRAPPER=/opt/otel-handler
OTEL_EXPORTER_OTLP_ENDPOINT=https://ingest.<REGION>.signoz.cloud:443
OTEL_EXPORTER_OTLP_HEADERS=signoz-ingestion-key=<SIGNOZ_INGESTION_KEY>
OTEL_RESOURCE_ATTRIBUTES=service.name=<SERVICE_NAME>
  • Set the <REGION> to match your SigNoz Cloud region.
  • Replace <SIGNOZ_INGESTION_KEY> with your SigNoz Cloud ingestion key.
  • <SERVICE_NAME>is the name of your Lambda function.

Add the OpenTelemetry Lambda Layer

Go to the Layers section and add the following ARN:

arn:aws:lambda:<region>:184161586896:layer:opentelemetry-nodejs-0_9_0:4

Replace <region> with your AWS region. Check OpenTelemetry Lambda releases for the latest version.

Test the Lambda Function

Run the function and verify traces and logs in SigNoz.

Was this page helpful?