From cec610c0b3e0e72821abdef8d80c0d23434746ec Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Tue, 25 Mar 2025 11:48:51 +0100 Subject: [PATCH 01/10] feat(browser): Add `diagnoseSdk()` function to programmatically detect possible issues --- packages/browser/src/diagnose-sdk.ts | 30 ++++++++++++++++++++++++++++ packages/browser/src/index.ts | 1 + 2 files changed, 31 insertions(+) create mode 100644 packages/browser/src/diagnose-sdk.ts diff --git a/packages/browser/src/diagnose-sdk.ts b/packages/browser/src/diagnose-sdk.ts new file mode 100644 index 000000000000..d454519a26f6 --- /dev/null +++ b/packages/browser/src/diagnose-sdk.ts @@ -0,0 +1,30 @@ +import { getCurrentScope } from '@sentry/core'; + +/** + * A function to diagnose why the SDK might not be successfully sending data. + * + * Possible return values wrapped in a Promise: + * - `"no-client-active"` - There was no active client when the function was called. This possibly means that the SDK was not initialized yet. + * - `"sentry-unreachable"` - There was no active client when the function was called. This possibly means that the SDK was not initialized yet. + */ +export async function diagnoseSdk(): Promise<'no-client-active' | 'sentry-unreachable' | void> { + const client = getCurrentScope().getClient(); + + if (!client) { + return 'no-client-active'; + } + + try { + await fetch( + 'https://o1337.ingest.sentry.io/api/1337/envelope/?sentry_version=7&sentry_key=1337&sentry_client=sentry.javascript.react%2F1.33.7', + { + body: '', + method: 'POST', + mode: 'cors', + credentials: 'omit', + }, + ); + } catch { + return 'sentry-unreachable'; + } +} diff --git a/packages/browser/src/index.ts b/packages/browser/src/index.ts index 47bef0cd6dae..51ac405d8558 100644 --- a/packages/browser/src/index.ts +++ b/packages/browser/src/index.ts @@ -71,3 +71,4 @@ export { launchDarklyIntegration, buildLaunchDarklyFlagUsedHandler } from './int export { openFeatureIntegration, OpenFeatureIntegrationHook } from './integrations/featureFlags/openfeature'; export { unleashIntegration } from './integrations/featureFlags/unleash'; export { statsigIntegration } from './integrations/featureFlags/statsig'; +export { diagnoseSdk } from './diagnose-sdk'; From 4f755add6e436df5b7ed101940ff8dd09fc57ff6 Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Tue, 25 Mar 2025 11:51:49 +0100 Subject: [PATCH 02/10] moar comment --- packages/browser/src/diagnose-sdk.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/browser/src/diagnose-sdk.ts b/packages/browser/src/diagnose-sdk.ts index d454519a26f6..5c1fbeddbba6 100644 --- a/packages/browser/src/diagnose-sdk.ts +++ b/packages/browser/src/diagnose-sdk.ts @@ -6,6 +6,8 @@ import { getCurrentScope } from '@sentry/core'; * Possible return values wrapped in a Promise: * - `"no-client-active"` - There was no active client when the function was called. This possibly means that the SDK was not initialized yet. * - `"sentry-unreachable"` - There was no active client when the function was called. This possibly means that the SDK was not initialized yet. + * + * If the function doesn't detect an issue it resolves to `undefined`. */ export async function diagnoseSdk(): Promise<'no-client-active' | 'sentry-unreachable' | void> { const client = getCurrentScope().getClient(); From b1703a896b6160af7746369e94696637df967deb Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Tue, 25 Mar 2025 11:54:01 +0100 Subject: [PATCH 03/10] more comment --- packages/browser/src/diagnose-sdk.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/browser/src/diagnose-sdk.ts b/packages/browser/src/diagnose-sdk.ts index 5c1fbeddbba6..d81919f6d6a6 100644 --- a/packages/browser/src/diagnose-sdk.ts +++ b/packages/browser/src/diagnose-sdk.ts @@ -17,7 +17,9 @@ export async function diagnoseSdk(): Promise<'no-client-active' | 'sentry-unreac } try { + // If fetch throws, there is likely an ad blocker active or there are other connective issues. await fetch( + // We want this to be as close as possible to an actual ingest URL so that ad blockers will actually block the request 'https://o1337.ingest.sentry.io/api/1337/envelope/?sentry_version=7&sentry_key=1337&sentry_client=sentry.javascript.react%2F1.33.7', { body: '', From 77b07ca81c524c6be8e39cc0255b483daa18670a Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Tue, 25 Mar 2025 11:54:58 +0100 Subject: [PATCH 04/10] comment --- packages/browser/src/diagnose-sdk.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/browser/src/diagnose-sdk.ts b/packages/browser/src/diagnose-sdk.ts index d81919f6d6a6..8e9211e276c2 100644 --- a/packages/browser/src/diagnose-sdk.ts +++ b/packages/browser/src/diagnose-sdk.ts @@ -5,7 +5,7 @@ import { getCurrentScope } from '@sentry/core'; * * Possible return values wrapped in a Promise: * - `"no-client-active"` - There was no active client when the function was called. This possibly means that the SDK was not initialized yet. - * - `"sentry-unreachable"` - There was no active client when the function was called. This possibly means that the SDK was not initialized yet. + * - `"sentry-unreachable"` - The Sentry SaaS servers were not reachable. This likely means that there is an ad blocker active on the page or that there are other connection issues. * * If the function doesn't detect an issue it resolves to `undefined`. */ From 30da2fac92eb3fb96224f26472df55118f74b064 Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Tue, 25 Mar 2025 11:55:34 +0100 Subject: [PATCH 05/10] simplify --- packages/browser/src/diagnose-sdk.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/packages/browser/src/diagnose-sdk.ts b/packages/browser/src/diagnose-sdk.ts index 8e9211e276c2..221b10606df6 100644 --- a/packages/browser/src/diagnose-sdk.ts +++ b/packages/browser/src/diagnose-sdk.ts @@ -1,4 +1,4 @@ -import { getCurrentScope } from '@sentry/core'; +import { getClient } from '@sentry/core'; /** * A function to diagnose why the SDK might not be successfully sending data. @@ -10,9 +10,7 @@ import { getCurrentScope } from '@sentry/core'; * If the function doesn't detect an issue it resolves to `undefined`. */ export async function diagnoseSdk(): Promise<'no-client-active' | 'sentry-unreachable' | void> { - const client = getCurrentScope().getClient(); - - if (!client) { + if (!getClient()) { return 'no-client-active'; } From 5ba8e38ded49c7380de6248369b065db74fdd461 Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Tue, 25 Mar 2025 12:02:58 +0100 Subject: [PATCH 06/10] check for dsn --- packages/browser/src/diagnose-sdk.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/packages/browser/src/diagnose-sdk.ts b/packages/browser/src/diagnose-sdk.ts index 221b10606df6..e84afbb3bc96 100644 --- a/packages/browser/src/diagnose-sdk.ts +++ b/packages/browser/src/diagnose-sdk.ts @@ -9,11 +9,19 @@ import { getClient } from '@sentry/core'; * * If the function doesn't detect an issue it resolves to `undefined`. */ -export async function diagnoseSdk(): Promise<'no-client-active' | 'sentry-unreachable' | void> { - if (!getClient()) { +export async function diagnoseSdkConnectivity(): Promise< + 'no-client-active' | 'sentry-unreachable' | 'no-dsn-configured' | void +> { + const client = getClient(); + + if (!client) { return 'no-client-active'; } + if (!client.getDsn()) { + return 'no-dsn-configured'; + } + try { // If fetch throws, there is likely an ad blocker active or there are other connective issues. await fetch( From 371e22849a0277a78538282d6334c8b466878070 Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Tue, 25 Mar 2025 12:12:18 +0100 Subject: [PATCH 07/10] export --- packages/browser/src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/browser/src/index.ts b/packages/browser/src/index.ts index 51ac405d8558..9103bbab99b5 100644 --- a/packages/browser/src/index.ts +++ b/packages/browser/src/index.ts @@ -71,4 +71,4 @@ export { launchDarklyIntegration, buildLaunchDarklyFlagUsedHandler } from './int export { openFeatureIntegration, OpenFeatureIntegrationHook } from './integrations/featureFlags/openfeature'; export { unleashIntegration } from './integrations/featureFlags/unleash'; export { statsigIntegration } from './integrations/featureFlags/statsig'; -export { diagnoseSdk } from './diagnose-sdk'; +export { diagnoseSdkConnectivity } from './diagnose-sdk'; From efbc46a6d41fc4ed7e633b846ad5a43600e16f2a Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Tue, 25 Mar 2025 12:15:58 +0100 Subject: [PATCH 08/10] different org --- packages/browser/src/diagnose-sdk.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/browser/src/diagnose-sdk.ts b/packages/browser/src/diagnose-sdk.ts index e84afbb3bc96..ded28d01baae 100644 --- a/packages/browser/src/diagnose-sdk.ts +++ b/packages/browser/src/diagnose-sdk.ts @@ -26,7 +26,8 @@ export async function diagnoseSdkConnectivity(): Promise< // If fetch throws, there is likely an ad blocker active or there are other connective issues. await fetch( // We want this to be as close as possible to an actual ingest URL so that ad blockers will actually block the request - 'https://o1337.ingest.sentry.io/api/1337/envelope/?sentry_version=7&sentry_key=1337&sentry_client=sentry.javascript.react%2F1.33.7', + // We are using the "sentry-sdks" org with id 447951 not to pollute any actual organizations. + 'https://o447951.ingest.sentry.io/api/1337/envelope/?sentry_version=7&sentry_key=1337&sentry_client=sentry.javascript.react%2F1.33.7', { body: '', method: 'POST', From fecaf0ebc961381f1ffedc1f3a6116c9bf378d3d Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Tue, 25 Mar 2025 12:25:05 +0100 Subject: [PATCH 09/10] empty object --- packages/browser/src/diagnose-sdk.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/browser/src/diagnose-sdk.ts b/packages/browser/src/diagnose-sdk.ts index ded28d01baae..1a38685c3e22 100644 --- a/packages/browser/src/diagnose-sdk.ts +++ b/packages/browser/src/diagnose-sdk.ts @@ -29,7 +29,7 @@ export async function diagnoseSdkConnectivity(): Promise< // We are using the "sentry-sdks" org with id 447951 not to pollute any actual organizations. 'https://o447951.ingest.sentry.io/api/1337/envelope/?sentry_version=7&sentry_key=1337&sentry_client=sentry.javascript.react%2F1.33.7', { - body: '', + body: '{}', method: 'POST', mode: 'cors', credentials: 'omit', From 7881e0663ff7bf2cd7a28540045a9cecce121c46 Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Tue, 25 Mar 2025 12:26:52 +0100 Subject: [PATCH 10/10] different client --- packages/browser/src/diagnose-sdk.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/browser/src/diagnose-sdk.ts b/packages/browser/src/diagnose-sdk.ts index 1a38685c3e22..e2399b8103b0 100644 --- a/packages/browser/src/diagnose-sdk.ts +++ b/packages/browser/src/diagnose-sdk.ts @@ -27,7 +27,7 @@ export async function diagnoseSdkConnectivity(): Promise< await fetch( // We want this to be as close as possible to an actual ingest URL so that ad blockers will actually block the request // We are using the "sentry-sdks" org with id 447951 not to pollute any actual organizations. - 'https://o447951.ingest.sentry.io/api/1337/envelope/?sentry_version=7&sentry_key=1337&sentry_client=sentry.javascript.react%2F1.33.7', + 'https://o447951.ingest.sentry.io/api/1337/envelope/?sentry_version=7&sentry_key=1337&sentry_client=sentry.javascript.browser%2F1.33.7', { body: '{}', method: 'POST',