Skip to content

Commit 523ede7

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

File tree

6 files changed

+50
-31
lines changed

6 files changed

+50
-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: 40 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,12 @@ 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);
7780

7881
let requestHeaders, responseHeaders, requestCookies, responseCookies;
7982

80-
if (getCurrentHub().shouldSendDefaultPii()) {
83+
if (this._getCurrentHub().shouldSendDefaultPii()) {
8184
[{ headers: requestHeaders, cookies: requestCookies }, { headers: responseHeaders, cookies: responseCookies }] =
8285
[
8386
{ cookieHeader: 'Cookie', obj: request },
@@ -113,7 +116,7 @@ export class HttpClient implements Integration {
113116
responseCookies,
114117
});
115118

116-
captureEvent(event);
119+
this._getCurrentHub().captureEvent(event);
117120
}
118121
}
119122

@@ -125,10 +128,10 @@ export class HttpClient implements Integration {
125128
* @param headers The HTTP headers
126129
*/
127130
private _xhrResponseHandler(xhr: XMLHttpRequest, method: string, headers: Record<string, string>): void {
128-
if (this._shouldCaptureResponse(xhr.status, xhr.responseURL)) {
131+
if (this._getCurrentHub && this._shouldCaptureResponse(xhr.status, xhr.responseURL)) {
129132
let requestHeaders, responseCookies, responseHeaders;
130133

131-
if (getCurrentHub().shouldSendDefaultPii()) {
134+
if (this._getCurrentHub().shouldSendDefaultPii()) {
132135
try {
133136
const cookieString = xhr.getResponseHeader('Set-Cookie') || xhr.getResponseHeader('set-cookie') || undefined;
134137

@@ -158,7 +161,7 @@ export class HttpClient implements Integration {
158161
responseCookies,
159162
});
160163

161-
captureEvent(event);
164+
this._getCurrentHub().captureEvent(event);
162165
}
163166
}
164167

@@ -362,7 +365,7 @@ export class HttpClient implements Integration {
362365
* @param url url to verify
363366
*/
364367
private _isSentryRequest(url: string): boolean {
365-
const client = getCurrentHub().getClient();
368+
const client = this._getCurrentHub && this._getCurrentHub().getClient();
366369

367370
if (!client) {
368371
return false;
@@ -397,22 +400,31 @@ export class HttpClient implements Integration {
397400
requestHeaders?: Record<string, string>;
398401
requestCookies?: Record<string, string>;
399402
}): 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),
403+
const message = `HTTP Client Error with status code: ${data.status}`;
404+
405+
const event: SentryEvent = {
406+
message,
407+
exception: {
408+
values: [
409+
{
410+
type: 'Error',
411+
value: message,
412+
},
413+
],
414+
},
415+
request: {
416+
url: data.url,
417+
method: data.method,
418+
headers: data.requestHeaders,
419+
cookies: data.requestCookies,
420+
},
421+
contexts: {
422+
response: {
423+
status_code: data.status,
424+
headers: data.responseHeaders,
425+
cookies: data.responseCookies,
426+
body_size: this._getResponseSizeFromHeaders(data.responseHeaders),
427+
},
416428
},
417429
};
418430

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)