OpenTelemetry interoperability on AWS Lambda

OpenTelemetry is a collection of tools, APIs, and SDKs. You can use it to instrument, generate, collect, and export telemetry data (metrics, logs, and traces) for analysis to get insights into your software's performance and behavior.

Enable OpenTelemetry

OpenTelemetry interoperability is disabled by default in Dynatrace, but you can manually enable it. For instance, to enable OpenTelemetry when you configure OneAgent using the environment variables, add DT_OPEN_TELEMETRY_ENABLE_INTEGRATION=true.

Enabling OpenTelemetry interoperability connects OneAgent to the OpenTelemetry API. This allows OneAgent on AWS Lambda to make use of the instrumentation packages and extensions available for their respective OpenTelemetry implementation, which enables monitoring of technologies like databases or messaging frameworks that aren't supported by OneAgent out of the box.

Note: If an OpenTelemetry SDK already exists, it will be evicted by OneAgent if this option is enabled.

OpenTelemetry Python

OneAgent version 1.219+

Using an OpenTelemetry Python instrumentation

OpenTelemetry for Python provides a number of instrumentation packages in their OpenTelemetry Python contributions repository.

The code snippet below shows how to instrument PostgreSQL calls to your Python Lambda function by using the aiopg instrumentation package.

import json
import aiopg

from config import Configuration
from opentelemetry.instrumentation.aiopg import AiopgInstrumentor

AiopgInstrumentor().instrument()

config = Configuration()

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

Using the OpenTelemetry Python API

The code snippet below shows how 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.

opentel-lambda

OpenTelemetry JavaScript (Node.js)

OneAgent version 1.229+

Using an OpenTelemetry Node.js instrumentation

OpenTelemetry for JavaScript provides a number of instrumentation packages in their OpenTelemetry JavaScript contributions repository.

The code snippet below shows how to instrument PostgreSQL calls to your Node.js Lambda function by using the opentelemetry-instrumentation-pg instrumentation package.

const { registerInstrumentations } = require('@opentelemetry/instrumentation');
const { PgInstrumentation } = require('@opentelemetry/instrumentation-pg');
const { Pool } = require('pg');

registerInstrumentations({
  instrumentations: [
    new PgInstrumentation(),
  ],
});

exports.handler = function(event, context, callback) {
  try {
    const pool = new Pool(/* DB connection information */);
    pool.connect((connectErr, client, release) => {
        if (connectErr) throw connectErr;
        release();
    });

    pool.query("SELECT * FROM users;", (err, ret) => {
      if (err) {
          throw err;
      }
      context.succeed(ret.rows);
    });
  } catch (e) {
    console.log(e);
  }
  pool.end();
}

Using the OpenTelemetry Node.js API

The code snippet below shows how OpenTelemetry JavaScript can be used in an SDK-like approach to trace additional operations that aren't covered by an instrumentation package.

const opentelemetry = require('@opentelemetry/api');

const tracer = opentelemetry.trace.getTracer('my-package-name');

exports.handler = function(event, context) {
  // create a span using the OTel API
  const span = tracer.startSpan("do some work");
  span.setAttribute('foo', 'bar');
  span.end();

  // ...

  const response = {
    statusCode: 200,
    body: JSON.stringify('Hello from Node.js'),
  };
  return response;
};

OpenTelemetry Java

OneAgent version 1.225+

Using the OpenTelemetry Java API

The code snippet below shows how OpenTelemetry Java can be used in an SDK-like approach to trace additional operations that aren't covered by an instrumentation package.

@Override
    public String handleRequest(Object input, Context context) {
        OpenTelemetrySdk openTelemetrySdk = OpenTelemetrySdk.builder().build();
        Tracer tracer = openTelemetrySdk.getTracer("instrumentation-library-name", "1.0.0");

        Span span = tracer.spanBuilder("do some work").startSpan();
        span.setAttribute("foo", "bar");
        span.end();

        // ....

        return "Hello from OpenTelemetry Java!";
    }