Skip to content

fix(tracing): Account for case where startTransaction returns undefined #7566

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
Mar 22, 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
11 changes: 7 additions & 4 deletions packages/core/src/tracing/trace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@ import type { Span } from './span';
/**
* Wraps a function with a transaction/span and finishes the span after the function is done.
*
* Note that if you have not enabled tracing extensions via `addTracingExtensions`, this function
* will not generate spans, and the `span` returned from the callback may be undefined.
*
* This function is meant to be used internally and may break at any time. Use at your own risk.
*
* @internal
* @private
*/
export function trace<T>(
context: TransactionContext,
callback: (span: Span) => T,
callback: (span?: Span) => T,
// eslint-disable-next-line @typescript-eslint/no-empty-function
onError: (error: unknown) => void = () => {},
): T {
Expand All @@ -32,15 +35,15 @@ export function trace<T>(
scope.setSpan(activeSpan);

function finishAndSetSpan(): void {
activeSpan.finish();
activeSpan && activeSpan.finish();
hub.getScope().setSpan(parentSpan);
}

let maybePromiseResult: T;
try {
maybePromiseResult = callback(activeSpan);
} catch (e) {
activeSpan.setStatus('internal_error');
activeSpan && activeSpan.setStatus('internal_error');
onError(e);
finishAndSetSpan();
throw e;
Expand All @@ -52,7 +55,7 @@ export function trace<T>(
finishAndSetSpan();
},
e => {
activeSpan.setStatus('internal_error');
activeSpan && activeSpan.setStatus('internal_error');
onError(e);
finishAndSetSpan();
},
Expand Down
23 changes: 21 additions & 2 deletions packages/core/test/lib/tracing/trace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,21 @@ describe('trace', () => {
}
});

it('should return the same value as the callback if transactions are undefined', async () => {
// @ts-ignore we are force overriding the transaction return to be undefined
// The `startTransaction` types are actually wrong - it can return undefined
// if tracingExtensions are not enabled
jest.spyOn(hub, 'startTransaction').mockReturnValue(undefined);
try {
const result = await trace({ name: 'GET users/[id]' }, () => {
return callback();
});
expect(result).toEqual(expected);
} catch (e) {
expect(e).toEqual(expected);
}
});

it('creates a transaction', async () => {
let ref: any = undefined;
client.on('finishTransaction', transaction => {
Expand Down Expand Up @@ -99,7 +114,9 @@ describe('trace', () => {
});
try {
await trace({ name: 'GET users/[id]' }, span => {
span.op = 'http.server';
if (span) {
span.op = 'http.server';
}
return callback();
});
} catch (e) {
Expand Down Expand Up @@ -138,7 +155,9 @@ describe('trace', () => {
try {
await trace({ name: 'GET users/[id]', parentSampled: true }, () => {
return trace({ name: 'SELECT * from users' }, childSpan => {
childSpan.op = 'db.query';
if (childSpan) {
childSpan.op = 'db.query';
}
return callback();
});
});
Expand Down