@@ -5,7 +5,15 @@ import type { Span as OtelSpan } from '@opentelemetry/sdk-trace-base';
55import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node' ;
66import { SemanticAttributes , SemanticResourceAttributes } from '@opentelemetry/semantic-conventions' ;
77import type { SpanStatusType } from '@sentry/core' ;
8- import { addTracingExtensions , createTransport , Hub , makeMain , Span as SentrySpan , Transaction } from '@sentry/core' ;
8+ import {
9+ addTracingExtensions ,
10+ createTransport ,
11+ Hub ,
12+ makeMain ,
13+ Scope ,
14+ Span as SentrySpan ,
15+ Transaction ,
16+ } from '@sentry/core' ;
917import { NodeClient } from '@sentry/node' ;
1018import { resolvedSyncPromise } from '@sentry/utils' ;
1119
@@ -79,13 +87,9 @@ describe('SentrySpanProcessor', () => {
7987 expect ( sentrySpanTransaction ?. parentSpanId ) . toEqual ( otelSpan . parentSpanId ) ;
8088 expect ( sentrySpanTransaction ?. spanId ) . toEqual ( otelSpan . spanContext ( ) . spanId ) ;
8189
82- expect ( hub . getScope ( ) ?. getSpan ( ) ) . toBeUndefined ( ) ;
83-
8490 otelSpan . end ( endTime ) ;
8591
8692 expect ( sentrySpanTransaction ?. endTimestamp ) . toBe ( endTimestampMs / 1000 ) ;
87-
88- expect ( hub . getScope ( ) ?. getSpan ( ) ) . toBeUndefined ( ) ;
8993 } ) ;
9094
9195 it ( 'creates a child span if there is a running transaction' , ( ) => {
@@ -789,6 +793,46 @@ describe('SentrySpanProcessor', () => {
789793 trace_id : otelSpan . spanContext ( ) . traceId ,
790794 } ) ;
791795 } ) ;
796+
797+ // Regression test for https://github.com/getsentry/sentry-javascript/issues/7538
798+ // Since otel context does not map to when Sentry hubs are cloned
799+ // we can't rely on the original hub at transaction creation to contain all
800+ // the scope information we want. Let's test to make sure that the information is
801+ // grabbed from the new hub.
802+ it ( 'handles when a different hub creates the transaction' , ( ) => {
803+ let sentryTransaction : any ;
804+
805+ client = new NodeClient ( {
806+ ...DEFAULT_NODE_CLIENT_OPTIONS ,
807+ tracesSampleRate : 1.0 ,
808+ } ) ;
809+
810+ client . on ( 'finishTransaction' , transaction => {
811+ sentryTransaction = transaction ;
812+ } ) ;
813+
814+ hub = new Hub ( client ) ;
815+ makeMain ( hub ) ;
816+
817+ const newHub = new Hub ( client , Scope . clone ( hub . getScope ( ) ) ) ;
818+ newHub . configureScope ( scope => {
819+ scope . setTag ( 'foo' , 'bar' ) ;
820+ } ) ;
821+
822+ const tracer = provider . getTracer ( 'default' ) ;
823+
824+ tracer . startActiveSpan ( 'GET /users' , parentOtelSpan => {
825+ tracer . startActiveSpan ( 'SELECT * FROM users;' , child => {
826+ makeMain ( newHub ) ;
827+ child . end ( ) ;
828+ } ) ;
829+
830+ parentOtelSpan . end ( ) ;
831+ } ) ;
832+
833+ // @ts -ignore Accessing private attributes
834+ expect ( sentryTransaction . _hub . getScope ( ) . _tags . foo ) . toEqual ( 'bar' ) ;
835+ } ) ;
792836} ) ;
793837
794838// OTEL expects a custom date format
0 commit comments