Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions packages/core/src/client.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable max-lines */
import { getEnvelopeEndpointWithUrlEncodedAuth } from './api';
import { DEFAULT_ENVIRONMENT } from './constants';
import { getCurrentScope, getIsolationScope, getTraceContextFromScope } from './currentScopes';
import { getCurrentScope, getIsolationScope, getTraceContextFromScope, withScope } from './currentScopes';
import { DEBUG_BUILD } from './debug-build';
import { createEventEnvelope, createSessionEnvelope } from './envelope';
import type { IntegrationIndex } from './integration';
Expand Down Expand Up @@ -36,8 +36,7 @@ import { getPossibleEventMessages } from './utils/eventUtils';
import { merge } from './utils/merge';
import { parseSampleRate } from './utils/parseSampleRate';
import { prepareEvent } from './utils/prepareEvent';
import { _getSpanForScope } from './utils/spanOnScope';
import { showSpanDropWarning, spanToTraceContext } from './utils/spanUtils';
import { getActiveSpan, showSpanDropWarning, spanToTraceContext } from './utils/spanUtils';
import { convertSpanJsonToTransactionEvent, convertTransactionEventToSpanJson } from './utils/transactionEvent';
import { createClientReportEnvelope } from './utils-hoist/clientreport';
import { dsnToString, makeDsn } from './utils-hoist/dsn';
Expand Down Expand Up @@ -1325,10 +1324,12 @@ export function _getTraceInfoFromScope(
return [undefined, undefined];
}

const span = _getSpanForScope(scope);
const traceContext = span ? spanToTraceContext(span) : getTraceContextFromScope(scope);
const dynamicSamplingContext = span
? getDynamicSamplingContextFromSpan(span)
: getDynamicSamplingContextFromScope(client, scope);
return [dynamicSamplingContext, traceContext];
return withScope(scope, () => {
const span = getActiveSpan();
const traceContext = span ? spanToTraceContext(span) : getTraceContextFromScope(scope);
const dynamicSamplingContext = span
? getDynamicSamplingContextFromSpan(span)
: getDynamicSamplingContextFromScope(client, scope);
return [dynamicSamplingContext, traceContext];
});
}
13 changes: 13 additions & 0 deletions packages/core/test/lib/tracing/trace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1781,6 +1781,19 @@ describe('getActiveSpan', () => {
const result = getActiveSpan();
expect(result).toBe(staticSpan);
});

it('handles active span when passing scopes to withScope', () => {
const [scope, span] = startSpan({ name: 'outer' }, span => {
return [getCurrentScope(), span];
});

const spanOnScope = withScope(scope, () => {
return getActiveSpan();
});

expect(spanOnScope).toBeDefined();
expect(spanOnScope).toBe(span);
});
});

describe('withActiveSpan()', () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/opentelemetry/src/asyncContextStrategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from './constants';
import { continueTrace, startInactiveSpan, startSpan, startSpanManual, withActiveSpan } from './trace';
import type { CurrentScopes } from './types';
import { getScopesFromContext } from './utils/contextData';
import { getContextFromScope, getScopesFromContext } from './utils/contextData';
import { getActiveSpan } from './utils/getActiveSpan';
import { getTraceData } from './utils/getTraceData';
import { suppressTracing } from './utils/suppressTracing';
Expand Down Expand Up @@ -48,7 +48,7 @@ export function setOpenTelemetryContextAsyncContextStrategy(): void {
}

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

// We depend on the otelContextManager to handle the context/hub
// We set the `SENTRY_FORK_SET_SCOPE_CONTEXT_KEY` context value, which is picked up by
Expand Down
15 changes: 15 additions & 0 deletions packages/opentelemetry/test/trace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1342,6 +1342,21 @@ describe('trace', () => {
});
});
});

describe('scope passing', () => {
it('handles active span when passing scopes to withScope', () => {
const [scope, span] = startSpan({ name: 'outer' }, span => {
return [getCurrentScope(), span];
});

const spanOnScope = withScope(scope, () => {
return getActiveSpan();
});

expect(spanOnScope).toBeDefined();
expect(spanOnScope).toBe(span);
});
});
});

describe('trace (tracing disabled)', () => {
Expand Down
Loading