Access to trace id?
uttrasey opened this issue · comments
Matt Uttridge commented
Expected Behavior
Hello, sorry this isn't actually a bug. I'm trying to figure out how to access the datadog trace ID from within my executing lambda? I'm using the datadog lambda extension (and all working well)
Actual Behavior
Not sure how to access it
Andrew Rodriguez commented
Hi @uttrasey you should be able to do something like
const tracer = require('dd-trace') //this is supplied by the Datadog-Node18-x lambda layer
module.exports.handler = async (event) => {
const span = tracer.scope().active(); // get the active span
const traceId = span ? span.context().toTraceId() : null; // get the trace id
console.log(`Current traceId: ${traceId}`);
return {
statusCode: 200,
body: JSON.stringify(
{
message: "Go Serverless v3.0! Your function executed successfully!",
input: event,
},
null,
2
),
};
};
Which when run in lambda with the following lambda layers
Datadog-Node18-x 91
Datadog-Extension 44
produces the following on the datadog serverless trace view
Got the motivation from: DataDog/dd-trace-js#2116
Matt Uttridge commented
Thanks for the thorough and totally awesome response @zARODz11z. This is exactly what I need.