What is @opentelemetry/instrumentation-aws-lambda?
@opentelemetry/instrumentation-aws-lambda is an npm package that provides automatic instrumentation for AWS Lambda functions using OpenTelemetry. It helps in collecting and exporting telemetry data such as traces and metrics, which can be used for monitoring and observability of serverless applications.
What are @opentelemetry/instrumentation-aws-lambda's main functionalities?
Automatic Tracing
This feature allows you to automatically trace AWS Lambda function invocations. The code sample demonstrates how to set up the NodeTracerProvider and register the AWS Lambda instrumentation.
const { NodeTracerProvider } = require('@opentelemetry/node');
const { AwsLambdaInstrumentation } = require('@opentelemetry/instrumentation-aws-lambda');
const { registerInstrumentations } = require('@opentelemetry/instrumentation');
const provider = new NodeTracerProvider();
provider.register();
registerInstrumentations({
instrumentations: [
new AwsLambdaInstrumentation(),
],
});
exports.handler = async (event) => {
// Your Lambda function code
};
Custom Span Attributes
This feature allows you to add custom attributes to spans within your AWS Lambda function. The code sample shows how to retrieve the current span and set a custom attribute.
const { context, trace } = require('@opentelemetry/api');
exports.handler = async (event) => {
const span = trace.getSpan(context.active());
if (span) {
span.setAttribute('custom.attribute', 'value');
}
// Your Lambda function code
};
Error Handling
This feature allows you to record exceptions and set the status of spans when errors occur in your AWS Lambda function. The code sample demonstrates how to catch errors, record them, and set the span status.
const { context, trace } = require('@opentelemetry/api');
exports.handler = async (event) => {
const span = trace.getSpan(context.active());
try {
// Your Lambda function code
} catch (error) {
if (span) {
span.recordException(error);
span.setStatus({ code: 2, message: error.message });
}
throw error;
}
};
Other packages similar to @opentelemetry/instrumentation-aws-lambda
@opentelemetry/instrumentation-http
This package provides automatic instrumentation for HTTP and HTTPS modules in Node.js. It is similar to @opentelemetry/instrumentation-aws-lambda in that it helps in collecting telemetry data, but it is specifically designed for HTTP/HTTPS requests rather than AWS Lambda functions.
@opentelemetry/instrumentation-express
This package provides automatic instrumentation for Express.js applications. It is similar to @opentelemetry/instrumentation-aws-lambda in terms of providing observability, but it focuses on Express.js middleware and routes instead of AWS Lambda functions.
datadog-lambda-js
This package provides automatic instrumentation and monitoring for AWS Lambda functions using Datadog. It is similar to @opentelemetry/instrumentation-aws-lambda in that it helps in collecting telemetry data for Lambda functions, but it is specifically designed to work with Datadog's monitoring platform.
OpenTelemetry AWS Lambda Instrumentation for Node.js
This module provides automatic instrumentation for AWS Lambda
.
This module is currently under active development and not ready for general use.
Installation
npm install --save @opentelemetry/instrumentation-aws-lambda
Usage
Create a file to initialize the instrumentation, such as lambda-wrapper.js
.
const { NodeTracerProvider } = require('@opentelemetry/node');
const { AwsLambdaInstrumentation } = require('@opentelemetry/instrumentation-aws-lambda');
const { registerInstrumentations } = require('@opentelemetry/instrumentation');
const provider = new NodeTracerProvider();
provider.register();
registerInstrumentations({
instrumentations: [
new AwsLambdaInstrumentation({
})
],
});
In your Lambda function configuration, add or update the NODE_OPTIONS
environment variable to require the wrapper, e.g.,
NODE_OPTIONS=--require lambda-wrapper
AWS Lambda Instrumentation Options
Options | Type | Description |
---|
requestHook | RequestHook (function) | Hook for adding custom attributes before lambda starts handling the request. Receives params: span, { event, context } |
responseHook | ResponseHook (function) | Hook for adding custom attributes before lambda returns the response. Receives params: span, { err?, res? } |
Hooks Usage Example
const { AwsLambdaInstrumentation } = require('@opentelemetry/instrumentation-aws-lambda');
new AwsLambdaInstrumentation({
requestHook: (span, { event, context }) => {
span.setAttributes('faas.name', context.functionName);
},
responseHook: (span, { err, res }) => {
if (err instanceof Error) span.setAttributes('faas.error', err.message);
if (res) span.setAttributes('faas.res', res);
}
})
Useful links
License
Apache 2.0 - See LICENSE for more information.