@@ -3,7 +3,14 @@ import type { ExportResult } from '@opentelemetry/core';
33import { ExportResultCode } from '@opentelemetry/core' ;
44import type { ReadableSpan , SpanExporter } from '@opentelemetry/sdk-trace-base' ;
55import { SemanticAttributes } from '@opentelemetry/semantic-conventions' ;
6- import { SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE , flush , getCurrentHub , getCurrentScope } from '@sentry/core' ;
6+ import {
7+ SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE ,
8+ flush ,
9+ getCurrentHub ,
10+ getCurrentScope ,
11+ getIsolationScope ,
12+ setCapturedScopesOnSpan ,
13+ } from '@sentry/core' ;
714import type { Scope , Span as SentrySpan , SpanOrigin , TransactionSource } from '@sentry/types' ;
815import { logger } from '@sentry/utils' ;
916
@@ -17,7 +24,7 @@ import type { SpanNode } from './utils/groupSpansWithParents';
1724import { groupSpansWithParents } from './utils/groupSpansWithParents' ;
1825import { mapStatus } from './utils/mapStatus' ;
1926import { parseSpanDescription } from './utils/parseSpanDescription' ;
20- import { getSpanFinishScope , getSpanHub , getSpanMetadata , getSpanScope } from './utils/spanData' ;
27+ import { getSpanFinishScopes , getSpanHub , getSpanMetadata , getSpanScope } from './utils/spanData' ;
2128
2229type SpanNodeCompleted = SpanNode & { span : ReadableSpan } ;
2330
@@ -109,24 +116,30 @@ function maybeSend(spans: ReadableSpan[]): ReadableSpan[] {
109116
110117 // Now finish the transaction, which will send it together with all the spans
111118 // We make sure to use the finish scope
112- const scope = getScopeForTransactionFinish ( span ) ;
113- transaction . finishWithScope ( convertOtelTimeToSeconds ( span . endTime ) , scope ) ;
119+ const { scope, isolationScope } = getScopesForTransactionFinish ( span ) ;
120+ setCapturedScopesOnSpan ( transaction , scope , isolationScope ) ;
121+
122+ transaction . end ( span . endTime ) ;
114123 } ) ;
115124
116125 return Array . from ( remaining )
117126 . map ( node => node . span )
118127 . filter ( ( span ) : span is ReadableSpan => ! ! span ) ;
119128}
120129
121- function getScopeForTransactionFinish ( span : ReadableSpan ) : Scope {
130+ function getScopesForTransactionFinish ( span : ReadableSpan ) : { scope : Scope ; isolationScope : Scope } {
122131 // The finish scope should normally always be there (and it is already a clone),
123132 // but for the sake of type safety we fall back to a clone of the current scope
124- const scope = getSpanFinishScope ( span ) || getCurrentScope ( ) . clone ( ) ;
133+ const scopes = getSpanFinishScopes ( span ) ;
134+ const scope = scopes ?. scope || getCurrentScope ( ) . clone ( ) ;
135+ const isolationScope = scopes ?. isolationScope || getIsolationScope ( ) ;
136+
125137 scope . setContext ( 'otel' , {
126138 attributes : removeSentryAttributes ( span . attributes ) ,
127139 resource : span . resource . attributes ,
128140 } ) ;
129- return scope ;
141+
142+ return { scope, isolationScope } ;
130143}
131144
132145function getCompletedRootNodes ( nodes : SpanNode [ ] ) : SpanNodeCompleted [ ] {
0 commit comments