-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathwrapMethodWithSentry.ts
More file actions
171 lines (155 loc) · 5.55 KB
/
wrapMethodWithSentry.ts
File metadata and controls
171 lines (155 loc) · 5.55 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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import type { DurableObjectStorage } from '@cloudflare/workers-types';
import {
captureException,
getClient,
isThenable,
type Scope,
SEMANTIC_ATTRIBUTE_SENTRY_OP,
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
startSpan,
withIsolationScope,
withScope,
} from '@sentry/core';
import type { CloudflareOptions } from './client';
import { flushAndDispose } from './flush';
import { isInstrumented, markAsInstrumented } from './instrument';
import { init } from './sdk';
/** Extended DurableObjectState with originalStorage exposed by instrumentContext */
interface InstrumentedDurableObjectState extends DurableObjectState {
originalStorage?: DurableObjectStorage;
}
type MethodWrapperOptions = {
spanName?: string;
spanOp?: string;
options: CloudflareOptions;
context: ExecutionContext | DurableObjectState;
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type UncheckedMethod = (...args: any[]) => any;
type OriginalMethod = UncheckedMethod;
/**
* Wraps a method with Sentry tracing.
*
* @param wrapperOptions - The options for the wrapper.
* @param handler - The method to wrap.
* @param callback - The callback to call.
* @param noMark - Whether to mark the method as instrumented.
* @returns The wrapped method.
*/
export function wrapMethodWithSentry<T extends OriginalMethod>(
wrapperOptions: MethodWrapperOptions,
handler: T,
callback?: (...args: Parameters<T>) => void,
noMark?: true,
): T {
if (isInstrumented(handler)) {
return handler;
}
if (!noMark) {
markAsInstrumented(handler);
}
return new Proxy(handler, {
apply(target, thisArg, args: Parameters<T>) {
const currentClient = getClient();
// if a client is already set, use withScope, otherwise use withIsolationScope
const sentryWithScope = currentClient ? withScope : withIsolationScope;
const wrappedFunction = (scope: Scope): unknown => {
// In certain situations, the passed context can become undefined.
// For example, for Astro while prerendering pages at build time.
// see: https://github.com/getsentry/sentry-javascript/issues/13217
const context = wrapperOptions.context as InstrumentedDurableObjectState | undefined;
const waitUntil = context?.waitUntil?.bind?.(context);
let currentClient = scope.getClient();
// Check if client exists AND is still usable (transport not disposed)
// This handles the case where a previous handler disposed the client
// but the scope still holds a reference to it (e.g., alarm handlers in Durable Objects)
if (!currentClient || !currentClient.getTransport()) {
const client = init({ ...wrapperOptions.options, ctx: context as unknown as ExecutionContext | undefined });
scope.setClient(client);
currentClient = client;
}
const clientToDispose = currentClient;
if (!wrapperOptions.spanName) {
try {
if (callback) {
callback(...args);
}
const result = Reflect.apply(target, thisArg, args);
if (isThenable(result)) {
return result.then(
(res: unknown) => {
waitUntil?.(flushAndDispose(clientToDispose));
return res;
},
(e: unknown) => {
captureException(e, {
mechanism: {
type: 'auto.faas.cloudflare.durable_object',
handled: false,
},
});
waitUntil?.(flushAndDispose(clientToDispose));
throw e;
},
);
} else {
waitUntil?.(flushAndDispose(clientToDispose));
return result;
}
} catch (e) {
captureException(e, {
mechanism: {
type: 'auto.faas.cloudflare.durable_object',
handled: false,
},
});
waitUntil?.(flushAndDispose(clientToDispose));
throw e;
}
}
const attributes = wrapperOptions.spanOp
? {
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: wrapperOptions.spanOp,
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.faas.cloudflare.durable_object',
}
: {};
return startSpan({ name: wrapperOptions.spanName, attributes }, () => {
try {
const result = Reflect.apply(target, thisArg, args);
if (isThenable(result)) {
return result.then(
(res: unknown) => {
waitUntil?.(flushAndDispose(clientToDispose));
return res;
},
(e: unknown) => {
captureException(e, {
mechanism: {
type: 'auto.faas.cloudflare.durable_object',
handled: false,
},
});
waitUntil?.(flushAndDispose(clientToDispose));
throw e;
},
);
} else {
waitUntil?.(flushAndDispose(clientToDispose));
return result;
}
} catch (e) {
captureException(e, {
mechanism: {
type: 'auto.faas.cloudflare.durable_object',
handled: false,
},
});
waitUntil?.(flushAndDispose(clientToDispose));
throw e;
}
});
};
return sentryWithScope(wrappedFunction);
},
});
}