|
| 1 | +import { Context, HrTime } from '@opentelemetry/api' |
| 2 | +import { ReadableSpan, Span, SpanProcessor } from '@opentelemetry/sdk-trace-base' |
| 3 | + |
| 4 | +import { checkTraceIdRatio, SpanLevel, type TailSamplingSpanInfo } from './sampling' |
| 5 | + |
| 6 | +interface BufferedSpan { |
| 7 | + context: Context | null |
| 8 | + event: 'end' | 'start' |
| 9 | + span: ReadableSpan | Span |
| 10 | +} |
| 11 | + |
| 12 | +interface TraceBuffer { |
| 13 | + spans: BufferedSpan[] |
| 14 | + startTime: HrTime |
| 15 | +} |
| 16 | + |
| 17 | +// Sentinel value indicating a buffer was already flushed |
| 18 | +const FLUSHED = Symbol('flushed') |
| 19 | + |
| 20 | +type TailCallback = (spanInfo: TailSamplingSpanInfo) => number |
| 21 | + |
| 22 | +function hrTimeToSeconds(hrTime: HrTime): number { |
| 23 | + return hrTime[0] + hrTime[1] / 1e9 |
| 24 | +} |
| 25 | + |
| 26 | +export class TailSamplingProcessor implements SpanProcessor { |
| 27 | + private buffers = new Map<string, TraceBuffer | typeof FLUSHED>() |
| 28 | + private tail: TailCallback |
| 29 | + private wrapped: SpanProcessor |
| 30 | + |
| 31 | + constructor(wrapped: SpanProcessor, tail: TailCallback) { |
| 32 | + this.wrapped = wrapped |
| 33 | + this.tail = tail |
| 34 | + } |
| 35 | + |
| 36 | + async forceFlush(): Promise<void> { |
| 37 | + return this.wrapped.forceFlush() |
| 38 | + } |
| 39 | + |
| 40 | + onEnd(span: ReadableSpan): void { |
| 41 | + const traceId = span.spanContext().traceId |
| 42 | + const entry = this.buffers.get(traceId) |
| 43 | + |
| 44 | + if (entry === FLUSHED) { |
| 45 | + this.wrapped.onEnd(span) |
| 46 | + return |
| 47 | + } |
| 48 | + |
| 49 | + if (!entry) { |
| 50 | + this.wrapped.onEnd(span) |
| 51 | + return |
| 52 | + } |
| 53 | + |
| 54 | + entry.spans.push({ context: null, event: 'end', span }) |
| 55 | + |
| 56 | + const isRoot = !span.parentSpanContext |
| 57 | + if (isRoot) { |
| 58 | + // Root span ended — check one last time, then discard if still buffered |
| 59 | + if (!this.checkSpan(span, null, 'end', entry)) { |
| 60 | + this.buffers.delete(traceId) |
| 61 | + } |
| 62 | + return |
| 63 | + } |
| 64 | + |
| 65 | + this.checkSpan(span, null, 'end', entry) |
| 66 | + } |
| 67 | + |
| 68 | + onStart(span: Span, parentContext: Context): void { |
| 69 | + const traceId = span.spanContext().traceId |
| 70 | + const entry = this.buffers.get(traceId) |
| 71 | + |
| 72 | + if (entry === FLUSHED) { |
| 73 | + this.wrapped.onStart(span, parentContext) |
| 74 | + return |
| 75 | + } |
| 76 | + |
| 77 | + const readable = span as unknown as ReadableSpan |
| 78 | + const isRoot = !readable.parentSpanContext |
| 79 | + if (isRoot && !entry) { |
| 80 | + const buffer: TraceBuffer = { spans: [], startTime: readable.startTime } |
| 81 | + this.buffers.set(traceId, buffer) |
| 82 | + buffer.spans.push({ context: parentContext, event: 'start', span }) |
| 83 | + this.checkSpan(span as unknown as ReadableSpan, parentContext, 'start', buffer) |
| 84 | + return |
| 85 | + } |
| 86 | + |
| 87 | + if (entry) { |
| 88 | + entry.spans.push({ context: parentContext, event: 'start', span }) |
| 89 | + this.checkSpan(span as unknown as ReadableSpan, parentContext, 'start', entry) |
| 90 | + return |
| 91 | + } |
| 92 | + |
| 93 | + // No buffer and not root — trace started before this processor was active |
| 94 | + this.wrapped.onStart(span, parentContext) |
| 95 | + } |
| 96 | + |
| 97 | + async shutdown(): Promise<void> { |
| 98 | + this.buffers.clear() |
| 99 | + return this.wrapped.shutdown() |
| 100 | + } |
| 101 | + |
| 102 | + private checkSpan(span: ReadableSpan, context: Context | null, event: 'end' | 'start', buffer: TraceBuffer): boolean { |
| 103 | + const duration = hrTimeToSeconds(span.startTime) - hrTimeToSeconds(buffer.startTime) |
| 104 | + const level = SpanLevel.fromSpan(span) |
| 105 | + |
| 106 | + const info: TailSamplingSpanInfo = { context, duration, event, level, span } |
| 107 | + const rate = this.tail(info) |
| 108 | + |
| 109 | + if (rate >= 1.0 || (rate > 0.0 && checkTraceIdRatio(span.spanContext().traceId, rate))) { |
| 110 | + this.flushBuffer(span.spanContext().traceId, buffer) |
| 111 | + return true |
| 112 | + } |
| 113 | + |
| 114 | + return false |
| 115 | + } |
| 116 | + |
| 117 | + private flushBuffer(traceId: string, buffer: TraceBuffer): void { |
| 118 | + this.buffers.set(traceId, FLUSHED) |
| 119 | + |
| 120 | + for (const { context, event, span } of buffer.spans) { |
| 121 | + if (event === 'start') { |
| 122 | + // context is always set for 'start' events |
| 123 | + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
| 124 | + this.wrapped.onStart(span as Span, context!) |
| 125 | + } else { |
| 126 | + this.wrapped.onEnd(span as ReadableSpan) |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | +} |
0 commit comments