Skip to content

feat(core): Return client from init method #12585

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 22 commits into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/angular/src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
functionToStringIntegration,
inboundFiltersIntegration,
} from '@sentry/core';
import type { Integration } from '@sentry/types';
import type { Client, Integration } from '@sentry/types';
import { logger } from '@sentry/utils';

import { IS_DEBUG_BUILD } from './flags';
Expand Down Expand Up @@ -44,7 +44,7 @@ export function getDefaultIntegrations(): Integration[] {
/**
* Inits the Angular SDK
*/
export function init(options: BrowserOptions): void {
export function init(options: BrowserOptions): Client | undefined {
const opts = {
defaultIntegrations: getDefaultIntegrations(),
...options,
Expand All @@ -53,7 +53,7 @@ export function init(options: BrowserOptions): void {
applySdkMetadata(opts, 'angular');

checkAndSetAngularVersion();
browserInit(opts);
return browserInit(opts);
}

function checkAndSetAngularVersion(): void {
Expand Down
4 changes: 4 additions & 0 deletions packages/angular/test/sdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,8 @@ describe('init', () => {

expect(angularDefaultIntegrations).toEqual(browserDefaultIntegrationsWithoutBrowserApiErrors);
});

it('returns client from init', () => {
expect(init({})).not.toBeUndefined();
});
});
8 changes: 5 additions & 3 deletions packages/astro/src/client/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
setTag,
} from '@sentry/browser';
import { applySdkMetadata, hasTracingEnabled } from '@sentry/core';
import type { Integration } from '@sentry/types';
import type { Client, Integration } from '@sentry/types';

// Tree-shakable guard to remove all code related to tracing
declare const __SENTRY_TRACING__: boolean;
Expand All @@ -16,17 +16,19 @@ declare const __SENTRY_TRACING__: boolean;
*
* @param options Configuration options for the SDK.
*/
export function init(options: BrowserOptions): void {
export function init(options: BrowserOptions): Client | undefined {
const opts = {
defaultIntegrations: getDefaultIntegrations(options),
...options,
};

applySdkMetadata(opts, 'astro', ['astro', 'browser']);

initBrowserSdk(opts);
const client = initBrowserSdk(opts);

setTag('runtime', 'browser');

return client;
}

function getDefaultIntegrations(options: BrowserOptions): Integration[] | undefined {
Expand Down
8 changes: 5 additions & 3 deletions packages/astro/src/server/sdk.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
import { applySdkMetadata } from '@sentry/core';
import type { NodeOptions } from '@sentry/node';
import type { NodeClient, NodeOptions } from '@sentry/node';
import { init as initNodeSdk, setTag } from '@sentry/node';

/**
*
* @param options
*/
export function init(options: NodeOptions): void {
export function init(options: NodeOptions): NodeClient | undefined {
const opts = {
...options,
};

applySdkMetadata(opts, 'astro', ['astro', 'node']);

initNodeSdk(opts);
const client = initNodeSdk(opts);

setTag('runtime', 'node');

return client;
}
4 changes: 4 additions & 0 deletions packages/astro/test/client/sdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,5 +122,9 @@ describe('Sentry client SDK', () => {
expect(getActiveSpan()).toBeUndefined();
});
});

it('returns client from init', () => {
expect(init({})).not.toBeUndefined();
});
});
});
4 changes: 4 additions & 0 deletions packages/astro/test/server/sdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,9 @@ describe('Sentry server SDK', () => {

expect(SentryNode.getIsolationScope().getScopeData().tags).toEqual({ runtime: 'node' });
});

it('returns client from init', () => {
expect(init({})).not.toBeUndefined();
});
});
});
6 changes: 3 additions & 3 deletions packages/aws-serverless/src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { existsSync } from 'fs';
import { hostname } from 'os';
import { basename, resolve } from 'path';
import { types } from 'util';
import type { NodeOptions } from '@sentry/node';
import type { NodeClient, NodeOptions } from '@sentry/node';
import {
SDK_VERSION,
captureException,
Expand Down Expand Up @@ -74,7 +74,7 @@ export function getDefaultIntegrations(_options: Options): Integration[] {
*
* @param options Configuration options for the SDK, @see {@link AWSLambdaOptions}.
*/
export function init(options: NodeOptions = {}): void {
export function init(options: NodeOptions = {}): NodeClient | undefined {
const opts = {
_metadata: {} as SdkMetadata,
defaultIntegrations: getDefaultIntegrations(options),
Expand All @@ -93,7 +93,7 @@ export function init(options: NodeOptions = {}): void {
version: SDK_VERSION,
};

initWithoutDefaultIntegrations(opts);
return initWithoutDefaultIntegrations(opts);
}

/** */
Expand Down
8 changes: 5 additions & 3 deletions packages/browser/src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
lastEventId,
startSession,
} from '@sentry/core';
import type { DsnLike, Integration, Options, UserFeedback } from '@sentry/types';
import type { Client, DsnLike, Integration, Options, UserFeedback } from '@sentry/types';
import { consoleSandbox, logger, stackParserFromStackParserOptions, supportsFetch } from '@sentry/utils';

import { addHistoryInstrumentationHandler } from '@sentry-internal/browser-utils';
Expand Down Expand Up @@ -139,7 +139,7 @@ declare const __SENTRY_RELEASE__: string | undefined;
*
* @see {@link BrowserOptions} for documentation on configuration options.
*/
export function init(browserOptions: BrowserOptions = {}): void {
export function init(browserOptions: BrowserOptions = {}): Client | undefined {
const options = applyDefaultOptions(browserOptions);

if (shouldShowBrowserExtensionError()) {
Expand All @@ -166,11 +166,13 @@ export function init(browserOptions: BrowserOptions = {}): void {
transport: options.transport || makeFetchTransport,
};

initAndBind(BrowserClient, clientOptions);
const client = initAndBind(BrowserClient, clientOptions);

if (options.autoSessionTracking) {
startSessionTracking();
}

return client;
}

/**
Expand Down
20 changes: 20 additions & 0 deletions packages/browser/test/unit/sdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,5 +209,25 @@ describe('init', () => {

consoleErrorSpy.mockRestore();
});

it("doesn't return a client on initialization error", () => {
const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});

Object.defineProperty(WINDOW, 'chrome', {
value: { runtime: { id: 'mock-extension-id' } },
writable: true,
});

const client = init(options);

expect(client).toBeUndefined();

consoleErrorSpy.mockRestore();
});
});

it('returns a client from init', () => {
const client = init();
expect(client).not.toBeUndefined();
});
});
5 changes: 3 additions & 2 deletions packages/bun/src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
linkedErrorsIntegration,
requestDataIntegration,
} from '@sentry/core';
import type { NodeClient } from '@sentry/node';
import {
consoleIntegration,
contextLinesIntegration,
Expand Down Expand Up @@ -91,13 +92,13 @@ export function getDefaultIntegrations(_options: Options): Integration[] {
*
* @see {@link BunOptions} for documentation on configuration options.
*/
export function init(options: BunOptions = {}): void {
export function init(options: BunOptions = {}): NodeClient | undefined {
options.clientClass = BunClient;
options.transport = options.transport || makeFetchTransport;

if (options.defaultIntegrations === undefined) {
options.defaultIntegrations = getDefaultIntegrations(options);
}

initNode(options);
return initNode(options);
}
4 changes: 4 additions & 0 deletions packages/bun/test/sdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@ test("calling init shouldn't fail", () => {
});
expect(true).toBe(true);
});

test('shuold return client from init', () => {
expect(init({})).not.toBeUndefined();
});
3 changes: 2 additions & 1 deletion packages/core/src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export type ClientClass<F extends Client, O extends ClientOptions> = new (option
export function initAndBind<F extends Client, O extends ClientOptions>(
clientClass: ClientClass<F, O>,
options: O,
): void {
): Client {
if (options.debug === true) {
if (DEBUG_BUILD) {
logger.enable();
Expand All @@ -35,6 +35,7 @@ export function initAndBind<F extends Client, O extends ClientOptions>(
const client = new clientClass(options);
setCurrentClient(client);
client.init();
return client;
}

/**
Expand Down
6 changes: 6 additions & 0 deletions packages/core/test/lib/sdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ describe('SDK', () => {
'afterAllSetup2',
]);
});

test('returns client from init', () => {
const options = getDefaultTestClientOptions({ dsn: PUBLIC_DSN });
const client = initAndBind(TestClient, options);
expect(client).not.toBeUndefined();
});
});
});

Expand Down
7 changes: 4 additions & 3 deletions packages/deno/src/sdk.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/consistent-type-imports */
import type { ServerRuntimeClientOptions } from '@sentry/core';
import {
dedupeIntegration,
Expand All @@ -6,7 +7,7 @@ import {
linkedErrorsIntegration,
} from '@sentry/core';
import { getIntegrationsToSetup, initAndBind } from '@sentry/core';
import type { Integration, Options, StackParser } from '@sentry/types';
import type { Client, Integration, Options, StackParser } from '@sentry/types';
import { createStackParser, nodeStackLineParser, stackParserFromStackParserOptions } from '@sentry/utils';

import { DenoClient } from './client';
Expand Down Expand Up @@ -82,7 +83,7 @@ const defaultStackParser: StackParser = createStackParser(nodeStackLineParser())
*
* @see {@link DenoOptions} for documentation on configuration options.
*/
export function init(options: DenoOptions = {}): void {
export function init(options: DenoOptions = {}): Client {
if (options.defaultIntegrations === undefined) {
options.defaultIntegrations = getDefaultIntegrations(options);
}
Expand All @@ -94,5 +95,5 @@ export function init(options: DenoOptions = {}): void {
transport: options.transport || makeFetchTransport,
};

initAndBind(DenoClient, clientOptions);
return initAndBind(DenoClient, clientOptions);
}
6 changes: 6 additions & 0 deletions packages/deno/test/sdk.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { assertNotEquals } from 'https://deno.land/[email protected]/assert/assert_not_equals.ts';
import { init } from '../build/index.mjs';

Deno.test('init() should return client', () => {
assertNotEquals(init({}), undefined);
});
10 changes: 6 additions & 4 deletions packages/ember/addon/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, app
import { GLOBAL_OBJ } from '@sentry/utils';
import Ember from 'ember';

import type { TransactionSource } from '@sentry/types';
import type { Client, TransactionSource } from '@sentry/types';
import type { EmberSentryConfig, GlobalConfig, OwnConfig } from './types';

function _getSentryInitConfig(): EmberSentryConfig['sentry'] {
Expand All @@ -21,7 +21,7 @@ function _getSentryInitConfig(): EmberSentryConfig['sentry'] {
/**
* Initialize the Sentry SDK for Ember.
*/
export function init(_runtimeConfig?: BrowserOptions): void {
export function init(_runtimeConfig?: BrowserOptions): Client | undefined {
const environmentConfig = getOwnConfig<OwnConfig>().sentryConfig;

assert('Missing configuration.', environmentConfig);
Expand All @@ -42,11 +42,11 @@ export function init(_runtimeConfig?: BrowserOptions): void {
const sentryInitConfig = _getSentryInitConfig();
Object.assign(sentryInitConfig, initConfig);

Sentry.init(initConfig);
const client = Sentry.init(initConfig);

if (macroCondition(isDevelopingApp())) {
if (environmentConfig.ignoreEmberOnErrorWarning) {
return;
return client;
}
next(null, function () {
warn(
Expand All @@ -58,6 +58,8 @@ export function init(_runtimeConfig?: BrowserOptions): void {
);
});
}

return client;
}

type RouteConstructor = new (...args: ConstructorParameters<typeof Route>) => Route;
Expand Down
5 changes: 3 additions & 2 deletions packages/gatsby/src/sdk.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { applySdkMetadata } from '@sentry/core';
import { init as reactInit } from '@sentry/react';

import type { Client } from '@sentry/types';
import type { GatsbyOptions } from './utils/types';

/**
* Inits the Sentry Gatsby SDK.
*/
export function init(options: GatsbyOptions): void {
export function init(options: GatsbyOptions): Client | undefined {
applySdkMetadata(options, 'gatsby');
reactInit({
return reactInit({
...options,
});
}
6 changes: 3 additions & 3 deletions packages/google-cloud-serverless/src/sdk.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { NodeOptions } from '@sentry/node';
import type { NodeClient, NodeOptions } from '@sentry/node';
import { SDK_VERSION, getDefaultIntegrationsWithoutPerformance, init as initNode } from '@sentry/node';
import type { Integration, Options, SdkMetadata } from '@sentry/types';

Expand Down Expand Up @@ -26,7 +26,7 @@ export function getDefaultIntegrations(_options: Options): Integration[] {
/**
* @see {@link Sentry.init}
*/
export function init(options: NodeOptions = {}): void {
export function init(options: NodeOptions = {}): NodeClient | undefined {
const opts = {
_metadata: {} as SdkMetadata,
defaultIntegrations: getDefaultIntegrations(options),
Expand All @@ -44,5 +44,5 @@ export function init(options: NodeOptions = {}): void {
version: SDK_VERSION,
};

initNode(opts);
return initNode(opts);
}
Loading
Loading