-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathSentryLink.ts
136 lines (114 loc) · 3.46 KB
/
SentryLink.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import {
ApolloError,
ApolloLink,
FetchResult,
NextLink,
Operation,
ServerError,
} from '@apollo/client/core';
import type { SeverityLevel } from '@sentry/core';
import { Observable } from 'zen-observable-ts';
import { makeBreadcrumb } from './breadcrumb';
import { FullOptions, SentryLinkOptions, withDefaults } from './options';
import {
attachBreadcrumbToSentry,
setFingerprint,
setTransaction,
} from './sentry';
export class SentryLink extends ApolloLink {
private readonly options: FullOptions;
constructor(options: SentryLinkOptions = {}) {
super();
this.options = withDefaults(options);
}
request(
operation: Operation,
forward: NextLink,
): Observable<FetchResult> | null {
const { options } = this;
if (options.shouldHandleOperation && !options.shouldHandleOperation(operation)) {
return forward(operation);
}
if (options.setTransaction) {
setTransaction(operation);
}
if (options.setFingerprint) {
setFingerprint(operation);
}
const { attachBreadcrumbs } = options;
if (!attachBreadcrumbs) {
return forward(operation);
}
const breadcrumb = makeBreadcrumb(operation, options);
// While this could be done more simplistically by simply subscribing,
// wrapping the observer in our own observer ensures we get the results
// before they are passed along to other observers. This guarantees we
// get to run our instrumentation before others observers potentially
// throw and thus flush the results to Sentry.
return new Observable<FetchResult>((originalObserver) => {
const subscription = forward(operation).subscribe({
next: (result) => {
breadcrumb.level = severityForResult(result);
if (attachBreadcrumbs.includeFetchResult) {
breadcrumb.data.fetchResult = result;
}
if (
attachBreadcrumbs.includeError &&
result.errors &&
result.errors.length > 0
) {
breadcrumb.data.error = new ApolloError({
graphQLErrors: result.errors,
});
}
originalObserver.next(result);
},
complete: () => {
attachBreadcrumbToSentry(
operation,
breadcrumb,
options,
);
originalObserver.complete();
},
error: (error) => {
breadcrumb.level = 'error';
let scrubbedError;
if (isServerError(error)) {
const { result, response, ...rest } = error;
scrubbedError = rest;
if (attachBreadcrumbs.includeFetchResult) {
breadcrumb.data.fetchResult = result;
}
} else {
scrubbedError = error;
}
if (attachBreadcrumbs.includeError) {
breadcrumb.data.error = scrubbedError;
}
attachBreadcrumbToSentry(
operation,
breadcrumb,
options,
);
originalObserver.error(error);
},
});
return () => {
subscription.unsubscribe();
};
});
}
}
function isServerError(error: unknown): error is ServerError {
return (
typeof error === 'object' &&
error !== null &&
'response' in error &&
'result' in error &&
'statusCode' in error
);
}
function severityForResult(result: FetchResult): SeverityLevel {
return result.errors && result.errors.length > 0 ? 'error' : 'info';
}