Skip to content

Commit 487ea6f

Browse files
authored
lazy load runtime metrics only when needed (#5254)
1 parent 7fc2f98 commit 487ea6f

File tree

3 files changed

+137
-8
lines changed

3 files changed

+137
-8
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
'use strict'
2+
3+
let runtimeMetrics
4+
5+
const noop = runtimeMetrics = {
6+
stop () {},
7+
track () {},
8+
boolean () {},
9+
histogram () {},
10+
count () {},
11+
gauge () {},
12+
increment () {},
13+
decrement () {}
14+
}
15+
16+
module.exports = {
17+
start (config) {
18+
if (!config?.runtimeMetrics) return
19+
20+
runtimeMetrics = require('./runtime_metrics')
21+
22+
Object.setPrototypeOf(module.exports, runtimeMetrics)
23+
24+
runtimeMetrics.start(config)
25+
},
26+
27+
stop () {
28+
runtimeMetrics.stop()
29+
30+
Object.setPrototypeOf(module.exports, runtimeMetrics = noop)
31+
}
32+
}
33+
34+
Object.setPrototypeOf(module.exports, noop)

packages/dd-trace/src/runtime_metrics.js renamed to packages/dd-trace/src/runtime_metrics/runtime_metrics.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44

55
const v8 = require('v8')
66
const os = require('os')
7-
const { DogStatsDClient } = require('./dogstatsd')
8-
const log = require('./log')
9-
const Histogram = require('./histogram')
7+
const { DogStatsDClient } = require('../dogstatsd')
8+
const log = require('../log')
9+
const Histogram = require('../histogram')
1010
const { performance, PerformanceObserver } = require('perf_hooks')
1111

12-
const { NODE_MAJOR, NODE_MINOR } = require('../../../version')
12+
const { NODE_MAJOR, NODE_MINOR } = require('../../../../version')
1313
const INTERVAL = 10 * 1000
1414

1515
// Node >=16 has PerformanceObserver with `gc` type, but <16.7 had a critical bug.

packages/dd-trace/test/runtime_metrics.spec.js

Lines changed: 99 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,101 @@ const isWindows = os.platform() === 'win32'
99

1010
const suiteDescribe = isWindows ? describe.skip : describe
1111

12+
suiteDescribe('runtimeMetrics (proxy)', () => {
13+
let runtimeMetrics
14+
let proxy
15+
16+
beforeEach(() => {
17+
runtimeMetrics = sinon.spy({
18+
start () {},
19+
stop () {},
20+
track () {},
21+
boolean () {},
22+
histogram () {},
23+
count () {},
24+
gauge () {},
25+
increment () {},
26+
decrement () {}
27+
})
28+
29+
proxy = proxyquire('../src/runtime_metrics', {
30+
'./runtime_metrics': runtimeMetrics
31+
})
32+
})
33+
34+
it('should be noop when disabled', () => {
35+
proxy.start()
36+
proxy.track()
37+
proxy.boolean()
38+
proxy.histogram()
39+
proxy.count()
40+
proxy.gauge()
41+
proxy.increment()
42+
proxy.decrement()
43+
proxy.stop()
44+
45+
expect(runtimeMetrics.start).to.not.have.been.called
46+
expect(runtimeMetrics.track).to.not.have.been.called
47+
expect(runtimeMetrics.boolean).to.not.have.been.called
48+
expect(runtimeMetrics.histogram).to.not.have.been.called
49+
expect(runtimeMetrics.count).to.not.have.been.called
50+
expect(runtimeMetrics.gauge).to.not.have.been.called
51+
expect(runtimeMetrics.increment).to.not.have.been.called
52+
expect(runtimeMetrics.decrement).to.not.have.been.called
53+
expect(runtimeMetrics.stop).to.not.have.been.called
54+
})
55+
56+
it('should proxy when enabled', () => {
57+
const config = { runtimeMetrics: true }
58+
59+
proxy.start(config)
60+
proxy.track()
61+
proxy.boolean()
62+
proxy.histogram()
63+
proxy.count()
64+
proxy.gauge()
65+
proxy.increment()
66+
proxy.decrement()
67+
proxy.stop()
68+
69+
expect(runtimeMetrics.start).to.have.been.calledWith(config)
70+
expect(runtimeMetrics.track).to.have.been.called
71+
expect(runtimeMetrics.boolean).to.have.been.called
72+
expect(runtimeMetrics.histogram).to.have.been.called
73+
expect(runtimeMetrics.count).to.have.been.called
74+
expect(runtimeMetrics.gauge).to.have.been.called
75+
expect(runtimeMetrics.increment).to.have.been.called
76+
expect(runtimeMetrics.decrement).to.have.been.called
77+
expect(runtimeMetrics.stop).to.have.been.called
78+
})
79+
80+
it('should be noop when disabled after being enabled', () => {
81+
const config = { runtimeMetrics: true }
82+
83+
proxy.start(config)
84+
proxy.stop()
85+
proxy.start()
86+
proxy.track()
87+
proxy.boolean()
88+
proxy.histogram()
89+
proxy.count()
90+
proxy.gauge()
91+
proxy.increment()
92+
proxy.decrement()
93+
proxy.stop()
94+
95+
expect(runtimeMetrics.start).to.have.been.calledOnce
96+
expect(runtimeMetrics.track).to.not.have.been.called
97+
expect(runtimeMetrics.boolean).to.not.have.been.called
98+
expect(runtimeMetrics.histogram).to.not.have.been.called
99+
expect(runtimeMetrics.count).to.not.have.been.called
100+
expect(runtimeMetrics.gauge).to.not.have.been.called
101+
expect(runtimeMetrics.increment).to.not.have.been.called
102+
expect(runtimeMetrics.decrement).to.not.have.been.called
103+
expect(runtimeMetrics.stop).to.have.been.calledOnce
104+
})
105+
})
106+
12107
suiteDescribe('runtimeMetrics', () => {
13108
let runtimeMetrics
14109
let config
@@ -31,8 +126,8 @@ suiteDescribe('runtimeMetrics', () => {
31126
flush: sinon.spy()
32127
}
33128

34-
runtimeMetrics = proxyquire('../src/runtime_metrics', {
35-
'./dogstatsd': {
129+
runtimeMetrics = proxyquire('../src/runtime_metrics/runtime_metrics', {
130+
'../dogstatsd': {
36131
DogStatsDClient: Client
37132
}
38133
})
@@ -301,8 +396,8 @@ suiteDescribe('runtimeMetrics', () => {
301396

302397
describe('without native runtimeMetrics', () => {
303398
beforeEach(() => {
304-
runtimeMetrics = proxyquire('../src/runtime_metrics', {
305-
'./dogstatsd': Client,
399+
runtimeMetrics = proxyquire('../src/runtime_metrics/runtime_metrics', {
400+
'../dogstatsd': Client,
306401
'node-gyp-build': sinon.stub().returns(null)
307402
})
308403
})

0 commit comments

Comments
 (0)