Skip to content

Commit

Permalink
feat(core): Expose isInitialized() to replace checking via `getClie…
Browse files Browse the repository at this point in the history
…nt` (#10296)

Currently, you can use `Sentry.getClient() !== undefined` to check if
Sentry was initialized. In v8, we want to change this so that this
_always_ returns a client (possibly a Noop client), so this check will
not work anymore there. Instead, we can provide a new util that does
this explicitly, where we can control what it checks under the hood.
  • Loading branch information
mydea committed Jan 23, 2024
1 parent a0b987a commit 675309d
Show file tree
Hide file tree
Showing 15 changed files with 48 additions and 2 deletions.
6 changes: 6 additions & 0 deletions MIGRATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions packages/astro/src/index.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export {
// eslint-disable-next-line deprecation/deprecation
getCurrentHub,
getClient,
isInitialized,
getCurrentScope,
getGlobalScope,
getIsolationScope,
Expand Down
1 change: 1 addition & 0 deletions packages/browser/src/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export {
// eslint-disable-next-line deprecation/deprecation
getCurrentHub,
getClient,
isInitialized,
getCurrentScope,
Hub,
// eslint-disable-next-line deprecation/deprecation
Expand Down
1 change: 1 addition & 0 deletions packages/bun/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export {
// eslint-disable-next-line deprecation/deprecation
getCurrentHub,
getClient,
isInitialized,
getCurrentScope,
getGlobalScope,
getIsolationScope,
Expand Down
7 changes: 7 additions & 0 deletions packages/core/src/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,13 @@ export function getClient<C extends Client>(): C | undefined {
return getCurrentHub().getClient<C>();
}

/**
* Returns true if Sentry has been properly initialized.
*/
export function isInitialized(): boolean {
return !!getClient();
}

/**
* Get the currently active scope.
*/
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export {
withScope,
withIsolationScope,
getClient,
isInitialized,
getCurrentScope,
startSession,
endSession,
Expand Down
18 changes: 18 additions & 0 deletions packages/core/test/lib/exports.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { GLOBAL_OBJ } from '@sentry/utils';
import {
Hub,
Scope,
Expand All @@ -16,6 +17,7 @@ import {
withIsolationScope,
withScope,
} from '../../src';
import { isInitialized } from '../../src/exports';
import { TestClient, getDefaultTestClientOptions } from '../mocks/client';

function getTestClient(): TestClient {
Expand Down Expand Up @@ -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);
});
});
1 change: 1 addition & 0 deletions packages/deno/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export {
// eslint-disable-next-line deprecation/deprecation
getCurrentHub,
getClient,
isInitialized,
getCurrentScope,
getGlobalScope,
getIsolationScope,
Expand Down
1 change: 1 addition & 0 deletions packages/node-experimental/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export type { Span } from './types';
export { startSpan, startSpanManual, startInactiveSpan, getActiveSpan } from '@sentry/opentelemetry';
export {
getClient,
isInitialized,
addBreadcrumb,
captureException,
captureEvent,
Expand Down
4 changes: 2 additions & 2 deletions packages/node-experimental/src/sdk/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
Expand Down
5 changes: 5 additions & 0 deletions packages/node-experimental/src/sdk/scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ export function getClient<C extends Client>(): 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
Expand Down
1 change: 1 addition & 0 deletions packages/node/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export {
// eslint-disable-next-line deprecation/deprecation
getCurrentHub,
getClient,
isInitialized,
getCurrentScope,
getGlobalScope,
getIsolationScope,
Expand Down
1 change: 1 addition & 0 deletions packages/serverless/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export {
// eslint-disable-next-line deprecation/deprecation
getCurrentHub,
getClient,
isInitialized,
getCurrentScope,
getGlobalScope,
getIsolationScope,
Expand Down
1 change: 1 addition & 0 deletions packages/sveltekit/src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export {
// eslint-disable-next-line deprecation/deprecation
getCurrentHub,
getClient,
isInitialized,
getCurrentScope,
getGlobalScope,
getIsolationScope,
Expand Down
1 change: 1 addition & 0 deletions packages/vercel-edge/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export {
// eslint-disable-next-line deprecation/deprecation
getCurrentHub,
getClient,
isInitialized,
getCurrentScope,
getGlobalScope,
getIsolationScope,
Expand Down

0 comments on commit 675309d

Please sign in to comment.