DataDog / dd-trace-js

JavaScript APM Tracer
https://docs.datadoghq.com/tracing/
Other
639 stars 302 forks source link

Decimal Trace ID format in logs #4524

Open francircle opened 2 months ago

francircle commented 2 months ago

Hi team,

According to Datadog docs, the trace ID has 128 bits by default, which is equivalent to 32 hex characters like 66884212000000004443333311122222.

I am able to see traceId like this in datadog, but for my winston logger, which instrumented by using dd-trace plugin like this

tracer.use('winston', {
    enabled: true,
  });

I see the trace id in dd object is in another format, it's a 19-digit decimal string like 5295287788888597036.

After I dug in the source code in dd-trace-js, I found this piece of logic

toTraceId (get128bitId = false) {
  if (get128bitId) {
    return this._traceId.toBuffer().length <= 8 && this._trace.tags[TRACE_ID_128]
      ? this._trace.tags[TRACE_ID_128] + this._traceId.toString(16).padStart(16, '0')
      : this._traceId.toString(16).padStart(32, '0')
  }
  return this._traceId.toString(10)
}

toSpanId (get128bitId = false) {
  if (get128bitId) {
    return this._spanId.toString(16).padStart(16, '0')
  }
  return this._spanId.toString(10)
}

I saw the 19-digit decimal string is actually the lower half of trace ID in decimal format. I wonder why it returns decimal string by default instead of the 32 hex chars traceId like we see in datadog dashboard and why is the get128bitId is false by default. Is there any reason behind this? Thanks!

jaridmargolin commented 1 month ago

I found that this can be altered by setting the environment variable DD_TRACE_128_BIT_TRACEID_LOGGING_ENABLED=true but it looks like it may contain some bugs. I just sent the following message in the Datadog slack apm chanel: https://datadoghq.slack.com/archives/C3SH3KCQG/p1722447251471329

francircle commented 1 month ago

@jaridmargolin thanks for sharing this. What kind of bugs you encountered?

jaridmargolin commented 4 weeks ago

Datadog submitted a patch that fixed the issue I was seeing: https://github.com/DataDog/dd-trace-js/commit/3e179d010a20a501d9b62522f11e97ee5d3ccbe0

You should now be free to use DD_TRACE_128_BIT_TRACEID_LOGGING_ENABLED=true. I'm not entirely sure why it isn't exposed as a config option.