11import type { Context } from '@opentelemetry/api' ;
22import { ROOT_CONTEXT , trace } from '@opentelemetry/api' ;
33import type { ReadableSpan , Span , SpanProcessor as SpanProcessorInterface } from '@opentelemetry/sdk-trace-base' ;
4+ import type { Client } from '@sentry/core' ;
45import {
56 addChildSpanToSpan ,
7+ captureSpan ,
68 getClient ,
79 getDefaultCurrentScope ,
810 getDefaultIsolationScope ,
@@ -11,7 +13,9 @@ import {
1113 setCapturedScopesOnSpan ,
1214} from '@sentry/core' ;
1315import { SEMANTIC_ATTRIBUTE_SENTRY_PARENT_IS_REMOTE } from './semanticAttributes' ;
16+ import type { ISentrySpanExporter } from './spanExporter' ;
1417import { SentrySpanExporter } from './spanExporter' ;
18+ import { StreamingSpanExporter } from './streamedSpanExporter' ;
1519import { getScopesFromContext } from './utils/contextData' ;
1620import { setIsSetup } from './utils/setupCheck' ;
1721
@@ -51,24 +55,22 @@ function onSpanStart(span: Span, parentContext: Context): void {
5155 client ?. emit ( 'spanStart' , span ) ;
5256}
5357
54- function onSpanEnd ( span : Span ) : void {
55- logSpanEnd ( span ) ;
56-
57- const client = getClient ( ) ;
58- client ?. emit ( 'spanEnd' , span ) ;
59- client ?. emit ( 'afterSpanEnd' , span ) ;
60- }
61-
6258/**
6359 * Converts OpenTelemetry Spans to Sentry Spans and sends them to Sentry via
6460 * the Sentry SDK.
6561 */
6662export class SentrySpanProcessor implements SpanProcessorInterface {
67- private _exporter : SentrySpanExporter ;
63+ private _exporter : ISentrySpanExporter ;
64+ private _client : Client | undefined ;
6865
69- public constructor ( options ?: { timeout ?: number } ) {
66+ public constructor ( options ?: { timeout ?: number ; client ?: Client } ) {
7067 setIsSetup ( 'SentrySpanProcessor' ) ;
71- this . _exporter = new SentrySpanExporter ( options ) ;
68+ this . _client = options ?. client ?? getClient ( ) ;
69+ if ( this . _client ?. getOptions ( ) . traceLifecycle === 'stream' ) {
70+ this . _exporter = new StreamingSpanExporter ( this . _client , { flushInterval : options ?. timeout } ) ;
71+ } else {
72+ this . _exporter = new SentrySpanExporter ( options ) ;
73+ }
7274 }
7375
7476 /**
@@ -94,8 +96,16 @@ export class SentrySpanProcessor implements SpanProcessorInterface {
9496
9597 /** @inheritDoc */
9698 public onEnd ( span : Span & ReadableSpan ) : void {
97- onSpanEnd ( span ) ;
99+ logSpanEnd ( span ) ;
100+
101+ this . _client ?. emit ( 'spanEnd' , span ) ;
98102
99- this . _exporter . export ( span ) ;
103+ if ( this . _client ?. getOptions ( ) . traceLifecycle === 'stream' ) {
104+ // we probably don't need to emit afterSpanEnd here but can call captureSpan directly.
105+ // might need to revisit but let's see.
106+ captureSpan ( span , this . _client ) ;
107+ } else {
108+ this . _exporter . export ( span ) ;
109+ }
100110 }
101111}
0 commit comments