Skip to content

Commit

Permalink
Merge branch 'master' into force-crash
Browse files Browse the repository at this point in the history
  • Loading branch information
robertomonteromiguel authored Jan 15, 2025
2 parents 7ed9770 + 9e36df0 commit 75b2283
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 17 deletions.
10 changes: 10 additions & 0 deletions benchmark/sirun/profiler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ if (PROFILER === 'space' || PROFILER === 'all') {
profilers.push(new SpaceProfiler())
}

if (profilers.length === 0) {
// Add a no-op "profiler"
profilers.push({
start: () => {},
stop: () => {},
profile: () => { return true },
encode: () => { Promise.resolve(true) }
})
}

const exporters = [{
export () {
profiler.stop()
Expand Down
6 changes: 3 additions & 3 deletions benchmark/sirun/run-all-variants.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

const fs = require('fs')
const path = require('path')
const { exec, getStdio } = require('./run-util')
const { exec, stdio } = require('./run-util')

process.env.DD_INSTRUMENTATION_TELEMETRY_ENABLED = 'false'

Expand All @@ -18,10 +18,10 @@ const env = Object.assign({}, process.env, { DD_TRACE_STARTUP_LOGS: 'false' })
const variants = metaJson.variants
for (const variant in variants) {
const variantEnv = Object.assign({}, env, { SIRUN_VARIANT: variant })
await exec('sirun', ['meta-temp.json'], { env: variantEnv, stdio: getStdio() })
await exec('sirun', ['meta-temp.json'], { env: variantEnv, stdio })
}
} else {
await exec('sirun', ['meta-temp.json'], { env, stdio: getStdio() })
await exec('sirun', ['meta-temp.json'], { env, stdio })
}

try {
Expand Down
4 changes: 2 additions & 2 deletions benchmark/sirun/run-one-variant.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

'use strict'

const { exec, getStdio } = require('./run-util')
const { exec, stdio } = require('./run-util')

process.env.DD_INSTRUMENTATION_TELEMETRY_ENABLED = 'false'

const env = Object.assign({}, process.env, { DD_TRACE_STARTUP_LOGS: 'false' })

exec('sirun', ['meta-temp.json'], { env, stdio: getStdio() })
exec('sirun', ['meta-temp.json'], { env, stdio })
6 changes: 1 addition & 5 deletions benchmark/sirun/run-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ function exec (...args) {
})
}

function getStdio () {
return ['inherit', 'pipe', 'inherit']
}

function streamAddVersion (input) {
input.rl = readline.createInterface({ input })
input.rl.on('line', function (line) {
Expand All @@ -39,6 +35,6 @@ function streamAddVersion (input) {

module.exports = {
exec,
getStdio,
stdio: ['inherit', 'pipe', 'inherit'],
streamAddVersion
}
7 changes: 4 additions & 3 deletions benchmark/sirun/runall.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,14 @@ nvm use 18
# run each test in parallel for a given version of Node.js
# once all of the tests have complete move on to the next version

export CPU_AFFINITY="${CPU_START_ID:-24}" # Benchmarking Platform convention
TOTAL_CPU_CORES=$(nproc 2>/dev/null || echo "24")
export CPU_AFFINITY="${CPU_START_ID:-$TOTAL_CPU_CORES}" # Benchmarking Platform convention

nvm use $MAJOR_VERSION # provided by each benchmark stage
export VERSION=`nvm current`
export ENABLE_AFFINITY=true
echo "using Node.js ${VERSION}"
CPU_AFFINITY="${CPU_START_ID:-24}" # reset for each node.js version
CPU_AFFINITY="${CPU_START_ID:-$TOTAL_CPU_CORES}" # reset for each node.js version
SPLITS=${SPLITS:-1}
GROUP=${GROUP:-1}

Expand All @@ -54,7 +55,7 @@ BENCH_END=$(($GROUP_SIZE*$GROUP))
BENCH_START=$(($BENCH_END-$GROUP_SIZE))

if [[ ${GROUP_SIZE} -gt 24 ]]; then
echo "Group size ${GROUP_SIZE} is larger than available number of CPU cores on Benchmarking Platform machines (24 cores)"
echo "Group size ${GROUP_SIZE} is larger than available number of CPU cores on Benchmarking Platform machines (${TOTAL_CPU_CORES} cores)"
exit 1
fi

Expand Down
46 changes: 43 additions & 3 deletions packages/dd-trace/src/opentelemetry/context_manager.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const { storage } = require('../../../datadog-core')
const { trace, ROOT_CONTEXT } = require('@opentelemetry/api')
const { trace, ROOT_CONTEXT, propagation } = require('@opentelemetry/api')
const DataDogSpanContext = require('../opentracing/span_context')

const SpanContext = require('./span_context')
Expand All @@ -18,17 +18,40 @@ class ContextManager {
const context = (activeSpan && activeSpan.context()) || store || ROOT_CONTEXT

if (!(context instanceof DataDogSpanContext)) {
const span = trace.getSpan(context)
// span instanceof NonRecordingSpan
if (span && span._spanContext && span._spanContext._ddContext && span._spanContext._ddContext._baggageItems) {
const baggages = span._spanContext._ddContext._baggageItems
const entries = {}
for (const [key, value] of Object.entries(baggages)) {
entries[key] = { value }
}
const otelBaggages = propagation.createBaggage(entries)
return propagation.setBaggage(context, otelBaggages)
}
return context
}

const baggages = JSON.parse(activeSpan.getAllBaggageItems())
const entries = {}
for (const [key, value] of Object.entries(baggages)) {
entries[key] = { value }
}
const otelBaggages = propagation.createBaggage(entries)

if (!context._otelSpanContext) {
const newSpanContext = new SpanContext(context)
context._otelSpanContext = newSpanContext
}
if (store && trace.getSpanContext(store) === context._otelSpanContext) {
return store
return otelBaggages
? propagation.setBaggage(store, otelBaggages)
: store
}
return trace.setSpanContext(store || ROOT_CONTEXT, context._otelSpanContext)
const wrappedContext = trace.setSpanContext(store || ROOT_CONTEXT, context._otelSpanContext)
return otelBaggages
? propagation.setBaggage(wrappedContext, otelBaggages)
: wrappedContext
}

with (context, fn, thisArg, ...args) {
Expand All @@ -38,9 +61,26 @@ class ContextManager {
const cb = thisArg == null ? fn : fn.bind(thisArg)
return this._store.run(context, cb, ...args)
}
const baggages = propagation.getBaggage(context)
let baggageItems = []
if (baggages) {
baggageItems = baggages.getAllEntries()
}
if (span && span._ddSpan) {
// does otel always override datadog?
span._ddSpan.removeAllBaggageItems()
for (const baggage of baggageItems) {
span._ddSpan.setBaggageItem(baggage[0], baggage[1].value)
}
return ddScope.activate(span._ddSpan, run)
}
// span instanceof NonRecordingSpan
if (span && span._spanContext && span._spanContext._ddContext && span._spanContext._ddContext._baggageItems) {
span._spanContext._ddContext._baggageItems = {}
for (const baggage of baggageItems) {
span._spanContext._ddContext._baggageItems[baggage[0]] = baggage[1].value
}
}
return run()
}

Expand Down
82 changes: 81 additions & 1 deletion packages/dd-trace/test/opentelemetry/context_manager.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,17 @@ require('../setup/tap')

const { expect } = require('chai')
const ContextManager = require('../../src/opentelemetry/context_manager')
const { ROOT_CONTEXT } = require('@opentelemetry/api')
const TracerProvider = require('../../src/opentelemetry/tracer_provider')
const { context, propagation, trace, ROOT_CONTEXT } = require('@opentelemetry/api')
const api = require('@opentelemetry/api')
const tracer = require('../../').init()

function makeSpan (...args) {
const tracerProvider = new TracerProvider()
tracerProvider.register()
const tracer = tracerProvider.getTracer()
return tracer.startSpan(...args)
}

describe('OTel Context Manager', () => {
let contextManager
Expand Down Expand Up @@ -114,4 +123,75 @@ describe('OTel Context Manager', () => {
})
expect(ret).to.equal('return value')
})

it('should propagate baggage from an otel span to a datadog span', () => {
const entries = {
foo: { value: 'bar' }
}
const baggage = propagation.createBaggage(entries)
const contextWithBaggage = propagation.setBaggage(context.active(), baggage)
const span = makeSpan('otel-to-dd')
const contextWithSpan = trace.setSpan(contextWithBaggage, span)
api.context.with(contextWithSpan, () => {
expect(tracer.scope().active().getBaggageItem('foo')).to.be.equal('bar')
})
})

it('should propagate baggage from a datadog span to an otel span', () => {
const baggageKey = 'raccoon'
const baggageVal = 'chunky'
const ddSpan = tracer.startSpan('dd-to-otel')
ddSpan.setBaggageItem(baggageKey, baggageVal)
tracer.scope().activate(ddSpan, () => {
const baggages = propagation.getActiveBaggage().getAllEntries()
expect(baggages.length).to.equal(1)
const baggage = baggages[0]
expect(baggage[0]).to.equal(baggageKey)
expect(baggage[1].value).to.equal(baggageVal)
})
})

it('should handle dd-otel baggage conflict', () => {
const ddSpan = tracer.startSpan('dd')
ddSpan.setBaggageItem('key1', 'dd1')
let contextWithUpdatedBaggages
tracer.scope().activate(ddSpan, () => {
let baggages = propagation.getBaggage(api.context.active())
baggages = baggages.setEntry('key1', { value: 'otel1' })
baggages = baggages.setEntry('key2', { value: 'otel2' })
contextWithUpdatedBaggages = propagation.setBaggage(api.context.active(), baggages)
})
expect(JSON.parse(ddSpan.getAllBaggageItems())).to.deep.equal({ key1: 'dd1' })
api.context.with(contextWithUpdatedBaggages, () => {
expect(JSON.parse(ddSpan.getAllBaggageItems())).to.deep.equal(
{ key1: 'otel1', key2: 'otel2' }
)
ddSpan.setBaggageItem('key2', 'dd2')
expect(propagation.getActiveBaggage().getAllEntries()).to.deep.equal(
[['key1', { value: 'otel1' }], ['key2', { value: 'dd2' }]]
)
})
})

it('should handle dd-otel baggage removal', () => {
const ddSpan = tracer.startSpan('dd')
ddSpan.setBaggageItem('key1', 'dd1')
ddSpan.setBaggageItem('key2', 'dd2')
let contextWithUpdatedBaggages
tracer.scope().activate(ddSpan, () => {
let baggages = propagation.getBaggage(api.context.active())
baggages = baggages.removeEntry('key1')
contextWithUpdatedBaggages = propagation.setBaggage(api.context.active(), baggages)
})
expect(JSON.parse(ddSpan.getAllBaggageItems())).to.deep.equal(
{ key1: 'dd1', key2: 'dd2' }
)
api.context.with(contextWithUpdatedBaggages, () => {
expect(JSON.parse(ddSpan.getAllBaggageItems())).to.deep.equal(
{ key2: 'dd2' }
)
ddSpan.removeBaggageItem('key2')
expect(propagation.getActiveBaggage().getAllEntries()).to.deep.equal([])
})
})
})

0 comments on commit 75b2283

Please sign in to comment.