Skip to content

Commit e85cdaa

Browse files
authoredMar 18, 2025··
fix runtime metrics histogram sending invalid data (#5414)
1 parent 28eaa41 commit e85cdaa

File tree

5 files changed

+43
-44
lines changed

5 files changed

+43
-44
lines changed
 

‎packages/dd-trace/src/dogstatsd.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ class MetricsAggregationClient {
234234
this._histograms[name].get(tag).record(value)
235235
}
236236

237-
count (name, count, tag, monotonic = false) {
237+
count (name, count, tag, monotonic = true) {
238238
if (typeof tag === 'boolean') {
239239
monotonic = tag
240240
tag = undefined
@@ -254,8 +254,8 @@ class MetricsAggregationClient {
254254
this._gauges[name].set(tag, value)
255255
}
256256

257-
increment (name, count = 1, tag, monotonic) {
258-
this.count(name, count, tag, monotonic)
257+
increment (name, count = 1, tag) {
258+
this.count(name, count, tag)
259259
}
260260

261261
decrement (name, count = 1, tag) {

‎packages/dd-trace/src/runtime_metrics/runtime_metrics.js

+13-14
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,11 @@ const runtimeMetrics = module.exports = {
111111
},
112112

113113
increment (name, tag, monotonic) {
114-
client && client.increment(name, 1, tag, monotonic)
114+
this.count(name, 1, tag, monotonic)
115115
},
116116

117117
decrement (name, tag) {
118-
client && client.decrement(name, 1, tag)
118+
this.count(name, -1, tag)
119119
}
120120
}
121121

@@ -211,7 +211,7 @@ function captureGCMetrics () {
211211
histogram('runtime.node.gc.pause', pauseAll)
212212

213213
for (const type in pause) {
214-
histogram('runtime.node.gc.pause.by.type', pause[type], [`gc_type:${type}`])
214+
histogram('runtime.node.gc.pause.by.type', pause[type], `gc_type:${type}`)
215215
}
216216

217217
gcProfiler.start()
@@ -265,7 +265,7 @@ function captureNativeMetrics () {
265265
if (type === 'all') {
266266
histogram('runtime.node.gc.pause', stats.gc[type])
267267
} else {
268-
histogram('runtime.node.gc.pause.by.type', stats.gc[type], [`gc_type:${type}`])
268+
histogram('runtime.node.gc.pause.by.type', stats.gc[type], `gc_type:${type}`)
269269
}
270270
})
271271

@@ -279,16 +279,15 @@ function captureNativeMetrics () {
279279
}
280280
}
281281

282-
function histogram (name, stats, tags) {
283-
tags = tags ? [].concat(tags) : []
284-
285-
if (tags.length > 0) {
286-
for (const tag of tags) {
287-
client.histogram(name, stats, tag)
288-
}
289-
} else {
290-
client.histogram(name, stats)
291-
}
282+
function histogram (name, stats, tag) {
283+
client.gauge(`${name}.min`, stats.min, tag)
284+
client.gauge(`${name}.max`, stats.max, tag)
285+
client.increment(`${name}.sum`, stats.sum, tag)
286+
client.increment(`${name}.total`, stats.sum, tag)
287+
client.gauge(`${name}.avg`, stats.avg, tag)
288+
client.increment(`${name}.count`, stats.count, tag)
289+
client.gauge(`${name}.median`, stats.median, tag)
290+
client.gauge(`${name}.95percentile`, stats.p95, tag)
292291
}
293292

294293
function startGCObserver () {

‎packages/dd-trace/test/custom-metrics.spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ describe('Custom Metrics', () => {
5353
if (stdout) console.log(stdout)
5454
if (stderr) console.error(stderr)
5555

56-
expect(metricsData.split('#')[0]).to.equal('page.views.data:1|g|')
56+
expect(metricsData.split('#')[0]).to.equal('page.views.data:1|c|')
5757

5858
done()
5959
})

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ describe('dogstatsd', () => {
382382
client.flush()
383383

384384
expect(udp4.send).to.have.been.called
385-
expect(udp4.send.firstCall.args[0].toString()).to.equal('test.count:20|g\n')
385+
expect(udp4.send.firstCall.args[0].toString()).to.equal('test.count:20|c\n')
386386
})
387387

388388
it('.increment() with default', () => {
@@ -393,7 +393,7 @@ describe('dogstatsd', () => {
393393
client.flush()
394394

395395
expect(udp4.send).to.have.been.called
396-
expect(udp4.send.firstCall.args[0].toString()).to.equal('test.count:2|g\n')
396+
expect(udp4.send.firstCall.args[0].toString()).to.equal('test.count:2|c\n')
397397
})
398398

399399
it('.decrement()', () => {
@@ -404,7 +404,7 @@ describe('dogstatsd', () => {
404404
client.flush()
405405

406406
expect(udp4.send).to.have.been.called
407-
expect(udp4.send.firstCall.args[0].toString()).to.equal('test.count:-20|g\n')
407+
expect(udp4.send.firstCall.args[0].toString()).to.equal('test.count:-20|c\n')
408408
})
409409

410410
it('.decrement() with default', () => {
@@ -415,7 +415,7 @@ describe('dogstatsd', () => {
415415
client.flush()
416416

417417
expect(udp4.send).to.have.been.called
418-
expect(udp4.send.firstCall.args[0].toString()).to.equal('test.count:-2|g\n')
418+
expect(udp4.send.firstCall.args[0].toString()).to.equal('test.count:-2|c\n')
419419
})
420420

421421
it('.distribution()', () => {

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

+22-22
Original file line numberDiff line numberDiff line change
@@ -218,31 +218,31 @@ suiteDescribe('runtimeMetrics', () => {
218218
expect(client.gauge).to.have.been.calledWith('runtime.node.heap.malloced_memory')
219219
expect(client.gauge).to.have.been.calledWith('runtime.node.heap.peak_malloced_memory')
220220

221-
expect(client.gauge).to.have.been.calledWith('runtime.node.event_loop.delay.max')
222-
expect(client.gauge).to.have.been.calledWith('runtime.node.event_loop.delay.min')
223-
expect(client.increment).to.have.been.calledWith('runtime.node.event_loop.delay.sum')
224-
expect(client.gauge).to.have.been.calledWith('runtime.node.event_loop.delay.avg')
225-
expect(client.gauge).to.have.been.calledWith('runtime.node.event_loop.delay.median')
226-
expect(client.gauge).to.have.been.calledWith('runtime.node.event_loop.delay.95percentile')
227-
expect(client.increment).to.have.been.calledWith('runtime.node.event_loop.delay.count')
221+
expect(client.gauge).to.have.been.calledWith('runtime.node.event_loop.delay.max', sinon.match.number)
222+
expect(client.gauge).to.have.been.calledWith('runtime.node.event_loop.delay.min', sinon.match.number)
223+
expect(client.increment).to.have.been.calledWith('runtime.node.event_loop.delay.sum', sinon.match.number)
224+
expect(client.gauge).to.have.been.calledWith('runtime.node.event_loop.delay.avg', sinon.match.number)
225+
expect(client.gauge).to.have.been.calledWith('runtime.node.event_loop.delay.median', sinon.match.number)
226+
expect(client.gauge).to.have.been.calledWith('runtime.node.event_loop.delay.95percentile', sinon.match.number)
227+
expect(client.increment).to.have.been.calledWith('runtime.node.event_loop.delay.count', sinon.match.number)
228228

229229
expect(client.gauge).to.have.been.calledWith('runtime.node.event_loop.utilization')
230230

231-
expect(client.gauge).to.have.been.calledWith('runtime.node.gc.pause.max')
232-
expect(client.gauge).to.have.been.calledWith('runtime.node.gc.pause.min')
233-
expect(client.increment).to.have.been.calledWith('runtime.node.gc.pause.sum')
234-
expect(client.gauge).to.have.been.calledWith('runtime.node.gc.pause.avg')
235-
expect(client.gauge).to.have.been.calledWith('runtime.node.gc.pause.median')
236-
expect(client.gauge).to.have.been.calledWith('runtime.node.gc.pause.95percentile')
237-
expect(client.increment).to.have.been.calledWith('runtime.node.gc.pause.count')
238-
239-
expect(client.gauge).to.have.been.calledWith('runtime.node.gc.pause.by.type.max')
240-
expect(client.gauge).to.have.been.calledWith('runtime.node.gc.pause.by.type.min')
241-
expect(client.increment).to.have.been.calledWith('runtime.node.gc.pause.by.type.sum')
242-
expect(client.gauge).to.have.been.calledWith('runtime.node.gc.pause.by.type.avg')
243-
expect(client.gauge).to.have.been.calledWith('runtime.node.gc.pause.by.type.median')
244-
expect(client.gauge).to.have.been.calledWith('runtime.node.gc.pause.by.type.95percentile')
245-
expect(client.increment).to.have.been.calledWith('runtime.node.gc.pause.by.type.count')
231+
expect(client.gauge).to.have.been.calledWith('runtime.node.gc.pause.max', sinon.match.number)
232+
expect(client.gauge).to.have.been.calledWith('runtime.node.gc.pause.min', sinon.match.number)
233+
expect(client.increment).to.have.been.calledWith('runtime.node.gc.pause.sum', sinon.match.number)
234+
expect(client.gauge).to.have.been.calledWith('runtime.node.gc.pause.avg', sinon.match.number)
235+
expect(client.gauge).to.have.been.calledWith('runtime.node.gc.pause.median', sinon.match.number)
236+
expect(client.gauge).to.have.been.calledWith('runtime.node.gc.pause.95percentile', sinon.match.number)
237+
expect(client.increment).to.have.been.calledWith('runtime.node.gc.pause.count', sinon.match.number)
238+
239+
expect(client.gauge).to.have.been.calledWith('runtime.node.gc.pause.by.type.max', sinon.match.number)
240+
expect(client.gauge).to.have.been.calledWith('runtime.node.gc.pause.by.type.min', sinon.match.number)
241+
expect(client.increment).to.have.been.calledWith('runtime.node.gc.pause.by.type.sum', sinon.match.number)
242+
expect(client.gauge).to.have.been.calledWith('runtime.node.gc.pause.by.type.avg', sinon.match.number)
243+
expect(client.gauge).to.have.been.calledWith('runtime.node.gc.pause.by.type.median', sinon.match.number)
244+
expect(client.gauge).to.have.been.calledWith('runtime.node.gc.pause.by.type.95percentile', sinon.match.number)
245+
expect(client.increment).to.have.been.calledWith('runtime.node.gc.pause.by.type.count', sinon.match.number)
246246
expect(client.increment).to.have.been.calledWith(
247247
'runtime.node.gc.pause.by.type.count', sinon.match.any, sinon.match(val => {
248248
return val && /^gc_type:[a-z_]+$/.test(val[0])

0 commit comments

Comments
 (0)
Please sign in to comment.