Skip to content

Commit

Permalink
Move HTTPClient integration to @sentry/integrations.
Browse files Browse the repository at this point in the history
  • Loading branch information
onurtemizkan committed Jan 4, 2023
1 parent f346e2a commit e8fb9cd
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 31 deletions.
2 changes: 1 addition & 1 deletion packages/browser/src/exports.ts
Expand Up @@ -58,4 +58,4 @@ export {
winjsStackLineParser,
} from './stack-parsers';
export { defaultIntegrations, forceLoad, init, lastEventId, onLoad, showReportDialog, flush, close, wrap } from './sdk';
export { GlobalHandlers, TryCatch, Breadcrumbs, LinkedErrors, HttpContext, HttpClient, Dedupe } from './integrations';
export { GlobalHandlers, TryCatch, Breadcrumbs, LinkedErrors, HttpContext, Dedupe } from './integrations';
1 change: 0 additions & 1 deletion packages/browser/src/integrations/index.ts
Expand Up @@ -4,4 +4,3 @@ export { Breadcrumbs } from './breadcrumbs';
export { LinkedErrors } from './linkederrors';
export { HttpContext } from './httpcontext';
export { Dedupe } from './dedupe';
export { HttpClient } from './httpclient';
@@ -1,10 +1,11 @@
import * as Sentry from '@sentry/browser';
import { HttpClient } from '@sentry/integrations';

window.Sentry = Sentry;

Sentry.init({
dsn: 'https://public@dsn.ingest.sentry.io/1337',
integrations: [new Sentry.Integrations.HttpClient()],
integrations: [new HttpClient()],
tracesSampleRate: 1,
sendDefaultPii: true,
});
@@ -1,9 +1,6 @@
import { captureEvent, getCurrentHub } from '@sentry/core';
import { Event as SentryEvent, Integration } from '@sentry/types';
import { Event as SentryEvent, EventProcessor, Hub, Integration } from '@sentry/types';
import { addExceptionMechanism, fill, GLOBAL_OBJ, logger, supportsNativeFetch } from '@sentry/utils';

import { eventFromUnknownInput } from '../eventbuilder';

export type HttpStatusCodeRange = [number, number] | number;
export type HttpRequestTarget = string | RegExp;
interface HttpClientOptions {
Expand Down Expand Up @@ -41,6 +38,11 @@ export class HttpClient implements Integration {

private readonly _options: HttpClientOptions;

/**
* Returns current hub.
*/
private _getCurrentHub?: () => Hub;

/**
* @inheritDoc
*
Expand All @@ -59,7 +61,8 @@ export class HttpClient implements Integration {
*
* @param options
*/
public setupOnce(): void {
public setupOnce(_: (callback: EventProcessor) => void, getCurrentHub: () => Hub): void {
this._getCurrentHub = getCurrentHub;
this._wrapFetch();
this._wrapXHR();
}
Expand All @@ -72,12 +75,13 @@ export class HttpClient implements Integration {
* @param requestInit The request init object
*/
private _fetchResponseHandler(requestInfo: RequestInfo, response: Response, requestInit?: RequestInit): void {
if (this._shouldCaptureResponse(response.status, response.url)) {
if (this._getCurrentHub && this._shouldCaptureResponse(response.status, response.url)) {
const request = new Request(requestInfo, requestInit);
const hub = this._getCurrentHub();

let requestHeaders, responseHeaders, requestCookies, responseCookies;

if (getCurrentHub().shouldSendDefaultPii()) {
if (hub.shouldSendDefaultPii()) {
[{ headers: requestHeaders, cookies: requestCookies }, { headers: responseHeaders, cookies: responseCookies }] =
[
{ cookieHeader: 'Cookie', obj: request },
Expand Down Expand Up @@ -113,7 +117,7 @@ export class HttpClient implements Integration {
responseCookies,
});

captureEvent(event);
hub.captureEvent(event);
}
}

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

if (getCurrentHub().shouldSendDefaultPii()) {
if (hub.shouldSendDefaultPii()) {
try {
const cookieString = xhr.getResponseHeader('Set-Cookie') || xhr.getResponseHeader('set-cookie') || undefined;

Expand Down Expand Up @@ -158,7 +163,7 @@ export class HttpClient implements Integration {
responseCookies,
});

captureEvent(event);
hub.captureEvent(event);
}
}

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

if (!client) {
return false;
Expand Down Expand Up @@ -397,22 +402,31 @@ export class HttpClient implements Integration {
requestHeaders?: Record<string, string>;
requestCookies?: Record<string, string>;
}): SentryEvent {
const event = eventFromUnknownInput(() => [], `HTTP Client Error with status code: ${data.status}`);

event.request = {
url: data.url,
method: data.method,
headers: data.requestHeaders,
cookies: data.requestCookies,
};

event.contexts = {
...event.contexts,
response: {
status_code: data.status,
headers: data.responseHeaders,
cookies: data.responseCookies,
body_size: this._getResponseSizeFromHeaders(data.responseHeaders),
const message = `HTTP Client Error with status code: ${data.status}`;

const event: SentryEvent = {
message,
exception: {
values: [
{
type: 'Error',
value: message,
},
],
},
request: {
url: data.url,
method: data.method,
headers: data.requestHeaders,
cookies: data.requestCookies,
},
contexts: {
response: {
status_code: data.status,
headers: data.responseHeaders,
cookies: data.responseCookies,
body_size: this._getResponseSizeFromHeaders(data.responseHeaders),
},
},
};

Expand Down
1 change: 1 addition & 0 deletions packages/integrations/src/index.ts
Expand Up @@ -7,3 +7,4 @@ export { ReportingObserver } from './reportingobserver';
export { RewriteFrames } from './rewriteframes';
export { SessionTiming } from './sessiontiming';
export { Transaction } from './transaction';
export { HttpClient } from './httpclient';
6 changes: 6 additions & 0 deletions packages/types/src/hub.ts
Expand Up @@ -227,4 +227,10 @@ export interface Hub {
* @param endSession If set the session will be marked as exited and removed from the scope
*/
captureSession(endSession?: boolean): void;

/**
* Returns if default PII should be sent to Sentry and propagated in ourgoing requests
* when Tracing is used.
*/
shouldSendDefaultPii(): boolean;
}

0 comments on commit e8fb9cd

Please sign in to comment.