AWS Lambda logs in context of traces
This page describes how to include TraceId
and SpanId
information in user-generated log messages. This way, you can associate log messages with the surrounding trace.
Prerequisites
- Set up AWS Lambda log collection or AWS log forwarding.
- Enable OpenTelemetry interoperability for the Lambda function.
- The log output generated by the AWS Lambda function must be formatted so that the
TraceId
andSpanId
information can be picked up. The expected format of the enriched fields in an unstructured log is as follows:- Encapsulated in square brackets
[]
with a!dt
prefix dt.trace_id
anddt.span_id
must be hex-encoded strings Example:[!dt dt.trace_id=0af7651916cd43dd8448eb211c80319c,dt.span_id=00f067aa0ba902b7]
- Encapsulated in square brackets
OpenTelemetry Python
In the example below, a dt_log
function has been created to enrich a given log message with the trace_id
and span_id
information. Printing this enriched message to stdout
associates the log message with the currently active span in the Dynatrace web UI.
from opentelemetry import trace
def dt_log(msg):
ctx = trace.get_current_span().get_span_context()
trace_id = format(ctx.trace_id, "032x")
span_id = format(ctx.span_id, "016x")
print("[!dt dt.trace_id={},dt.span_id={}] - {}".format(trace_id, span_id, msg));
def lambda_handler(event, context):
msg = "Hello from AWS Lambda Python"
dt_log(msg)
return {
"statusCode": 200,
"body": msg
}
OpenTelemetry JavaScript (Node.js)
In the example below, a dt_log
function has been created to enrich a given log message with the trace_id
and span_id
information. Printing this enriched message to stdout
associates the log message with the currently active span in the Dynatrace web UI.
const opentelemetry = require('@opentelemetry/api');
function dtLog(msg) {
let current_span = opentelemetry.trace.getSpan(opentelemetry.context.active());
let trace_id = current_span.spanContext().traceId;
let span_id = current_span.spanContext().spanId;
console.log(`[!dt dt.trace_id=${trace_id},dt.span_id=${span_id}] - ${msg}`);
}
exports.handler = function(event, context) {
let msg = "Hello from AWS Lambda Node.js"
dtLog(msg);
context.succeed({
statusCode: 200,
body: msg
});
};
OpenTelemetry Java
In the example below, a dtLog
method has been created to enrich a given log message with the TraceId
and SpanId
information. Printing this enriched message via System.out
associates the log message with the currently active span in the Dynatrace web UI.
package com.amazonaws.lambda.demo;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.SpanContext;
public class HelloJava implements RequestHandler<Object, String> {
private static void dtLog(final String msg) {
SpanContext spanContext = Span.current().getSpanContext();
System.out.printf(
"[!dt dt.trace_id=%s,dt.span_id=%s] - %s%n",
spanContext.getTraceId(),
spanContext.getSpanId(),
msg
);
}
@Override
public String handleRequest(Object input, Context context) {
String msg = "Hello from AWS Lambda Java";
dtLog(msg);
return msg;
}
}