1- import type { Envelope , IntegrationFn , Span , SpanV2JSON } from '@sentry/core' ;
2- import { createEnvelope , debug , defineIntegration , isV2BeforeSendSpanCallback , spanToV2JSON } from '@sentry/core' ;
1+ import type { IntegrationFn , Span , SpanV2JSON } from '@sentry/core' ;
2+ import {
3+ debug ,
4+ defineIntegration ,
5+ getDynamicSamplingContextFromSpan ,
6+ isV2BeforeSendSpanCallback ,
7+ spanToV2JSON ,
8+ } from '@sentry/core' ;
39import { DEBUG_BUILD } from '../debug-build' ;
10+ import { createSpanV2Envelope } from '@sentry/core/build/types/envelope' ;
411
512export interface SpanStreamingOptions {
613 batchLimit : number ;
@@ -35,13 +42,14 @@ const _spanStreamingIntegration = ((userOptions?: Partial<SpanStreamingOptions>)
3542 const initialMessage = 'spanStreamingIntegration requires' ;
3643 const fallbackMsg = 'Falling back to static trace lifecycle.' ;
3744
38- if ( DEBUG_BUILD && clientOptions . traceLifecycle !== 'streamed' ) {
39- debug . warn ( `${ initialMessage } \`traceLifecycle\` to be set to "streamed"! ${ fallbackMsg } ` ) ;
45+ if ( clientOptions . traceLifecycle !== 'streamed' ) {
46+ DEBUG_BUILD && debug . warn ( `${ initialMessage } \`traceLifecycle\` to be set to "streamed"! ${ fallbackMsg } ` ) ;
4047 return ;
4148 }
4249
43- if ( DEBUG_BUILD && beforeSendSpan && ! isV2BeforeSendSpanCallback ( beforeSendSpan ) ) {
44- debug . warn ( `${ initialMessage } a beforeSendSpan callback using \`makeV2Callback\`! ${ fallbackMsg } ` ) ;
50+ if ( beforeSendSpan && ! isV2BeforeSendSpanCallback ( beforeSendSpan ) ) {
51+ DEBUG_BUILD &&
52+ debug . warn ( `${ initialMessage } a beforeSendSpan callback using \`makeV2Callback\`! ${ fallbackMsg } ` ) ;
4553 return ;
4654 }
4755
@@ -54,6 +62,8 @@ const _spanStreamingIntegration = ((userOptions?: Partial<SpanStreamingOptions>)
5462 }
5563 } ) ;
5664
65+ // For now, we send all spans on local segment (root) span end.
66+ // TODO: This will change once we have more concrete ideas about a universal SDK data buffer.
5767 client . on ( 'segmentSpanEnd' , segmentSpan => {
5868 const traceId = segmentSpan . spanContext ( ) . traceId ;
5969 const spansOfTrace = traceMap . get ( traceId ) ;
@@ -65,25 +75,24 @@ const _spanStreamingIntegration = ((userOptions?: Partial<SpanStreamingOptions>)
6575
6676 const serializedSpans = Array . from ( spansOfTrace ?? [ ] ) . map ( span => {
6777 const serializedSpan = spanToV2JSON ( span ) ;
68- const finalSpan = beforeSendSpan ? beforeSendSpan ( serializedSpan ) : serializedSpan ;
69- return finalSpan ;
78+ return beforeSendSpan ? beforeSendSpan ( serializedSpan ) : serializedSpan ;
7079 } ) ;
7180
7281 const batches : SpanV2JSON [ ] [ ] = [ ] ;
7382 for ( let i = 0 ; i < serializedSpans . length ; i += options . batchLimit ) {
7483 batches . push ( serializedSpans . slice ( i , i + options . batchLimit ) ) ;
7584 }
7685
77- debug . log ( `Sending trace ${ traceId } in ${ batches . length } batche${ batches . length === 1 ? '' : 's' } ` ) ;
86+ DEBUG_BUILD &&
87+ debug . log ( `Sending trace ${ traceId } in ${ batches . length } batche${ batches . length === 1 ? '' : 's' } ` ) ;
7888
7989 // TODO: Apply scopes to spans
80-
81- // TODO: Apply beforeSendSpan to spans
82-
8390 // TODO: Apply ignoreSpans to spans
8491
92+ const dsc = getDynamicSamplingContextFromSpan ( segmentSpan ) ;
93+
8594 for ( const batch of batches ) {
86- const envelope = createSpanStreamEnvelope ( batch ) ;
95+ const envelope = createSpanV2Envelope ( batch , dsc , client ) ;
8796 // no need to handle client reports for network errors,
8897 // buffer overflows or rate limiting here. All of this is handled
8998 // by client and transport.
@@ -99,7 +108,3 @@ const _spanStreamingIntegration = ((userOptions?: Partial<SpanStreamingOptions>)
99108} ) satisfies IntegrationFn ;
100109
101110export const spanStreamingIntegration = defineIntegration ( _spanStreamingIntegration ) ;
102-
103- function createSpanStreamEnvelope ( serializedSpans : StreamedSpanJSON [ ] ) : Envelope {
104- return createEnvelope < SpanEnvelope > ( headers , [ item ] ) ;
105- }
0 commit comments