import tracer from 'dd-trace';
import { version } from '../../package.json';
import { buildVars } from '#root/.build/buildVars';
tracer.init({
service: process.env.DD_SERVICE || 'my-fe-app',
version: version ? `${version}-${buildVars.GIT_SHA}` : '0.0.0',
tags: {
pid: process.pid,
},
runtimeMetrics: true,
logInjection: true,
});
Calling this
DatadogSpanContext.toTraceId()method on what appears to be a valid span can sometimes error out with an undefined innerthis._traceIdvalue:dd-trace-js/packages/dd-trace/src/opentracing/span_context.js
Lines 33 to 40 in 497c0b0
Suggested Solution
While I am not sure what might cause this scenario, a quick fix would be to make the dot property access of
this._traceIda bit more defensive:Replication Steps
Using:
Initialize the tracer on startup:
Try to defensively get the active span's
traceIDat runtimeExpected result
traceIDis either a valid Trace ID from the current span orundefinedin which case we default to'n/a/based on the code aboveActual result
We regularly encounter an error whereby we seem to have a defined
span(i.e. notnullorundefined) with an empty or missingDatadogSpanContext._traceIDproperty such that when we calltoTraceId()on it we get the following error:Unit Tests
Here is a failing test we can add to
packages/dd-trace/test/opentracing/span_context.spec.jsto demonstrate the issue