-
Notifications
You must be signed in to change notification settings - Fork 407
Expand file tree
/
Copy pathdogstatsd.js
More file actions
471 lines (372 loc) · 11.8 KB
/
Copy pathdogstatsd.js
File metadata and controls
471 lines (372 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
'use strict'
const dgram = require('dgram')
const isIP = require('net').isIP
const { channel } = require('dc-polyfill')
const { storage } = require('../../datadog-core')
const request = require('./exporters/common/request')
const log = require('./log')
const Histogram = require('./histogram')
const { entityId } = require('./exporters/common/docker')
const legacyStorage = storage('legacy')
const MAX_BUFFER_SIZE = 1024 // limit from the agent
const TYPE_COUNTER = 'c'
const TYPE_GAUGE = 'g'
const TYPE_DISTRIBUTION = 'd'
const TYPE_HISTOGRAM = 'h'
const identityRefreshChannel = channel('datadog:identity:refresh')
/**
* @import { DogStatsD } from "../../../index.d.ts"
* @implements {DogStatsD}
*/
class DogStatsDClient {
#lookup
#tagsPrefix
constructor (options) {
this.#lookup = options.lookup
if (options.metricsProxyUrl) {
this._httpOptions = {
method: 'POST',
url: options.metricsProxyUrl.toString(),
path: '/dogstatsd/v2/proxy',
}
}
this._host = options.host
this._family = isIP(this._host)
this._port = options.port
this._tags = options.tags
this.#tagsPrefix = this._tags.length ? `|#${this._tags.join(',')}` : ''
this._queue = []
this._buffer = ''
this._offset = 0
this._udp4 = this._socket('udp4')
this._udp6 = this._socket('udp6')
}
/**
* Recomputes the cached tags and tag-prefix (mirrors the constructor) after a `config.tags`
* change, e.g. a MicroVM clone resume.
*
* Buffered lines have the old prefix baked in, and on a clone resume they were produced during
* the image build, so every clone holds the same bytes — flushing them would submit one identical
* copy per clone. Dropping is right here for that reason only: for a tag change on a live process
* the buffer holds unique data whose old tags are still correct, so that case wants a flush
* before the swap.
*
* @param {string[]} tags - DogStatsD-formatted tags (e.g. `['key:value']`)
* @returns {boolean} True if the tag prefix actually changed (and buffered lines were dropped)
*/
updateTags (tags) {
const tagsPrefix = tags.length ? `|#${tags.join(',')}` : ''
this._tags = tags
if (tagsPrefix === this.#tagsPrefix) return false
this.#tagsPrefix = tagsPrefix
this._queue = []
this._buffer = ''
this._offset = 0
return true
}
increment (stat, value, tags) {
this._add(stat, value, TYPE_COUNTER, tags)
}
decrement (stat, value, tags) {
this._add(stat, -value, TYPE_COUNTER, tags)
}
gauge (stat, value, tags) {
this._add(stat, value, TYPE_GAUGE, tags)
}
distribution (stat, value, tags) {
this._add(stat, value, TYPE_DISTRIBUTION, tags)
}
histogram (stat, value, tags) {
this._add(stat, value, TYPE_HISTOGRAM, tags)
}
flush () {
const queue = this._enqueue()
if (queue.length === 0) return
log.debug('Flushing %s metrics via %s', queue.length, this._httpOptions ? 'HTTP' : 'UDP')
this._queue = []
if (this._httpOptions) {
this._sendHttp(queue)
} else {
this._sendUdp(queue)
}
}
_sendHttp (queue) {
const buffer = Buffer.concat(queue)
request(buffer, this._httpOptions, (err) => {
if (err) {
log.error('DogStatsDClient: HTTP error from agent: %s', err.message, err)
if (err.status === 404) {
// Inside this if-block, we have connectivity to the agent, but
// we're not getting a 200 from the proxy endpoint. If it's a 404,
// then we know we'll never have the endpoint, so just clear out the
// options. Either way, we can give UDP a try.
this._httpOptions = undefined
}
this._sendUdp(queue)
}
})
}
_sendUdp (queue) {
// dgram resolves the local address via the instrumented dns.lookup when it
// binds on first send; the noop store keeps that self-traffic off the trace.
legacyStorage.run({ noop: true }, () => {
if (this._family === 0) {
this.#lookup(this._host, (error, address, family) => {
if (error) return log.error('DogStatsDClient: Host not found', error)
this._sendUdpFromQueue(queue, address, family)
})
} else {
this._sendUdpFromQueue(queue, this._host, this._family)
}
})
}
_sendUdpFromQueue (queue, address, family) {
const socket = family === 6 ? this._udp6 : this._udp4
for (const buffer of queue) {
log.debug('Sending to DogStatsD: %s', buffer)
socket.send(buffer, 0, buffer.length, this._port, address)
}
}
_add (stat, value, type, tags) {
let message = `${stat}:${value}|${type}`
if (tags?.length) {
message += this.#tagsPrefix
? `${this.#tagsPrefix},${tags.join(',')}`
: `|#${tags.join(',')}`
} else {
message += this.#tagsPrefix
}
if (entityId) {
message += `|c:${entityId}`
}
this._write(`${message}\n`)
}
_write (message) {
const offset = Buffer.byteLength(message)
if (this._offset + offset > MAX_BUFFER_SIZE) {
this._enqueue()
}
this._offset += offset
this._buffer += message
}
_enqueue () {
if (this._offset > 0) {
this._queue.push(Buffer.from(this._buffer))
this._buffer = ''
this._offset = 0
}
return this._queue
}
_socket (type) {
const socket = dgram.createSocket(type)
socket.on('error', () => {})
socket.unref?.()
return socket
}
/**
* @param {import('./config/config-base')} config - Tracer configuration
*/
static generateClientConfig (config) {
const tags = []
if (config.tags) {
for (const [key, value] of Object.entries(config.tags)) {
// Skip runtime-id unless enabled as cardinality may be too high
if (typeof value === 'string' && (key !== 'runtime-id' || config.runtimeMetricsRuntimeId)) {
// https://docs.datadoghq.com/tagging/#defining-tags
const valueStripped = value.replaceAll(/[^a-z0-9_:./-]/ig, '_')
tags.push(`${key}:${valueStripped}`)
}
}
}
const clientConfig = {
host: config.dogstatsd.hostname,
port: config.dogstatsd.port,
tags,
lookup: config.lookup,
}
if (config.url) {
clientConfig.metricsProxyUrl = config.url
}
return clientConfig
}
}
class MetricsAggregationClient {
constructor (client) {
this._client = client
this.reset()
}
/**
* Recomputes the wrapped client's cached tags (e.g. after a MicroVM clone resume). Pending
* counters/gauges/histograms were aggregated under the old identity, so they're reset along
* with the client's buffered lines — but only if the tags actually changed, so a no-op resume
* doesn't discard in-flight aggregation for nothing.
* @param {string[]} tags - DogStatsD-formatted tags (e.g. `['key:value']`)
*/
updateTags (tags) {
if (this._client.updateTags(tags)) {
this.reset()
}
}
flush () {
this._captureCounters()
this._captureGauges()
this._captureHistograms()
this._client.flush()
}
reset () {
this._counters = new Map()
this._gauges = new Map()
this._histograms = new Map()
}
// TODO: Aggregate with a histogram and send the buckets to the client.
distribution (name, value, tags) {
this._client.distribution(name, value, tags)
}
boolean (name, value, tags) {
this.gauge(name, value ? 1 : 0, tags)
}
histogram (name, value, tags) {
const node = this._ensureTree(this._histograms, name, tags, null)
if (!node.value) {
node.value = new Histogram()
}
node.value.record(value)
}
count (name, count, tags = [], monotonic = true) {
if (typeof tags === 'boolean') {
monotonic = tags
tags = []
}
const container = monotonic ? this._counters : this._gauges
const node = this._ensureTree(container, name, tags, 0)
node.value += count
}
gauge (name, value, tags) {
const node = this._ensureTree(this._gauges, name, tags, 0)
node.value = value
}
increment (name, count = 1, tags) {
this.count(name, count, tags)
}
decrement (name, count = 1, tags) {
this.count(name, -count, tags)
}
_captureGauges () {
this._captureTree(this._gauges, (node, name, tags) => {
this._client.gauge(name, node.value, tags)
})
this._gauges.clear()
}
_captureCounters () {
this._captureTree(this._counters, (node, name, tags) => {
this._client.increment(name, node.value, tags)
})
this._counters.clear()
}
_captureHistograms () {
this._captureTree(this._histograms, (node, name, tags) => {
const stats = node.value
this._client.gauge(`${name}.min`, stats.min, tags)
this._client.gauge(`${name}.max`, stats.max, tags)
this._client.increment(`${name}.sum`, stats.sum, tags)
this._client.increment(`${name}.total`, stats.sum, tags)
this._client.gauge(`${name}.avg`, stats.avg, tags)
this._client.increment(`${name}.count`, stats.count, tags)
this._client.gauge(`${name}.median`, stats.median, tags)
this._client.gauge(`${name}.95percentile`, stats.p95, tags)
})
this._histograms.clear()
}
_captureTree (tree, fn) {
for (const [name, root] of tree) {
this._captureNode(root, name, [], fn)
}
}
_captureNode (node, name, tags, fn) {
if (node.touched) {
fn(node, name, tags)
}
for (const [tag, next] of node.nodes) {
tags.push(tag)
this._captureNode(next, name, tags, fn)
tags.pop()
}
}
_ensureTree (tree, name, tags = [], value) {
if (!Array.isArray(tags)) {
tags = [tags]
}
let node = this._ensureNode(tree, name, value)
for (const tag of tags) {
node = this._ensureNode(node.nodes, tag, value)
}
node.touched = true
return node
}
_ensureNode (container, key, value) {
let node = container.get(key)
if (!node) {
node = { nodes: new Map(), touched: false, value }
if (typeof key === 'string') {
container.set(key, node)
}
}
return node
}
}
/**
* This is a simplified user-facing proxy to the underlying DogStatsDClient instance
*
* @implements {DogStatsD}
*/
class CustomMetrics {
#client
constructor (config) {
const clientConfig = DogStatsDClient.generateClientConfig(config)
this.#client = new MetricsAggregationClient(new DogStatsDClient(clientConfig))
// CustomMetrics has process-lifetime flush handlers and no stop hook, so this shares that lifetime.
identityRefreshChannel.subscribe(() => {
this.#client.updateTags(DogStatsDClient.generateClientConfig(config).tags)
})
const flush = this.flush.bind(this)
// TODO(bengl) this magic number should be configurable
setInterval(flush, 10 * 1000).unref?.()
globalThis[Symbol.for('dd-trace')].beforeExitHandlers.add(flush)
}
increment (stat, value = 1, tags) {
this.#client.increment(stat, value, CustomMetrics.tagTranslator(tags))
}
decrement (stat, value = 1, tags) {
this.#client.decrement(stat, value, CustomMetrics.tagTranslator(tags))
}
gauge (stat, value, tags) {
this.#client.gauge(stat, value, CustomMetrics.tagTranslator(tags))
}
distribution (stat, value, tags) {
this.#client.distribution(stat, value, CustomMetrics.tagTranslator(tags))
}
histogram (stat, value, tags) {
this.#client.histogram(stat, value, CustomMetrics.tagTranslator(tags))
}
flush () {
return this.#client.flush()
}
/**
* Exposing { tagName: 'tagValue' } to the end user
* These are translated into [ 'tagName:tagValue' ] for internal use
*/
static tagTranslator (objTags) {
if (Array.isArray(objTags)) return objTags
const arrTags = []
if (!objTags) return arrTags
for (const [key, value] of Object.entries(objTags)) {
arrTags.push(`${key}:${value}`)
}
return arrTags
}
}
module.exports = {
DogStatsDClient,
CustomMetrics,
MetricsAggregationClient,
}