Skip to content

Commit 5a4d301

Browse files
BridgeARszegedi
authored andcommitted
fix: improve runtime metrics (#6344)
Things fixed / improved: * runtime.node.event_loop.utilization is now working as expected (the metric was formerly calculating the diff on each iteration on top of the diff instead of on the actual former value) * runtime.node.gc.pause* is now only sending data, if the metric contains a count > 0 * Stop tracking gc metrics on native side when it is deactivated to track those while event loop tracking is also deactivated * Slightly faster implementation for a couple of metrics * Implemented a best effort fallback mode for the event loop delay in case the native library can’t be loaded * Cleaning up code that was there for old unsupported Node.js versions * Created lots of tests that are now checking that values are in a correct range (almost all lines are now fully covered and the tests are significantly more robust about actually finding issues) * Refactored dogstatsd to use private properties instead of underscored * Improved client config in dogstatsd to only use a single loop instead of multiple filters * Made sure calling runtimeMetrics.start() multiple times without calling stop in-between will automatically clean up former tracking
1 parent 72d9e1e commit 5a4d301

File tree

3 files changed

+883
-536
lines changed

3 files changed

+883
-536
lines changed

packages/dd-trace/src/dogstatsd.js

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -156,23 +156,19 @@ class DogStatsDClient {
156156
return socket
157157
}
158158

159-
static generateClientConfig (config = {}) {
159+
static generateClientConfig (config) {
160160
const tags = []
161161

162162
if (config.tags) {
163-
Object.keys(config.tags)
164-
.filter(key => typeof config.tags[key] === 'string')
165-
.filter(key => {
166-
// Skip runtime-id unless enabled as cardinality may be too high
167-
if (key !== 'runtime-id') return true
168-
return config.runtimeMetricsRuntimeId
169-
})
170-
.forEach(key => {
163+
for (const [key, value] of Object.entries(config.tags)) {
164+
// Skip runtime-id unless enabled as cardinality may be too high
165+
if (typeof value === 'string' && (key !== 'runtime-id' || config.runtimeMetricsRuntimeId)) {
171166
// https://docs.datadoghq.com/tagging/#defining-tags
172-
const value = config.tags[key].replaceAll(/[^a-z0-9_:./-]/ig, '_')
167+
const valueStripped = value.replaceAll(/[^a-z0-9_:./-]/ig, '_')
173168

174-
tags.push(`${key}:${value}`)
175-
})
169+
tags.push(`${key}:${valueStripped}`)
170+
}
171+
}
176172
}
177173

178174
const clientConfig = {
@@ -216,7 +212,7 @@ class MetricsAggregationClient {
216212
this._histograms = new Map()
217213
}
218214

219-
// TODO: Aggerate with a histogram and send the buckets to the client.
215+
// TODO: Aggregate with a histogram and send the buckets to the client.
220216
distribution (name, value, tags) {
221217
this._client.distribution(name, value, tags)
222218
}
@@ -352,9 +348,10 @@ class MetricsAggregationClient {
352348
* @implements {DogStatsD}
353349
*/
354350
class CustomMetrics {
351+
#client
355352
constructor (config) {
356353
const clientConfig = DogStatsDClient.generateClientConfig(config)
357-
this._client = new MetricsAggregationClient(new DogStatsDClient(clientConfig))
354+
this.#client = new MetricsAggregationClient(new DogStatsDClient(clientConfig))
358355

359356
const flush = this.flush.bind(this)
360357

@@ -365,27 +362,27 @@ class CustomMetrics {
365362
}
366363

367364
increment (stat, value = 1, tags) {
368-
this._client.increment(stat, value, CustomMetrics.tagTranslator(tags))
365+
this.#client.increment(stat, value, CustomMetrics.tagTranslator(tags))
369366
}
370367

371368
decrement (stat, value = 1, tags) {
372-
this._client.decrement(stat, value, CustomMetrics.tagTranslator(tags))
369+
this.#client.decrement(stat, value, CustomMetrics.tagTranslator(tags))
373370
}
374371

375372
gauge (stat, value, tags) {
376-
this._client.gauge(stat, value, CustomMetrics.tagTranslator(tags))
373+
this.#client.gauge(stat, value, CustomMetrics.tagTranslator(tags))
377374
}
378375

379376
distribution (stat, value, tags) {
380-
this._client.distribution(stat, value, CustomMetrics.tagTranslator(tags))
377+
this.#client.distribution(stat, value, CustomMetrics.tagTranslator(tags))
381378
}
382379

383380
histogram (stat, value, tags) {
384-
this._client.histogram(stat, value, CustomMetrics.tagTranslator(tags))
381+
this.#client.histogram(stat, value, CustomMetrics.tagTranslator(tags))
385382
}
386383

387384
flush () {
388-
return this._client.flush()
385+
return this.#client.flush()
389386
}
390387

391388
/**

0 commit comments

Comments
 (0)