Skip to content

Commit 13b4b47

Browse files
authored
feat(core): Allow to use continueTrace without callback (#9615)
Inspired by recent comments about the continueTrace method, I figured we can actually update this in a backwards-compatible way to _also_ allow to use it without a callback.
1 parent 68bb820 commit 13b4b47

File tree

2 files changed

+64
-2
lines changed

2 files changed

+64
-2
lines changed

packages/core/src/tracing/trace.ts

+23-2
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,23 @@ export function getActiveSpan(): Span | undefined {
203203
return getCurrentHub().getScope().getSpan();
204204
}
205205

206+
export function continueTrace({
207+
sentryTrace,
208+
baggage,
209+
}: {
210+
sentryTrace: Parameters<typeof tracingContextFromHeaders>[0];
211+
baggage: Parameters<typeof tracingContextFromHeaders>[1];
212+
}): Partial<TransactionContext>;
213+
export function continueTrace<V>(
214+
{
215+
sentryTrace,
216+
baggage,
217+
}: {
218+
sentryTrace: Parameters<typeof tracingContextFromHeaders>[0];
219+
baggage: Parameters<typeof tracingContextFromHeaders>[1];
220+
},
221+
callback: (transactionContext: Partial<TransactionContext>) => V,
222+
): V;
206223
/**
207224
* Continue a trace from `sentry-trace` and `baggage` values.
208225
* These values can be obtained from incoming request headers,
@@ -219,8 +236,8 @@ export function continueTrace<V>(
219236
sentryTrace: Parameters<typeof tracingContextFromHeaders>[0];
220237
baggage: Parameters<typeof tracingContextFromHeaders>[1];
221238
},
222-
callback: (transactionContext: Partial<TransactionContext>) => V,
223-
): V {
239+
callback?: (transactionContext: Partial<TransactionContext>) => V,
240+
): V | Partial<TransactionContext> {
224241
const hub = getCurrentHub();
225242
const currentScope = hub.getScope();
226243

@@ -242,6 +259,10 @@ export function continueTrace<V>(
242259
}),
243260
};
244261

262+
if (!callback) {
263+
return transactionContext;
264+
}
265+
245266
return callback(transactionContext);
246267
}
247268

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

+41
Original file line numberDiff line numberDiff line change
@@ -320,4 +320,45 @@ describe('continueTrace', () => {
320320

321321
expect(scope['_sdkProcessingMetadata']).toEqual({});
322322
});
323+
324+
it('returns response of callback', () => {
325+
const expectedContext = {
326+
metadata: {
327+
dynamicSamplingContext: {},
328+
},
329+
parentSampled: false,
330+
parentSpanId: '1121201211212012',
331+
traceId: '12312012123120121231201212312012',
332+
};
333+
334+
const result = continueTrace(
335+
{
336+
sentryTrace: '12312012123120121231201212312012-1121201211212012-0',
337+
baggage: undefined,
338+
},
339+
ctx => {
340+
return { ctx };
341+
},
342+
);
343+
344+
expect(result).toEqual({ ctx: expectedContext });
345+
});
346+
347+
it('works without a callback', () => {
348+
const expectedContext = {
349+
metadata: {
350+
dynamicSamplingContext: {},
351+
},
352+
parentSampled: false,
353+
parentSpanId: '1121201211212012',
354+
traceId: '12312012123120121231201212312012',
355+
};
356+
357+
const ctx = continueTrace({
358+
sentryTrace: '12312012123120121231201212312012-1121201211212012-0',
359+
baggage: undefined,
360+
});
361+
362+
expect(ctx).toEqual(expectedContext);
363+
});
323364
});

0 commit comments

Comments
 (0)