/
utils.ts
81 lines (72 loc) · 3.06 KB
/
utils.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import type { DynamicSamplingContext, StackFrame, TraceparentData } from '@sentry/types';
import {
baggageHeaderToDynamicSamplingContext,
basename,
escapeStringForRegex,
extractTraceparentData,
GLOBAL_OBJ,
join,
} from '@sentry/utils';
import type { RequestEvent } from '@sveltejs/kit';
import { WRAPPED_MODULE_SUFFIX } from '../vite/autoInstrument';
import type { GlobalWithSentryValues } from '../vite/injectGlobalValues';
/**
* Takes a request event and extracts traceparent and DSC data
* from the `sentry-trace` and `baggage` DSC headers.
*/
export function getTracePropagationData(event: RequestEvent): {
traceparentData?: TraceparentData;
dynamicSamplingContext?: Partial<DynamicSamplingContext>;
} {
const sentryTraceHeader = event.request.headers.get('sentry-trace');
const baggageHeader = event.request.headers.get('baggage');
const traceparentData = sentryTraceHeader ? extractTraceparentData(sentryTraceHeader) : undefined;
const dynamicSamplingContext = baggageHeaderToDynamicSamplingContext(baggageHeader);
return { traceparentData, dynamicSamplingContext };
}
/**
* A custom iteratee function for the `RewriteFrames` integration.
*
* Does the same as the default iteratee, but also removes the `module` property from the
* frame to improve issue grouping.
*
* For some reason, our stack trace processing pipeline isn't able to resolve the bundled
* module name to the original file name correctly, leading to individual error groups for
* each module. Removing the `module` field makes the grouping algorithm fall back to the
* `filename` field, which is correctly resolved and hence grouping works as expected.
*/
export function rewriteFramesIteratee(frame: StackFrame): StackFrame {
if (!frame.filename) {
return frame;
}
const globalWithSentryValues: GlobalWithSentryValues = GLOBAL_OBJ;
const svelteKitBuildOutDir = globalWithSentryValues.__sentry_sveltekit_output_dir;
const prefix = 'app:///';
// Check if the frame filename begins with `/` or a Windows-style prefix such as `C:\`
const isWindowsFrame = /^[a-zA-Z]:\\/.test(frame.filename);
const startsWithSlash = /^\//.test(frame.filename);
if (isWindowsFrame || startsWithSlash) {
const filename = isWindowsFrame
? frame.filename
.replace(/^[a-zA-Z]:/, '') // remove Windows-style prefix
.replace(/\\/g, '/') // replace all `\\` instances with `/`
: frame.filename;
let strippedFilename;
if (svelteKitBuildOutDir) {
strippedFilename = filename.replace(
new RegExp(`^.*${escapeStringForRegex(join(svelteKitBuildOutDir, 'server'))}/`),
'',
);
} else {
strippedFilename = basename(filename);
}
frame.filename = `${prefix}${strippedFilename}`;
}
delete frame.module;
// In dev-mode, the WRAPPED_MODULE_SUFFIX is still present in the frame's file name.
// We need to remove it to make sure that the frame's filename matches the actual file
if (frame.filename.endsWith(WRAPPED_MODULE_SUFFIX)) {
frame.filename = frame.filename.slice(0, -WRAPPED_MODULE_SUFFIX.length);
}
return frame;
}