Skip to content

Commit dc3d594

Browse files
committed
DEBUG
1 parent 0eaeacb commit dc3d594

File tree

3 files changed

+53
-5
lines changed

3 files changed

+53
-5
lines changed

integration-tests/profiler/codehotspots.js

Lines changed: 17 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,12 @@ 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+
const CPEDContextCount = NativeWallProfiler.prototype.getCPEDContextCount()
75+
console.log(indicator, codeContext, 'activeSpan:', active.spanId, ', sampleContext:', sampleContext.spanId, ', CPEDContextCount:', CPEDContextCount)
76+
}
77+
6378
tracer.profilerStarted().then(runBusySpans)

integration-tests/profiler/profiler.spec.js

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

354355
await processExitPromise(proc, timeout)

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

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,14 @@ const {
1717
const { isWebServerSpan, endpointNameFromTags, getStartedSpans } = require('../webspan-utils')
1818

1919
let beforeCh
20+
let lastInstance
2021
const enterCh = dc.channel('dd-trace:storage:enter')
2122
const spanFinishCh = dc.channel('dd-trace:span:finish')
2223
const profilerTelemetryMetrics = telemetryMetrics.manager.namespace('profilers')
2324

2425
const ProfilingContext = Symbol('NativeWallProfiler.ProfilingContext')
2526

26-
let kSampleCount
27+
let kSampleCount, kCPEDContextCount
2728

2829
function getActiveSpan () {
2930
const store = storage('legacy').getStore()
@@ -154,6 +155,7 @@ class NativeWallProfiler {
154155

155156
this._logger = options.logger
156157
this._started = false
158+
lastInstance = this
157159
}
158160

159161
codeHotspotsEnabled () {
@@ -170,6 +172,7 @@ class NativeWallProfiler {
170172
this._mapper = mapper
171173
this._pprof = require('@datadog/pprof')
172174
kSampleCount = this._pprof.time.constants.kSampleCount
175+
kCPEDContextCount = this._pprof.time.constants.kCPEDContextCount
173176

174177
// pprof otherwise crashes in worker threads
175178
if (!process._startProfilerIdleNotifier) {
@@ -285,6 +288,15 @@ class NativeWallProfiler {
285288
return profilingContext
286289
}
287290

291+
_getSampleContext () {
292+
const context = this._pprof.time.getContext()
293+
return this._useAsyncContextFrame ? context : context.ref
294+
}
295+
296+
_getCPEDContextCount () {
297+
return this._profilerState[kCPEDContextCount]
298+
}
299+
288300
_setNewContext () {
289301
this._pprof.time.setContext(
290302
this._currentContext = {
@@ -421,4 +433,24 @@ class NativeWallProfiler {
421433
}
422434
}
423435

436+
NativeWallProfiler.prototype.getActiveSpan = function () {
437+
const span = getActiveSpan()
438+
if (span === undefined) {
439+
return {}
440+
}
441+
const spanData = lastInstance._getProfilingContext(span)
442+
updateContext(spanData)
443+
return { spanId: spanData.spanId, rootSpanId: spanData.rootSpanId }
444+
}
445+
NativeWallProfiler.prototype.getSampleContext = function () {
446+
const ctx = lastInstance._getSampleContext()
447+
if (ctx === undefined) {
448+
return {}
449+
}
450+
updateContext(ctx)
451+
return { spanId: ctx.spanId, rootSpanId: ctx.rootSpanId }
452+
}
453+
NativeWallProfiler.prototype.getCPEDContextCount = function () {
454+
return lastInstance._getCPEDContextCount()
455+
}
424456
module.exports = NativeWallProfiler

0 commit comments

Comments
 (0)