|
| 1 | +import type { Client } from '../client'; |
| 2 | +import { DEBUG_BUILD } from '../debug-build'; |
| 3 | +import { createSpanV2Envelope } from '../envelope'; |
| 4 | +import { getDynamicSamplingContextFromSpan } from '../tracing/dynamicSamplingContext'; |
| 5 | +import type { SpanV2JSON, SpanV2JSONWithSegmentRef } from '../types-hoist/span'; |
| 6 | +import { debug } from '../utils/debug-logger'; |
| 7 | + |
| 8 | +export interface SpanBufferOptions { |
| 9 | + /** Max spans per trace before auto-flush (default: 1000) */ |
| 10 | + maxSpanLimit?: number; |
| 11 | + /** Flush interval in ms (default: 5000) */ |
| 12 | + flushInterval?: number; |
| 13 | +} |
| 14 | + |
| 15 | +/** |
| 16 | + * A buffer for span JSON objects that flushes them to Sentry in Span v2 envelopes. |
| 17 | + * Handles interval-based flushing, size thresholds, and graceful shutdown. |
| 18 | + */ |
| 19 | +export class SpanBuffer { |
| 20 | + private _spanTreeMap: Map<string, Set<SpanV2JSONWithSegmentRef>>; |
| 21 | + private _flushIntervalId: ReturnType<typeof setInterval> | null; |
| 22 | + private _client: Client; |
| 23 | + private _maxSpanLimit: number; |
| 24 | + private _flushInterval: number; |
| 25 | + |
| 26 | + public constructor(client: Client, options?: SpanBufferOptions) { |
| 27 | + this._spanTreeMap = new Map(); |
| 28 | + this._client = client; |
| 29 | + |
| 30 | + const { maxSpanLimit, flushInterval } = options ?? {}; |
| 31 | + |
| 32 | + this._maxSpanLimit = maxSpanLimit && maxSpanLimit > 0 && maxSpanLimit <= 1000 ? maxSpanLimit : 1000; |
| 33 | + this._flushInterval = flushInterval && flushInterval > 0 ? flushInterval : 5_000; |
| 34 | + |
| 35 | + this._flushIntervalId = setInterval(() => { |
| 36 | + this.flush(); |
| 37 | + }, this._flushInterval); |
| 38 | + |
| 39 | + this._client.on('flush', () => { |
| 40 | + this.flush(); |
| 41 | + }); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Add a span to the buffer. |
| 46 | + */ |
| 47 | + public addSpan(spanJSON: SpanV2JSONWithSegmentRef): void { |
| 48 | + const traceId = spanJSON.trace_id; |
| 49 | + let traceBucket = this._spanTreeMap.get(traceId); |
| 50 | + if (traceBucket) { |
| 51 | + traceBucket.add(spanJSON); |
| 52 | + } else { |
| 53 | + traceBucket = new Set([spanJSON]); |
| 54 | + this._spanTreeMap.set(traceId, traceBucket); |
| 55 | + } |
| 56 | + |
| 57 | + if (traceBucket.size >= this._maxSpanLimit) { |
| 58 | + this._flushTrace(traceId); |
| 59 | + this._debounceFlushInterval(); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Flush all buffered traces. |
| 65 | + */ |
| 66 | + public flush(): void { |
| 67 | + if (!this._spanTreeMap.size) { |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + DEBUG_BUILD && debug.log(`Flushing span tree map with ${this._spanTreeMap.size} traces`); |
| 72 | + |
| 73 | + this._spanTreeMap.forEach((_, traceId) => { |
| 74 | + this._flushTrace(traceId); |
| 75 | + }); |
| 76 | + this._debounceFlushInterval(); |
| 77 | + } |
| 78 | + |
| 79 | + private _flushTrace(traceId: string): void { |
| 80 | + const traceBucket = this._spanTreeMap.get(traceId); |
| 81 | + if (!traceBucket) { |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + if (!traceBucket.size) { |
| 86 | + this._spanTreeMap.delete(traceId); |
| 87 | + return; |
| 88 | + } |
| 89 | + |
| 90 | + const firstSpanJSON = traceBucket.values().next().value; |
| 91 | + |
| 92 | + const segmentSpan = firstSpanJSON?._segmentSpan; |
| 93 | + if (!segmentSpan) { |
| 94 | + DEBUG_BUILD && debug.warn('No segment span reference found on span JSON, cannot compute DSC'); |
| 95 | + this._spanTreeMap.delete(traceId); |
| 96 | + return; |
| 97 | + } |
| 98 | + |
| 99 | + const dsc = getDynamicSamplingContextFromSpan(segmentSpan); |
| 100 | + |
| 101 | + const cleanedSpans: SpanV2JSON[] = Array.from(traceBucket).map(spanJSON => { |
| 102 | + // eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 103 | + const { _segmentSpan, ...cleanSpanJSON } = spanJSON; |
| 104 | + return cleanSpanJSON; |
| 105 | + }); |
| 106 | + |
| 107 | + const envelope = createSpanV2Envelope(cleanedSpans, dsc, this._client); |
| 108 | + |
| 109 | + DEBUG_BUILD && debug.log(`Sending span envelope for trace ${traceId} with ${cleanedSpans.length} spans`); |
| 110 | + |
| 111 | + this._client.sendEnvelope(envelope).then(null, reason => { |
| 112 | + DEBUG_BUILD && debug.error('Error while sending span stream envelope:', reason); |
| 113 | + }); |
| 114 | + |
| 115 | + this._spanTreeMap.delete(traceId); |
| 116 | + } |
| 117 | + |
| 118 | + private _debounceFlushInterval(): void { |
| 119 | + if (this._flushIntervalId) { |
| 120 | + clearInterval(this._flushIntervalId); |
| 121 | + } |
| 122 | + this._flushIntervalId = setInterval(() => { |
| 123 | + this.flush(); |
| 124 | + }, this._flushInterval); |
| 125 | + } |
| 126 | +} |
0 commit comments