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
25 changes: 23 additions & 2 deletions packages/core/src/tracing/trace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,23 @@ export function getActiveSpan(): Span | undefined {
return getCurrentHub().getScope().getSpan();
}

export function continueTrace({
sentryTrace,
baggage,
}: {
sentryTrace: Parameters<typeof tracingContextFromHeaders>[0];
baggage: Parameters<typeof tracingContextFromHeaders>[1];
}): Partial<TransactionContext>;
export function continueTrace<V>(
{
sentryTrace,
baggage,
}: {
sentryTrace: Parameters<typeof tracingContextFromHeaders>[0];
baggage: Parameters<typeof tracingContextFromHeaders>[1];
},
callback: (transactionContext: Partial<TransactionContext>) => V,
): V;
/**
* Continue a trace from `sentry-trace` and `baggage` values.
* These values can be obtained from incoming request headers,
Expand All @@ -219,8 +236,8 @@ export function continueTrace<V>(
sentryTrace: Parameters<typeof tracingContextFromHeaders>[0];
baggage: Parameters<typeof tracingContextFromHeaders>[1];
},
callback: (transactionContext: Partial<TransactionContext>) => V,
): V {
callback?: (transactionContext: Partial<TransactionContext>) => V,
): V | Partial<TransactionContext> {
const hub = getCurrentHub();
const currentScope = hub.getScope();

Expand All @@ -242,6 +259,10 @@ export function continueTrace<V>(
}),
};

if (!callback) {
return transactionContext;
}

return callback(transactionContext);
}

Expand Down
41 changes: 41 additions & 0 deletions packages/core/test/lib/tracing/trace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,4 +320,45 @@ describe('continueTrace', () => {

expect(scope['_sdkProcessingMetadata']).toEqual({});
});

it('returns response of callback', () => {
const expectedContext = {
metadata: {
dynamicSamplingContext: {},
},
parentSampled: false,
parentSpanId: '1121201211212012',
traceId: '12312012123120121231201212312012',
};

const result = continueTrace(
{
sentryTrace: '12312012123120121231201212312012-1121201211212012-0',
baggage: undefined,
},
ctx => {
return { ctx };
},
);

expect(result).toEqual({ ctx: expectedContext });
});

it('works without a callback', () => {
const expectedContext = {
metadata: {
dynamicSamplingContext: {},
},
parentSampled: false,
parentSpanId: '1121201211212012',
traceId: '12312012123120121231201212312012',
};

const ctx = continueTrace({
sentryTrace: '12312012123120121231201212312012-1121201211212012-0',
baggage: undefined,
});

expect(ctx).toEqual(expectedContext);
});
});