Skip to content

Commit

Permalink
clean up code
Browse files Browse the repository at this point in the history
  • Loading branch information
wconti27 committed Jan 3, 2025
1 parent 9c57dac commit 6ba59bb
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 79 deletions.
4 changes: 3 additions & 1 deletion packages/dd-trace/src/noop/span.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
116 changes: 38 additions & 78 deletions packages/dd-trace/src/opentracing/tracer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}
}

Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 6ba59bb

Please sign in to comment.