-
Notifications
You must be signed in to change notification settings - Fork 407
Expand file tree
/
Copy pathwriter.js
More file actions
84 lines (70 loc) · 2.45 KB
/
Copy pathwriter.js
File metadata and controls
84 lines (70 loc) · 2.45 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
'use strict'
const { channel } = require('dc-polyfill')
const log = require('../../log')
const { MAX_SIZE: MAX_CHUNK_SIZE } = require('../../msgpack')
const request = require('./request')
const { safeJSONStringify } = require('./util')
const firstFlushChannel = channel('dd-trace:exporter:first-flush')
class Writer {
constructor ({ url, beforeFirstFlush }) {
this._url = url
this._beforeFirstFlush = beforeFirstFlush
}
#isFirstFlush = true
flush (done = () => {}) {
const count = this._encoder.count()
if (!request.writable) {
this._encoder.reset()
done()
} else if (count > 0) {
if (this.#isFirstFlush && firstFlushChannel.hasSubscribers && this._beforeFirstFlush) {
this.#isFirstFlush = false
this._beforeFirstFlush()
}
let payload
try {
payload = this._encoder.makePayload()
} catch (error) {
if (error.code !== 'ERR_MSGPACK_CHUNK_OVERFLOW') throw error
// Multi-chunk encoders (v0.5, CI Visibility) only learn the assembled
// payload exceeds the cap when `makePayload` stitches the chunks
// together, after `encode` already returned — so the encode-time catch
// never sees it. Drop the queued payload here instead of letting the
// RangeError escape into the host application; the agent would reject
// the oversized payload at the network boundary anyway.
this._encoder.reset()
log.error('Writer dropped %d trace(s) that exceeded the %d byte chunk cap', count, MAX_CHUNK_SIZE)
done()
return
}
this._sendPayload(payload, count, done)
} else {
done()
}
}
append (payload) {
if (!request.writable) {
// eslint-disable-next-line eslint-rules/eslint-log-printf-style
log.debug(() => `Maximum number of active requests reached. Payload discarded: ${safeJSONStringify(payload)}`)
return
}
// eslint-disable-next-line eslint-rules/eslint-log-printf-style
log.debug(() => `Encoding payload: ${safeJSONStringify(payload)}`)
this._encode(payload)
}
_encode (payload) {
this._encoder.encode(payload)
}
setUrl (url) {
this._url = url
}
/**
* Discards whatever's queued in the encoder. Used on a MicroVM clone resume, where anything
* buffered before the snapshot would otherwise flush under every clone's identity.
* @returns {void}
*/
resetPendingBatch () {
this._encoder.reset()
}
}
module.exports = Writer