Skip to content

Commit e72c047

Browse files
authored
ref(browser): Refactor sentry breadcrumb to use hook (#8892)
Noticed that this is a bit tightly coupled in the browser client, and could be simplified by using a hook.
1 parent e3dda4c commit e72c047

File tree

4 files changed

+39
-42
lines changed

4 files changed

+39
-42
lines changed

packages/browser/src/client.ts

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ import { createClientReportEnvelope, dsnToString, getSDKSource, logger } from '@
1515

1616
import { eventFromException, eventFromMessage } from './eventbuilder';
1717
import { WINDOW } from './helpers';
18-
import type { Breadcrumbs } from './integrations';
19-
import { BREADCRUMB_INTEGRATION_ID } from './integrations/breadcrumbs';
2018
import type { BrowserTransportOptions } from './transports/types';
2119
import { createUserFeedbackEnvelope } from './userfeedback';
2220

@@ -91,26 +89,6 @@ export class BrowserClient extends BaseClient<BrowserClientOptions> {
9189
return eventFromMessage(this._options.stackParser, message, level, hint, this._options.attachStacktrace);
9290
}
9391

94-
/**
95-
* @inheritDoc
96-
*/
97-
public sendEvent(event: Event, hint?: EventHint): void {
98-
// We only want to add the sentry event breadcrumb when the user has the breadcrumb integration installed and
99-
// activated its `sentry` option.
100-
// We also do not want to use the `Breadcrumbs` class here directly, because we do not want it to be included in
101-
// bundles, if it is not used by the SDK.
102-
// This all sadly is a bit ugly, but we currently don't have a "pre-send" hook on the integrations so we do it this
103-
// way for now.
104-
const breadcrumbIntegration = this.getIntegrationById(BREADCRUMB_INTEGRATION_ID) as Breadcrumbs | undefined;
105-
// We check for definedness of `addSentryBreadcrumb` in case users provided their own integration with id
106-
// "Breadcrumbs" that does not have this function.
107-
if (breadcrumbIntegration && breadcrumbIntegration.addSentryBreadcrumb) {
108-
breadcrumbIntegration.addSentryBreadcrumb(event);
109-
}
110-
111-
super.sendEvent(event, hint);
112-
}
113-
11492
/**
11593
* Sends user feedback to Sentry.
11694
*/

packages/browser/src/integrations/breadcrumbs.ts

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@ interface BreadcrumbsOptions {
4141
/** maxStringLength gets capped to prevent 100 breadcrumbs exceeding 1MB event payload size */
4242
const MAX_ALLOWED_STRING_LENGTH = 1024;
4343

44-
export const BREADCRUMB_INTEGRATION_ID = 'Breadcrumbs';
45-
4644
/**
4745
* Default Breadcrumbs instrumentations
4846
* TODO: Deprecated - with v6, this will be renamed to `Instrument`
@@ -51,7 +49,7 @@ export class Breadcrumbs implements Integration {
5149
/**
5250
* @inheritDoc
5351
*/
54-
public static id: string = BREADCRUMB_INTEGRATION_ID;
52+
public static id: string = 'Breadcrumbs';
5553

5654
/**
5755
* @inheritDoc
@@ -104,28 +102,30 @@ export class Breadcrumbs implements Integration {
104102
if (this.options.history) {
105103
addInstrumentationHandler('history', _historyBreadcrumb);
106104
}
107-
}
108-
109-
/**
110-
* Adds a breadcrumb for Sentry events or transactions if this option is enabled.
111-
*/
112-
public addSentryBreadcrumb(event: SentryEvent): void {
113105
if (this.options.sentry) {
114-
getCurrentHub().addBreadcrumb(
115-
{
116-
category: `sentry.${event.type === 'transaction' ? 'transaction' : 'event'}`,
117-
event_id: event.event_id,
118-
level: event.level,
119-
message: getEventDescription(event),
120-
},
121-
{
122-
event,
123-
},
124-
);
106+
const client = getCurrentHub().getClient();
107+
client && client.on && client.on('beforeSendEvent', addSentryBreadcrumb);
125108
}
126109
}
127110
}
128111

112+
/**
113+
* Adds a breadcrumb for Sentry events or transactions if this option is enabled.
114+
*/
115+
function addSentryBreadcrumb(event: SentryEvent): void {
116+
getCurrentHub().addBreadcrumb(
117+
{
118+
category: `sentry.${event.type === 'transaction' ? 'transaction' : 'event'}`,
119+
event_id: event.event_id,
120+
level: event.level,
121+
message: getEventDescription(event),
122+
},
123+
{
124+
event,
125+
},
126+
);
127+
}
128+
129129
/**
130130
* A HOC that creaes a function that creates breadcrumbs from DOM API calls.
131131
* This is a HOC so that we get access to dom options in the closure.

packages/core/src/baseclient.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,8 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
322322
* @inheritDoc
323323
*/
324324
public sendEvent(event: Event, hint: EventHint = {}): void {
325+
this.emit('beforeSendEvent', event, hint);
326+
325327
if (this._dsn) {
326328
let env = createEventEnvelope(event, this._dsn, this._options._metadata, this._options.tunnel);
327329

@@ -381,6 +383,9 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
381383
/** @inheritdoc */
382384
public on(hook: 'beforeEnvelope', callback: (envelope: Envelope) => void): void;
383385

386+
/** @inheritdoc */
387+
public on(hook: 'beforeSendEvent', callback: (event: Event, hint?: EventHint) => void): void;
388+
384389
/** @inheritdoc */
385390
public on(
386391
hook: 'afterSendEvent',
@@ -412,6 +417,9 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
412417
/** @inheritdoc */
413418
public emit(hook: 'beforeEnvelope', envelope: Envelope): void;
414419

420+
/** @inheritdoc */
421+
public emit(hook: 'beforeSendEvent', event: Event, hint?: EventHint): void;
422+
415423
/** @inheritdoc */
416424
public emit(hook: 'afterSendEvent', event: Event, sendResponse: TransportMakeRequestResponse | void): void;
417425

packages/types/src/client.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,11 @@ export interface Client<O extends ClientOptions = ClientOptions> {
176176
*/
177177
on?(hook: 'beforeEnvelope', callback: (envelope: Envelope) => void): void;
178178

179+
/**
180+
* Register a callback for before an event is sent.
181+
*/
182+
on?(hook: 'beforeSendEvent', callback: (event: Event, hint?: EventHint | void) => void): void;
183+
179184
/**
180185
* Register a callback for when an event has been sent.
181186
*/
@@ -212,6 +217,12 @@ export interface Client<O extends ClientOptions = ClientOptions> {
212217
*/
213218
emit?(hook: 'beforeEnvelope', envelope: Envelope): void;
214219

220+
/*
221+
* Fire a hook event before sending an event. Expects to be given an Event & EventHint as the
222+
* second/third argument.
223+
*/
224+
emit?(hook: 'beforeSendEvent', event: Event, hint?: EventHint): void;
225+
215226
/*
216227
* Fire a hook event after sending an event. Expects to be given an Event as the
217228
* second argument.

0 commit comments

Comments
 (0)