DataDog / datadog-lambda-js

The Datadog AWS Lambda Library for Node

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Access to trace id?

uttrasey opened this issue · comments

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

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
Screenshot 2023-06-21 at 9 13 17 AM

Got the motivation from: DataDog/dd-trace-js#2116

Thanks for the thorough and totally awesome response @zARODz11z. This is exactly what I need.