-
Notifications
You must be signed in to change notification settings - Fork 406
Expand file tree
/
Copy pathindex.js
More file actions
96 lines (81 loc) · 2.28 KB
/
Copy pathindex.js
File metadata and controls
96 lines (81 loc) · 2.28 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
'use strict'
const { URL } = require('url')
const log = require('../../log')
const Writer = require('./writer')
class AgentExporter {
#timer
#activeFlushes = new Set()
constructor (config, prioritySampler) {
this._config = config
const { lookup, protocolVersion, stats = {}, apmTracingEnabled } = config
this._url = config.url
const headers = {}
if (stats.DD_TRACE_STATS_COMPUTATION_ENABLED || apmTracingEnabled === false) {
headers['Datadog-Client-Computed-Stats'] = 'yes'
}
this._writer = new Writer({
url: this._url,
prioritySampler,
lookup,
protocolVersion,
headers,
})
globalThis[Symbol.for('dd-trace')].beforeExitHandlers.add(this.flush.bind(this))
}
setUrl (url) {
try {
url = new URL(url)
this._url = url
this._writer.setUrl(url)
} catch (e) {
log.warn(e.stack)
}
}
export (spans) {
this._writer.append(spans)
const { flushInterval } = this._config
if (flushInterval === 0) {
this.#flush()
} else if (this.#timer === undefined) {
this.#timer = setTimeout(() => {
this.#flush()
this.#timer = undefined
}, flushInterval)
this.#timer.unref?.()
}
}
flush (done = () => {}) {
clearTimeout(this.#timer)
this.#timer = undefined
// Snapshot before the boundary flush so a failed encoding cannot cause a
// Vercel lifecycle flush to abandon exports that were already in flight.
let activeFlushes = [...this.#activeFlushes]
try {
this.#flush()
} catch (error) {
log.error('Failed to flush traces: %s', error.message)
}
activeFlushes = [...new Set([...activeFlushes, ...this.#activeFlushes])]
if (activeFlushes.length === 0) return done()
let pending = activeFlushes.length
const complete = () => {
if (--pending === 0) done()
}
for (const flush of activeFlushes) flush.callbacks.push(complete)
}
#flush () {
const flush = { callbacks: [] }
this.#activeFlushes.add(flush)
const complete = () => {
this.#activeFlushes.delete(flush)
for (const callback of flush.callbacks) callback()
}
try {
this._writer.flush(complete)
} catch (error) {
complete()
throw error
}
}
}
module.exports = AgentExporter