-
Notifications
You must be signed in to change notification settings - Fork 407
Expand file tree
/
Copy pathindex.js
More file actions
63 lines (56 loc) · 1.52 KB
/
Copy pathindex.js
File metadata and controls
63 lines (56 loc) · 1.52 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
'use strict'
const log = require('../../log')
const { Writer } = require('./writer')
class SpanStatsExporter {
#activeFlushes = new Set()
constructor (config) {
this._url = config.url
this._writer = new Writer({ url: this._url })
}
export (payload, done) {
if (done) {
const activeFlushes = [...this.#activeFlushes]
let pending = activeFlushes.length + 1
const complete = () => {
if (--pending === 0) done()
}
for (const flush of activeFlushes) flush.callbacks.push(complete)
this._writer.append(payload)
try {
this.#flush(complete)
} catch (error) {
// `#flush` has notified the boundary request; keep waiting for prior exports.
log.error('Failed to flush span stats: %s', error.message)
}
return
}
this._writer.append(payload)
this.#flush()
}
flush (done) {
const activeFlushes = [...this.#activeFlushes]
let pending = activeFlushes.length + 1
const complete = () => {
if (--pending === 0) done?.()
}
for (const flush of activeFlushes) flush.callbacks.push(complete)
this.#flush(complete)
}
#flush (done) {
const flush = { callbacks: done ? [done] : [] }
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 = {
SpanStatsExporter,
}