11import * as api from '@opentelemetry/api' ;
2- import type { Carrier , Hub , RunWithAsyncContextOptions } from '@sentry/core' ;
3- import { ensureHubOnCarrier , getHubFromCarrier , setAsyncContextStrategy } from '@sentry/core' ;
2+ import type { Hub , RunWithAsyncContextOptions } from '@sentry/core' ;
3+ import { setAsyncContextStrategy } from '@sentry/core' ;
44
5- const hubKey = api . createContextKey ( 'sentry_hub' ) ;
5+ import { OTEL_CONTEXT_HUB_KEY } from './otelContextManager' ;
66
77/**
88 * Sets the async context strategy to use follow the OTEL context under the hood.
@@ -12,15 +12,10 @@ export function setOtelContextAsyncContextStrategy(): void {
1212 const ctx = api . context . active ( ) ;
1313
1414 // Returning undefined means the global hub will be used
15- return ctx . getValue ( hubKey ) as Hub | undefined ;
16- }
17-
18- function createNewHub ( parent : Hub | undefined ) : Hub {
19- const carrier : Carrier = { } ;
20- ensureHubOnCarrier ( carrier , parent ) ;
21- return getHubFromCarrier ( carrier ) ;
15+ return ctx . getValue ( OTEL_CONTEXT_HUB_KEY ) as Hub | undefined ;
2216 }
2317
18+ /* This is more or less a NOOP - we rely on the OTEL context manager for this */
2419 function runWithAsyncContext < T > ( callback : ( ) => T , options : RunWithAsyncContextOptions ) : T {
2520 const existingHub = getCurrentHub ( ) ;
2621
@@ -30,11 +25,10 @@ export function setOtelContextAsyncContextStrategy(): void {
3025 return callback ( ) ;
3126 }
3227
33- const newHub = createNewHub ( existingHub ) ;
34-
3528 const ctx = api . context . active ( ) ;
3629
37- return api . context . with ( ctx . setValue ( hubKey , newHub ) , ( ) => {
30+ // We depend on the otelContextManager to handle the context/hub
31+ return api . context . with ( ctx , ( ) => {
3832 return callback ( ) ;
3933 } ) ;
4034 }
0 commit comments