Skip to content

chore: Introduce internal getHubAndIntegration #4279

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

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions packages/browser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"dependencies": {
"@sentry/core": "6.16.1",
"@sentry/types": "6.16.1",
"@sentry/hub": "6.16.1",
"@sentry/utils": "6.16.1",
"tslib": "^1.9.3"
},
Expand Down
15 changes: 8 additions & 7 deletions packages/browser/src/integrations/dedupe.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Event, EventProcessor, Exception, Hub, Integration, StackFrame } from '@sentry/types';
import { addGlobalEventProcessor, getHubAndIntegration } from '@sentry/hub';
import { Event, Exception, Integration, StackFrame } from '@sentry/types';
import { logger } from '@sentry/utils';

/** Deduplication filter */
Expand All @@ -21,21 +22,21 @@ export class Dedupe implements Integration {
/**
* @inheritDoc
*/
public setupOnce(addGlobalEventProcessor: (callback: EventProcessor) => void, getCurrentHub: () => Hub): void {
public setupOnce(): void {
addGlobalEventProcessor((currentEvent: Event) => {
const self = getCurrentHub().getIntegration(Dedupe);
if (self) {
const [hub, integration] = getHubAndIntegration(Dedupe);
if (hub && integration) {
// Juuust in case something goes wrong
try {
if (self._shouldDropEvent(currentEvent, self._previousEvent)) {
if (integration._shouldDropEvent(currentEvent, integration._previousEvent)) {
logger.warn(`Event dropped due to being a duplicate of previously captured event.`);
return null;
}
} catch (_oO) {
return (self._previousEvent = currentEvent);
return (integration._previousEvent = currentEvent);
}

return (self._previousEvent = currentEvent);
return (integration._previousEvent = currentEvent);
}
return currentEvent;
});
Expand Down
22 changes: 13 additions & 9 deletions packages/browser/src/integrations/globalhandlers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
import { getCurrentHub } from '@sentry/core';
import { Event, EventHint, Hub, Integration, Primitive, Severity } from '@sentry/types';
import { getHubAndIntegration } from '@sentry/hub';
import { Hub, Event, EventHint, Integration, Primitive, Severity } from '@sentry/types';
import {
addExceptionMechanism,
addInstrumentationHandler,
Expand Down Expand Up @@ -78,9 +78,10 @@ function _installGlobalOnErrorHandler(): void {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
callback: (data: { msg: any; url: any; line: any; column: any; error: any }) => {
const [hub, attachStacktrace] = getHubAndAttachStacktrace();
if (!hub.getIntegration(GlobalHandlers)) {
if (!hub) {
return;
}

const { msg, url, line, column, error } = data;
if (shouldIgnoreOnError() || (error && error.__sentry_own_request__)) {
return;
Expand Down Expand Up @@ -111,7 +112,7 @@ function _installGlobalOnUnhandledRejectionHandler(): void {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
callback: (e: any) => {
const [hub, attachStacktrace] = getHubAndAttachStacktrace();
if (!hub.getIntegration(GlobalHandlers)) {
if (!hub) {
return;
}
let error = e;
Expand Down Expand Up @@ -252,9 +253,12 @@ function addMechanismAndCapture(hub: Hub, error: EventHint['originalException'],
});
}

function getHubAndAttachStacktrace(): [Hub, boolean | undefined] {
const hub = getCurrentHub();
const client = hub.getClient();
const attachStacktrace = client && client.getOptions().attachStacktrace;
return [hub, attachStacktrace];
function getHubAndAttachStacktrace(): [Hub | undefined, boolean | undefined] {
const [hub, integration] = getHubAndIntegration(GlobalHandlers);
if (hub && integration) {
const client = hub.getClient();
const attachStacktrace = client && client.getOptions().attachStacktrace;
return [hub, attachStacktrace];
}
return [undefined, undefined];
}
6 changes: 3 additions & 3 deletions packages/browser/src/integrations/linkederrors.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { addGlobalEventProcessor, getCurrentHub } from '@sentry/core';
import { addGlobalEventProcessor, getHubAndIntegration } from '@sentry/hub';
import { Event, EventHint, Exception, ExtendedError, Integration } from '@sentry/types';
import { isInstanceOf } from '@sentry/utils';

Expand Down Expand Up @@ -43,8 +43,8 @@ export class LinkedErrors implements Integration {
*/
public setupOnce(): void {
addGlobalEventProcessor((event: Event, hint?: EventHint) => {
const self = getCurrentHub().getIntegration(LinkedErrors);
if (self) {
const [hub, self] = getHubAndIntegration(LinkedErrors);
if (hub && self) {
const handler = self._handler && self._handler.bind(self);
return typeof handler === 'function' ? handler(event, hint) : event;
}
Expand Down
5 changes: 3 additions & 2 deletions packages/browser/src/integrations/useragent.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { addGlobalEventProcessor, getCurrentHub } from '@sentry/core';
import { addGlobalEventProcessor, getHubAndIntegration } from '@sentry/hub';
import { Event, Integration } from '@sentry/types';
import { getGlobalObject } from '@sentry/utils';

Expand All @@ -21,7 +21,8 @@ export class UserAgent implements Integration {
*/
public setupOnce(): void {
addGlobalEventProcessor((event: Event) => {
if (getCurrentHub().getIntegration(UserAgent)) {
const [hub, integration] = getHubAndIntegration(UserAgent);
if (hub && integration) {
// if none of the information we want exists, don't bother
if (!global.navigator && !global.location && !global.document) {
return event;
Expand Down
10 changes: 10 additions & 0 deletions packages/hub/src/hub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ import { consoleSandbox, dateTimestampInSeconds, getGlobalObject, isNodeEnv, log
import { Scope } from './scope';
import { Session } from './session';

/**
* Utility to return the hub an integration at once.
*/
export function getHubAndIntegration<T extends Integration>(
integration: IntegrationClass<T>,
): [HubInterface | null, T | null] {
const hub = getCurrentHub();
return hub ? [hub, hub.getIntegration(integration)] : [null, null];
}

/**
* API compatibility version of this hub.
*
Expand Down
1 change: 1 addition & 0 deletions packages/hub/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export {
getCurrentHub,
getHubFromCarrier,
getMainCarrier,
getHubAndIntegration,
Hub,
makeMain,
setHubOnCarrier,
Expand Down
1 change: 1 addition & 0 deletions packages/integrations/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"dependencies": {
"@sentry/types": "6.16.1",
"@sentry/utils": "6.16.1",
"@sentry/hub": "6.16.1",
"localforage": "^1.8.1",
"tslib": "^1.9.3"
},
Expand Down
16 changes: 5 additions & 11 deletions packages/integrations/src/angular.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Event, EventProcessor, Hub, Integration } from '@sentry/types';
import { getHubAndIntegration } from '@sentry/hub';
import { Event, Integration } from '@sentry/types';
import { getGlobalObject, logger } from '@sentry/utils';

// See https://github.com/angular/angular.js/blob/v1.4.7/src/minErr.js
Expand Down Expand Up @@ -37,11 +38,6 @@ export class Angular implements Integration {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
private readonly _module: any;

/**
* Returns current hub.
*/
private _getCurrentHub?: () => Hub;

/**
* @inheritDoc
*/
Expand All @@ -64,13 +60,11 @@ export class Angular implements Integration {
/**
* @inheritDoc
*/
public setupOnce(_: (callback: EventProcessor) => void, getCurrentHub: () => Hub): void {
public setupOnce(): void {
if (!this._module) {
return;
}

this._getCurrentHub = getCurrentHub;

// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
this._module.config([
'$provide',
Expand All @@ -88,9 +82,9 @@ export class Angular implements Integration {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
private _$exceptionHandlerDecorator($delegate: any): any {
return (exception: Error, cause?: string): void => {
const hub = this._getCurrentHub && this._getCurrentHub();
const [hub, integration] = getHubAndIntegration(Angular);

if (hub && hub.getIntegration(Angular)) {
if (hub && integration) {
hub.withScope(scope => {
if (cause) {
scope.setExtra('cause', cause);
Expand Down
9 changes: 5 additions & 4 deletions packages/integrations/src/captureconsole.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { EventProcessor, Hub, Integration, Severity } from '@sentry/types';
import { getHubAndIntegration } from '@sentry/hub';
import { Integration, Severity } from '@sentry/types';
import { fill, getGlobalObject, safeJoin } from '@sentry/utils';

const global = getGlobalObject<Window | NodeJS.Global>();
Expand Down Expand Up @@ -32,7 +33,7 @@ export class CaptureConsole implements Integration {
/**
* @inheritDoc
*/
public setupOnce(_: (callback: EventProcessor) => void, getCurrentHub: () => Hub): void {
public setupOnce(): void {
if (!('console' in global)) {
return;
}
Expand All @@ -44,9 +45,9 @@ export class CaptureConsole implements Integration {

// eslint-disable-next-line @typescript-eslint/no-explicit-any
fill(global.console, level, (originalConsoleLevel: () => any) => (...args: any[]): void => {
const hub = getCurrentHub();
const [hub, integration] = getHubAndIntegration(CaptureConsole);

if (hub.getIntegration(CaptureConsole)) {
if (hub && integration) {
hub.withScope(scope => {
scope.setLevel(Severity.fromString(level));
scope.setExtra('arguments', args);
Expand Down
9 changes: 5 additions & 4 deletions packages/integrations/src/debug.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Event, EventHint, EventProcessor, Hub, Integration } from '@sentry/types';
import { addGlobalEventProcessor, getHubAndIntegration } from '@sentry/hub';
import { Event, EventHint, Integration } from '@sentry/types';
import { consoleSandbox } from '@sentry/utils';

/** JSDoc */
Expand Down Expand Up @@ -36,10 +37,10 @@ export class Debug implements Integration {
/**
* @inheritDoc
*/
public setupOnce(addGlobalEventProcessor: (callback: EventProcessor) => void, getCurrentHub: () => Hub): void {
public setupOnce(): void {
addGlobalEventProcessor((event: Event, hint?: EventHint) => {
const self = getCurrentHub().getIntegration(Debug);
if (self) {
const [hub, self] = getHubAndIntegration(Debug);
if (hub && self) {
if (self._options.debugger) {
// eslint-disable-next-line no-debugger
debugger;
Expand Down
9 changes: 5 additions & 4 deletions packages/integrations/src/dedupe.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Event, EventProcessor, Exception, Hub, Integration, StackFrame } from '@sentry/types';
import { addGlobalEventProcessor, getHubAndIntegration } from '@sentry/hub';
import { Event, Exception, Integration, StackFrame } from '@sentry/types';
import { logger } from '@sentry/utils';

/** Deduplication filter */
Expand All @@ -21,10 +22,10 @@ export class Dedupe implements Integration {
/**
* @inheritDoc
*/
public setupOnce(addGlobalEventProcessor: (callback: EventProcessor) => void, getCurrentHub: () => Hub): void {
public setupOnce(): void {
addGlobalEventProcessor((currentEvent: Event) => {
const self = getCurrentHub().getIntegration(Dedupe);
if (self) {
const [hub, self] = getHubAndIntegration(Dedupe);
if (hub && self) {
// Juuust in case something goes wrong
try {
if (self._shouldDropEvent(currentEvent, self._previousEvent)) {
Expand Down
19 changes: 11 additions & 8 deletions packages/integrations/src/ember.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { EventProcessor, Hub, Integration } from '@sentry/types';
import { getHubAndIntegration } from '@sentry/hub';
import { Integration } from '@sentry/types';
import { getGlobalObject, isInstanceOf, logger } from '@sentry/utils';

/** JSDoc */
Expand Down Expand Up @@ -31,7 +32,7 @@ export class Ember implements Integration {
/**
* @inheritDoc
*/
public setupOnce(_: (callback: EventProcessor) => void, getCurrentHub: () => Hub): void {
public setupOnce(): void {
if (!this._Ember) {
logger.error('EmberIntegration is missing an Ember instance');
return;
Expand All @@ -41,8 +42,9 @@ export class Ember implements Integration {
const oldOnError = this._Ember.onerror;

this._Ember.onerror = (error: Error): void => {
if (getCurrentHub().getIntegration(Ember)) {
getCurrentHub().captureException(error, { originalException: error });
const [hub, integration] = getHubAndIntegration(Ember);
if (hub && integration) {
hub.captureException(error, { originalException: error });
}

if (typeof oldOnError === 'function') {
Expand All @@ -54,14 +56,15 @@ export class Ember implements Integration {

// eslint-disable-next-line @typescript-eslint/no-explicit-any
this._Ember.RSVP.on('error', (reason: unknown): void => {
if (getCurrentHub().getIntegration(Ember)) {
getCurrentHub().withScope(scope => {
const [hub, integration] = getHubAndIntegration(Ember);
if (hub && integration) {
hub.withScope(scope => {
if (isInstanceOf(reason, Error)) {
scope.setExtra('context', 'Unhandled Promise error detected');
getCurrentHub().captureException(reason, { originalException: reason as Error });
hub.captureException(reason, { originalException: reason as Error });
} else {
scope.setExtra('reason', reason);
getCurrentHub().captureMessage('Unhandled Promise error detected');
hub.captureMessage('Unhandled Promise error detected');
}
});
}
Expand Down
12 changes: 7 additions & 5 deletions packages/integrations/src/extraerrordata.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Event, EventHint, EventProcessor, ExtendedError, Hub, Integration } from '@sentry/types';
import { addGlobalEventProcessor, getHubAndIntegration } from '@sentry/hub';
import { Event, EventHint, ExtendedError, Integration } from '@sentry/types';
import { isError, isPlainObject, logger, normalize } from '@sentry/utils';

/** JSDoc */
Expand Down Expand Up @@ -26,13 +27,14 @@ export class ExtraErrorData implements Integration {
/**
* @inheritDoc
*/
public setupOnce(addGlobalEventProcessor: (callback: EventProcessor) => void, getCurrentHub: () => Hub): void {
public setupOnce(): void {
addGlobalEventProcessor((event: Event, hint?: EventHint) => {
const self = getCurrentHub().getIntegration(ExtraErrorData);
if (!self) {
const [hub, self] = getHubAndIntegration(ExtraErrorData);
if (!hub || !self) {
return event;
} else {
return self.enhanceEventWithErrorData(event, hint);
}
return self.enhanceEventWithErrorData(event, hint);
});
}

Expand Down
Loading