Skip to content

Commit

Permalink
refactor custom metrics to be self-contained
Browse files Browse the repository at this point in the history
  • Loading branch information
bengl committed Dec 31, 2024
1 parent 4d6a8e3 commit 8c89cf4
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 31 deletions.
8 changes: 8 additions & 0 deletions packages/dd-trace/src/dogstatsd.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,14 @@ class CustomMetrics {
constructor (config) {
const clientConfig = DogStatsDClient.generateClientConfig(config)
this.dogstatsd = new DogStatsDClient(clientConfig)

this._boundFlush = this.flush.bind(this)

// TODO(bengl) this magic number should be configurable
this._flushInterval = setInterval(this._boundFlush, 10 * 1000)
this._flushInterval.unref()

process.once('beforeExit', this._boundFlush)
}

increment (stat, value = 1, tags) {
Expand Down
9 changes: 0 additions & 9 deletions packages/dd-trace/src/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,7 @@ class Tracer extends NoopProxy {
telemetry.start(config, this._pluginManager)

if (config.dogstatsd) {
// Custom Metrics
this.dogstatsd = new dogstatsd.CustomMetrics(config)

setInterval(() => {
this.dogstatsd.flush()
}, 10 * 1000).unref()

process.once('beforeExit', () => {
this.dogstatsd.flush()
})
}

if (config.spanLeakDebug > 0) {
Expand Down
15 changes: 15 additions & 0 deletions packages/dd-trace/test/dogstatsd.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -431,5 +431,20 @@ describe('dogstatsd', () => {
expect(udp4.send).to.have.been.called
expect(udp4.send.firstCall.args[0].toString()).to.equal('test.histogram:10|h\n')
})

it('should flush via interval', () => {
const clock = sinon.useFakeTimers()

client = new CustomMetrics({ dogstatsd: {} })

client.gauge('test.avg', 10, { foo: 'bar' })

expect(udp4.send).not.to.have.been.called

clock.tick(10 * 1000)

expect(udp4.send).to.have.been.called
expect(udp4.send.firstCall.args[0].toString()).to.equal('test.avg:10|g|#foo:bar\n')
})
})
})
22 changes: 0 additions & 22 deletions packages/dd-trace/test/proxy.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -401,28 +401,6 @@ describe('TracerProxy', () => {
proxy.dogstatsd.increment('foo')
})

it('should call custom metrics flush via interval', () => {
const clock = sinon.useFakeTimers()

config.dogstatsd = {
hostname: 'localhost',
port: 9876
}
config.tags = {
service: 'photos',
env: 'prod',
version: '1.2.3'
}

proxy.init()

expect(dogStatsD._flushes()).to.equal(0)

clock.tick(10000)

expect(dogStatsD._flushes()).to.equal(1)
})

it('should expose real metrics methods after init when configured', () => {
config.dogstatsd = {
hostname: 'localhost',
Expand Down

0 comments on commit 8c89cf4

Please sign in to comment.