Skip to content

Commit 9c860cd

Browse files
committed
DEBUG
1 parent 5f43ff6 commit 9c860cd

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
lines changed

integration-tests/profiler/codehotspots.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const DDTrace = require('dd-trace')
44
const tracer = DDTrace.init()
5+
const NativeWallProfiler = require('dd-trace/packages/dd-trace/src/profiling/profilers/wall')
56

67
// Busy cycle duration is communicated in nanoseconds through the environment
78
// variable by the test. On first execution, it'll be 10 * the sampling period
@@ -29,12 +30,18 @@ function busyLoop () {
2930
let counter = 0
3031

3132
function runBusySpans () {
32-
tracer.trace('x' + counter, { type: 'web', resource: `endpoint-${counter}` }, (_, done) => {
33+
const id1 = `x-${counter}`
34+
tracer.trace(id1, { type: 'web', resource: `endpoint-${counter}` }, (_, done) => {
35+
logData(id1)
3336
setImmediate(() => {
37+
logData(`${id1} timeout`)
3438
for (let i = 0; i < 3; ++i) {
3539
const z = i
36-
tracer.trace('y' + i, (_, done2) => {
40+
const id2 = `y-${counter}-${i}`
41+
tracer.trace(id2, (_, done2) => {
42+
logData(id2)
3743
const busyWork = () => {
44+
logData(`${id2}-timeout`)
3845
busyLoop()
3946
done2()
4047
if (z === 2) {
@@ -60,4 +67,11 @@ function runBusySpans () {
6067
})
6168
}
6269

70+
function logData (codeContext) {
71+
const active = NativeWallProfiler.prototype.getActiveSpan()
72+
const sampleContext = NativeWallProfiler.prototype.getSampleContext()
73+
const indicator = (active.spanId === sampleContext.spanId) ? '✅' : '❌'
74+
console.log(indicator, codeContext, 'activeSpan:', active.spanId, ', sampleContext:', sampleContext.spanId)
75+
}
76+
6377
tracer.profilerStarted().then(runBusySpans)

integration-tests/profiler/profiler.spec.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,15 +337,16 @@ describe('profiler', () => {
337337
DD_TRACE_AGENT_PORT: agent.port
338338
}
339339
// With Node 23 or later, test the profiler with async context frame use.
340-
let execArgv = []
340+
const execArgv = []
341341
if (satisfies(process.versions.node, '>=23.0.0')) {
342342
env.DD_PROFILING_USE_ASYNC_CONTEXT_FRAME = 1
343343
if (!satisfies(process.versions.node, '>=24.0.0')) {
344344
// For Node 23, use the experimental command line flag for Node to enable
345345
// async context frame. Node 24 has it enabled by default.
346-
execArgv = ['--experimental-async-context-frame']
346+
execArgv.push('--experimental-async-context-frame')
347347
}
348348
}
349+
console.log({path: path.join(cwd, 'profiler/codehotspots.js'), env, execArgv })
349350
const proc = fork(path.join(cwd, 'profiler/codehotspots.js'), { cwd, env, execArgv })
350351

351352
await processExitPromise(proc, timeout)

packages/dd-trace/src/profiling/profilers/wall.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const {
1616
const { isWebServerSpan, endpointNameFromTags, getStartedSpans } = require('../webspan-utils')
1717

1818
let beforeCh
19+
let lastInstance
1920
const enterCh = dc.channel('dd-trace:storage:enter')
2021
const spanFinishCh = dc.channel('dd-trace:span:finish')
2122
const profilerTelemetryMetrics = telemetryMetrics.manager.namespace('profilers')
@@ -155,6 +156,7 @@ class NativeWallProfiler {
155156

156157
this._logger = options.logger
157158
this._started = false
159+
lastInstance = this
158160
}
159161

160162
codeHotspotsEnabled () {
@@ -286,6 +288,11 @@ class NativeWallProfiler {
286288
return profilingContext
287289
}
288290

291+
_getSampleContext () {
292+
const context = this._pprof.time.getContext()
293+
return this._useAsyncContextFrame ? context : context.ref
294+
}
295+
289296
_setNewContext () {
290297
this._pprof.time.setContext(
291298
this._currentContext = {
@@ -422,4 +429,21 @@ class NativeWallProfiler {
422429
}
423430
}
424431

432+
NativeWallProfiler.prototype.getActiveSpan = function () {
433+
const span = getActiveSpan()
434+
if (span === undefined) {
435+
return {}
436+
}
437+
const spanData = lastInstance._getProfilingContext(span)
438+
updateContext(spanData)
439+
return { spanId: spanData.spanId, rootSpanId: spanData.rootSpanId }
440+
}
441+
NativeWallProfiler.prototype.getSampleContext = function () {
442+
const ctx = lastInstance._getSampleContext()
443+
if (ctx === undefined) {
444+
return {}
445+
}
446+
updateContext(ctx)
447+
return { spanId: ctx.spanId, rootSpanId: ctx.rootSpanId }
448+
}
425449
module.exports = NativeWallProfiler

0 commit comments

Comments
 (0)