OpenTelemetry interoperability in Python

OpenTelemetry interoperability connects the Dynatrace AWS Lambda extension to the OpenTelemetry Python instrumentation to use the instrumentation packages and extensions. You can then monitor technologies like databases or messaging frameworks that aren't supported by Dynatrace AWS Lambda extension out of the box.

Before you start

  • Ensure that OpenTelemetry interoperability is enabled.

  • Verify that the installed OpenTelemetry Python API version is compatible with the Dynatrace AWS Lambda extension. The following table lists the compatible versions:

    OneAgent version
    Maximum OpenTelemetry API version
    1.233+
    1.7.x
    1.235+
    1.8.x
    1.239+
    1.9.x
    1.243+
    1.11.x
    1.249+
    1.12.x
    1.253+
    1.13.x
    1.257+
    1.14.x
    1.259+
    1.15.x
    1.265+
    1.17.x
    1.269+
    1.18.x
    1.273+
    1.19.x
    1.277+
    1.20.x
    1.281+
    1.21.x
    1.285+
    1.22.x
    1.291+
    1.24.x
    1.295+
    1.25.x
    1.299+
    1.26.x

Use OpenTelemetry Python instrumentation

OpenTelemetry for Python provides several instrumentation packages in their OpenTelemetry Python contributions repository.

The following code example shows how to instrument PostgreSQL queries in a Python Lambda function by using the aiopg instrumentation package.

import json
import aiopg
from opentelemetry.instrumentation.aiopg import AiopgInstrumentor
AiopgInstrumentor().instrument()
def lambda_handler(event, context):
return {
'statusCode': 200,
'body': json.dumps(execute_query())
}
def execute_query():
result = []
with aiopg.connect(database='my_db') as conn:
with conn.cursor() as cur:
cur.execute("SELECT 'hello db';")
for row in cur:
result.append(row)
return result

To instrument boto3, the AWS SDK for Python, OpenTelemetry provides the botocore instrumentation package.

The code example below shows how the botocore instrumentation can be used to add observability for calls to a DynamoDB database (Dynatrace version 1.244+).

import boto3
import json
from opentelemetry.instrumentation.botocore import BotocoreInstrumentor
BotocoreInstrumentor().instrument()
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('MyTable')
def lambda_handler(event, handler):
result = table.get_item(Key={'mykey': 42})
return {
"statusCode": 200,
"answer": json.dumps(result.get("Item"))
}

DynamoDB service screen

Use OpenTelemetry Python API

OpenTelemetry Python can be used in an SDK-like approach to trace additional operations that aren't covered by an instrumentation package.

import json
from opentelemetry import trace
def lambda_handler(event, context):
tracer = trace.get_tracer(__name__)
with tracer.start_as_current_span("do work"):
# do work
with tracer.start_as_current_span("do some more work") as span:
span.set_attribute("foo", "bar")
# do some more work
return {
'statusCode': 200,
'body': json.dumps('Hello from Hello world from OpenTelemetry Python!')
}

These spans are displayed on the Code level tab.

OpenTelemetry lambda

Trace AWS SQS and SNS messages with Python

OneAgent version 1.253+ for SQS OneAgent version 1.257+ for SNS

You can use open-source instrumentation packages to trace AWS SQS and SNS messages and collect them via the Dynatrace AWS Lambda extension.

Step 1 Install the required dependencies

Step 2 Send an SQS/SNS message

The boto3 package is available out of the box if the code runs in AWS Lambda, but you can also install it using pip install -U boto3.

This code defining a function named lambda_handler can be used

  • Inside AWS Lambda (we recommend monitoring it with our AWS Lambda layer)

  • Outside AWS Lambda (monitoring is performed with OpenTelemetry and exported to Dynatrace via OTLP/HTTP ingest)

    You might want to remove the function parameters and return value.

Step 3 Receive an SQS/SNS message