OpenTelemetry AWS lambda Instrumentation

The opentelemetry-instrumentation-aws-lambda package provides an Instrumentor to traces calls within a Python AWS Lambda function.

Usage

# Copy this snippet into an AWS Lambda function

import boto3
from opentelemetry.instrumentation.botocore import BotocoreInstrumentor
from opentelemetry.instrumentation.aws_lambda import AwsLambdaInstrumentor

# Enable instrumentation
BotocoreInstrumentor().instrument()
AwsLambdaInstrumentor().instrument()

# Lambda function
def lambda_handler(event, context):
    s3 = boto3.resource('s3')
    for bucket in s3.buckets.all():
        print(bucket.name)

    return "200 OK"

API

The instrument method accepts the following keyword args:

tracer_provider (TracerProvider) - an optional tracer provider meter_provider (MeterProvider) - an optional meter provider event_context_extractor (Callable) - a function that returns an OTel Trace Context given the Lambda Event the AWS Lambda was invoked with this function signature is: def event_context_extractor(lambda_event: Any) -> Context for example:

from opentelemetry.instrumentation.aws_lambda import AwsLambdaInstrumentor

def custom_event_context_extractor(lambda_event):
    # If the `TraceContextTextMapPropagator` is the global propagator, we
    # can use it to parse out the context from the HTTP Headers.
    return get_global_textmap().extract(lambda_event["foo"]["headers"])

AwsLambdaInstrumentor().instrument(
    event_context_extractor=custom_event_context_extractor
)

class opentelemetry.instrumentation.aws_lambda.AwsLambdaInstrumentor(*args, **kwargs)[source]

Bases: BaseInstrumentor

instrumentation_dependencies()[source]

Return a list of python packages with versions that the will be instrumented.

The format should be the same as used in requirements.txt or pyproject.toml.

For example, if an instrumentation instruments requests 1.x, this method should look like: :rtype: Collection[str]

def instrumentation_dependencies(self) -> Collection[str]:

return [‘requests ~= 1.0’]

This will ensure that the instrumentation will only be used when the specified library is present in the environment.