From 6ba59bbf48d30f38e21041ab167cf200ea567f4d Mon Sep 17 00:00:00 2001 From: William Conti Date: Fri, 3 Jan 2025 10:21:27 -0500 Subject: [PATCH] clean up code --- packages/dd-trace/src/noop/span.js | 4 +- packages/dd-trace/src/opentracing/tracer.js | 116 +++++++------------- 2 files changed, 41 insertions(+), 79 deletions(-) diff --git a/packages/dd-trace/src/noop/span.js b/packages/dd-trace/src/noop/span.js index f0af76dc807..42812e02dab 100644 --- a/packages/dd-trace/src/noop/span.js +++ b/packages/dd-trace/src/noop/span.js @@ -42,8 +42,10 @@ class NoopSpan { const spanId = id() if (parent) { + // necessary for trace level configuration. This pattern returns the first valid span context that is not a + // NoopSpanContext, aka the next parent span in the trace that will be kept. if (options.keepParent) { - return parent + return parent.context() } return new NoopSpanContext({ diff --git a/packages/dd-trace/src/opentracing/tracer.js b/packages/dd-trace/src/opentracing/tracer.js index ae5fdd93fcc..b732436e5b1 100644 --- a/packages/dd-trace/src/opentracing/tracer.js +++ b/packages/dd-trace/src/opentracing/tracer.js @@ -49,42 +49,6 @@ class DatadogTracer { } startSpan (name, options = {}) { - const updateParentSpan = (ctx, span, options, name) => { - let tags - if (span) { - tags = span?.context()._tags ?? {} - } else if (ctx) { - tags = ctx?._tags ?? {} - } else { - return - } - - // Find the highest index in operations keys - const keys = Object.keys(tags).filter(key => key.startsWith('operations.')) - const indices = keys.map(key => parseInt(key.split('.')[1], 10)) // Extract the index from the keys - const nextIndex = new Set(indices).size - - // Update operations object with new tags using dot notation - span.setTag(`operations.${nextIndex}.name`, name) - - // Loop through options to add sub tags under the new index - Object.entries(options).forEach(([key, value]) => { - if (key !== 'childOf' && key !== 'tags') { - span.setTag(`operations.${nextIndex}.${key}`, value) - } - }) - - if (options.tags) { - // Loop through options to add sub tags under the new index - Object.entries(options.tags).forEach(([key, value]) => { - if (key !== 'childOf') { - span.setTag(`operations.${nextIndex}.tags.${key}`, value) - } - }) - } - return nextIndex - } - const parent = options.childOf ? getContext(options.childOf) : getParent(options.references) @@ -94,48 +58,10 @@ class DatadogTracer { 'service.name': options?.tags?.service ? String(options.tags.service) : this._service } - // zero trace level indicates service exit / entry span only - if (this._config.traceLevel === 0) { - // if the parent is a SpanContext, this is a distributed trace and should create a child span - // if the parent is a Span or NoopSpan, this is from the same service - if ( - parent instanceof Span || parent instanceof NoopSpan || - options.childOf instanceof Span || options.childOf instanceof NoopSpan - ) { - let metaIndex = 0 - // if (parent instanceof Span) { - // metaIndex = updateParentSpan(null, options.childOf, options, name) - // } else if (parent instanceof SpanContext) { - // metaIndex = updateParentSpan(null, options.childOf, options, name) - // } - return new NoopSpan(this, parent, { keepParent: true, metaIndex }) - } - } else if (this._config.traceLevel === 1) { - if (parent) { - if ( - options?.tags && parent._tags && options?.tags['span.kind'] && - parent._tags['span.kind'] === options.tags['span.kind'] - ) { - let metaIndex = 0 - // if (parent instanceof Span) { - // metaIndex = updateParentSpan(null, options.childOf, options, name) - // } else if (parent instanceof SpanContext) { - // metaIndex = updateParentSpan(null, options.childOf, options, name) - // } - return new NoopSpan(this, parent, { keepParent: true, metaIndex }) - } - } - } else if (this._config.traceLevel === 2) { - if (parent) { - if (options?.tags?.component && parent?._tags?.component === options?.tags?.component) { - let metaIndex = 0 - // if (parent instanceof Span) { - // metaIndex = updateParentSpan(null, options.childOf, options, name) - // } else if (parent instanceof SpanContext) { - // metaIndex = updateParentSpan(null, options.childOf, options, name) - // } - return new NoopSpan(this, parent, { keepParent: true, metaIndex }) - } + if (this._config.traceLevel !== 'debug') { + const traceLevelSpan = this._useTraceLevel(parent, options) + if (traceLevelSpan) { + return traceLevelSpan } } @@ -187,6 +113,40 @@ class DatadogTracer { return null } } + + _useTraceLevel (parent, options) { + // service trace level indicates service exit / entry spans only + if (this._config.traceLevel === 'service') { + // if the parent is a SpanContext, this is a distributed trace and should create a child span + // if the parent is a Span or NoopSpan, this is from the same service + if ( + parent instanceof Span || parent instanceof NoopSpan || + options.childOf instanceof Span || options.childOf instanceof NoopSpan + ) { + return new NoopSpan(this, parent, { keepParent: true }) + } + } else if (this._config.traceLevel === 'span.kind') { + // span.kind trace level indicates eliminates repeated spans with the same span.kind + if (parent) { + if ( + options?.tags && parent._tags && options?.tags['span.kind'] && + parent._tags['span.kind'] === options.tags['span.kind'] + ) { + return new NoopSpan(this, parent, { keepParent: true }) + } + } + } else if (this._config.traceLevel === 'integration') { + // integration trace level indicates eliminates repeated spans from the same integration + if (parent) { + if (options?.tags?.component && parent?._tags?.component === options?.tags?.component) { + return new NoopSpan(this, parent, { keepParent: true }) + } + } + } else { + log.warn(`Received invalid Datadog Trace Level Configuration: ${this._config.traceLevel}`) + return null + } + } } function getContext (spanContext) {