Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@opentelemetry/instrumentation-aws-lambda

open-telemetry5.5mApache-2.00.51.0TypeScript support: included

OpenTelemetry instrumentation for AWS Lambda function invocations

aws-lambda, instrumentation, nodejs, opentelemetry, profiling, tracing

readme

OpenTelemetry AWS Lambda Instrumentation for Node.js

NPM Published Version Apache License

component owners: @jj22ee

This module provides automatic instrumentation for the AWS Lambda module, which may be loaded using the @opentelemetry/sdk-trace-node package and is included in the @opentelemetry/auto-instrumentations-node bundle.

If total installation size is not constrained, it is recommended to use the @opentelemetry/auto-instrumentations-node bundle with @opentelemetry/sdk-node for the most seamless instrumentation experience.

Compatible with OpenTelemetry JS API and SDK 1.0+.

This module is currently under active development and not ready for general use.

Installation

npm install --save @opentelemetry/instrumentation-aws-lambda

Supported Versions

  • This package will instrument the lambda execution regardless of versions.

Usage

Create a file to initialize the instrumentation, such as lambda-wrapper.js.

const { NodeTracerProvider } = require('@opentelemetry/sdk-trace-node');
const { AwsLambdaInstrumentation } = require('@opentelemetry/instrumentation-aws-lambda');
const { registerInstrumentations } = require('@opentelemetry/instrumentation');

const provider = new NodeTracerProvider();
provider.register();

registerInstrumentations({
  instrumentations: [
    new AwsLambdaInstrumentation({
        // see under for available configuration
    })
  ],
});

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? }
eventContextExtractor EventContextExtractor (function) Function for providing custom context extractor in order to support different event types that are handled by AWS Lambda (e.g., SQS, CloudWatch, Kinesis, API Gateway).
lambdaHandler string By default, this instrumentation automatically determines the Lambda handler function to instrument. This option is used to override that behavior by explicitly specifying the Lambda handler to instrument. See Specifying the Lambda Handler for additional information.

Hooks Usage Example

const { AwsLambdaInstrumentation } = require('@opentelemetry/instrumentation-aws-lambda');

new AwsLambdaInstrumentation({
    requestHook: (span, { event, context }) => {
        span.setAttribute('faas.name', context.functionName);
    },
    responseHook: (span, { err, res }) => {
        if (err instanceof Error) span.setAttribute('faas.error', err.message);
        if (res) span.setAttribute('faas.res', res);
    }
})

Specifying the Lambda Handler

The instrumentation will attempt to automatically determine the Lambda handler function to instrument. To do this, it relies on the _HANDLER environment variable which is set by the Lambda runtime. For most use cases, this will accurately represent the handler that should be targeted by this instrumentation.

There exist use cases where the _HANDLER environment variable does not accurately represent the module that should be targeted by this instrumentation. For these use cases, the lambdaHandler option can be used to explicitly specify the Lambda handler that should be instrumented.

To better explain when lambdaHandler should be specified, consider how some telemetry tools, such as Datadog, are instrumented into the Lambda runtime. Datadog does this by overriding the handler function with a wrapper function that is loaded via a Lambda Layer. In these examples, the Lambda's handler will point to the Datadog wrapper and not to the actual handler that should be instrumented. In cases like this, lambdaHandler should be used to explicitly specify the handler that should be instrumented.

The lambdaHandler should be specified as a string in the format <file>.<handler>, where <file> is the name of the file that contains the handler and <handler> is the name of the handler function. For example, if the handler is defined in the file index.js and the handler function is named handler, the lambdaHandler should be specified as index.handler.

One way to determine if the lambdaHandler option should be used is to check the handler defined on your Lambda. This can be done by determining the value of the _HANDLER environment variable or by viewing the Runtime Settings of your Lambda in AWS Console. If the handler is what you expect, then the instrumentation should work without the lambdaHandler option. If the handler points to something else, then the lambdaHandler option should be used to explicitly specify the handler that should be instrumented.

Context Propagation

AWS Active Tracing can provide a parent context for the span generated by this instrumentation. Note that the span generated by Active Tracing is always reported only to AWS X-Ray. Therefore, if the OpenTelemetry SDK is configured to export traces to a backend other than AWS X-Ray, this will result in a broken trace.

If you use version <=0.46.0 of this package, then the Active Tracing context is used as the parent context by default if present. In this case, in order to prevent broken traces, set the disableAwsContextPropagation option to false. Additional propagators can be added in the TracerProvider configuration.

If you use version >0.46.0, the Active Tracing context is no longer used by default. In order to enable it, include the AWSXRayLambdaPropagator propagator in the list of propagators provided to the TracerProvider via its configuration, or by including xray-lambda in the OTEL_PROPAGATORS environment variable (see the example below on using the env variable).

Note that there are two AWS-related propagators: AWSXRayPropagator and AWSXRayLambdaPropagator. Here is a guideline for when to use one or the other:

  • If you export traces to AWS X-Ray, then use the AWSXRayLambdaPropagator or the xray-lambda value in the OTEL_PROPAGATORS environment variable. This will handle the active tracing lambda context as well as X-Ray HTTP headers.
  • If you export traces to a backend other than AWS X-Ray, then use the AWSXrayPropagator or xray in the environment variable. This propagator only handles the X-Ray HTTP headers.

Examples:

  1. Active Tracing is enabled and the OpenTelemetry SDK is configured to export traces to AWS X-Ray. In this case, configure the SDK to use the AWSXRayLambdaPropagator.
const { NodeTracerProvider } = require('@opentelemetry/sdk-trace-node');
const { AWSXRayLambdaPropagator } = require('@opentelemetry/propagator-aws-xray-lambda');

const provider = new NodeTracerProvider();
provider.register({
  propagator: new AWSXRayLambdaPropagator()
});

Alternatively, use the getPropagators() function from the auto-configuration-propagators package, and set the OTEL_PROPAGATORS environment variable to xray-lambda.

const { NodeTracerProvider } = require('@opentelemetry/sdk-trace-node');
const { getPropagator } = require('@opentelemetry/auto-configuration-propagators');

const provider = new NodeTracerProvider();
provider.register({
  propagator: getPropagator()
});
  1. The OpenTelemetry SDK is configured to export traces to a backend other than AWX X-Ray, but the lambda function is invoked by other AWS services which send the context using the X-Ray HTTP headers. In this case, include the AWSXRayPropagator, which extracts context from the HTTP header but not the Lambda Active Tracing context.
const { NodeTracerProvider } = require('@opentelemetry/sdk-trace-node');
const { AWSXRayLambdaPropagator } = require('@opentelemetry/propagator-aws-xray-lambda');

const provider = new NodeTracerProvider();
provider.register({
  propagator: new AWSXRayPropagator()
});

Alternatively, use the auto-configuration-package as in example #1 and set the OTEL_PROPAGATORS environment variable to xray.

For additional information, see the documentation for lambda semantic conventions.

Semantic Conventions

This package uses @opentelemetry/semantic-conventions version 1.22+, which implements Semantic Convention Version 1.7.0

Attributes collected:

Attribute Short Description
cloud.account.id The cloud account ID the resource is assigned to.
faas.execution The execution ID of the current function execution.
faas.id The unique ID of the single function that this runtime instance executes.

License

Apache 2.0 - See LICENSE for more information.

changelog

CHANGELOG

As of v0.25.1 (2022-01-24) changelog content has moved to separate CHANGELOG.md files for each package. Use this search for a list of all CHANGELOG.md files in this repo.

Unreleased

0.25.0

:bug: Bug Fix

  • #619 fix: GraphQL throws TypeError: Cannot read property 'startToken' of undefined (@obecny)
  • #643 fix(user-interaction): event listeners have wrong this when listening for bubbled events (@t2t2)
  • #562 fix(mysql): bind get connection callback to active context (@sstone1)
  • #589 fix(hapi-instrumentation): close spans on errors in instrumented functions (@CptSchnitz)
  • #580 fix: redis instrumentation loses context when using callbacks (@aspectom)

:rocket: Enhancement

  • Other
  • opentelemetry-test-utils
    • #593 feat: move aws/gcp detectors from opentelemetry-js repo (@legendecas)

:house: Internal

  • opentelemetry-test-utils
    • #641 chore(mysql2): adding TAV script (@YanivD)
    • #639 build(test-utils): marking test-utils as non private so it can be published (@blumamir)
    • #596 docs(test-utils): add README.md for @opentelemetry/test-utils (@Rauno56)
  • Other

:memo: Documentation

  • opentelemetry-browser-extension-autoinjection

Committers: 16

0.24.0

:bug: Bug Fix

:rocket: Enhancement

  • opentelemetry-browser-extension-autoinjection, opentelemetry-host-metrics, opentelemetry-id-generator-aws-xray, opentelemetry-test-utils
  • opentelemetry-host-metrics
  • Other
    • #571 fix(instrumentation-hapi): change root span name to route name (@CptSchnitz)
    • #566 feat(aws-lambda): added eventContextExtractor config option (@prsnca)

:house: Internal

  • #592 chore(deps): upgrade @types/pino to be compatible with latest sonic-stream types (@legendecas)
  • #583 style: use single quotes everywhere and add a rule to eslint (@CptSchnitz)
  • #549 chore: enable typescript 4.3 option noImplicitOverride (@Flarna)

Committers: 8

0.23.0

:bug: Bug Fix

  • #557 chore: aligning target for esm build with core repo (@obecny)

:rocket: Enhancement

  • opentelemetry-browser-extension-autoinjection
  • opentelemetry-host-metrics, opentelemetry-id-generator-aws-xray, opentelemetry-test-utils
  • Other
    • #533 feat(mongo instrumentation): added response hook option (@prsnca)
    • #546 feat(aws-lambda): disableAwsContextPropagation config option (@nirsky)
    • #528 feat: postgresql responseHook support (@nata7che)
  • opentelemetry-test-utils

:house: Internal

  • #554 chore: remove unneeded ts-node dev-dependency (@Flarna)

Committers: 9

0.22.0

:bug: Bug Fix

  • #537 fix(instrumentation-user-interaction): support clicks in React apps (@kkruk-sumo)

:rocket: Enhancement

  • opentelemetry-host-metrics, opentelemetry-id-generator-aws-xray, opentelemetry-test-utils

Committers: 2

0.21.0

:bug: Bug Fix

:rocket: Enhancement

  • opentelemetry-host-metrics, opentelemetry-id-generator-aws-xray, opentelemetry-test-utils
  • Other

Committers: 6

0.20.0

:bug: Bug Fix

  • #488 fix: dns plugin remove hostname attribute (@svrnm)
  • #468 moving dev dependency for types to main dependency (@obecny)

:rocket: Enhancement

  • Other
    • #517 feat: use rpcMetadata to update http span name #464 (@vmarchaud)
    • #441 feat(instrumentation-document-load): documentLoad attributes enhancement (@kkruk-sumo)
  • opentelemetry-host-metrics, opentelemetry-id-generator-aws-xray, opentelemetry-test-utils
  • opentelemetry-test-utils
    • #470 chore: removing usage of timed event from api (@obecny)

:house: Internal

  • Other
  • opentelemetry-host-metrics, opentelemetry-id-generator-aws-xray, opentelemetry-test-utils
    • #455 Update gts, eslint, typescript and hapi dependencies (@Flarna)

:memo: Documentation

  • #472 docs: Explicitly state that express instrumentation does not export spans without http instrumentation (@svrnm)
  • #450 chore: prefer use of global TracerProvider/MeterProvider (@Flarna)

Committers: 16

0.16.0

:boom: Breaking Change

  • opentelemetry-host-metrics

:bug: Bug Fix

  • #403 chore: fixing context propagation on mongo callback (@obecny)

:rocket: Enhancement

  • opentelemetry-host-metrics, opentelemetry-id-generator-aws-xray, opentelemetry-test-utils
  • opentelemetry-id-generator-aws-xray
  • Other

:house: Internal

  • opentelemetry-host-metrics, opentelemetry-test-utils
  • Other
    • #434 chore: clean up images from restify example (@Rauno56)

:memo: Documentation

Committers: 9

0.15.0

:rocket: Enhancement

  • Other
  • auto-instrumentation-web
  • auto-instrumentation-node
    • #379 chore: creating meta package for default auto instrumentations for node (@obecny)
  • opentelemetry-instrumentation-hapi
  • opentelemetry-instrumentation-koa
  • opentelemetry-instrumentation-mysql
  • opentelemetry-instrumentation-net
  • opentelemetry-host-metrics
    • #395 chore: fixing broken links, updating to correct base url, replacing gitter with github discussions (@obecny)

:house: Internal

  • opentelemetry-host-metrics, opentelemetry-test-utils
    • #408 chore: bump otel dependencies to latest patch (@dyladan)
  • Other

:memo: Documentation

  • opentelemetry-host-metrics

Committers: 9

0.14.0

:bug: Bug Fix

:rocket: Enhancement

  • #354 refactor: migrate mongodb to instrumentation #250 (@vmarchaud)
  • #381 chore: fixing the graphql example and allowing support version of graph from ver 14 (@obecny)
  • #372 feat(instrumentation-ioredis): add requireParentSpan option to config (@blumamir)

:house: Internal

Committers: 5

0.13.1

:rocket: Enhancement

:house: Internal

Committers: 4

0.13.0

:bug: Bug Fix

  • opentelemetry-test-utils
    • #239 fix(plugin-ioredis): end span on response from the server and set span status according to response (@blumamir)
  • Other

:rocket: Enhancement

  • Other
  • opentelemetry-host-metrics, opentelemetry-test-utils

:memo: Documentation

  • opentelemetry-host-metrics

Committers: 7

0.12.1

:bug: Bug Fix

:rocket: Enhancement

  • #273 feat: enable root span route instrumentation without any express layer spans (@shyimo)
  • #298 Add CodeQL Security Scans (@amanbrar1999)

Committers: 7

0.12.0

:bug: Bug Fix

  • #241 fix(ioredis): set net.peer.name attribute according to spec (@blumamir)

:rocket: Enhancement

  • Other
  • opentelemetry-host-metrics
    • #266 chore: refactoring host metrics, aligning with semantic conventions (@obecny)

:house: Internal

  • opentelemetry-host-metrics, opentelemetry-test-utils
  • Other
    • #259 fix(plugin-document-load): check if getEntriesByType is available before using it (@mhennoch)
    • #257 docs(readme): add @opentelemetry/instrumentation-graphql (@Hongbo-Miao)

Committers: 7

0.11.0

:bug: Bug Fix

  • #221 fix: wrapper function for hapi route & plugins (@jk1z)
  • #225 pg spans disconnected from parent (@obecny)
  • #208 [mysql] fix: ensure span name is a string to avoid [object Object] as span name (@naseemkullah)
  • #175 fix: accept EventListener callbacks (@johnbley)
  • #188 fix(express): listen for finish event on response for async express layer #107 (@vmarchaud)

:rocket: Enhancement

Committers: 9

0.10.0

:bug: Bug Fix

:tada: New Plugins

:rocket: Enhancement

:house: Internal

Committers: 9

0.9.0

:rocket: (Enhancement)

:bug: (Bug Fix)

  • #158 fix: patch removeEventListener to properly remove patched callbacks (@johnbley)

Committers: 10

0.8.0 (@opentelemetry/propagator-grpc-census-binary)

:rocket: (Enhancement)

Committers: 1

0.8.0

Released 2020-05-29

:rocket: (Enhancement)

  • #30 Support OpenTelemetry SDK 0.8.x (@dyladan)
  • opentelemetry-plugin-mongodb
  • opentelemetry-plugin-ioredis
    • #33 feat(opentelemetry-plugin-ioredis): provide a custom serializer fn for db.statement (@marcoreni)

Committers: 3

0.7.0

Released 2020-04-27

:bug: (Bug Fix)

  • opentelemetry-plugin-express
  • opentelemetry-plugin-mongodb
    • #5 fix(mongodb): avoid double patching when enable is called twice (@vmarchaud)
  • opentelemetry-plugin-mongodb
    • #3 Prevent double wrapping pg pool query (@dyladan)

:rocket: (Enhancement)

  • opentelemetry-plugin-express

Committers: 3

0.6.1

Released 2020-04-08

For details about this release and all previous releases, see https://github.com/open-telemetry/opentelemetry-js/blob/main/CHANGELOG.md