Skip to content

Commit ac25f30

Browse files
authored
refactor custom metrics to be self-contained (#5061)
1 parent 85c5c4f commit ac25f30

4 files changed

Lines changed: 23 additions & 31 deletions

File tree

packages/dd-trace/src/dogstatsd.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,14 @@ class CustomMetrics {
190190
constructor (config) {
191191
const clientConfig = DogStatsDClient.generateClientConfig(config)
192192
this.dogstatsd = new DogStatsDClient(clientConfig)
193+
194+
this._boundFlush = this.flush.bind(this)
195+
196+
// TODO(bengl) this magic number should be configurable
197+
this._flushInterval = setInterval(this._boundFlush, 10 * 1000)
198+
this._flushInterval.unref()
199+
200+
process.once('beforeExit', this._boundFlush)
193201
}
194202

195203
increment (stat, value = 1, tags) {

packages/dd-trace/src/proxy.js

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,7 @@ class Tracer extends NoopProxy {
6767
telemetry.start(config, this._pluginManager)
6868

6969
if (config.dogstatsd) {
70-
// Custom Metrics
7170
this.dogstatsd = new dogstatsd.CustomMetrics(config)
72-
73-
setInterval(() => {
74-
this.dogstatsd.flush()
75-
}, 10 * 1000).unref()
76-
77-
process.once('beforeExit', () => {
78-
this.dogstatsd.flush()
79-
})
8071
}
8172

8273
if (config.spanLeakDebug > 0) {

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,5 +431,20 @@ describe('dogstatsd', () => {
431431
expect(udp4.send).to.have.been.called
432432
expect(udp4.send.firstCall.args[0].toString()).to.equal('test.histogram:10|h\n')
433433
})
434+
435+
it('should flush via interval', () => {
436+
const clock = sinon.useFakeTimers()
437+
438+
client = new CustomMetrics({ dogstatsd: {} })
439+
440+
client.gauge('test.avg', 10, { foo: 'bar' })
441+
442+
expect(udp4.send).not.to.have.been.called
443+
444+
clock.tick(10 * 1000)
445+
446+
expect(udp4.send).to.have.been.called
447+
expect(udp4.send.firstCall.args[0].toString()).to.equal('test.avg:10|g|#foo:bar\n')
448+
})
434449
})
435450
})

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

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -402,28 +402,6 @@ describe('TracerProxy', () => {
402402
proxy.dogstatsd.increment('foo')
403403
})
404404

405-
it('should call custom metrics flush via interval', () => {
406-
const clock = sinon.useFakeTimers()
407-
408-
config.dogstatsd = {
409-
hostname: 'localhost',
410-
port: 9876
411-
}
412-
config.tags = {
413-
service: 'photos',
414-
env: 'prod',
415-
version: '1.2.3'
416-
}
417-
418-
proxy.init()
419-
420-
expect(dogStatsD._flushes()).to.equal(0)
421-
422-
clock.tick(10000)
423-
424-
expect(dogStatsD._flushes()).to.equal(1)
425-
})
426-
427405
it('should expose real metrics methods after init when configured', () => {
428406
config.dogstatsd = {
429407
hostname: 'localhost',

0 commit comments

Comments
 (0)