Skip to content

Commit e8fb9cd

Browse files
committed
Move HTTPClient integration to @sentry/integrations.
1 parent f346e2a commit e8fb9cd

File tree

6 files changed

+52
-31
lines changed

6 files changed

+52
-31
lines changed

packages/browser/src/exports.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,4 @@ export {
5858
winjsStackLineParser,
5959
} from './stack-parsers';
6060
export { defaultIntegrations, forceLoad, init, lastEventId, onLoad, showReportDialog, flush, close, wrap } from './sdk';
61-
export { GlobalHandlers, TryCatch, Breadcrumbs, LinkedErrors, HttpContext, HttpClient, Dedupe } from './integrations';
61+
export { GlobalHandlers, TryCatch, Breadcrumbs, LinkedErrors, HttpContext, Dedupe } from './integrations';

packages/browser/src/integrations/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,3 @@ export { Breadcrumbs } from './breadcrumbs';
44
export { LinkedErrors } from './linkederrors';
55
export { HttpContext } from './httpcontext';
66
export { Dedupe } from './dedupe';
7-
export { HttpClient } from './httpclient';
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import * as Sentry from '@sentry/browser';
2+
import { HttpClient } from '@sentry/integrations';
23

34
window.Sentry = Sentry;
45

56
Sentry.init({
67
dsn: 'https://[email protected]/1337',
7-
integrations: [new Sentry.Integrations.HttpClient()],
8+
integrations: [new HttpClient()],
89
tracesSampleRate: 1,
910
sendDefaultPii: true,
1011
});

packages/browser/src/integrations/httpclient.ts renamed to packages/integrations/src/httpclient.ts

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
import { captureEvent, getCurrentHub } from '@sentry/core';
2-
import { Event as SentryEvent, Integration } from '@sentry/types';
1+
import { Event as SentryEvent, EventProcessor, Hub, Integration } from '@sentry/types';
32
import { addExceptionMechanism, fill, GLOBAL_OBJ, logger, supportsNativeFetch } from '@sentry/utils';
43

5-
import { eventFromUnknownInput } from '../eventbuilder';
6-
74
export type HttpStatusCodeRange = [number, number] | number;
85
export type HttpRequestTarget = string | RegExp;
96
interface HttpClientOptions {
@@ -41,6 +38,11 @@ export class HttpClient implements Integration {
4138

4239
private readonly _options: HttpClientOptions;
4340

41+
/**
42+
* Returns current hub.
43+
*/
44+
private _getCurrentHub?: () => Hub;
45+
4446
/**
4547
* @inheritDoc
4648
*
@@ -59,7 +61,8 @@ export class HttpClient implements Integration {
5961
*
6062
* @param options
6163
*/
62-
public setupOnce(): void {
64+
public setupOnce(_: (callback: EventProcessor) => void, getCurrentHub: () => Hub): void {
65+
this._getCurrentHub = getCurrentHub;
6366
this._wrapFetch();
6467
this._wrapXHR();
6568
}
@@ -72,12 +75,13 @@ export class HttpClient implements Integration {
7275
* @param requestInit The request init object
7376
*/
7477
private _fetchResponseHandler(requestInfo: RequestInfo, response: Response, requestInit?: RequestInit): void {
75-
if (this._shouldCaptureResponse(response.status, response.url)) {
78+
if (this._getCurrentHub && this._shouldCaptureResponse(response.status, response.url)) {
7679
const request = new Request(requestInfo, requestInit);
80+
const hub = this._getCurrentHub();
7781

7882
let requestHeaders, responseHeaders, requestCookies, responseCookies;
7983

80-
if (getCurrentHub().shouldSendDefaultPii()) {
84+
if (hub.shouldSendDefaultPii()) {
8185
[{ headers: requestHeaders, cookies: requestCookies }, { headers: responseHeaders, cookies: responseCookies }] =
8286
[
8387
{ cookieHeader: 'Cookie', obj: request },
@@ -113,7 +117,7 @@ export class HttpClient implements Integration {
113117
responseCookies,
114118
});
115119

116-
captureEvent(event);
120+
hub.captureEvent(event);
117121
}
118122
}
119123

@@ -125,10 +129,11 @@ export class HttpClient implements Integration {
125129
* @param headers The HTTP headers
126130
*/
127131
private _xhrResponseHandler(xhr: XMLHttpRequest, method: string, headers: Record<string, string>): void {
128-
if (this._shouldCaptureResponse(xhr.status, xhr.responseURL)) {
132+
if (this._getCurrentHub && this._shouldCaptureResponse(xhr.status, xhr.responseURL)) {
129133
let requestHeaders, responseCookies, responseHeaders;
134+
const hub = this._getCurrentHub();
130135

131-
if (getCurrentHub().shouldSendDefaultPii()) {
136+
if (hub.shouldSendDefaultPii()) {
132137
try {
133138
const cookieString = xhr.getResponseHeader('Set-Cookie') || xhr.getResponseHeader('set-cookie') || undefined;
134139

@@ -158,7 +163,7 @@ export class HttpClient implements Integration {
158163
responseCookies,
159164
});
160165

161-
captureEvent(event);
166+
hub.captureEvent(event);
162167
}
163168
}
164169

@@ -362,7 +367,7 @@ export class HttpClient implements Integration {
362367
* @param url url to verify
363368
*/
364369
private _isSentryRequest(url: string): boolean {
365-
const client = getCurrentHub().getClient();
370+
const client = this._getCurrentHub && this._getCurrentHub().getClient();
366371

367372
if (!client) {
368373
return false;
@@ -397,22 +402,31 @@ export class HttpClient implements Integration {
397402
requestHeaders?: Record<string, string>;
398403
requestCookies?: Record<string, string>;
399404
}): SentryEvent {
400-
const event = eventFromUnknownInput(() => [], `HTTP Client Error with status code: ${data.status}`);
401-
402-
event.request = {
403-
url: data.url,
404-
method: data.method,
405-
headers: data.requestHeaders,
406-
cookies: data.requestCookies,
407-
};
408-
409-
event.contexts = {
410-
...event.contexts,
411-
response: {
412-
status_code: data.status,
413-
headers: data.responseHeaders,
414-
cookies: data.responseCookies,
415-
body_size: this._getResponseSizeFromHeaders(data.responseHeaders),
405+
const message = `HTTP Client Error with status code: ${data.status}`;
406+
407+
const event: SentryEvent = {
408+
message,
409+
exception: {
410+
values: [
411+
{
412+
type: 'Error',
413+
value: message,
414+
},
415+
],
416+
},
417+
request: {
418+
url: data.url,
419+
method: data.method,
420+
headers: data.requestHeaders,
421+
cookies: data.requestCookies,
422+
},
423+
contexts: {
424+
response: {
425+
status_code: data.status,
426+
headers: data.responseHeaders,
427+
cookies: data.responseCookies,
428+
body_size: this._getResponseSizeFromHeaders(data.responseHeaders),
429+
},
416430
},
417431
};
418432

packages/integrations/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ export { ReportingObserver } from './reportingobserver';
77
export { RewriteFrames } from './rewriteframes';
88
export { SessionTiming } from './sessiontiming';
99
export { Transaction } from './transaction';
10+
export { HttpClient } from './httpclient';

packages/types/src/hub.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,4 +227,10 @@ export interface Hub {
227227
* @param endSession If set the session will be marked as exited and removed from the scope
228228
*/
229229
captureSession(endSession?: boolean): void;
230+
231+
/**
232+
* Returns if default PII should be sent to Sentry and propagated in ourgoing requests
233+
* when Tracing is used.
234+
*/
235+
shouldSendDefaultPii(): boolean;
230236
}

0 commit comments

Comments
 (0)