From bfa0c7aaafcfb7a2cc202c1a2a4ce5cc6119e090 Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Mon, 19 Jun 2023 13:53:25 +0200 Subject: [PATCH 1/2] fix(core): Only start spans in `trace` if tracing is enabled --- packages/core/src/tracing/trace.ts | 16 ++++++++--- packages/core/test/lib/tracing/trace.test.ts | 28 ++++++++++++++++++++ 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/packages/core/src/tracing/trace.ts b/packages/core/src/tracing/trace.ts index 8e7844d23988..2864377bfc04 100644 --- a/packages/core/src/tracing/trace.ts +++ b/packages/core/src/tracing/trace.ts @@ -2,13 +2,15 @@ import type { TransactionContext } from '@sentry/types'; import { isThenable } from '@sentry/utils'; import { getCurrentHub } from '../hub'; +import { hasTracingEnabled } from '../utils/hasTracingEnabled'; 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. + * Note that if you have not enabled tracing extensions via `addTracingExtensions` + * or you didn't set `tracesSampleRate`, this function will not generate spans + * and the `span` returned from the callback will be undefined. * * This function is meant to be used internally and may break at any time. Use at your own risk. * @@ -31,7 +33,15 @@ export function trace( const scope = hub.getScope(); const parentSpan = scope.getSpan(); - const activeSpan = parentSpan ? parentSpan.startChild(ctx) : hub.startTransaction(ctx); + + function getActiveSpan(): Span | undefined { + if (!hasTracingEnabled()) { + return undefined; + } + return parentSpan ? parentSpan.startChild(ctx) : hub.startTransaction(ctx); + } + + const activeSpan = getActiveSpan(); scope.setSpan(activeSpan); function finishAndSetSpan(): void { diff --git a/packages/core/test/lib/tracing/trace.test.ts b/packages/core/test/lib/tracing/trace.test.ts index 064c41dc123a..23039d7c1b4e 100644 --- a/packages/core/test/lib/tracing/trace.test.ts +++ b/packages/core/test/lib/tracing/trace.test.ts @@ -185,5 +185,33 @@ describe('trace', () => { } expect(onError).toHaveBeenCalledTimes(isError ? 1 : 0); }); + + it("doesn't create spans but calles onError if tracing is disabled", async () => { + const options = getDefaultTestClientOptions({ + /* we don't set tracesSampleRate or tracesSampler */ + }); + client = new TestClient(options); + hub = new Hub(client); + makeMain(hub); + + const startTxnSpy = jest.spyOn(hub, 'startTransaction'); + + const onError = jest.fn(); + try { + await trace( + { name: 'GET users/[id]' }, + () => { + return callback(); + }, + onError, + ); + } catch (e) { + expect(onError).toHaveBeenCalledTimes(1); + expect(onError).toHaveBeenCalledWith(e); + } + expect(onError).toHaveBeenCalledTimes(isError ? 1 : 0); + + expect(startTxnSpy).not.toHaveBeenCalled(); + }); }); }); From 12bcf609d098fbebb77b28d1aa268664b145c17c Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Tue, 20 Jun 2023 09:52:59 +0200 Subject: [PATCH 2/2] Update packages/core/test/lib/tracing/trace.test.ts --- packages/core/test/lib/tracing/trace.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/test/lib/tracing/trace.test.ts b/packages/core/test/lib/tracing/trace.test.ts index 23039d7c1b4e..bff1c425c2a0 100644 --- a/packages/core/test/lib/tracing/trace.test.ts +++ b/packages/core/test/lib/tracing/trace.test.ts @@ -186,7 +186,7 @@ describe('trace', () => { expect(onError).toHaveBeenCalledTimes(isError ? 1 : 0); }); - it("doesn't create spans but calles onError if tracing is disabled", async () => { + it("doesn't create spans but calls onError if tracing is disabled", async () => { const options = getDefaultTestClientOptions({ /* we don't set tracesSampleRate or tracesSampler */ });