Skip to content

Commit cd14035

Browse files
committed
fix(core): Use consistent continueTrace implementation in core
1 parent 25874a9 commit cd14035

File tree

12 files changed

+36
-20
lines changed

12 files changed

+36
-20
lines changed

packages/astro/src/index.types.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ export declare function flush(timeout?: number | undefined): PromiseLike<boolean
2727
// eslint-disable-next-line deprecation/deprecation
2828
export declare const getCurrentHub: typeof clientSdk.getCurrentHub;
2929
export declare const getClient: typeof clientSdk.getClient;
30-
export declare const continueTrace: typeof clientSdk.continueTrace;
3130

3231
export declare const Span: clientSdk.Span;
3332

packages/core/src/asyncContext/types.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { Scope } from '../scope';
22
import type { getTraceData } from '../utils/traceData';
33
import type {
4+
continueTrace,
45
startInactiveSpan,
56
startSpan,
67
startSpanManual,
@@ -68,4 +69,11 @@ export interface AsyncContextStrategy {
6869

6970
/** Get trace data as serialized string values for propagation via `sentry-trace` and `baggage`. */
7071
getTraceData?: typeof getTraceData;
72+
73+
/**
74+
* Continue a trace from `sentry-trace` and `baggage` values.
75+
* These values can be obtained from incoming request headers, or in the browser from `<meta name="sentry-trace">`
76+
* and `<meta name="baggage">` HTML tags.
77+
*/
78+
continueTrace?: typeof continueTrace;
7179
}

packages/core/src/tracing/trace.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -192,15 +192,20 @@ export function startInactiveSpan(options: StartSpanOptions): Span {
192192
* be attached to the incoming trace.
193193
*/
194194
export const continueTrace = <V>(
195-
{
196-
sentryTrace,
197-
baggage,
198-
}: {
195+
options: {
199196
sentryTrace: Parameters<typeof propagationContextFromHeaders>[0];
200197
baggage: Parameters<typeof propagationContextFromHeaders>[1];
201198
},
202199
callback: () => V,
203200
): V => {
201+
const carrier = getMainCarrier();
202+
const acs = getAsyncContextStrategy(carrier);
203+
if (acs.continueTrace) {
204+
return acs.continueTrace(options, callback);
205+
}
206+
207+
const { sentryTrace, baggage } = options;
208+
204209
return withScope(scope => {
205210
const propagationContext = propagationContextFromHeaders(sentryTrace, baggage);
206211
scope.setPropagationContext(propagationContext);

packages/node/src/index.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,6 @@ export type { NodeOptions } from './types';
5858
export { addRequestDataToEvent, DEFAULT_USER_INCLUDES, extractRequestData } from '@sentry/core';
5959

6060
export {
61-
// These are custom variants that need to be used instead of the core one
62-
// As they have slightly different implementations
63-
continueTrace,
6461
// This needs exporting so the NodeClient can be used without calling init
6562
setOpenTelemetryContextAsyncContextStrategy as setNodeAsyncContextStrategy,
6663
} from '@sentry/opentelemetry';
@@ -105,6 +102,7 @@ export {
105102
getIsolationScope,
106103
getTraceData,
107104
getTraceMetaTags,
105+
continueTrace,
108106
withScope,
109107
withIsolationScope,
110108
captureException,

packages/nuxt/src/index.types.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,3 @@ export declare const linkedErrorsIntegration: typeof clientSdk.linkedErrorsInteg
1414
export declare const contextLinesIntegration: typeof clientSdk.contextLinesIntegration;
1515
export declare const getDefaultIntegrations: (options: Options) => Integration[];
1616
export declare const defaultStackParser: StackParser;
17-
export declare const continueTrace: typeof clientSdk.continueTrace;

packages/opentelemetry/src/asyncContextStrategy.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
SENTRY_FORK_SET_ISOLATION_SCOPE_CONTEXT_KEY,
77
SENTRY_FORK_SET_SCOPE_CONTEXT_KEY,
88
} from './constants';
9-
import { startInactiveSpan, startSpan, startSpanManual, withActiveSpan } from './trace';
9+
import { continueTrace, startInactiveSpan, startSpan, startSpanManual, withActiveSpan } from './trace';
1010
import type { CurrentScopes } from './types';
1111
import { getScopesFromContext } from './utils/contextData';
1212
import { getActiveSpan } from './utils/getActiveSpan';
@@ -103,6 +103,7 @@ export function setOpenTelemetryContextAsyncContextStrategy(): void {
103103
getActiveSpan,
104104
suppressTracing,
105105
getTraceData,
106+
continueTrace,
106107
// The types here don't fully align, because our own `Span` type is narrower
107108
// than the OTEL one - but this is OK for here, as we now we'll only have OTEL spans passed around
108109
withActiveSpan: withActiveSpan as typeof defaultWithActiveSpan,

packages/opentelemetry/src/trace.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,28 @@
11
import type { Context, Span, SpanContext, SpanOptions, Tracer } from '@opentelemetry/api';
22
import { SpanStatusCode, TraceFlags, context, trace } from '@opentelemetry/api';
33
import { suppressTracing } from '@opentelemetry/core';
4-
import type { Client, DynamicSamplingContext, Scope, Span as SentrySpan, TraceContext } from '@sentry/core';
4+
import type {
5+
Client,
6+
DynamicSamplingContext,
7+
Scope,
8+
Span as SentrySpan,
9+
TraceContext,
10+
continueTrace as baseContinueTrace,
11+
} from '@sentry/core';
512
import {
613
SDK_VERSION,
714
SEMANTIC_ATTRIBUTE_SENTRY_OP,
8-
continueTrace as baseContinueTrace,
915
getClient,
1016
getCurrentScope,
1117
getDynamicSamplingContextFromScope,
1218
getDynamicSamplingContextFromSpan,
1319
getRootSpan,
1420
getTraceContextFromScope,
1521
handleCallbackErrors,
22+
propagationContextFromHeaders,
1623
spanToJSON,
1724
spanToTraceContext,
25+
withScope,
1826
} from '@sentry/core';
1927
import { continueTraceAsRemoteSpan } from './propagator';
2028
import type { OpenTelemetryClient, OpenTelemetrySpanContext } from './types';
@@ -247,7 +255,10 @@ function getContextForScope(scope?: Scope): Context {
247255
* It propagates the trace as a remote span, in addition to setting it on the propagation context.
248256
*/
249257
export function continueTrace<T>(options: Parameters<typeof baseContinueTrace>[0], callback: () => T): T {
250-
return baseContinueTrace(options, () => {
258+
return withScope(scope => {
259+
const { sentryTrace, baggage } = options;
260+
const propagationContext = propagationContextFromHeaders(sentryTrace, baggage);
261+
scope.setPropagationContext(propagationContext);
251262
return continueTraceAsRemoteSpan(context.active(), options, callback);
252263
});
253264
}

packages/remix/src/index.types.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ declare const runtime: 'client' | 'server';
3333
// eslint-disable-next-line deprecation/deprecation
3434
export declare const getCurrentHub: typeof clientSdk.getCurrentHub;
3535
export declare const getClient: typeof clientSdk.getClient;
36-
export declare const continueTrace: typeof clientSdk.continueTrace;
3736

3837
export const close = runtime === 'client' ? clientSdk.close : serverSdk.close;
3938
export const flush = runtime === 'client' ? clientSdk.flush : serverSdk.flush;

packages/remix/src/utils/instrumentServer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
SEMANTIC_ATTRIBUTE_SENTRY_OP,
55
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
66
SEMANTIC_ATTRIBUTE_SENTRY_SOURCE,
7+
continueTrace,
78
fill,
89
getActiveSpan,
910
getClient,
@@ -19,7 +20,6 @@ import {
1920
winterCGRequestToRequestData,
2021
withIsolationScope,
2122
} from '@sentry/core';
22-
import { continueTrace } from '@sentry/opentelemetry';
2323
import { DEBUG_BUILD } from './debug-build';
2424
import { captureRemixServerException, errorHandleDataFunction, errorHandleDocumentRequestFunction } from './errors';
2525
import { getFutureFlagsServer, getRemixVersionFromBuild } from './futureFlags';

packages/solidstart/src/index.types.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,3 @@ export declare const getClient: typeof clientSdk.getClient;
2424
export declare function close(timeout?: number | undefined): PromiseLike<boolean>;
2525
export declare function flush(timeout?: number | undefined): PromiseLike<boolean>;
2626
export declare function lastEventId(): string | undefined;
27-
28-
export declare const continueTrace: typeof clientSdk.continueTrace;

packages/sveltekit/src/index.types.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,4 @@ export declare function close(timeout?: number | undefined): PromiseLike<boolean
5050
export declare function flush(timeout?: number | undefined): PromiseLike<boolean>;
5151
export declare function lastEventId(): string | undefined;
5252

53-
export declare const continueTrace: typeof clientSdk.continueTrace;
54-
5553
export declare function trackComponent(options: clientSdk.TrackingOptions): ReturnType<typeof clientSdk.trackComponent>;

packages/sveltekit/src/server/handle.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { Span } from '@sentry/core';
22
import {
33
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
44
SEMANTIC_ATTRIBUTE_SENTRY_SOURCE,
5+
continueTrace,
56
getActiveSpan,
67
getCurrentScope,
78
getDefaultIsolationScope,
@@ -13,7 +14,6 @@ import {
1314
winterCGRequestToRequestData,
1415
withIsolationScope,
1516
} from '@sentry/core';
16-
import { continueTrace } from '@sentry/node';
1717
import type { Handle, ResolveOptions } from '@sveltejs/kit';
1818

1919
import { DEBUG_BUILD } from '../common/debug-build';

0 commit comments

Comments
 (0)