Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lazy load runtime metrics only when needed #5254

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions packages/dd-trace/src/runtime_metrics/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'use strict'

let runtimeMetrics

const noop = runtimeMetrics = {
stop () {},
track () {},
boolean () {},
histogram () {},
count () {},
gauge () {},
increment () {},
decrement () {}
}

module.exports = {
start (config) {
if (!config?.runtimeMetrics) return

runtimeMetrics = require('./runtime_metrics')

Object.setPrototypeOf(module.exports, runtimeMetrics)

runtimeMetrics.start(config)
},

stop () {
runtimeMetrics.stop()

Object.setPrototypeOf(module.exports, runtimeMetrics = noop)
}
Comment on lines +25 to +31

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I liked how the other PR replaced the whole object and it didn't allow going back. I guess when it's loaded once, we would not really need that anymore?

Suggested change
},
stop () {
runtimeMetrics.stop()
Object.setPrototypeOf(module.exports, runtimeMetrics = noop)
}
},

}

Object.setPrototypeOf(module.exports, noop)
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

const v8 = require('v8')
const os = require('os')
const { DogStatsDClient } = require('./dogstatsd')
const log = require('./log')
const Histogram = require('./histogram')
const { DogStatsDClient } = require('../dogstatsd')
const log = require('../log')
const Histogram = require('../histogram')
const { performance, PerformanceObserver } = require('perf_hooks')

const { NODE_MAJOR, NODE_MINOR } = require('../../../version')
const { NODE_MAJOR, NODE_MINOR } = require('../../../../version')
const INTERVAL = 10 * 1000

// Node >=16 has PerformanceObserver with `gc` type, but <16.7 had a critical bug.
Expand Down
103 changes: 99 additions & 4 deletions packages/dd-trace/test/runtime_metrics.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,101 @@ const isWindows = os.platform() === 'win32'

const suiteDescribe = isWindows ? describe.skip : describe

suiteDescribe('runtimeMetrics (proxy)', () => {
let runtimeMetrics
let proxy

beforeEach(() => {
runtimeMetrics = sinon.spy({
start () {},
stop () {},
track () {},
boolean () {},
histogram () {},
count () {},
gauge () {},
increment () {},
decrement () {}
})

proxy = proxyquire('../src/runtime_metrics', {
'./runtime_metrics': runtimeMetrics
})
})

it('should be noop when disabled', () => {
proxy.start()
proxy.track()
proxy.boolean()
proxy.histogram()
proxy.count()
proxy.gauge()
proxy.increment()
proxy.decrement()
proxy.stop()

expect(runtimeMetrics.start).to.not.have.been.called
expect(runtimeMetrics.track).to.not.have.been.called
expect(runtimeMetrics.boolean).to.not.have.been.called
expect(runtimeMetrics.histogram).to.not.have.been.called
expect(runtimeMetrics.count).to.not.have.been.called
expect(runtimeMetrics.gauge).to.not.have.been.called
expect(runtimeMetrics.increment).to.not.have.been.called
expect(runtimeMetrics.decrement).to.not.have.been.called
expect(runtimeMetrics.stop).to.not.have.been.called
})

it('should proxy when enabled', () => {
const config = { runtimeMetrics: true }

proxy.start(config)
proxy.track()
proxy.boolean()
proxy.histogram()
proxy.count()
proxy.gauge()
proxy.increment()
proxy.decrement()
proxy.stop()

expect(runtimeMetrics.start).to.have.been.calledWith(config)
expect(runtimeMetrics.track).to.have.been.called
expect(runtimeMetrics.boolean).to.have.been.called
expect(runtimeMetrics.histogram).to.have.been.called
expect(runtimeMetrics.count).to.have.been.called
expect(runtimeMetrics.gauge).to.have.been.called
expect(runtimeMetrics.increment).to.have.been.called
expect(runtimeMetrics.decrement).to.have.been.called
expect(runtimeMetrics.stop).to.have.been.called
})

it('should be noop when disabled after being enabled', () => {
const config = { runtimeMetrics: true }

proxy.start(config)
proxy.stop()
proxy.start()
proxy.track()
proxy.boolean()
proxy.histogram()
proxy.count()
proxy.gauge()
proxy.increment()
proxy.decrement()
proxy.stop()

expect(runtimeMetrics.start).to.have.been.calledOnce
expect(runtimeMetrics.track).to.not.have.been.called
expect(runtimeMetrics.boolean).to.not.have.been.called
expect(runtimeMetrics.histogram).to.not.have.been.called
expect(runtimeMetrics.count).to.not.have.been.called
expect(runtimeMetrics.gauge).to.not.have.been.called
expect(runtimeMetrics.increment).to.not.have.been.called
expect(runtimeMetrics.decrement).to.not.have.been.called
expect(runtimeMetrics.stop).to.have.been.calledOnce
})
})

suiteDescribe('runtimeMetrics', () => {
let runtimeMetrics
let config
Expand All @@ -31,8 +126,8 @@ suiteDescribe('runtimeMetrics', () => {
flush: sinon.spy()
}

runtimeMetrics = proxyquire('../src/runtime_metrics', {
'./dogstatsd': {
runtimeMetrics = proxyquire('../src/runtime_metrics/runtime_metrics', {
'../dogstatsd': {
DogStatsDClient: Client
}
})
Expand Down Expand Up @@ -301,8 +396,8 @@ suiteDescribe('runtimeMetrics', () => {

describe('without native runtimeMetrics', () => {
beforeEach(() => {
runtimeMetrics = proxyquire('../src/runtime_metrics', {
'./dogstatsd': Client,
runtimeMetrics = proxyquire('../src/runtime_metrics/runtime_metrics', {
'../dogstatsd': Client,
'node-gyp-build': sinon.stub().returns(null)
})
})
Expand Down
Loading