From 3071ef7eb45b2126d26bc30475d16dc3b6f14131 Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Fri, 1 Aug 2025 11:46:21 +0200 Subject: [PATCH 1/4] fix(browser): Ensure request from `diagnoseSdkConnectivity` does not create span --- .../diagnoseSdkConnectivity/init.js | 9 +++++ .../diagnoseSdkConnectivity/subject.js | 1 + .../diagnoseSdkConnectivity/test.ts | 38 +++++++++++++++++++ packages/browser/src/diagnose-sdk.ts | 32 ++++++++-------- packages/browser/test/diagnose-sdk.test.ts | 14 +++++++ 5 files changed, 79 insertions(+), 15 deletions(-) create mode 100644 dev-packages/browser-integration-tests/suites/public-api/diagnoseSdkConnectivity/init.js create mode 100644 dev-packages/browser-integration-tests/suites/public-api/diagnoseSdkConnectivity/subject.js create mode 100644 dev-packages/browser-integration-tests/suites/public-api/diagnoseSdkConnectivity/test.ts diff --git a/dev-packages/browser-integration-tests/suites/public-api/diagnoseSdkConnectivity/init.js b/dev-packages/browser-integration-tests/suites/public-api/diagnoseSdkConnectivity/init.js new file mode 100644 index 000000000000..8c0a0cd9fca4 --- /dev/null +++ b/dev-packages/browser-integration-tests/suites/public-api/diagnoseSdkConnectivity/init.js @@ -0,0 +1,9 @@ +import * as Sentry from '@sentry/browser'; + +window.Sentry = Sentry; + +Sentry.init({ + dsn: 'https://public@dsn.ingest.sentry.io/1337', + integrations: [Sentry.browserTracingIntegration({ idleTimeout: 3000, childSpanTimeout: 3000 })], + tracesSampleRate: 1, +}); diff --git a/dev-packages/browser-integration-tests/suites/public-api/diagnoseSdkConnectivity/subject.js b/dev-packages/browser-integration-tests/suites/public-api/diagnoseSdkConnectivity/subject.js new file mode 100644 index 000000000000..c1239ac16b0e --- /dev/null +++ b/dev-packages/browser-integration-tests/suites/public-api/diagnoseSdkConnectivity/subject.js @@ -0,0 +1 @@ +Sentry.diagnoseSdkConnectivity().then(res => console.log('SDK connectivity:', res)); diff --git a/dev-packages/browser-integration-tests/suites/public-api/diagnoseSdkConnectivity/test.ts b/dev-packages/browser-integration-tests/suites/public-api/diagnoseSdkConnectivity/test.ts new file mode 100644 index 000000000000..9deac29c3746 --- /dev/null +++ b/dev-packages/browser-integration-tests/suites/public-api/diagnoseSdkConnectivity/test.ts @@ -0,0 +1,38 @@ +import { expect } from '@playwright/test'; +import { sentryTest } from '../../../utils/fixtures'; +import { envelopeRequestParser, shouldSkipTracingTest, waitForTransactionRequest } from '../../../utils/helpers'; + +sentryTest('makes a call to sentry.io to diagnose SDK connectivity', async ({ getLocalTestUrl, page }) => { + if (shouldSkipTracingTest()) { + sentryTest.skip(); + } + + const pageloadRequestPromise = waitForTransactionRequest(page, e => e.contexts?.trace?.op === 'pageload'); + + // mock sdk connectivity url to avoid making actual request to sentry.io + page.route('**/api/4509632503087104/envelope/**/*', route => { + return route.fulfill({ + status: 200, + body: '{}', + }); + }); + + let diagnoseMessage: string | undefined; + page.on('console', msg => { + if (msg.text().includes('SDK connectivity:')) { + diagnoseMessage = msg.text(); + } + }); + + const url = await getLocalTestUrl({ testDir: __dirname }); + await page.goto(url); + + const pageLoadEvent = envelopeRequestParser(await pageloadRequestPromise); + + // udnefined is expected and means the request was successful + expect(diagnoseMessage).toEqual('SDK connectivity: undefined'); + + // the request to sentry.io should not be traced, hence no http.client span should be sent. + const httpClientSpans = pageLoadEvent.spans?.filter(s => s.op === 'http.client'); + expect(httpClientSpans).toHaveLength(0); +}); diff --git a/packages/browser/src/diagnose-sdk.ts b/packages/browser/src/diagnose-sdk.ts index a8b433856f01..0ad4bef69d6c 100644 --- a/packages/browser/src/diagnose-sdk.ts +++ b/packages/browser/src/diagnose-sdk.ts @@ -1,4 +1,4 @@ -import { getClient } from '@sentry/core'; +import { getClient, suppressTracing } from '@sentry/core'; /** * A function to diagnose why the SDK might not be successfully sending data. @@ -23,20 +23,22 @@ export async function diagnoseSdkConnectivity(): Promise< } try { - // If fetch throws, there is likely an ad blocker active or there are other connective issues. - await fetch( - // We are using the - // - "sentry-sdks" org with id 447951 not to pollute any actual organizations. - // - "diagnose-sdk-connectivity" project with id 4509632503087104 - // - the public key of said org/project, which is disabled in the project settings - // => this DSN: https://c1dfb07d783ad5325c245c1fd3725390@o447951.ingest.us.sentry.io/4509632503087104 (i.e. disabled) - 'https://o447951.ingest.sentry.io/api/4509632503087104/envelope/?sentry_version=7&sentry_key=c1dfb07d783ad5325c245c1fd3725390&sentry_client=sentry.javascript.browser%2F1.33.7', - { - body: '{}', - method: 'POST', - mode: 'cors', - credentials: 'omit', - }, + await suppressTracing(() => + // If fetch throws, there is likely an ad blocker active or there are other connective issues. + fetch( + // We are using the + // - "sentry-sdks" org with id 447951 not to pollute any actual organizations. + // - "diagnose-sdk-connectivity" project with id 4509632503087104 + // - the public key of said org/project, which is disabled in the project settings + // => this DSN: https://c1dfb07d783ad5325c245c1fd3725390@o447951.ingest.us.sentry.io/4509632503087104 (i.e. disabled) + 'https://o447951.ingest.sentry.io/api/4509632503087104/envelope/?sentry_version=7&sentry_key=c1dfb07d783ad5325c245c1fd3725390&sentry_client=sentry.javascript.browser%2F1.33.7', + { + body: '{}', + method: 'POST', + mode: 'cors', + credentials: 'omit', + }, + ), ); } catch { return 'sentry-unreachable'; diff --git a/packages/browser/test/diagnose-sdk.test.ts b/packages/browser/test/diagnose-sdk.test.ts index 36584a97f63b..5bc05dc6cf56 100644 --- a/packages/browser/test/diagnose-sdk.test.ts +++ b/packages/browser/test/diagnose-sdk.test.ts @@ -162,4 +162,18 @@ describe('diagnoseSdkConnectivity', () => { credentials: 'omit', }); }); + + it('calls suppressTracing to avoid tracing the fetch call to sentry', async () => { + const suppressTracingSpy = vi.spyOn(sentryCore, 'suppressTracing'); + + const mockClient: Partial = { + getDsn: vi.fn().mockReturnValue('https://test@example.com/123'), + }; + mockGetClient.mockReturnValue(mockClient); + mockFetch.mockResolvedValue(new Response('{}', { status: 200 })); + + await diagnoseSdkConnectivity(); + + expect(suppressTracingSpy).toHaveBeenCalledTimes(1); + }); }); From e99dbc946b47497803177ee82ee8d8f9fafb73db Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Fri, 1 Aug 2025 11:50:23 +0200 Subject: [PATCH 2/4] improve test --- .../diagnoseSdkConnectivity/test.ts | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/dev-packages/browser-integration-tests/suites/public-api/diagnoseSdkConnectivity/test.ts b/dev-packages/browser-integration-tests/suites/public-api/diagnoseSdkConnectivity/test.ts index 9deac29c3746..0a113b277992 100644 --- a/dev-packages/browser-integration-tests/suites/public-api/diagnoseSdkConnectivity/test.ts +++ b/dev-packages/browser-integration-tests/suites/public-api/diagnoseSdkConnectivity/test.ts @@ -17,11 +17,12 @@ sentryTest('makes a call to sentry.io to diagnose SDK connectivity', async ({ ge }); }); - let diagnoseMessage: string | undefined; - page.on('console', msg => { - if (msg.text().includes('SDK connectivity:')) { - diagnoseMessage = msg.text(); - } + const diagnoseMessagePromise = new Promise(resolve => { + page.on('console', msg => { + if (msg.text().includes('SDK connectivity:')) { + resolve(msg.text()); + } + }); }); const url = await getLocalTestUrl({ testDir: __dirname }); @@ -30,9 +31,17 @@ sentryTest('makes a call to sentry.io to diagnose SDK connectivity', async ({ ge const pageLoadEvent = envelopeRequestParser(await pageloadRequestPromise); // udnefined is expected and means the request was successful - expect(diagnoseMessage).toEqual('SDK connectivity: undefined'); + expect(await diagnoseMessagePromise).toEqual('SDK connectivity: undefined'); // the request to sentry.io should not be traced, hence no http.client span should be sent. const httpClientSpans = pageLoadEvent.spans?.filter(s => s.op === 'http.client'); expect(httpClientSpans).toHaveLength(0); + + // no fetch breadcrumb should be sent (only breadcrumb for the console log) + expect(pageLoadEvent.breadcrumbs).toEqual([ + expect.objectContaining({ + category: 'console', + message: 'SDK connectivity: undefined', + }), + ]); }); From 1a3503ccd6e495424cfb81ec5c1a111a5b4d03e7 Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Fri, 1 Aug 2025 11:56:32 +0200 Subject: [PATCH 3/4] Update dev-packages/browser-integration-tests/suites/public-api/diagnoseSdkConnectivity/test.ts Co-authored-by: Andrei <168741329+andreiborza@users.noreply.github.com> --- .../suites/public-api/diagnoseSdkConnectivity/test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dev-packages/browser-integration-tests/suites/public-api/diagnoseSdkConnectivity/test.ts b/dev-packages/browser-integration-tests/suites/public-api/diagnoseSdkConnectivity/test.ts index 0a113b277992..de7f1988193b 100644 --- a/dev-packages/browser-integration-tests/suites/public-api/diagnoseSdkConnectivity/test.ts +++ b/dev-packages/browser-integration-tests/suites/public-api/diagnoseSdkConnectivity/test.ts @@ -30,7 +30,7 @@ sentryTest('makes a call to sentry.io to diagnose SDK connectivity', async ({ ge const pageLoadEvent = envelopeRequestParser(await pageloadRequestPromise); - // udnefined is expected and means the request was successful + // undefined is expected and means the request was successful expect(await diagnoseMessagePromise).toEqual('SDK connectivity: undefined'); // the request to sentry.io should not be traced, hence no http.client span should be sent. From b8c71881b0b1333b765a79173f3efe434c501b1e Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Fri, 1 Aug 2025 13:57:42 +0200 Subject: [PATCH 4/4] skip bundle integration tests --- .../suites/public-api/diagnoseSdkConnectivity/test.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/dev-packages/browser-integration-tests/suites/public-api/diagnoseSdkConnectivity/test.ts b/dev-packages/browser-integration-tests/suites/public-api/diagnoseSdkConnectivity/test.ts index de7f1988193b..294e60b34bfd 100644 --- a/dev-packages/browser-integration-tests/suites/public-api/diagnoseSdkConnectivity/test.ts +++ b/dev-packages/browser-integration-tests/suites/public-api/diagnoseSdkConnectivity/test.ts @@ -3,7 +3,9 @@ import { sentryTest } from '../../../utils/fixtures'; import { envelopeRequestParser, shouldSkipTracingTest, waitForTransactionRequest } from '../../../utils/helpers'; sentryTest('makes a call to sentry.io to diagnose SDK connectivity', async ({ getLocalTestUrl, page }) => { - if (shouldSkipTracingTest()) { + const bundle = process.env.PW_BUNDLE as string | undefined; + if (shouldSkipTracingTest() || !!bundle) { + // the CDN bundle doesn't export diagnoseSdkConnectivity. So skipping the test for bundles. sentryTest.skip(); }