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

refactor custom metrics to be self-contained #5061

Open
wants to merge 1 commit 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
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)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why are _boundFlush and _flushInterval object level properties, and not just const boundFlush and const flushInterval?


// TODO(bengl) this magic number should be configurable
Copy link
Member

Choose a reason for hiding this comment

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

This must match the timing of the agent, so it could be a footgun to make it 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
Loading