|
| 1 | +import type { Envelope, IntegrationFn, Span, SpanV2JSON } from '@sentry/core'; |
| 2 | +import { createEnvelope, debug, defineIntegration, isV2BeforeSendSpanCallback, spanToV2JSON } from '@sentry/core'; |
| 3 | +import { DEBUG_BUILD } from '../debug-build'; |
| 4 | + |
| 5 | +export interface SpanStreamingOptions { |
| 6 | + batchLimit: number; |
| 7 | +} |
| 8 | + |
| 9 | +const _spanStreamingIntegration = ((userOptions?: Partial<SpanStreamingOptions>) => { |
| 10 | + const validatedUserProvidedBatchLimit = |
| 11 | + userOptions?.batchLimit && userOptions.batchLimit <= 1000 && userOptions.batchLimit >= 1 |
| 12 | + ? userOptions.batchLimit |
| 13 | + : undefined; |
| 14 | + |
| 15 | + if (DEBUG_BUILD && userOptions?.batchLimit && !validatedUserProvidedBatchLimit) { |
| 16 | + debug.warn('SpanStreaming batchLimit must be between 1 and 1000, defaulting to 1000'); |
| 17 | + } |
| 18 | + |
| 19 | + const options: SpanStreamingOptions = { |
| 20 | + batchLimit: |
| 21 | + userOptions?.batchLimit && userOptions.batchLimit <= 1000 && userOptions.batchLimit >= 1 |
| 22 | + ? userOptions.batchLimit |
| 23 | + : 1000, |
| 24 | + ...userOptions, |
| 25 | + }; |
| 26 | + |
| 27 | + const traceMap = new Map<string, Set<Span>>(); |
| 28 | + |
| 29 | + return { |
| 30 | + name: 'SpanStreaming', |
| 31 | + setup(client) { |
| 32 | + const clientOptions = client.getOptions(); |
| 33 | + const beforeSendSpan = clientOptions.beforeSendSpan; |
| 34 | + |
| 35 | + const initialMessage = 'spanStreamingIntegration requires'; |
| 36 | + const fallbackMsg = 'Falling back to static trace lifecycle.'; |
| 37 | + |
| 38 | + if (DEBUG_BUILD && clientOptions.traceLifecycle !== 'streamed') { |
| 39 | + debug.warn(`${initialMessage} \`traceLifecycle\` to be set to "streamed"! ${fallbackMsg}`); |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + if (DEBUG_BUILD && beforeSendSpan && !isV2BeforeSendSpanCallback(beforeSendSpan)) { |
| 44 | + debug.warn(`${initialMessage} a beforeSendSpan callback using \`makeV2Callback\`! ${fallbackMsg}`); |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + client.on('spanEnd', span => { |
| 49 | + const spanBuffer = traceMap.get(span.spanContext().traceId); |
| 50 | + if (spanBuffer) { |
| 51 | + spanBuffer.add(span); |
| 52 | + } else { |
| 53 | + traceMap.set(span.spanContext().traceId, new Set([span])); |
| 54 | + } |
| 55 | + }); |
| 56 | + |
| 57 | + client.on('segmentSpanEnd', segmentSpan => { |
| 58 | + const traceId = segmentSpan.spanContext().traceId; |
| 59 | + const spansOfTrace = traceMap.get(traceId); |
| 60 | + |
| 61 | + if (!spansOfTrace?.size) { |
| 62 | + traceMap.delete(traceId); |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + const serializedSpans = Array.from(spansOfTrace ?? []).map(span => { |
| 67 | + const serializedSpan = spanToV2JSON(span); |
| 68 | + const finalSpan = beforeSendSpan ? beforeSendSpan(serializedSpan) : serializedSpan; |
| 69 | + return finalSpan; |
| 70 | + }); |
| 71 | + |
| 72 | + const batches: SpanV2JSON[][] = []; |
| 73 | + for (let i = 0; i < serializedSpans.length; i += options.batchLimit) { |
| 74 | + batches.push(serializedSpans.slice(i, i + options.batchLimit)); |
| 75 | + } |
| 76 | + |
| 77 | + debug.log(`Sending trace ${traceId} in ${batches.length} batche${batches.length === 1 ? '' : 's'}`); |
| 78 | + |
| 79 | + // TODO: Apply scopes to spans |
| 80 | + |
| 81 | + // TODO: Apply beforeSendSpan to spans |
| 82 | + |
| 83 | + // TODO: Apply ignoreSpans to spans |
| 84 | + |
| 85 | + for (const batch of batches) { |
| 86 | + const envelope = createSpanStreamEnvelope(batch); |
| 87 | + // no need to handle client reports for network errors, |
| 88 | + // buffer overflows or rate limiting here. All of this is handled |
| 89 | + // by client and transport. |
| 90 | + client.sendEnvelope(envelope).then(null, reason => { |
| 91 | + DEBUG_BUILD && debug.error('Error while sending span stream envelope:', reason); |
| 92 | + }); |
| 93 | + } |
| 94 | + |
| 95 | + traceMap.delete(traceId); |
| 96 | + }); |
| 97 | + }, |
| 98 | + }; |
| 99 | +}) satisfies IntegrationFn; |
| 100 | + |
| 101 | +export const spanStreamingIntegration = defineIntegration(_spanStreamingIntegration); |
| 102 | + |
| 103 | +function createSpanStreamEnvelope(serializedSpans: StreamedSpanJSON[]): Envelope { |
| 104 | + return createEnvelope<SpanEnvelope>(headers, [item]); |
| 105 | +} |
0 commit comments