-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathwrapPageComponentWithSentry.ts
90 lines (82 loc) · 2.88 KB
/
wrapPageComponentWithSentry.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
82
83
84
85
86
87
88
89
90
import { captureException, extractTraceparentData, getCurrentScope, withIsolationScope } from '@sentry/core';
interface FunctionComponent {
(...args: unknown[]): unknown;
}
interface ClassComponent {
new (...args: unknown[]): {
props?: unknown;
render(...args: unknown[]): unknown;
};
}
function isReactClassComponent(target: unknown): target is ClassComponent {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
return typeof target === 'function' && target?.prototype?.isReactComponent;
}
/**
* Wraps a page component with Sentry error instrumentation.
*/
export function wrapPageComponentWithSentry(pageComponent: FunctionComponent | ClassComponent): unknown {
if (isReactClassComponent(pageComponent)) {
return class SentryWrappedPageComponent extends pageComponent {
public render(...args: unknown[]): unknown {
return withIsolationScope(() => {
const scope = getCurrentScope();
// We extract the sentry trace data that is put in the component props by datafetcher wrappers
const sentryTraceData =
typeof this.props === 'object' &&
this.props !== null &&
'_sentryTraceData' in this.props &&
typeof this.props._sentryTraceData === 'string'
? this.props._sentryTraceData
: undefined;
if (sentryTraceData) {
const traceparentData = extractTraceparentData(sentryTraceData);
scope.setContext('trace', {
span_id: traceparentData?.parentSpanId,
trace_id: traceparentData?.traceId,
});
}
try {
return super.render(...args);
} catch (e) {
captureException(e, {
mechanism: {
handled: false,
},
});
throw e;
}
});
}
};
} else if (typeof pageComponent === 'function') {
return new Proxy(pageComponent, {
apply(target, thisArg, argArray: [{ _sentryTraceData?: string } | undefined]) {
return withIsolationScope(() => {
const scope = getCurrentScope();
// We extract the sentry trace data that is put in the component props by datafetcher wrappers
const sentryTraceData = argArray?.[0]?._sentryTraceData;
if (sentryTraceData) {
const traceparentData = extractTraceparentData(sentryTraceData);
scope.setContext('trace', {
span_id: traceparentData?.parentSpanId,
trace_id: traceparentData?.traceId,
});
}
try {
return target.apply(thisArg, argArray);
} catch (e) {
captureException(e, {
mechanism: {
handled: false,
},
});
throw e;
}
});
},
});
} else {
return pageComponent;
}
}