Skip to content

Commit cf116d0

Browse files
committed
Adapt to API changes in pprof-nodejs
1 parent fe3b51c commit cf116d0

3 files changed

Lines changed: 52 additions & 22 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
"@datadog/native-iast-rewriter": "2.0.1",
7171
"@datadog/native-iast-taint-tracking": "^1.5.0",
7272
"@datadog/native-metrics": "^2.0.0",
73-
"@datadog/pprof": "2.2.3",
73+
"@datadog/pprof": "3.0.0",
7474
"@datadog/sketches-js": "^2.1.0",
7575
"@opentelemetry/api": "^1.0.0",
7676
"@opentelemetry/core": "^1.14.0",

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

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,19 @@
33
class NativeWallProfiler {
44
constructor (options = {}) {
55
this.type = 'wall'
6-
this._samplingInterval = options.samplingInterval || 1e6 / 99 // 99hz
6+
this._samplingIntervalMicros = options.samplingInterval || 1e6 / 99 // 99hz
7+
this._flushIntervalMillis = options.flushInterval || 60 * 1e3 // 60 seconds
8+
this._codeHotspotsEnabled = !!options.codeHotspotsEnabled
79
this._mapper = undefined
810
this._pprof = undefined
11+
12+
this._logger = options.logger
13+
this._started = false
914
}
1015

1116
start ({ mapper } = {}) {
17+
if (this._started) return
18+
1219
this._mapper = mapper
1320
this._pprof = require('@datadog/pprof')
1421

@@ -20,27 +27,31 @@ class NativeWallProfiler {
2027
process._stopProfilerIdleNotifier = () => {}
2128
}
2229

23-
this._record()
30+
this._pprof.time.start({
31+
intervalMicros: this._samplingIntervalMicros,
32+
durationMillis: this._flushIntervalMillis,
33+
sourceMapper: this._mapper,
34+
customLabels: this._codeHotspotsEnabled,
35+
lineNumbers: false })
36+
37+
this._started = true
2438
}
2539

2640
profile () {
27-
if (!this._stop) return
28-
return this._stop(true)
41+
if (!this._started) return
42+
return this._pprof.time.stop(true)
2943
}
3044

3145
encode (profile) {
3246
return this._pprof.encode(profile)
3347
}
3448

3549
stop () {
36-
if (!this._stop) return
37-
this._stop()
38-
this._stop = undefined
39-
}
50+
if (!this._started) return
4051

41-
_record () {
42-
this._stop = this._pprof.time.start(this._samplingInterval, null,
43-
this._mapper, false)
52+
const profile = this._pprof.time.stop()
53+
this._started = false
54+
return profile
4455
}
4556
}
4657

packages/dd-trace/test/profiling/profilers/wall.spec.js

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,13 @@ const sinon = require('sinon')
99
describe('profilers/native/wall', () => {
1010
let NativeWallProfiler
1111
let pprof
12-
let stop
1312

1413
beforeEach(() => {
15-
stop = sinon.stub().returns('profile')
1614
pprof = {
1715
encode: sinon.stub().returns(Promise.resolve()),
1816
time: {
19-
start: sinon.stub().returns(stop)
17+
start: sinon.stub(),
18+
stop: sinon.stub().returns('profile')
2019
}
2120
}
2221

@@ -45,24 +44,35 @@ describe('profilers/native/wall', () => {
4544
process._stopProfilerIdleNotifier = stop
4645

4746
sinon.assert.calledOnce(pprof.time.start)
48-
sinon.assert.calledWith(pprof.time.start, 1e6 / 99)
47+
sinon.assert.calledWith(pprof.time.start,
48+
{ intervalMicros: 1e6 / 99,
49+
durationMillis: 60000,
50+
sourceMapper: undefined,
51+
customLabels: false,
52+
lineNumbers: false })
4953
})
5054

5155
it('should use the provided configuration options', () => {
5256
const samplingInterval = 500
5357
const profiler = new NativeWallProfiler({ samplingInterval })
5458

5559
profiler.start()
60+
profiler.stop()
5661

57-
sinon.assert.calledWith(pprof.time.start, samplingInterval)
62+
sinon.assert.calledWith(pprof.time.start,
63+
{ intervalMicros: 500,
64+
durationMillis: 60000,
65+
sourceMapper: undefined,
66+
customLabels: false,
67+
lineNumbers: false })
5868
})
5969

6070
it('should not stop when not started', () => {
6171
const profiler = new NativeWallProfiler()
6272

6373
profiler.stop()
6474

65-
sinon.assert.notCalled(stop)
75+
sinon.assert.notCalled(pprof.time.stop)
6676
})
6777

6878
it('should stop the internal time profiler', () => {
@@ -71,7 +81,7 @@ describe('profilers/native/wall', () => {
7181
profiler.start()
7282
profiler.stop()
7383

74-
sinon.assert.calledOnce(stop)
84+
sinon.assert.calledOnce(pprof.time.stop)
7585
})
7686

7787
it('should stop the internal time profiler only once', () => {
@@ -81,7 +91,7 @@ describe('profilers/native/wall', () => {
8191
profiler.stop()
8292
profiler.stop()
8393

84-
sinon.assert.calledOnce(stop)
94+
sinon.assert.calledOnce(pprof.time.stop)
8595
})
8696

8797
it('should collect profiles from the internal time profiler', () => {
@@ -93,8 +103,9 @@ describe('profilers/native/wall', () => {
93103

94104
expect(profile).to.equal('profile')
95105

96-
sinon.assert.calledOnce(stop)
106+
sinon.assert.calledOnce(pprof.time.stop)
97107
sinon.assert.calledOnce(pprof.time.start)
108+
profiler.stop()
98109
})
99110

100111
it('should encode profiles from the pprof time profiler', () => {
@@ -106,6 +117,8 @@ describe('profilers/native/wall', () => {
106117

107118
profiler.encode(profile)
108119

120+
profiler.stop()
121+
109122
sinon.assert.calledOnce(pprof.encode)
110123
})
111124

@@ -115,7 +128,13 @@ describe('profilers/native/wall', () => {
115128
const mapper = {}
116129

117130
profiler.start({ mapper })
131+
profiler.stop()
118132

119-
sinon.assert.calledWith(pprof.time.start, 1e6 / 99, null, mapper, false)
133+
sinon.assert.calledWith(pprof.time.start,
134+
{ intervalMicros: 1e6 / 99,
135+
durationMillis: 60000,
136+
sourceMapper: mapper,
137+
customLabels: false,
138+
lineNumbers: false })
120139
})
121140
})

0 commit comments

Comments
 (0)