diff --git a/packages/dd-trace/src/opentelemetry/tracer.js b/packages/dd-trace/src/opentelemetry/tracer.js index bf2a0c3f86b..740847f9fc0 100644 --- a/packages/dd-trace/src/opentelemetry/tracer.js +++ b/packages/dd-trace/src/opentelemetry/tracer.js @@ -6,8 +6,10 @@ const { sanitizeAttributes } = require('@opentelemetry/core') const Sampler = require('./sampler') const Span = require('./span') const id = require('../id') +const log = require('../log') const SpanContext = require('./span_context') const TextMapPropagator = require('../opentracing/propagation/text_map') +const TraceState = require('../opentracing/propagation/tracestate') class Tracer { constructor (library, config, tracerProvider) { @@ -39,7 +41,49 @@ class Tracer { // Extracted method to create span context for a new span _createSpanContextForNewSpan (context) { const { traceId, spanId, traceFlags, traceState } = context - return TextMapPropagator._convertOtelContextToDatadog(traceId, spanId, traceFlags, traceState) + return this._convertOtelContextToDatadog(traceId, spanId, traceFlags, traceState) + } + + _convertOtelContextToDatadog (traceId, spanId, traceFlag, ts, meta = {}) { + const origin = null + let samplingPriority = traceFlag + + ts = ts?.traceparent || null + + if (ts) { + // Use TraceState.fromString to parse the tracestate header + const traceState = TraceState.fromString(ts) + let ddTraceStateData = null + + // Extract Datadog specific trace state data + traceState.forVendor('dd', (state) => { + ddTraceStateData = state + return state // You might need to adjust this part based on actual logic needed + }) + + if (ddTraceStateData) { + // Assuming ddTraceStateData is now a Map or similar structure containing Datadog trace state data + // Extract values as needed, similar to the original logic + const samplingPriorityTs = ddTraceStateData.get('s') + const origin = ddTraceStateData.get('o') + // Convert Map to object for meta + const otherPropagatedTags = Object.fromEntries(ddTraceStateData.entries()) + + // Update meta and samplingPriority based on extracted values + Object.assign(meta, otherPropagatedTags) + samplingPriority = TextMapPropagator._getSamplingPriority(traceFlag, parseInt(samplingPriorityTs, 10), origin) + } else { + log.debug(`no dd list member in tracestate from incoming request: ${ts}`) + } + } + + const spanContext = new SpanContext({ + traceId: id(traceId, 16), spanId: id(), tags: meta, parentId: id(spanId, 16) + }) + + spanContext._sampling = { priority: samplingPriority } + spanContext._trace = { origin } + return spanContext } startSpan (name, options = {}, context = api.context.active()) { diff --git a/packages/dd-trace/src/opentracing/propagation/text_map.js b/packages/dd-trace/src/opentracing/propagation/text_map.js index fd7d32760cb..cefe9e9ffae 100644 --- a/packages/dd-trace/src/opentracing/propagation/text_map.js +++ b/packages/dd-trace/src/opentracing/propagation/text_map.js @@ -3,7 +3,6 @@ const pick = require('../../../../datadog-core/src/utils/src/pick') const id = require('../../id') const DatadogSpanContext = require('../span_context') -const OtelSpanContext = require('../../opentelemetry/span_context') const log = require('../../log') const TraceState = require('./tracestate') const tags = require('../../../../../ext/tags') @@ -693,48 +692,6 @@ class TextMapPropagator { return spanContext._traceId.toString(16) } - static _convertOtelContextToDatadog (traceId, spanId, traceFlag, ts, meta = {}) { - const origin = null - let samplingPriority = traceFlag - - ts = ts?.traceparent || null - - if (ts) { - // Use TraceState.fromString to parse the tracestate header - const traceState = TraceState.fromString(ts) - let ddTraceStateData = null - - // Extract Datadog specific trace state data - traceState.forVendor('dd', (state) => { - ddTraceStateData = state - return state // You might need to adjust this part based on actual logic needed - }) - - if (ddTraceStateData) { - // Assuming ddTraceStateData is now a Map or similar structure containing Datadog trace state data - // Extract values as needed, similar to the original logic - const samplingPriorityTs = ddTraceStateData.get('s') - const origin = ddTraceStateData.get('o') - // Convert Map to object for meta - const otherPropagatedTags = Object.fromEntries(ddTraceStateData.entries()) - - // Update meta and samplingPriority based on extracted values - Object.assign(meta, otherPropagatedTags) - samplingPriority = TextMapPropagator._getSamplingPriority(traceFlag, parseInt(samplingPriorityTs, 10), origin) - } else { - log.debug(`no dd list member in tracestate from incoming request: ${ts}`) - } - } - - const spanContext = new OtelSpanContext({ - traceId: id(traceId, 16), spanId: id(), tags: meta, parentId: id(spanId, 16) - }) - - spanContext._sampling = { priority: samplingPriority } - spanContext._trace = { origin } - return spanContext - } - static _getSamplingPriority (traceparentSampled, tracestateSamplingPriority, origin = null) { const fromRumWithoutPriority = !tracestateSamplingPriority && origin === 'rum'