Skip to content

Commit 4f36293

Browse files
committed
fix(opentelemetry): Ensure _getTraceInfoFromScope works
Also ensure that `withScope(scope, callback)` maintains the active span from the passed in scope.
1 parent 72057b6 commit 4f36293

File tree

4 files changed

+40
-11
lines changed

4 files changed

+40
-11
lines changed

packages/core/src/client.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable max-lines */
22
import { getEnvelopeEndpointWithUrlEncodedAuth } from './api';
33
import { DEFAULT_ENVIRONMENT } from './constants';
4-
import { getCurrentScope, getIsolationScope, getTraceContextFromScope } from './currentScopes';
4+
import { getCurrentScope, getIsolationScope, getTraceContextFromScope, withScope } from './currentScopes';
55
import { DEBUG_BUILD } from './debug-build';
66
import { createEventEnvelope, createSessionEnvelope } from './envelope';
77
import type { IntegrationIndex } from './integration';
@@ -36,8 +36,7 @@ import { getPossibleEventMessages } from './utils/eventUtils';
3636
import { merge } from './utils/merge';
3737
import { parseSampleRate } from './utils/parseSampleRate';
3838
import { prepareEvent } from './utils/prepareEvent';
39-
import { _getSpanForScope } from './utils/spanOnScope';
40-
import { showSpanDropWarning, spanToTraceContext } from './utils/spanUtils';
39+
import { getActiveSpan, showSpanDropWarning, spanToTraceContext } from './utils/spanUtils';
4140
import { convertSpanJsonToTransactionEvent, convertTransactionEventToSpanJson } from './utils/transactionEvent';
4241
import { createClientReportEnvelope } from './utils-hoist/clientreport';
4342
import { dsnToString, makeDsn } from './utils-hoist/dsn';
@@ -1325,10 +1324,12 @@ export function _getTraceInfoFromScope(
13251324
return [undefined, undefined];
13261325
}
13271326

1328-
const span = _getSpanForScope(scope);
1329-
const traceContext = span ? spanToTraceContext(span) : getTraceContextFromScope(scope);
1330-
const dynamicSamplingContext = span
1331-
? getDynamicSamplingContextFromSpan(span)
1332-
: getDynamicSamplingContextFromScope(client, scope);
1333-
return [dynamicSamplingContext, traceContext];
1327+
return withScope(scope, () => {
1328+
const span = getActiveSpan();
1329+
const traceContext = span ? spanToTraceContext(span) : getTraceContextFromScope(scope);
1330+
const dynamicSamplingContext = span
1331+
? getDynamicSamplingContextFromSpan(span)
1332+
: getDynamicSamplingContextFromScope(client, scope);
1333+
return [dynamicSamplingContext, traceContext];
1334+
});
13341335
}

packages/core/test/lib/tracing/trace.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1781,6 +1781,19 @@ describe('getActiveSpan', () => {
17811781
const result = getActiveSpan();
17821782
expect(result).toBe(staticSpan);
17831783
});
1784+
1785+
it('handles active span when passing scopes to withScope', () => {
1786+
const [scope, span] = startSpan({ name: 'outer' }, span => {
1787+
return [getCurrentScope(), span];
1788+
});
1789+
1790+
const spanOnScope = withScope(scope, () => {
1791+
return getActiveSpan();
1792+
});
1793+
1794+
expect(spanOnScope).toBeDefined();
1795+
expect(spanOnScope).toBe(span);
1796+
});
17841797
});
17851798

17861799
describe('withActiveSpan()', () => {

packages/opentelemetry/src/asyncContextStrategy.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
} from './constants';
99
import { continueTrace, startInactiveSpan, startSpan, startSpanManual, withActiveSpan } from './trace';
1010
import type { CurrentScopes } from './types';
11-
import { getScopesFromContext } from './utils/contextData';
11+
import { getContextFromScope, getScopesFromContext } from './utils/contextData';
1212
import { getActiveSpan } from './utils/getActiveSpan';
1313
import { getTraceData } from './utils/getTraceData';
1414
import { suppressTracing } from './utils/suppressTracing';
@@ -48,7 +48,7 @@ export function setOpenTelemetryContextAsyncContextStrategy(): void {
4848
}
4949

5050
function withSetScope<T>(scope: Scope, callback: (scope: Scope) => T): T {
51-
const ctx = api.context.active();
51+
const ctx = getContextFromScope(scope) || api.context.active();
5252

5353
// We depend on the otelContextManager to handle the context/hub
5454
// We set the `SENTRY_FORK_SET_SCOPE_CONTEXT_KEY` context value, which is picked up by

packages/opentelemetry/test/trace.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1342,6 +1342,21 @@ describe('trace', () => {
13421342
});
13431343
});
13441344
});
1345+
1346+
describe('scope passing', () => {
1347+
it('handles active span when passing scopes to withScope', () => {
1348+
const [scope, span] = startSpan({ name: 'outer' }, span => {
1349+
return [getCurrentScope(), span];
1350+
});
1351+
1352+
const spanOnScope = withScope(scope, () => {
1353+
return getActiveSpan();
1354+
});
1355+
1356+
expect(spanOnScope).toBeDefined();
1357+
expect(spanOnScope).toBe(span);
1358+
});
1359+
});
13451360
});
13461361

13471362
describe('trace (tracing disabled)', () => {

0 commit comments

Comments
 (0)