diff --git a/MIGRATION.md b/MIGRATION.md index 3ec81623c175..f84558f6f580 100644 --- a/MIGRATION.md +++ b/MIGRATION.md @@ -10,6 +10,12 @@ npx @sentry/migr8@latest This will let you select which updates to run, and automatically update your code. Make sure to still review all code changes! +## Deprecate using `getClient()` to check if the SDK was initialized + +In v8, `getClient()` will stop returning `undefined` if `Sentry.init()` was not called. For cases where this may be used +to check if Sentry was actually initialized, using `getClient()` will thus not work anymore. Instead, you should use the +new `Sentry.isInitialized()` utility to check this. + ## Deprecate `getCurrentHub()` In v8, you will no longer have a Hub, only Scopes as a concept. This also means that `getCurrentHub()` will eventually diff --git a/packages/astro/src/index.server.ts b/packages/astro/src/index.server.ts index 3074a061a2d8..2197c47381da 100644 --- a/packages/astro/src/index.server.ts +++ b/packages/astro/src/index.server.ts @@ -28,6 +28,7 @@ export { // eslint-disable-next-line deprecation/deprecation getCurrentHub, getClient, + isInitialized, getCurrentScope, getGlobalScope, getIsolationScope, diff --git a/packages/browser/src/exports.ts b/packages/browser/src/exports.ts index 50a282cd1406..5d012bdea2b7 100644 --- a/packages/browser/src/exports.ts +++ b/packages/browser/src/exports.ts @@ -40,6 +40,7 @@ export { // eslint-disable-next-line deprecation/deprecation getCurrentHub, getClient, + isInitialized, getCurrentScope, Hub, // eslint-disable-next-line deprecation/deprecation diff --git a/packages/bun/src/index.ts b/packages/bun/src/index.ts index 5d5abce58f48..393e534e12ee 100644 --- a/packages/bun/src/index.ts +++ b/packages/bun/src/index.ts @@ -45,6 +45,7 @@ export { // eslint-disable-next-line deprecation/deprecation getCurrentHub, getClient, + isInitialized, getCurrentScope, getGlobalScope, getIsolationScope, diff --git a/packages/core/src/exports.ts b/packages/core/src/exports.ts index fa011fc672ac..e1fc9a6102ac 100644 --- a/packages/core/src/exports.ts +++ b/packages/core/src/exports.ts @@ -394,6 +394,13 @@ export function getClient(): C | undefined { return getCurrentHub().getClient(); } +/** + * Returns true if Sentry has been properly initialized. + */ +export function isInitialized(): boolean { + return !!getClient(); +} + /** * Get the currently active scope. */ diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index f2f33a884123..b9b4b045497e 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -31,6 +31,7 @@ export { withScope, withIsolationScope, getClient, + isInitialized, getCurrentScope, startSession, endSession, diff --git a/packages/core/test/lib/exports.test.ts b/packages/core/test/lib/exports.test.ts index f0975065f12e..cb4c9fbdd16d 100644 --- a/packages/core/test/lib/exports.test.ts +++ b/packages/core/test/lib/exports.test.ts @@ -1,3 +1,4 @@ +import { GLOBAL_OBJ } from '@sentry/utils'; import { Hub, Scope, @@ -16,6 +17,7 @@ import { withIsolationScope, withScope, } from '../../src'; +import { isInitialized } from '../../src/exports'; import { TestClient, getDefaultTestClientOptions } from '../mocks/client'; function getTestClient(): TestClient { @@ -315,3 +317,19 @@ describe('session APIs', () => { }); }); }); + +describe('isInitialized', () => { + it('returns false if no client is setup', () => { + GLOBAL_OBJ.__SENTRY__.hub = undefined; + expect(isInitialized()).toBe(false); + }); + + it('returns true if client is setup', () => { + const client = getTestClient(); + const hub = new Hub(client); + // eslint-disable-next-line deprecation/deprecation + makeMain(hub); + + expect(isInitialized()).toBe(true); + }); +}); diff --git a/packages/deno/src/index.ts b/packages/deno/src/index.ts index d4759e71ce78..7adfcae9a894 100644 --- a/packages/deno/src/index.ts +++ b/packages/deno/src/index.ts @@ -44,6 +44,7 @@ export { // eslint-disable-next-line deprecation/deprecation getCurrentHub, getClient, + isInitialized, getCurrentScope, getGlobalScope, getIsolationScope, diff --git a/packages/node-experimental/src/index.ts b/packages/node-experimental/src/index.ts index 8a6a60dfcd48..b19dad4adfd7 100644 --- a/packages/node-experimental/src/index.ts +++ b/packages/node-experimental/src/index.ts @@ -17,6 +17,7 @@ export type { Span } from './types'; export { startSpan, startSpanManual, startInactiveSpan, getActiveSpan } from '@sentry/opentelemetry'; export { getClient, + isInitialized, addBreadcrumb, captureException, captureEvent, diff --git a/packages/node-experimental/src/sdk/api.ts b/packages/node-experimental/src/sdk/api.ts index a163205ce1ec..ae8450d58a7c 100644 --- a/packages/node-experimental/src/sdk/api.ts +++ b/packages/node-experimental/src/sdk/api.ts @@ -22,9 +22,9 @@ import { getContextFromScope, getScopesFromContext, setScopesOnContext } from '. import type { ExclusiveEventHintOrCaptureContext } from '../utils/prepareEvent'; import { parseEventHintOrCaptureContext } from '../utils/prepareEvent'; import type { Scope } from './scope'; -import { getClient, getCurrentScope, getGlobalScope, getIsolationScope } from './scope'; +import { getClient, getCurrentScope, getGlobalScope, getIsolationScope, isInitialized } from './scope'; -export { getCurrentScope, getGlobalScope, getIsolationScope, getClient }; +export { getCurrentScope, getGlobalScope, getIsolationScope, getClient, isInitialized }; export { setCurrentScope, setIsolationScope } from './scope'; /** diff --git a/packages/node-experimental/src/sdk/scope.ts b/packages/node-experimental/src/sdk/scope.ts index 5b9d56dc4b84..e55667992839 100644 --- a/packages/node-experimental/src/sdk/scope.ts +++ b/packages/node-experimental/src/sdk/scope.ts @@ -65,6 +65,11 @@ export function getClient(): C { return {} as C; } +/** If the SDK was initialized. */ +export function isInitialized(): boolean { + return !!getClient().getDsn(); +} + /** A fork of the classic scope with some otel specific stuff. */ export class Scope extends OpenTelemetryScope implements ScopeInterface { // Overwrite this if you want to use a specific isolation scope here diff --git a/packages/node/src/index.ts b/packages/node/src/index.ts index 86ee3e78b961..4f563316338b 100644 --- a/packages/node/src/index.ts +++ b/packages/node/src/index.ts @@ -44,6 +44,7 @@ export { // eslint-disable-next-line deprecation/deprecation getCurrentHub, getClient, + isInitialized, getCurrentScope, getGlobalScope, getIsolationScope, diff --git a/packages/serverless/src/index.ts b/packages/serverless/src/index.ts index 3fe65ee74ea1..3fef9fb94283 100644 --- a/packages/serverless/src/index.ts +++ b/packages/serverless/src/index.ts @@ -32,6 +32,7 @@ export { // eslint-disable-next-line deprecation/deprecation getCurrentHub, getClient, + isInitialized, getCurrentScope, getGlobalScope, getIsolationScope, diff --git a/packages/sveltekit/src/server/index.ts b/packages/sveltekit/src/server/index.ts index 91fb9e2d7771..7d523ee55fd9 100644 --- a/packages/sveltekit/src/server/index.ts +++ b/packages/sveltekit/src/server/index.ts @@ -25,6 +25,7 @@ export { // eslint-disable-next-line deprecation/deprecation getCurrentHub, getClient, + isInitialized, getCurrentScope, getGlobalScope, getIsolationScope, diff --git a/packages/vercel-edge/src/index.ts b/packages/vercel-edge/src/index.ts index b9c18cb6dc1b..abeb6d54b870 100644 --- a/packages/vercel-edge/src/index.ts +++ b/packages/vercel-edge/src/index.ts @@ -44,6 +44,7 @@ export { // eslint-disable-next-line deprecation/deprecation getCurrentHub, getClient, + isInitialized, getCurrentScope, getGlobalScope, getIsolationScope,