Skip to content

Commit 8387ed3

Browse files
committed
ref: Don't wrap xhr/fetch for sentry breadcrumbs
1 parent c6a2ec9 commit 8387ed3

File tree

2 files changed

+52
-51
lines changed

2 files changed

+52
-51
lines changed

packages/browser/src/client.ts

+30
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { DsnLike, Event, EventHint } from '@sentry/types';
33
import { getGlobalObject, logger } from '@sentry/utils';
44

55
import { BrowserBackend, BrowserOptions } from './backend';
6+
import { Breadcrumbs } from './integrations/index';
67
import { SDK_NAME, SDK_VERSION } from './version';
78

89
/**
@@ -69,6 +70,35 @@ export class BrowserClient extends BaseClient<BrowserBackend, BrowserOptions> {
6970
return super._prepareEvent(event, scope, hint);
7071
}
7172

73+
/**
74+
* @inheritDoc
75+
*/
76+
public captureEvent(event: Event, hint?: EventHint, scope?: Scope): string | undefined {
77+
let eventId: string | undefined = hint && hint.event_id;
78+
this._processing = true;
79+
80+
this._processEvent(event, hint, scope)
81+
.then(finalEvent => {
82+
// We need to check for finalEvent in case beforeSend returned null
83+
eventId = finalEvent && finalEvent.event_id;
84+
// We do this here to not parse any requests if they are outgoing to Sentry
85+
// We log breadcrumbs if the integration has them enabled
86+
if (finalEvent) {
87+
const integration = this.getIntegration(Breadcrumbs);
88+
if (integration) {
89+
integration.addSentryBreadcrumb(finalEvent);
90+
}
91+
}
92+
this._processing = false;
93+
})
94+
.then(null, reason => {
95+
logger.error(reason);
96+
this._processing = false;
97+
});
98+
99+
return eventId;
100+
}
101+
72102
/**
73103
* Show a report dialog to the user to send feedback to a specific event.
74104
*

packages/browser/src/integrations/breadcrumbs.ts

+22-51
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
1-
import { API, getCurrentHub } from '@sentry/core';
2-
import { Integration, Severity } from '@sentry/types';
1+
import { getCurrentHub } from '@sentry/core';
2+
import { Event, Integration, Severity } from '@sentry/types';
33
import {
44
addInstrumentationHandler,
55
getEventDescription,
66
getGlobalObject,
77
htmlTreeAsString,
8-
logger,
98
parseUrl,
109
safeJoin,
1110
} from '@sentry/utils';
1211

13-
import { BrowserClient } from '../client';
14-
1512
/**
1613
* @hidden
1714
*/
@@ -67,6 +64,26 @@ export class Breadcrumbs implements Integration {
6764
};
6865
}
6966

67+
/**
68+
* Create a breadcrumb of `sentry` from the events themselves
69+
*/
70+
public addSentryBreadcrumb(event: Event): void {
71+
if (!this._options.sentry) {
72+
return;
73+
}
74+
getCurrentHub().addBreadcrumb(
75+
{
76+
category: `sentry.${event.type === 'transaction' ? 'transaction' : 'event'}`,
77+
event_id: event.event_id,
78+
level: event.level,
79+
message: getEventDescription(event),
80+
},
81+
{
82+
event,
83+
},
84+
);
85+
}
86+
7087
/**
7188
* Creates breadcrumbs from console API calls
7289
*/
@@ -151,11 +168,6 @@ export class Breadcrumbs implements Integration {
151168

152169
return;
153170
}
154-
155-
// We only capture issued sentry requests
156-
if (this._options.sentry && handlerData.xhr.__sentry_own_request__) {
157-
addSentryBreadcrumb(handlerData.args[0]);
158-
}
159171
}
160172

161173
/**
@@ -167,24 +179,6 @@ export class Breadcrumbs implements Integration {
167179
return;
168180
}
169181

170-
const client = getCurrentHub().getClient<BrowserClient>();
171-
const dsn = client && client.getDsn();
172-
if (this._options.sentry && dsn) {
173-
const filterUrl = new API(dsn).getBaseApiEndpoint();
174-
// if Sentry key appears in URL, don't capture it as a request
175-
// but rather as our own 'sentry' type breadcrumb
176-
if (
177-
filterUrl &&
178-
handlerData.fetchData.url.indexOf(filterUrl) !== -1 &&
179-
handlerData.fetchData.method === 'POST' &&
180-
handlerData.args[1] &&
181-
handlerData.args[1].body
182-
) {
183-
addSentryBreadcrumb(handlerData.args[1].body);
184-
return;
185-
}
186-
}
187-
188182
if (handlerData.error) {
189183
getCurrentHub().addBreadcrumb(
190184
{
@@ -306,26 +300,3 @@ export class Breadcrumbs implements Integration {
306300
}
307301
}
308302
}
309-
310-
/**
311-
* Create a breadcrumb of `sentry` from the events themselves
312-
*/
313-
function addSentryBreadcrumb(serializedData: string): void {
314-
// There's always something that can go wrong with deserialization...
315-
try {
316-
const event = JSON.parse(serializedData);
317-
getCurrentHub().addBreadcrumb(
318-
{
319-
category: `sentry.${event.type === 'transaction' ? 'transaction' : 'event'}`,
320-
event_id: event.event_id,
321-
level: event.level || Severity.fromString('error'),
322-
message: getEventDescription(event),
323-
},
324-
{
325-
event,
326-
},
327-
);
328-
} catch (_oO) {
329-
logger.error('Error while adding sentry type breadcrumb');
330-
}
331-
}

0 commit comments

Comments
 (0)