Skip to content

Commit cc30c14

Browse files
authored
fix(core): Ensure standalone spans are not sent if SDK is disabled (#14088)
Change the sending logic for standalone spans to to use the client's `sendEnvelope` method which we generally use to send envelopes (sessions, client reports, checkins, metrics (RIP), and also events). This has a minor implication: We will now also emit a `beforeEnvelope` client hook event for sending standalone spans. fixes #14082
1 parent e68865a commit cc30c14

File tree

5 files changed

+47
-7
lines changed

5 files changed

+47
-7
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import * as Sentry from '@sentry/browser';
2+
3+
window.Sentry = Sentry;
4+
5+
window.fetchCallCount = 0;
6+
window.spanEnded = false;
7+
8+
const originalWindowFetch = window.fetch;
9+
window.fetch = (...args) => {
10+
window.fetchCallCount++;
11+
return originalWindowFetch(...args);
12+
};
13+
14+
Sentry.init({
15+
dsn: 'https://[email protected]/1337',
16+
tracesSampleRate: 1.0,
17+
enabled: false,
18+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Sentry.startSpan({ name: 'standalone_segment_span', experimental: { standalone: true } }, () => {});
2+
3+
window.spanEnded = true;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { expect } from '@playwright/test';
2+
3+
import { sentryTest } from '../../../../utils/fixtures';
4+
import { shouldSkipTracingTest } from '../../../../utils/helpers';
5+
6+
sentryTest("doesn't send a standalone span envelope if SDK is disabled", async ({ getLocalTestPath, page }) => {
7+
if (shouldSkipTracingTest()) {
8+
sentryTest.skip();
9+
}
10+
11+
const url = await getLocalTestPath({ testDir: __dirname });
12+
await page.goto(url);
13+
14+
// @ts-expect-error this exists in the test init/subject
15+
await page.waitForFunction(() => !!window.spanEnded);
16+
await page.waitForTimeout(2000);
17+
18+
// @ts-expect-error this exists in the test init
19+
const fetchCallCount = await page.evaluate(() => window.fetchCallCount);
20+
// We expect no fetch calls because the SDK is disabled
21+
expect(fetchCallCount).toBe(0);
22+
});

packages/core/src/baseclient.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
565565

566566
if (this._isEnabled() && this._transport) {
567567
return this._transport.send(envelope).then(null, reason => {
568-
DEBUG_BUILD && logger.error('Error while sending event:', reason);
568+
DEBUG_BUILD && logger.error('Error while sending envelope:', reason);
569569
return reason;
570570
});
571571
}

packages/core/src/tracing/sentrySpan.ts

+3-6
Original file line numberDiff line numberDiff line change
@@ -429,10 +429,7 @@ function sendSpanEnvelope(envelope: SpanEnvelope): void {
429429
return;
430430
}
431431

432-
const transport = client.getTransport();
433-
if (transport) {
434-
transport.send(envelope).then(null, reason => {
435-
DEBUG_BUILD && logger.error('Error while sending span:', reason);
436-
});
437-
}
432+
// sendEnvelope should not throw
433+
// eslint-disable-next-line @typescript-eslint/no-floating-promises
434+
client.sendEnvelope(envelope);
438435
}

0 commit comments

Comments
 (0)