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' ;
3
9
import { DEBUG_BUILD } from '../debug-build' ;
10
+ import { createSpanV2Envelope } from '@sentry/core/build/types/envelope' ;
4
11
5
12
export interface SpanStreamingOptions {
6
13
batchLimit : number ;
@@ -35,13 +42,14 @@ const _spanStreamingIntegration = ((userOptions?: Partial<SpanStreamingOptions>)
35
42
const initialMessage = 'spanStreamingIntegration requires' ;
36
43
const fallbackMsg = 'Falling back to static trace lifecycle.' ;
37
44
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 } ` ) ;
40
47
return ;
41
48
}
42
49
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 } ` ) ;
45
53
return ;
46
54
}
47
55
@@ -54,6 +62,8 @@ const _spanStreamingIntegration = ((userOptions?: Partial<SpanStreamingOptions>)
54
62
}
55
63
} ) ;
56
64
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.
57
67
client . on ( 'segmentSpanEnd' , segmentSpan => {
58
68
const traceId = segmentSpan . spanContext ( ) . traceId ;
59
69
const spansOfTrace = traceMap . get ( traceId ) ;
@@ -65,25 +75,24 @@ const _spanStreamingIntegration = ((userOptions?: Partial<SpanStreamingOptions>)
65
75
66
76
const serializedSpans = Array . from ( spansOfTrace ?? [ ] ) . map ( span => {
67
77
const serializedSpan = spanToV2JSON ( span ) ;
68
- const finalSpan = beforeSendSpan ? beforeSendSpan ( serializedSpan ) : serializedSpan ;
69
- return finalSpan ;
78
+ return beforeSendSpan ? beforeSendSpan ( serializedSpan ) : serializedSpan ;
70
79
} ) ;
71
80
72
81
const batches : SpanV2JSON [ ] [ ] = [ ] ;
73
82
for ( let i = 0 ; i < serializedSpans . length ; i += options . batchLimit ) {
74
83
batches . push ( serializedSpans . slice ( i , i + options . batchLimit ) ) ;
75
84
}
76
85
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' } ` ) ;
78
88
79
89
// TODO: Apply scopes to spans
80
-
81
- // TODO: Apply beforeSendSpan to spans
82
-
83
90
// TODO: Apply ignoreSpans to spans
84
91
92
+ const dsc = getDynamicSamplingContextFromSpan ( segmentSpan ) ;
93
+
85
94
for ( const batch of batches ) {
86
- const envelope = createSpanStreamEnvelope ( batch ) ;
95
+ const envelope = createSpanV2Envelope ( batch , dsc , client ) ;
87
96
// no need to handle client reports for network errors,
88
97
// buffer overflows or rate limiting here. All of this is handled
89
98
// by client and transport.
@@ -99,7 +108,3 @@ const _spanStreamingIntegration = ((userOptions?: Partial<SpanStreamingOptions>)
99
108
} ) satisfies IntegrationFn ;
100
109
101
110
export const spanStreamingIntegration = defineIntegration ( _spanStreamingIntegration ) ;
102
-
103
- function createSpanStreamEnvelope ( serializedSpans : StreamedSpanJSON [ ] ) : Envelope {
104
- return createEnvelope < SpanEnvelope > ( headers , [ item ] ) ;
105
- }
0 commit comments