forked from getsentry/sentry-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttpcontext.ts
45 lines (39 loc) · 1.35 KB
/
httpcontext.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import { addGlobalEventProcessor, getCurrentHub } from '@sentry/core';
import { Event, Integration } from '@sentry/types';
import { WINDOW } from '@sentry/utils';
/** HttpContext integration collects information about HTTP request headers */
export class HttpContext implements Integration {
/**
* @inheritDoc
*/
public static id: string = 'HttpContext';
/**
* @inheritDoc
*/
public name: string = HttpContext.id;
/**
* @inheritDoc
*/
public setupOnce(): void {
addGlobalEventProcessor((event: Event) => {
if (getCurrentHub().getIntegration(HttpContext)) {
// if none of the information we want exists, don't bother
if (!WINDOW.navigator && !WINDOW.location && !WINDOW.document) {
return event;
}
// grab as much info as exists and add it to the event
const url = (event.request && event.request.url) || (WINDOW.location && WINDOW.location.href);
const { referrer } = WINDOW.document || {};
const { userAgent } = WINDOW.navigator || {};
const headers = {
...(event.request && event.request.headers),
...(referrer && { Referer: referrer }),
...(userAgent && { 'User-Agent': userAgent }),
};
const request = { ...(url && { url }), headers };
return { ...event, request };
}
return event;
});
}
}