Skip to content

Commit 54c7c51

Browse files
authored
Improve performance of dogstatsd client and ignore non-string name/tags (#5630)
* performance improvements * safeguard against non-string keys * add comment to explain the need for wrapping spies
1 parent c1d53e2 commit 54c7c51

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

packages/dd-trace/src/dogstatsd.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -308,12 +308,16 @@ class MetricsAggregationClient {
308308
}
309309

310310
for (const [tag, next] of node.nodes) {
311-
this._captureNode(next, name, tags.concat(tag), fn)
311+
tags.push(tag)
312+
this._captureNode(next, name, tags, fn)
313+
tags.pop()
312314
}
313315
}
314316

315-
_ensureTree (tree, name, tags, value) {
316-
tags = tags ? [].concat(tags) : []
317+
_ensureTree (tree, name, tags = [], value) {
318+
if (!Array.isArray(tags)) {
319+
tags = [tags]
320+
}
317321

318322
let node = this._ensureNode(tree, name, value)
319323

@@ -331,7 +335,10 @@ class MetricsAggregationClient {
331335

332336
if (!node) {
333337
node = { nodes: new Map(), touched: false, value }
334-
container.set(key, node)
338+
339+
if (typeof key === 'string') {
340+
container.set(key, node)
341+
}
335342
}
336343

337344
return node

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,22 @@ suiteDescribe('runtimeMetrics', () => {
113113
let Client
114114

115115
beforeEach(() => {
116+
// This is needed because sinon spies keep references to arguments which
117+
// breaks tests because the tags parameter is now mutated right after the
118+
// call.
119+
const wrapSpy = (client, spy) => {
120+
return function (stat, value, tags) {
121+
return spy.call(client, stat, value, [].concat(tags))
122+
}
123+
}
124+
116125
Client = sinon.spy(function () {
117-
return client
126+
return {
127+
gauge: wrapSpy(client, client.gauge),
128+
increment: wrapSpy(client, client.increment),
129+
histogram: wrapSpy(client, client.histogram),
130+
flush: client.flush.bind(client)
131+
}
118132
})
119133

120134
Client.generateClientConfig = DogStatsDClient.generateClientConfig

0 commit comments

Comments
 (0)