-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathhttpcontext.ts
More file actions
46 lines (41 loc) · 1.38 KB
/
httpcontext.ts
File metadata and controls
46 lines (41 loc) · 1.38 KB
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
46
import { defineIntegration, safeSetSpanJSONAttributes } from '@sentry/core';
import { getHttpRequestData, WINDOW } from '../helpers';
/**
* Collects information about HTTP request headers and
* attaches them to the event.
*/
export const httpContextIntegration = defineIntegration(() => {
return {
name: 'HttpContext',
preprocessEvent(event) {
// if none of the information we want exists, don't bother
if (!WINDOW.navigator && !WINDOW.location && !WINDOW.document) {
return;
}
const reqData = getHttpRequestData();
const headers = {
...reqData.headers,
...event.request?.headers,
};
event.request = {
...reqData,
...event.request,
headers,
};
},
processSegmentSpan(span) {
// if none of the information we want exists, don't bother
if (!WINDOW.navigator && !WINDOW.location && !WINDOW.document) {
return;
}
const reqData = getHttpRequestData();
safeSetSpanJSONAttributes(span, {
// Coerce empty string to undefined so the helper's nullish check drops it,
// rather than writing an empty `url.full` attribute onto the span.
'url.full': reqData.url || undefined,
'http.request.header.user_agent': reqData.headers['User-Agent'],
'http.request.header.referer': reqData.headers['Referer'],
});
},
};
});