Skip to content

Commit 13c9b6d

Browse files
Stephen Belangerszegedi
authored andcommitted
Exclude empty telemetry metrics (#3421)
1 parent 890c950 commit 13c9b6d

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

packages/dd-trace/src/telemetry/metrics.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ function mapToJsonArray (map) {
2525
return Array.from(map.values()).map(v => v.toJSON())
2626
}
2727

28+
function hasPoints (metric) {
29+
return metric.points.length > 0
30+
}
31+
2832
class Metric {
2933
constructor (namespace, metric, common, tags) {
3034
this.namespace = namespace.toString()
@@ -172,10 +176,16 @@ class MetricsCollection extends Map {
172176

173177
toJSON () {
174178
if (!this.size) return
179+
180+
const series = mapToJsonArray(this)
181+
.filter(hasPoints)
182+
183+
if (!series.length) return
184+
175185
const { namespace } = this
176186
return {
177187
namespace,
178-
series: mapToJsonArray(this)
188+
series
179189
}
180190
}
181191
}

packages/dd-trace/test/telemetry/metrics.spec.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,33 @@ describe('metrics', () => {
157157
]
158158
})
159159
})
160+
161+
it('should not send empty metrics', () => {
162+
const manager = new metrics.NamespaceManager()
163+
164+
const ns = manager.namespace('test')
165+
166+
const metric = ns.count('metric', { bar: 'baz' })
167+
metric.inc()
168+
metric.reset()
169+
170+
const config = {
171+
hostname: 'localhost',
172+
port: 12345,
173+
tags: {
174+
'runtime-id': 'abc123'
175+
}
176+
}
177+
const application = {
178+
language_name: 'nodejs',
179+
tracer_version: '1.2.3'
180+
}
181+
const host = {}
182+
183+
manager.send(config, application, host)
184+
185+
expect(sendData).to.not.have.been.called
186+
})
160187
})
161188

162189
describe('Namespace', () => {
@@ -249,6 +276,18 @@ describe('metrics', () => {
249276
}
250277
})
251278
})
279+
280+
it('should skip empty metrics', () => {
281+
const ns = new metrics.Namespace('test')
282+
const metric = ns.count('foo', { bar: 'baz' })
283+
metric.inc()
284+
metric.reset()
285+
286+
expect(ns.toJSON()).to.deep.equal({
287+
distributions: undefined,
288+
metrics: undefined
289+
})
290+
})
252291
})
253292

254293
describe('CountMetric', () => {

0 commit comments

Comments
 (0)