Skip to content

feat: Allow passing null to withActiveSpan #10717

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
Feb 19, 2024
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
10 changes: 6 additions & 4 deletions packages/core/src/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,16 +127,18 @@ export function setUser(user: User | null): ReturnType<Hub['setUser']> {
}

/**
* Forks the current scope and sets the provided span as active span in the context of the provided callback.
* Forks the current scope and sets the provided span as active span in the context of the provided callback. Can be
* passed `null` to start an entirely new span tree.
*
* @param span Spans started in the context of the provided callback will be children of this span.
* @param span Spans started in the context of the provided callback will be children of this span. If `null` is passed,
* spans started within the callback will not be attached to a parent span.
* @param callback Execution context in which the provided span will be active. Is passed the newly forked scope.
* @returns the value returned from the provided callback function.
*/
export function withActiveSpan<T>(span: Span, callback: (scope: ScopeInterface) => T): T {
export function withActiveSpan<T>(span: Span | null, callback: (scope: ScopeInterface) => T): T {
return withScope(scope => {
// eslint-disable-next-line deprecation/deprecation
scope.setSpan(span);
scope.setSpan(span || undefined);
return callback(scope);
});
}
Expand Down
9 changes: 9 additions & 0 deletions packages/core/test/lib/scope.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -582,4 +582,13 @@ describe('withActiveSpan()', () => {
});
});
});

it('when `null` is passed, no span should be active within the callback', () => {
expect.assertions(1);
startSpan({ name: 'parent-span' }, () => {
withActiveSpan(null, () => {
expect(getActiveSpan()).toBeUndefined();
});
});
});
});
42 changes: 42 additions & 0 deletions packages/node-experimental/test/sdk/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,46 @@ describe('withActiveSpan()', () => {
expect.anything(),
);
});

it('when `null` is passed, no span should be active within the callback', () => {
expect.assertions(1);
startSpan({ name: 'parent-span' }, () => {
withActiveSpan(null, () => {
expect(getActiveSpan()).toBeUndefined();
});
});
});

it('when `null` is passed, should start a new trace for new spans', async () => {
const beforeSendTransaction = jest.fn(() => null);
mockSdkInit({ enableTracing: true, beforeSendTransaction });
const client = getClient();

startSpan({ name: 'parent-span' }, () => {
withActiveSpan(null, () => {
startSpan({ name: 'child-span' }, () => {});
});
});

await client.flush();

expect(beforeSendTransaction).toHaveBeenCalledTimes(2);

// The child span should be a child of the inactive span
expect(beforeSendTransaction).toHaveBeenCalledWith(
expect.objectContaining({
transaction: 'parent-span',
spans: expect.not.arrayContaining([expect.objectContaining({ description: 'child-span' })]),
}),
expect.anything(),
);

// The floating span should be a separate transaction
expect(beforeSendTransaction).toHaveBeenCalledWith(
expect.objectContaining({
transaction: 'child-span',
}),
expect.anything(),
);
});
});
10 changes: 6 additions & 4 deletions packages/opentelemetry/src/trace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,16 @@ export function startInactiveSpan(spanContext: OpenTelemetrySpanContext): Span {
}

/**
* Forks the current scope and sets the provided span as active span in the context of the provided callback.
* Forks the current scope and sets the provided span as active span in the context of the provided callback. Can be
* passed `null` to start an entirely new span tree.
*
* @param span Spans started in the context of the provided callback will be children of this span.
* @param span Spans started in the context of the provided callback will be children of this span. If `null` is passed,
* spans started within the callback will not be attached to a parent span.
* @param callback Execution context in which the provided span will be active. Is passed the newly forked scope.
* @returns the value returned from the provided callback function.
*/
export function withActiveSpan<T>(span: Span, callback: (scope: Scope) => T): T {
const newContextWithActiveSpan = trace.setSpan(context.active(), span);
export function withActiveSpan<T>(span: Span | null, callback: (scope: Scope) => T): T {
const newContextWithActiveSpan = span ? trace.setSpan(context.active(), span) : trace.deleteSpan(context.active());
return context.with(newContextWithActiveSpan, () => callback(getCurrentScope()));
}

Expand Down