Skip to content

Commit cb6912a

Browse files
authored
ref(core): Make on and emit required on client (#10603)
This allows us to remove a bunch of code around fallbacks etc. Esp. in replay this also allows us to remove a bunch of code around breadcrumbs etc. note @JonasBa I did not remove this in profiling-node yet because the test rely on this quite a bunch, can you take a look at this when you've got some time?
1 parent d2ef1cb commit cb6912a

File tree

34 files changed

+75
-397
lines changed

34 files changed

+75
-397
lines changed

packages/angular/src/errorhandler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ class SentryErrorHandler implements AngularErrorHandler {
118118
if (this._options.showDialog) {
119119
const client = Sentry.getClient();
120120

121-
if (client && client.on && !this._registeredAfterSendEventHandler) {
121+
if (client && !this._registeredAfterSendEventHandler) {
122122
client.on('afterSendEvent', (event: Event) => {
123123
if (!event.type) {
124124
// eslint-disable-next-line deprecation/deprecation
@@ -128,7 +128,7 @@ class SentryErrorHandler implements AngularErrorHandler {
128128

129129
// We only want to register this hook once in the lifetime of the error handler
130130
this._registeredAfterSendEventHandler = true;
131-
} else if (!client || !client.on) {
131+
} else if (!client) {
132132
Sentry.showReportDialog({ ...this._options.dialogOptions, eventId });
133133
}
134134
}

packages/browser/src/integrations/breadcrumbs.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ const _breadcrumbsIntegration = ((options: Partial<BreadcrumbsOptions> = {}) =>
8888
if (_options.history) {
8989
addHistoryInstrumentationHandler(_getHistoryBreadcrumbHandler(client));
9090
}
91-
if (_options.sentry && client.on) {
91+
if (_options.sentry) {
9292
client.on('beforeSendEvent', _getSentryBreadcrumbHandler(client));
9393
}
9494
},

packages/browser/src/profiling/integration.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,6 @@ const _browserProfilingIntegration = (() => {
3535
}
3636
}
3737

38-
if (typeof client.on !== 'function') {
39-
logger.warn('[Profiling] Client does not support hooks, profiling will be disabled');
40-
return;
41-
}
42-
4338
client.on('startTransaction', (transaction: Transaction) => {
4439
if (shouldProfileTransaction(transaction)) {
4540
startProfileForTransaction(transaction);

packages/core/src/hub.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -419,9 +419,7 @@ export class Hub implements HubInterface {
419419

420420
if (finalBreadcrumb === null) return;
421421

422-
if (client.emit) {
423-
client.emit('beforeAddBreadcrumb', finalBreadcrumb, hint);
424-
}
422+
client.emit('beforeAddBreadcrumb', finalBreadcrumb, hint);
425423

426424
// TODO(v8): I know this comment doesn't make much sense because the hub will be deprecated but I still wanted to
427425
// write it down. In theory, we would have to add the breadcrumbs to the isolation scope here, however, that would

packages/core/src/integration.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ export function setupIntegration(client: Client, integration: Integration, integ
140140
integration.setup(client);
141141
}
142142

143-
if (client.on && typeof integration.preprocessEvent === 'function') {
143+
if (typeof integration.preprocessEvent === 'function') {
144144
const callback = integration.preprocessEvent.bind(integration) as typeof integration.preprocessEvent;
145145
client.on('preprocessEvent', (event, hint) => callback(event, hint, client));
146146
}

packages/core/src/integrations/metadata.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@ const _moduleMetadataIntegration = (() => {
1212
// TODO v8: Remove this
1313
setupOnce() {}, // eslint-disable-line @typescript-eslint/no-empty-function
1414
setup(client) {
15-
if (typeof client.on !== 'function') {
16-
return;
17-
}
18-
1915
// We need to strip metadata from stack frames before sending them to Sentry since these are client side only.
2016
client.on('beforeEnvelope', envelope => {
2117
forEachEnvelopeItem(envelope, (item, type) => {

packages/core/src/tracing/dynamicSamplingContext.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export function getDynamicSamplingContextFromClient(trace_id: string, client: Cl
2323
trace_id,
2424
}) as DynamicSamplingContext;
2525

26-
client.emit && client.emit('createDsc', dsc);
26+
client.emit('createDsc', dsc);
2727

2828
return dsc;
2929
}
@@ -81,7 +81,7 @@ export function getDynamicSamplingContextFromSpan(span: Span): Readonly<Partial<
8181

8282
dsc.sampled = String(spanIsSampled(txn));
8383

84-
client.emit && client.emit('createDsc', dsc);
84+
client.emit('createDsc', dsc);
8585

8686
return dsc;
8787
}

packages/core/src/tracing/hubextensions.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ The transaction will not be sampled. Please use the ${configInstrumenter} instru
7878
if (transaction.isRecording()) {
7979
transaction.initSpanRecorder(options._experiments && (options._experiments.maxSpans as number));
8080
}
81-
if (client && client.emit) {
81+
if (client) {
8282
client.emit('startTransaction', transaction);
8383
}
8484
return transaction;
@@ -125,7 +125,7 @@ export function startIdleTransaction(
125125
if (transaction.isRecording()) {
126126
transaction.initSpanRecorder(options._experiments && (options._experiments.maxSpans as number));
127127
}
128-
if (client && client.emit) {
128+
if (client) {
129129
client.emit('startTransaction', transaction);
130130
}
131131
return transaction;

packages/core/src/tracing/transaction.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ export class Transaction extends SpanClass implements TransactionInterface {
276276

277277
// eslint-disable-next-line deprecation/deprecation
278278
const client = this._hub.getClient();
279-
if (client && client.emit) {
279+
if (client) {
280280
client.emit('finishTransaction', this);
281281
}
282282

packages/core/test/lib/base.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1957,11 +1957,11 @@ describe('BaseClient', () => {
19571957
traceId: '86f39e84263a4de99c326acab3bfe3bd',
19581958
} as Transaction;
19591959

1960-
client.on?.('startTransaction', transaction => {
1960+
client.on('startTransaction', transaction => {
19611961
expect(transaction).toEqual(mockTransaction);
19621962
});
19631963

1964-
client.emit?.('startTransaction', mockTransaction);
1964+
client.emit('startTransaction', mockTransaction);
19651965
});
19661966

19671967
it('should call a beforeEnvelope hook', () => {
@@ -1974,11 +1974,11 @@ describe('BaseClient', () => {
19741974
{},
19751975
] as Envelope;
19761976

1977-
client.on?.('beforeEnvelope', envelope => {
1977+
client.on('beforeEnvelope', envelope => {
19781978
expect(envelope).toEqual(mockEnvelope);
19791979
});
19801980

1981-
client.emit?.('beforeEnvelope', mockEnvelope);
1981+
client.emit('beforeEnvelope', mockEnvelope);
19821982
});
19831983
});
19841984
});

packages/feedback/src/util/prepareFeedbackEvent.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@ export async function prepareFeedbackEvent({
1717
event,
1818
}: PrepareFeedbackEventParams): Promise<FeedbackEvent | null> {
1919
const eventHint = {};
20-
if (client.emit) {
21-
client.emit('preprocessEvent', event, eventHint);
22-
}
20+
client.emit('preprocessEvent', event, eventHint);
2321

2422
const preparedEvent = (await prepareEvent(
2523
client.getOptions(),

packages/feedback/src/util/sendFeedbackRequest.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,7 @@ export async function sendFeedbackRequest(
5151
return;
5252
}
5353

54-
if (client.emit) {
55-
client.emit('beforeSendFeedback', feedbackEvent, { includeReplay: Boolean(includeReplay) });
56-
}
54+
client.emit('beforeSendFeedback', feedbackEvent, { includeReplay: Boolean(includeReplay) });
5755

5856
const envelope = createEventEnvelope(feedbackEvent, dsn, client.getOptions()._metadata, client.getOptions().tunnel);
5957

packages/integrations/src/debug.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,6 @@ const _debugIntegration = ((options: DebugOptions = {}) => {
2323
// TODO v8: Remove this
2424
setupOnce() {}, // eslint-disable-line @typescript-eslint/no-empty-function
2525
setup(client) {
26-
if (!client.on) {
27-
return;
28-
}
29-
3026
client.on('beforeSendEvent', (event: Event, hint?: EventHint) => {
3127
if (_options.debugger) {
3228
// eslint-disable-next-line no-debugger

packages/node-experimental/src/sdk/api.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,7 @@ export function addBreadcrumb(breadcrumb: Breadcrumb, hint?: BreadcrumbHint): vo
134134

135135
if (finalBreadcrumb === null) return;
136136

137-
if (client.emit) {
138-
client.emit('beforeAddBreadcrumb', finalBreadcrumb, hint);
139-
}
137+
client.emit('beforeAddBreadcrumb', finalBreadcrumb, hint);
140138

141139
getIsolationScope().addBreadcrumb(finalBreadcrumb, maxBreadcrumbs);
142140
}

packages/node/src/integrations/spotlight.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,6 @@ function connectToSpotlight(client: Client, options: Required<SpotlightConnectio
6565

6666
let failedRequests = 0;
6767

68-
if (typeof client.on !== 'function') {
69-
logger.warn('[Spotlight] Cannot connect to spotlight due to missing method on SDK client (`client.on`)');
70-
return;
71-
}
72-
7368
client.on('beforeEnvelope', (envelope: Envelope) => {
7469
if (failedRequests > 3) {
7570
logger.warn('[Spotlight] Disabled Sentry -> Spotlight integration due to too many failed requests');

packages/node/test/integrations/spotlight.test.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -120,16 +120,6 @@ describe('Spotlight', () => {
120120
integration.setup!(client);
121121
expect(loggerSpy).toHaveBeenCalledWith(expect.stringContaining('Invalid sidecar URL: invalid-url'));
122122
});
123-
124-
it("the client doesn't support life cycle hooks", () => {
125-
const integration = spotlightIntegration({ sidecarUrl: 'http://mylocalhost:8969' });
126-
const clientWithoutHooks = { ...client };
127-
// @ts-expect-error - this is fine in tests
128-
delete client.on;
129-
// @ts-expect-error - this is fine in tests
130-
integration.setup(clientWithoutHooks);
131-
expect(loggerSpy).toHaveBeenCalledWith(expect.stringContaining(' missing method on SDK client (`client.on`)'));
132-
});
133123
});
134124

135125
it('warns if the NODE_ENV variable doesn\'t equal "development"', () => {

packages/opentelemetry-node/src/spanprocessor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ export class SentrySpanProcessor implements OtelSpanProcessor {
114114
const client = getClient();
115115

116116
const mutableOptions = { drop: false };
117-
client && client.emit && client?.emit('otelSpanEnd', otelSpan, mutableOptions);
117+
client && client.emit('otelSpanEnd', otelSpan, mutableOptions);
118118

119119
if (mutableOptions.drop) {
120120
clearSpan(otelSpanId);

packages/opentelemetry-node/test/propagator.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ describe('SentryPropagator', () => {
4545
getDsn: () => ({
4646
publicKey: 'abc',
4747
}),
48+
emit: () => {},
4849
};
4950
// @ts-expect-error Use mock client for unit tests
5051
// eslint-disable-next-line deprecation/deprecation

packages/opentelemetry/src/custom/transaction.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export function startTransaction(hub: HubInterface, transactionContext: Transact
1717
// Any sampling decision happens in OpenTelemetry's sampler
1818
transaction.initSpanRecorder(options._experiments && (options._experiments.maxSpans as number));
1919

20-
if (client && client.emit) {
20+
if (client) {
2121
client.emit('startTransaction', transaction);
2222
}
2323
return transaction;

packages/opentelemetry/test/propagator.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ describe('SentryPropagator', () => {
3939
getDsn: () => ({
4040
publicKey: 'abc',
4141
}),
42+
emit: () => {},
4243
};
4344

4445
// @ts-expect-error Use mock client for unit tests

packages/react/src/errorboundary.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ class ErrorBoundary extends React.Component<ErrorBoundaryProps, ErrorBoundarySta
108108
this._openFallbackReportDialog = true;
109109

110110
const client = getClient();
111-
if (client && client.on && props.showDialog) {
111+
if (client && props.showDialog) {
112112
this._openFallbackReportDialog = false;
113113
client.on('afterSendEvent', event => {
114114
if (!event.type && this._lastEventId && event.event_id === this._lastEventId) {

packages/replay/src/coreHandlers/handleBreadcrumbs.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ type BreadcrumbWithCategory = Required<Pick<Breadcrumb, 'category'>>;
1616
export function handleBreadcrumbs(replay: ReplayContainer): void {
1717
const client = getClient();
1818

19-
if (!client || !client.on) {
19+
if (!client) {
2020
return;
2121
}
2222

packages/replay/src/coreHandlers/handleFetch.ts

Lines changed: 0 additions & 42 deletions
This file was deleted.

packages/replay/src/coreHandlers/handleGlobalEvent.ts

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,13 @@ import { DEBUG_BUILD } from '../debug-build';
55
import type { ReplayContainer } from '../types';
66
import { isErrorEvent, isFeedbackEvent, isReplayEvent, isTransactionEvent } from '../util/eventUtils';
77
import { isRrwebError } from '../util/isRrwebError';
8-
import { handleAfterSendEvent } from './handleAfterSendEvent';
98
import { addFeedbackBreadcrumb } from './util/addFeedbackBreadcrumb';
109
import { shouldSampleForBufferEvent } from './util/shouldSampleForBufferEvent';
1110

1211
/**
1312
* Returns a listener to be added to `addEventProcessor(listener)`.
1413
*/
15-
export function handleGlobalEventListener(
16-
replay: ReplayContainer,
17-
includeAfterSendEventHandling = false,
18-
): (event: Event, hint: EventHint) => Event | null {
19-
const afterSendHandler = includeAfterSendEventHandling ? handleAfterSendEvent(replay) : undefined;
20-
14+
export function handleGlobalEventListener(replay: ReplayContainer): (event: Event, hint: EventHint) => Event | null {
2115
return Object.assign(
2216
(event: Event, hint: EventHint) => {
2317
// Do nothing if replay has been disabled
@@ -73,13 +67,6 @@ export function handleGlobalEventListener(
7367
event.tags = { ...event.tags, replayId: replay.getSessionId() };
7468
}
7569

76-
// In cases where a custom client is used that does not support the new hooks (yet),
77-
// we manually call this hook method here
78-
if (afterSendHandler) {
79-
// Pretend the error had a 200 response so we always capture it
80-
afterSendHandler(event, { statusCode: 200 });
81-
}
82-
8370
return event;
8471
},
8572
{ id: 'Replay' },

packages/replay/src/coreHandlers/handleNetworkBreadcrumbs.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,10 @@ import type {
66
TextEncoderInternal,
77
XhrBreadcrumbData,
88
} from '@sentry/types';
9-
import { addFetchInstrumentationHandler, addXhrInstrumentationHandler, logger } from '@sentry/utils';
9+
import { logger } from '@sentry/utils';
1010

1111
import { DEBUG_BUILD } from '../debug-build';
1212
import type { FetchHint, ReplayContainer, ReplayNetworkOptions, XhrHint } from '../types';
13-
import { handleFetchSpanListener } from './handleFetch';
14-
import { handleXhrSpanListener } from './handleXhr';
1513
import { captureFetchBreadcrumbToReplay, enrichFetchBreadcrumb } from './util/fetchUtils';
1614
import { captureXhrBreadcrumbToReplay, enrichXhrBreadcrumb } from './util/xhrUtils';
1715

@@ -50,12 +48,8 @@ export function handleNetworkBreadcrumbs(replay: ReplayContainer): void {
5048
networkResponseHeaders,
5149
};
5250

53-
if (client && client.on) {
51+
if (client) {
5452
client.on('beforeAddBreadcrumb', (breadcrumb, hint) => beforeAddNetworkBreadcrumb(options, breadcrumb, hint));
55-
} else {
56-
// Fallback behavior
57-
addFetchInstrumentationHandler(handleFetchSpanListener(replay));
58-
addXhrInstrumentationHandler(handleXhrSpanListener(replay));
5953
}
6054
} catch {
6155
// Do nothing

0 commit comments

Comments
 (0)