|
2 | 2 |
|
3 | 3 | const DEFAULT_DRAIN_THRESHOLD = 5000 |
4 | 4 |
|
| 5 | +/** |
| 6 | + * A local root span leads its chunk so the WASM pipeline treats it as the chunk |
| 7 | + * root. Mirror of `#isLocalRoot` in packages/dd-trace/src/exporters/native/index.js. |
| 8 | + * |
| 9 | + * @param {object} span |
| 10 | + * @returns {boolean} |
| 11 | + */ |
| 12 | +function isLocalRoot (span) { |
| 13 | + const context = span.context() |
| 14 | + |
| 15 | + if (!context._parentId) return true |
| 16 | + if (context._isRemote) return true |
| 17 | + |
| 18 | + const trace = context._trace |
| 19 | + return Boolean(trace) && trace.started.length > 0 && trace.started[0] === span |
| 20 | +} |
| 21 | + |
| 22 | +/** |
| 23 | + * Mirror of `#syncTraceTags` in the native exporter: trace-level tags live on |
| 24 | + * the trace object and are stamped onto the chunk's local root before export. |
| 25 | + * |
| 26 | + * @param {object} span |
| 27 | + */ |
| 28 | +function syncTraceTags (span) { |
| 29 | + const context = span.context() |
| 30 | + const traceTags = context._trace?.tags |
| 31 | + |
| 32 | + if (!traceTags) return |
| 33 | + |
| 34 | + for (const [key, value] of Object.entries(traceTags)) { |
| 35 | + // Don't overwrite existing span tags. |
| 36 | + if (value !== undefined && value !== null && !context.hasTag(key)) { |
| 37 | + context.setTag(key, value) |
| 38 | + } |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +/** |
| 43 | + * Split staged chunks into one `flushSpansGrouped` group per trace, local root |
| 44 | + * first. Mirror of `#groupsFromSpanChunks(spanChunks, true)` in the native |
| 45 | + * exporter, which is the shape the shipped flush path uses. |
| 46 | + * |
| 47 | + * @param {Array<Array<object>>} spanChunks |
| 48 | + * @returns {Array<{spanIds: Uint8Array[], firstIsLocalRoot: boolean}>} |
| 49 | + */ |
| 50 | +function groupsFromSpanChunks (spanChunks) { |
| 51 | + const groups = [] |
| 52 | + for (const spans of spanChunks) { |
| 53 | + const byTrace = new Map() |
| 54 | + for (const span of spans) { |
| 55 | + const trace = span.context()._trace |
| 56 | + let group = byTrace.get(trace) |
| 57 | + if (group === undefined) { group = []; byTrace.set(trace, group) } |
| 58 | + group.push(span) |
| 59 | + } |
| 60 | + |
| 61 | + for (const group of byTrace.values()) { |
| 62 | + const root = group.find(isLocalRoot) |
| 63 | + const firstIsLocalRoot = root !== undefined |
| 64 | + let ordered = group |
| 65 | + if (firstIsLocalRoot) { |
| 66 | + syncTraceTags(root) |
| 67 | + if (group[0] !== root) { |
| 68 | + ordered = [root, ...group.filter(span => span !== root)] |
| 69 | + } |
| 70 | + } |
| 71 | + groups.push({ |
| 72 | + spanIds: ordered.map(span => span.context()._nativeSpanId), |
| 73 | + firstIsLocalRoot, |
| 74 | + }) |
| 75 | + } |
| 76 | + } |
| 77 | + return groups |
| 78 | +} |
| 79 | + |
| 80 | +/** |
| 81 | + * Periodically move finished native spans out of WASM storage so a long bench |
| 82 | + * loop does not grow the native span map without bound. |
| 83 | + * |
| 84 | + * Staging mirrors the shipped export path: each processor export call is kept as |
| 85 | + * its own trace chunk, every chunk is split into one group per trace with the |
| 86 | + * local root first, and the groups go through the public |
| 87 | + * `nativeSpans.flushSpansGrouped`. Staging a single chunk for all pending spans |
| 88 | + * instead would skip the per-trace `prepareChunk` and the per-chunk trace-tag |
| 89 | + * stamping production pays on every flush, so the bench would report the cost of |
| 90 | + * a pipeline we do not ship. |
| 91 | + * |
| 92 | + * @param {object} tracer Initialized tracer |
| 93 | + * @param {number} [threshold] Pending spans that trigger a drain |
| 94 | + */ |
5 | 95 | function createNativeSpanDrain (tracer, threshold = DEFAULT_DRAIN_THRESHOLD) { |
6 | 96 | const nativeSpans = tracer._tracer._nativeSpans |
7 | | - const pendingSpanIds = nativeSpans ? [] : null |
| 97 | + // JS-only mode has nothing in native storage: every entry point stays a no-op. |
| 98 | + const pendingChunks = nativeSpans ? [] : null |
| 99 | + let pendingCount = 0 |
| 100 | + let flushedGroups = 0 |
| 101 | + let problems = 0 |
| 102 | + const reported = new Set() |
8 | 103 |
|
9 | | - function add (span) { |
10 | | - if (pendingSpanIds) { |
11 | | - pendingSpanIds.push(span.context()._nativeSpanId) |
12 | | - } |
| 104 | + // A silent catch would let a run that never staged or sent a single chunk |
| 105 | + // report clean numbers, hiding exactly the work these benches claim to |
| 106 | + // measure. Print the first occurrence of each distinct failure, count the rest |
| 107 | + // and summarize at exit, so a broken drain is visible without flooding the |
| 108 | + // sirun output on every one of the hundreds of drains a run performs. |
| 109 | + function report (message) { |
| 110 | + problems++ |
| 111 | + if (reported.has(message)) return |
| 112 | + reported.add(message) |
| 113 | + process.stderr.write(`native span drain: ${message}\n`) |
| 114 | + } |
| 115 | + |
| 116 | + if (pendingChunks) { |
| 117 | + process.on('exit', () => { |
| 118 | + if (problems > 0) { |
| 119 | + process.stderr.write( |
| 120 | + `native span drain: ${problems} failed drain(s), ${flushedGroups} trace group(s) flushed\n` |
| 121 | + ) |
| 122 | + } else if (flushedGroups === 0) { |
| 123 | + process.stderr.write('native span drain: no trace group was ever flushed\n') |
| 124 | + } |
| 125 | + }) |
13 | 126 | } |
14 | 127 |
|
15 | 128 | function addAll (spans) { |
16 | | - if (!pendingSpanIds) return |
| 129 | + if (!pendingChunks || spans.length === 0) return |
17 | 130 |
|
18 | | - for (const span of spans) { |
19 | | - pendingSpanIds.push(span.context()._nativeSpanId) |
20 | | - } |
| 131 | + // SpanProcessor reassigns `trace.started` rather than mutating it, so |
| 132 | + // holding this array is safe — the real exporter buffers it the same way. |
| 133 | + pendingChunks.push(spans) |
| 134 | + pendingCount += spans.length |
21 | 135 | } |
22 | 136 |
|
23 | 137 | async function drain () { |
24 | | - if (!pendingSpanIds || pendingSpanIds.length === 0) return |
| 138 | + if (!pendingChunks || pendingCount === 0) return |
25 | 139 |
|
26 | | - nativeSpans.flushChangeQueue() |
| 140 | + const groups = groupsFromSpanChunks(pendingChunks) |
| 141 | + pendingChunks.length = 0 |
| 142 | + pendingCount = 0 |
27 | 143 |
|
28 | | - const spanIds = Buffer.allocUnsafe(pendingSpanIds.length * 8) |
29 | | - let offset = 0 |
30 | | - for (const spanId of pendingSpanIds) { |
31 | | - spanIds.set(spanId, offset) |
32 | | - offset += 8 |
| 144 | + try { |
| 145 | + // flushSpansGrouped drains the change queue itself, then prepares one |
| 146 | + // chunk per group and sends them as a single request. |
| 147 | + const response = await nativeSpans.flushSpansGrouped(groups) |
| 148 | + if (response === 'no spans to flush') { |
| 149 | + report(`staged no chunk for ${groups.length} trace group(s)`) |
| 150 | + } else { |
| 151 | + flushedGroups += groups.length |
| 152 | + } |
| 153 | + } catch (err) { |
| 154 | + report(`flushSpansGrouped rejected: ${err?.message ?? err}`) |
33 | 155 | } |
34 | | - |
35 | | - nativeSpans._state.prepareChunk(pendingSpanIds.length, false, spanIds) |
36 | | - await nativeSpans._state.sendPreparedChunk().catch(() => {}) |
37 | | - pendingSpanIds.length = 0 |
38 | 156 | } |
39 | 157 |
|
40 | 158 | function needsDrain () { |
41 | | - return pendingSpanIds && pendingSpanIds.length >= threshold |
| 159 | + return pendingCount >= threshold |
42 | 160 | } |
43 | 161 |
|
44 | | - return { add, addAll, drain, needsDrain } |
| 162 | + return { addAll, drain, needsDrain } |
45 | 163 | } |
46 | 164 |
|
47 | 165 | module.exports = { createNativeSpanDrain } |
0 commit comments