Skip to content

Commit 23f6b47

Browse files
committed
DEBUG
1 parent 680248b commit 23f6b47

File tree

3 files changed

+51
-3
lines changed

3 files changed

+51
-3
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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,7 @@ describe('profiler', () => {
349349
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()
@@ -131,6 +132,7 @@ class NativeWallProfiler {
131132

132133
this._logger = options.logger
133134
this._started = false
135+
lastInstance = this
134136
}
135137

136138
codeHotspotsEnabled () {
@@ -147,6 +149,7 @@ class NativeWallProfiler {
147149
this._mapper = mapper
148150
this._pprof = require('@datadog/pprof')
149151
kSampleCount = this._pprof.time.constants.kSampleCount
152+
kCPEDContextCount = this._pprof.time.constants.kCPEDContextCount
150153

151154
// pprof otherwise crashes in worker threads
152155
if (!process._startProfilerIdleNotifier) {
@@ -274,6 +277,15 @@ class NativeWallProfiler {
274277
return profilingContext
275278
}
276279

280+
_getSampleContext () {
281+
const context = this._pprof.time.getContext()
282+
return this._useAsyncContextFrame ? context : context.ref
283+
}
284+
285+
_getCPEDContextCount () {
286+
return this._profilerState[kCPEDContextCount]
287+
}
288+
277289
_setNewContext () {
278290
this._pprof.time.setContext(
279291
this._currentContext = {
@@ -411,4 +423,24 @@ class NativeWallProfiler {
411423
}
412424
}
413425

426+
NativeWallProfiler.prototype.getActiveSpan = function () {
427+
const span = getActiveSpan()
428+
if (span === undefined) {
429+
return {}
430+
}
431+
const spanData = lastInstance._getProfilingContext(span)
432+
updateContext(spanData)
433+
return { spanId: spanData.spanId, rootSpanId: spanData.rootSpanId }
434+
}
435+
NativeWallProfiler.prototype.getSampleContext = function () {
436+
const ctx = lastInstance._getSampleContext()
437+
if (ctx === undefined) {
438+
return {}
439+
}
440+
updateContext(ctx)
441+
return { spanId: ctx.spanId, rootSpanId: ctx.rootSpanId }
442+
}
443+
NativeWallProfiler.prototype.getCPEDContextCount = function () {
444+
return lastInstance._getCPEDContextCount()
445+
}
414446
module.exports = NativeWallProfiler

0 commit comments

Comments
 (0)