-
Notifications
You must be signed in to change notification settings - Fork 407
Expand file tree
/
Copy pathindex.js
More file actions
79 lines (71 loc) · 2.01 KB
/
Copy pathindex.js
File metadata and controls
79 lines (71 loc) · 2.01 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
'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, onFlush: this.#trackWriterFlush.bind(this) })
}
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 {
const flushWriter = this._writer.flushDirect ?? this._writer.flush
flushWriter.call(this._writer, complete)
} catch (error) {
complete()
throw error
}
}
#trackWriterFlush (flush, done) {
const activeFlush = { callbacks: done ? [done] : [] }
this.#activeFlushes.add(activeFlush)
const complete = () => {
this.#activeFlushes.delete(activeFlush)
for (const callback of activeFlush.callbacks) callback()
}
try {
flush(complete)
} catch (error) {
complete()
throw error
}
}
}
module.exports = {
SpanStatsExporter,
}