Skip to content

Commit

Permalink
feat(core): Add addEventProcessor method
Browse files Browse the repository at this point in the history
And deprecate `addGlobalEventProcessor()` and `getGlobalEventProcessors()`.

In v8, all event processors will be on the client only, streamlining this a bit and preventing global "pollution".
  • Loading branch information
mydea committed Nov 14, 2023
1 parent 010eb69 commit e0a3f1a
Show file tree
Hide file tree
Showing 47 changed files with 127 additions and 54 deletions.
2 changes: 2 additions & 0 deletions packages/astro/src/index.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import { sentryAstro } from './integration';

// Hence, we export everything from the Node SDK explicitly:
export {
// eslint-disable-next-line deprecation/deprecation
addGlobalEventProcessor,
addEventProcessor,
addBreadcrumb,
captureException,
captureEvent,
Expand Down
2 changes: 2 additions & 0 deletions packages/browser/src/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ export type { BrowserOptions } from './client';
export type { ReportDialogOptions } from './helpers';

export {
// eslint-disable-next-line deprecation/deprecation
addGlobalEventProcessor,
addEventProcessor,
addBreadcrumb,
addIntegration,
captureException,
Expand Down
2 changes: 1 addition & 1 deletion packages/browser/src/integrations/dedupe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class Dedupe implements Integration {
}

/** @inheritDoc */
public setupOnce(_addGlobaleventProcessor: unknown, _getCurrentHub: unknown): void {
public setupOnce(_addGlobalEventProcessor: unknown, _getCurrentHub: unknown): void {
// noop
}

Expand Down
2 changes: 2 additions & 0 deletions packages/bun/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ export type { TransactionNamingScheme } from '@sentry/node';
export type { BunOptions } from './types';

export {
// eslint-disable-next-line deprecation/deprecation
addGlobalEventProcessor,
addEventProcessor,
addBreadcrumb,
addIntegration,
captureException,
Expand Down
20 changes: 19 additions & 1 deletion packages/core/src/eventProcessors.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,39 @@
import type { Event, EventHint, EventProcessor } from '@sentry/types';
import { getGlobalSingleton, isThenable, logger, SyncPromise } from '@sentry/utils';

import { getCurrentHub } from './hub';

/**
* Returns the global event processors.
* @deprecated Global event processors will be removed in v8.
*/
export function getGlobalEventProcessors(): EventProcessor[] {
return getGlobalSingleton<EventProcessor[]>('globalEventProcessors', () => []);
}

/**
* Add a EventProcessor to be kept globally.
* @param callback EventProcessor to add
* @deprecated Use `addEventProcessor` instead. Global event processors will be removed in v8.
*/
export function addGlobalEventProcessor(callback: EventProcessor): void {
// eslint-disable-next-line deprecation/deprecation
getGlobalEventProcessors().push(callback);
}

/**
* Add an event processor to the current client.
* This event processor will run for all events processed by this client.
*/
export function addEventProcessor(callback: EventProcessor): void {
const client = getCurrentHub().getClient();

if (!client || !client.addEventProcessor) {
return;
}

client.addEventProcessor(callback);
}

/**
* Process an array of event processors, returning the processed event (or `null` if the event was dropped).
*/
Expand Down
6 changes: 5 additions & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ export {
export { makeSession, closeSession, updateSession } from './session';
export { SessionFlusher } from './sessionflusher';
export { Scope } from './scope';
export { addGlobalEventProcessor } from './eventProcessors';
export {
// eslint-disable-next-line deprecation/deprecation
addGlobalEventProcessor,
addEventProcessor,
} from './eventProcessors';
export { getEnvelopeEndpointWithUrlEncodedAuth, getReportDialogEndpoint } from './api';
export { BaseClient } from './baseclient';
export { ServerRuntimeClient } from './server-runtime-client';
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ export function setupIntegration(client: Client, integration: Integration, integ
integrationIndex[integration.name] = integration;

if (installedIntegrations.indexOf(integration.name) === -1) {
// eslint-disable-next-line deprecation/deprecation
integration.setupOnce(addGlobalEventProcessor, getCurrentHub);
installedIntegrations.push(integration.name);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/integrations/inboundfilters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export class InboundFilters implements Integration {
/**
* @inheritDoc
*/
public setupOnce(_addGlobaleventProcessor: unknown, _getCurrentHub: unknown): void {
public setupOnce(_addGlobalEventProcessor: unknown, _getCurrentHub: unknown): void {
// noop
}

Expand Down
7 changes: 6 additions & 1 deletion packages/core/src/scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,12 @@ export class Scope implements ScopeInterface {

// TODO (v8): Update this order to be: Global > Client > Scope
return notifyEventProcessors(
[...(additionalEventProcessors || []), ...getGlobalEventProcessors(), ...this._eventProcessors],
[
...(additionalEventProcessors || []),
// eslint-disable-next-line deprecation/deprecation
...getGlobalEventProcessors(),
...this._eventProcessors,
],
event,
hint,
);
Expand Down
10 changes: 9 additions & 1 deletion packages/core/src/utils/prepareEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,15 @@ export function prepareEvent(
} else {
// Apply client & global event processors even if there is no scope
// TODO (v8): Update the order to be Global > Client
result = notifyEventProcessors([...clientEventProcessors, ...getGlobalEventProcessors()], prepared, hint);
result = notifyEventProcessors(
[
...clientEventProcessors,
// eslint-disable-next-line deprecation/deprecation
...getGlobalEventProcessors(),
],
prepared,
hint,
);
}

return result.then(evt => {
Expand Down
2 changes: 2 additions & 0 deletions packages/deno/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ export type { AddRequestDataToEventOptions } from '@sentry/utils';
export type { DenoOptions } from './types';

export {
// eslint-disable-next-line deprecation/deprecation
addGlobalEventProcessor,
addEventProcessor,
addBreadcrumb,
captureException,
captureEvent,
Expand Down
2 changes: 1 addition & 1 deletion packages/e2e-tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ A standardized frontend test application has the following features:
be done with an event processor:

```ts
Sentry.addGlobalEventProcessor(event => {
Sentry.addEventProcessor(event => {
if (
event.type === 'transaction' &&
(event.contexts?.trace?.op === 'pageload' || event.contexts?.trace?.op === 'navigation')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Sentry.init({
// that it will also get attached to your source maps
});

Sentry.addGlobalEventProcessor(event => {
Sentry.addEventProcessor(event => {
if (
event.type === 'transaction' &&
(event.contexts?.trace?.op === 'pageload' || event.contexts?.trace?.op === 'navigation')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Sentry.init({
integrations: [new Sentry.Integrations.LocalVariables()],
});

Sentry.addGlobalEventProcessor(event => {
Sentry.addEventProcessor(event => {
global.transactionIds = global.transactionIds || [];

if (event.type === 'transaction') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Sentry.init({
replaysOnErrorSampleRate: 1.0, // If you're not already sampling the entire session, change the sample rate to 100% when sampling sessions where errors occur.
});

Sentry.addGlobalEventProcessor(event => {
Sentry.addEventProcessor(event => {
if (
event.type === 'transaction' &&
(event.contexts?.trace?.op === 'pageload' || event.contexts?.trace?.op === 'navigation')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Sentry.init({
replaysOnErrorSampleRate: 1.0, // If you're not already sampling the entire session, change the sample rate to 100% when sampling sessions where errors occur.
});

Sentry.addGlobalEventProcessor(event => {
Sentry.addEventProcessor(event => {
if (
event.type === 'transaction' &&
(event.contexts?.trace?.op === 'pageload' || event.contexts?.trace?.op === 'navigation')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ app.listen(port, () => {
console.log(`Example app listening on port ${port}`);
});

Sentry.addGlobalEventProcessor(event => {
Sentry.addEventProcessor(event => {
global.transactionIds = global.transactionIds || [];

if (event.type === 'transaction') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ Object.defineProperty(window, 'sentryReplayId', {
},
});

Sentry.addGlobalEventProcessor(event => {
Sentry.addEventProcessor(event => {
if (
event.type === 'transaction' &&
(event.contexts?.trace?.op === 'pageload' || event.contexts?.trace?.op === 'navigation')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Object.defineProperty(window, 'sentryReplayId', {
},
});

Sentry.addGlobalEventProcessor(event => {
Sentry.addEventProcessor(event => {
if (
event.type === 'transaction' &&
(event.contexts?.trace?.op === 'pageload' || event.contexts?.trace?.op === 'navigation')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Sentry.init({
release: 'e2e-test',
});

Sentry.addGlobalEventProcessor(event => {
Sentry.addEventProcessor(event => {
if (
event.type === 'transaction' &&
(event.contexts?.trace?.op === 'pageload' || event.contexts?.trace?.op === 'navigation')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ Object.defineProperty(window, 'sentryReplayId', {
},
});

Sentry.addGlobalEventProcessor(event => {
Sentry.addEventProcessor(event => {
if (
event.type === 'transaction' &&
(event.contexts?.trace?.op === 'pageload' || event.contexts?.trace?.op === 'navigation')
Expand Down
2 changes: 1 addition & 1 deletion packages/ember/tests/test-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ declare global {
}
}

Sentry.addGlobalEventProcessor(event => {
Sentry.addEventProcessor(event => {
if (isTesting()) {
if (!window._sentryTestEvents) {
window._sentryTestEvents = [];
Expand Down
3 changes: 2 additions & 1 deletion packages/hub/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ export const getCurrentHub = getCurrentHubCore;
/**
* @deprecated This export has moved to @sentry/core. The @sentry/hub package will be removed in v8.
*/
export const addGlobalEventProcessor = addGlobalEventProcessorCore;

export const addGlobalEventProcessor = addGlobalEventProcessorCore; // eslint-disable-line deprecation/deprecation

/**
* @deprecated This export has moved to @sentry/core. The @sentry/hub package will be removed in v8.
Expand Down
1 change: 1 addition & 0 deletions packages/hub/test/scope.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,7 @@ describe('Scope', () => {
const localScope = new Scope();
localScope.setExtra('a', 'b');

// eslint-disable-next-line deprecation/deprecation
addGlobalEventProcessor((processedEvent: Event) => {
processedEvent.dist = '1';
return processedEvent;
Expand Down
2 changes: 1 addition & 1 deletion packages/integrations/src/contextlines.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export class ContextLines implements Integration {
/**
* @inheritDoc
*/
public setupOnce(_addGlobaleventProcessor: unknown, _getCurrentHub: unknown): void {
public setupOnce(_addGlobalEventProcessor: unknown, _getCurrentHub: unknown): void {
// noop
}

Expand Down
2 changes: 1 addition & 1 deletion packages/integrations/src/dedupe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class Dedupe implements Integration {
}

/** @inheritDoc */
public setupOnce(_addGlobaleventProcessor: unknown, _getCurrentHub: unknown): void {
public setupOnce(_addGlobalEventProcessor: unknown, _getCurrentHub: unknown): void {
// noop
}

Expand Down
2 changes: 1 addition & 1 deletion packages/integrations/src/extraerrordata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class ExtraErrorData implements Integration {
/**
* @inheritDoc
*/
public setupOnce(_addGlobaleventProcessor: unknown, _getCurrentHub: unknown): void {
public setupOnce(_addGlobalEventProcessor: unknown, _getCurrentHub: unknown): void {
// noop
}

Expand Down
2 changes: 1 addition & 1 deletion packages/integrations/src/rewriteframes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class RewriteFrames implements Integration {
/**
* @inheritDoc
*/
public setupOnce(_addGlobaleventProcessor: unknown, _getCurrentHub: unknown): void {
public setupOnce(_addGlobalEventProcessor: unknown, _getCurrentHub: unknown): void {
// noop
}

Expand Down
2 changes: 1 addition & 1 deletion packages/integrations/src/sessiontiming.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class SessionTiming implements Integration {
/**
* @inheritDoc
*/
public setupOnce(_addGlobaleventProcessor: unknown, _getCurrentHub: unknown): void {
public setupOnce(_addGlobalEventProcessor: unknown, _getCurrentHub: unknown): void {
// noop
}

Expand Down
2 changes: 1 addition & 1 deletion packages/integrations/src/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class Transaction implements Integration {
/**
* @inheritDoc
*/
public setupOnce(_addGlobaleventProcessor: unknown, _getCurrentHub: unknown): void {
public setupOnce(_addGlobalEventProcessor: unknown, _getCurrentHub: unknown): void {
// noop
}

Expand Down
2 changes: 2 additions & 0 deletions packages/node-experimental/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ export {
extractRequestData,
deepReadDirSync,
getModuleFromFilename,
// eslint-disable-next-line deprecation/deprecation
addGlobalEventProcessor,
addEventProcessor,
addBreadcrumb,
captureException,
captureEvent,
Expand Down
4 changes: 2 additions & 2 deletions packages/node/src/anr/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { Event, Session, StackFrame } from '@sentry/types';
import { logger, watchdogTimer } from '@sentry/utils';
import { spawn } from 'child_process';

import { addGlobalEventProcessor, captureEvent, flush, getCurrentHub } from '..';
import { addEventProcessor, captureEvent, flush, getCurrentHub } from '..';
import { captureStackTrace } from './debugger';

const DEFAULT_INTERVAL = 50;
Expand Down Expand Up @@ -191,7 +191,7 @@ function handleChildProcess(options: Options): void {
});
}

addGlobalEventProcessor(event => {
addEventProcessor(event => {
// Strip sdkProcessingMetadata from all child process events to remove trace info
delete event.sdkProcessingMetadata;
event.tags = {
Expand Down
2 changes: 2 additions & 0 deletions packages/node/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ export type { TransactionNamingScheme } from './requestdata';
export type { NodeOptions } from './types';

export {
// eslint-disable-next-line deprecation/deprecation
addGlobalEventProcessor,
addEventProcessor,
addBreadcrumb,
addIntegration,
captureException,
Expand Down
4 changes: 2 additions & 2 deletions packages/opentelemetry-node/src/spanprocessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Context } from '@opentelemetry/api';
import { context, SpanKind, trace } from '@opentelemetry/api';
import { suppressTracing } from '@opentelemetry/core';
import type { Span as OtelSpan, SpanProcessor as OtelSpanProcessor } from '@opentelemetry/sdk-trace-base';
import { addGlobalEventProcessor, addTracingExtensions, getCurrentHub, Transaction } from '@sentry/core';
import { addEventProcessor, addTracingExtensions, getCurrentHub, Transaction } from '@sentry/core';
import type { DynamicSamplingContext, Span as SentrySpan, TraceparentData, TransactionContext } from '@sentry/types';
import { logger } from '@sentry/utils';

Expand All @@ -21,7 +21,7 @@ export class SentrySpanProcessor implements OtelSpanProcessor {
public constructor() {
addTracingExtensions();

addGlobalEventProcessor(event => {
addEventProcessor(event => {
const otelSpan = trace && trace.getActiveSpan && (trace.getActiveSpan() as OtelSpan | undefined);
if (!otelSpan) {
return event;
Expand Down
Loading

0 comments on commit e0a3f1a

Please sign in to comment.