Skip to content

feat(core): Add continueTrace method #9164

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 4, 2023
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
1 change: 1 addition & 0 deletions packages/browser/src/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export {
startSpan,
startInactiveSpan,
startSpanManual,
continueTrace,
SDK_VERSION,
setContext,
setExtra,
Expand Down
1 change: 1 addition & 0 deletions packages/bun/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export {
startSpan,
startInactiveSpan,
startSpanManual,
continueTrace,
} from '@sentry/core';
export type { SpanStatusType } from '@sentry/core';
export { autoDiscoverNodePerformanceMonitoringIntegrations } from '@sentry/node';
Expand Down
12 changes: 10 additions & 2 deletions packages/core/src/tracing/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,16 @@ export { extractTraceparentData, getActiveTransaction } from './utils';
// eslint-disable-next-line deprecation/deprecation
export { SpanStatus } from './spanstatus';
export type { SpanStatusType } from './span';
// eslint-disable-next-line deprecation/deprecation
export { trace, getActiveSpan, startSpan, startInactiveSpan, startActiveSpan, startSpanManual } from './trace';
export {
trace,
getActiveSpan,
startSpan,
startInactiveSpan,
// eslint-disable-next-line deprecation/deprecation
startActiveSpan,
startSpanManual,
continueTrace,
} from './trace';
export { getDynamicSamplingContextFromClient } from './dynamicSamplingContext';
export { setMeasurement } from './measurement';
export { sampleTransaction } from './sampling';
44 changes: 43 additions & 1 deletion packages/core/src/tracing/trace.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { TransactionContext } from '@sentry/types';
import { isThenable } from '@sentry/utils';
import { dropUndefinedKeys, isThenable, logger, tracingContextFromHeaders } from '@sentry/utils';

import type { Hub } from '../hub';
import { getCurrentHub } from '../hub';
Expand Down Expand Up @@ -203,6 +203,48 @@ export function getActiveSpan(): Span | undefined {
return getCurrentHub().getScope().getSpan();
}

/**
* Continue a trace from `sentry-trace` and `baggage` values.
* These values can be obtained from incoming request headers,
* or in the browser from `<meta name="sentry-trace">` and `<meta name="baggage">` HTML tags.
*
* It also takes an optional `request` option, which if provided will also be added to the scope & transaction metadata.
* The callback receives a transactionContext that may be used for `startTransaction` or `startSpan`.
*/
export function continueTrace<V>(
{
sentryTrace,
baggage,
}: {
sentryTrace: Parameters<typeof tracingContextFromHeaders>[0];
baggage: Parameters<typeof tracingContextFromHeaders>[1];
},
callback: (transactionContext: Partial<TransactionContext>) => V,
): V {
const hub = getCurrentHub();
const currentScope = hub.getScope();

const { traceparentData, dynamicSamplingContext, propagationContext } = tracingContextFromHeaders(
sentryTrace,
baggage,
);

currentScope.setPropagationContext(propagationContext);

if (__DEBUG_BUILD__ && traceparentData) {
logger.log(`[Tracing] Continuing trace ${traceparentData.traceId}.`);
}

const transactionContext: Partial<TransactionContext> = {
...traceparentData,
metadata: dropUndefinedKeys({
dynamicSamplingContext: traceparentData && !dynamicSamplingContext ? {} : dynamicSamplingContext,
}),
};

return callback(transactionContext);
}

function createChildSpanOrTransaction(
hub: Hub,
parentSpan: Span | undefined,
Expand Down
153 changes: 152 additions & 1 deletion packages/core/test/lib/tracing/trace.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { addTracingExtensions, Hub, makeMain } from '../../../src';
import { startSpan } from '../../../src/tracing';
import { continueTrace, startSpan } from '../../../src/tracing';
import { getDefaultTestClientOptions, TestClient } from '../../mocks/client';

beforeAll(() => {
Expand Down Expand Up @@ -170,3 +170,154 @@ describe('startSpan', () => {
});
});
});

describe('continueTrace', () => {
beforeEach(() => {
const options = getDefaultTestClientOptions({ tracesSampleRate: 0.0 });
client = new TestClient(options);
hub = new Hub(client);
makeMain(hub);
});

it('works without trace & baggage data', () => {
const expectedContext = {
metadata: {},
};

const result = continueTrace({ sentryTrace: undefined, baggage: undefined }, ctx => {
expect(ctx).toEqual(expectedContext);
return ctx;
});

expect(result).toEqual(expectedContext);

const scope = hub.getScope();

expect(scope.getPropagationContext()).toEqual({
sampled: undefined,
spanId: expect.any(String),
traceId: expect.any(String),
});

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

it('works with trace data', () => {
const expectedContext = {
metadata: {
dynamicSamplingContext: {},
},
parentSampled: false,
parentSpanId: '1121201211212012',
traceId: '12312012123120121231201212312012',
};

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

expect(result).toEqual(expectedContext);

const scope = hub.getScope();

expect(scope.getPropagationContext()).toEqual({
sampled: false,
parentSpanId: '1121201211212012',
spanId: expect.any(String),
traceId: '12312012123120121231201212312012',
});

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

it('works with trace & baggage data', () => {
const expectedContext = {
metadata: {
dynamicSamplingContext: {
environment: 'production',
version: '1.0',
},
},
parentSampled: true,
parentSpanId: '1121201211212012',
traceId: '12312012123120121231201212312012',
};

const result = continueTrace(
{
sentryTrace: '12312012123120121231201212312012-1121201211212012-1',
baggage: 'sentry-version=1.0,sentry-environment=production',
},
ctx => {
expect(ctx).toEqual(expectedContext);
return ctx;
},
);

expect(result).toEqual(expectedContext);

const scope = hub.getScope();

expect(scope.getPropagationContext()).toEqual({
dsc: {
environment: 'production',
version: '1.0',
},
sampled: true,
parentSpanId: '1121201211212012',
spanId: expect.any(String),
traceId: '12312012123120121231201212312012',
});

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

it('works with trace & 3rd party baggage data', () => {
const expectedContext = {
metadata: {
dynamicSamplingContext: {
environment: 'production',
version: '1.0',
},
},
parentSampled: true,
parentSpanId: '1121201211212012',
traceId: '12312012123120121231201212312012',
};

const result = continueTrace(
{
sentryTrace: '12312012123120121231201212312012-1121201211212012-1',
baggage: 'sentry-version=1.0,sentry-environment=production,dogs=great,cats=boring',
},
ctx => {
expect(ctx).toEqual(expectedContext);
return ctx;
},
);

expect(result).toEqual(expectedContext);

const scope = hub.getScope();

expect(scope.getPropagationContext()).toEqual({
dsc: {
environment: 'production',
version: '1.0',
},
sampled: true,
parentSpanId: '1121201211212012',
spanId: expect.any(String),
traceId: '12312012123120121231201212312012',
});

expect(scope['_sdkProcessingMetadata']).toEqual({});
});
});
1 change: 1 addition & 0 deletions packages/node/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export {
startActiveSpan,
startInactiveSpan,
startSpanManual,
continueTrace,
} from '@sentry/core';
export type { SpanStatusType } from '@sentry/core';
export { autoDiscoverNodePerformanceMonitoringIntegrations } from './tracing';
Expand Down
1 change: 1 addition & 0 deletions packages/serverless/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,5 @@ export {
startActiveSpan,
startInactiveSpan,
startSpanManual,
continueTrace,
} from '@sentry/node';
1 change: 1 addition & 0 deletions packages/sveltekit/src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export {
startActiveSpan,
startInactiveSpan,
startSpanManual,
continueTrace,
} from '@sentry/node';

// We can still leave this for the carrier init and type exports
Expand Down
1 change: 1 addition & 0 deletions packages/vercel-edge/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export {
startSpan,
startInactiveSpan,
startSpanManual,
continueTrace,
} from '@sentry/core';
export type { SpanStatusType } from '@sentry/core';

Expand Down