Skip to content

Commit 49d4b44

Browse files
committed
DEBUG
1 parent 5f43ff6 commit 49d4b44

File tree

3 files changed

+45
-4
lines changed

3 files changed

+45
-4
lines changed

integration-tests/profiler/codehotspots.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
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')
6+
57

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

3133
function runBusySpans () {
32-
tracer.trace('x' + counter, { type: 'web', resource: `endpoint-${counter}` }, (_, done) => {
34+
const id1 = `x-${counter}`
35+
tracer.trace(id1, { type: 'web', resource: `endpoint-${counter}` }, (_, done) => {
36+
logData(id1)
3337
setImmediate(() => {
38+
logData(`${id1} timeout`)
3439
for (let i = 0; i < 3; ++i) {
3540
const z = i
36-
tracer.trace('y' + i, (_, done2) => {
41+
const id2 = `y-${counter}-${i}`
42+
tracer.trace(id2, (_, done2) => {
43+
logData(id2)
3744
const busyWork = () => {
45+
logData(`${id2}-timeout`)
3846
busyLoop()
3947
done2()
4048
if (z === 2) {
@@ -60,4 +68,11 @@ function runBusySpans () {
6068
})
6169
}
6270

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

integration-tests/profiler/profiler.spec.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,15 +337,17 @@ 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')) {
342+
execArgv.push('--expose-internals')
342343
env.DD_PROFILING_USE_ASYNC_CONTEXT_FRAME = 1
343344
if (!satisfies(process.versions.node, '>=24.0.0')) {
344345
// For Node 23, use the experimental command line flag for Node to enable
345346
// async context frame. Node 24 has it enabled by default.
346-
execArgv = ['--experimental-async-context-frame']
347+
execArgv.push('--experimental-async-context-frame')
347348
}
348349
}
350+
console.log({path: path.join(cwd, 'profiler/codehotspots.js'), env, execArgv })
349351
const proc = fork(path.join(cwd, 'profiler/codehotspots.js'), { cwd, env, execArgv })
350352

351353
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)