|
| 1 | +/* eslint-disable @typescript-eslint/unbound-method */ |
| 2 | +import { |
| 3 | + captureException, |
| 4 | + flush, |
| 5 | + getClient, |
| 6 | + SEMANTIC_ATTRIBUTE_SENTRY_OP, |
| 7 | + SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, |
| 8 | + startSpan, |
| 9 | + withIsolationScope, |
| 10 | + withScope, |
| 11 | +} from '@sentry/core'; |
| 12 | +import type { DurableObject } from 'cloudflare:workers'; |
| 13 | +import { setAsyncLocalStorageAsyncContextStrategy } from './async'; |
| 14 | +import type { CloudflareOptions } from './client'; |
| 15 | +import { isInstrumented, markAsInstrumented } from './instrument'; |
| 16 | +import { wrapRequestHandler } from './request'; |
| 17 | +import { init } from './sdk'; |
| 18 | + |
| 19 | +type MethodWrapperOptions = { |
| 20 | + spanName?: string; |
| 21 | + spanOp?: string; |
| 22 | + options: CloudflareOptions; |
| 23 | + context: ExecutionContext | DurableObjectState; |
| 24 | +}; |
| 25 | + |
| 26 | +// eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 27 | +function wrapMethodWithSentry<T extends (...args: any[]) => any>( |
| 28 | + wrapperOptions: MethodWrapperOptions, |
| 29 | + handler: T, |
| 30 | + callback?: (...args: Parameters<T>) => void, |
| 31 | +): T { |
| 32 | + if (isInstrumented(handler)) { |
| 33 | + return handler; |
| 34 | + } |
| 35 | + |
| 36 | + markAsInstrumented(handler); |
| 37 | + |
| 38 | + return new Proxy(handler, { |
| 39 | + apply(target, thisArg, args: Parameters<T>) { |
| 40 | + const currentClient = getClient(); |
| 41 | + // if a client is already set, use withScope, otherwise use withIsolationScope |
| 42 | + const sentryWithScope = currentClient ? withScope : withIsolationScope; |
| 43 | + return sentryWithScope(async scope => { |
| 44 | + // In certain situations, the passed context can become undefined. |
| 45 | + // For example, for Astro while prerendering pages at build time. |
| 46 | + // see: https://github.com/getsentry/sentry-javascript/issues/13217 |
| 47 | + const context = wrapperOptions.context as ExecutionContext | undefined; |
| 48 | + |
| 49 | + const currentClient = scope.getClient(); |
| 50 | + if (!currentClient) { |
| 51 | + const client = init(wrapperOptions.options); |
| 52 | + scope.setClient(client); |
| 53 | + } |
| 54 | + |
| 55 | + if (!wrapperOptions.spanName) { |
| 56 | + try { |
| 57 | + if (callback) { |
| 58 | + callback(...args); |
| 59 | + } |
| 60 | + return await Reflect.apply(target, thisArg, args); |
| 61 | + } catch (e) { |
| 62 | + captureException(e, { |
| 63 | + mechanism: { |
| 64 | + type: 'cloudflare_durableobject', |
| 65 | + handled: false, |
| 66 | + }, |
| 67 | + }); |
| 68 | + throw e; |
| 69 | + } finally { |
| 70 | + context?.waitUntil(flush(2000)); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + const attributes = wrapperOptions.spanOp |
| 75 | + ? { |
| 76 | + [SEMANTIC_ATTRIBUTE_SENTRY_OP]: wrapperOptions.spanOp, |
| 77 | + [SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.faas.cloudflare_durableobjects', |
| 78 | + } |
| 79 | + : {}; |
| 80 | + |
| 81 | + // Only create these spans if they have a parent span. |
| 82 | + return startSpan({ name: wrapperOptions.spanName, attributes, onlyIfParent: true }, async () => { |
| 83 | + try { |
| 84 | + return await Reflect.apply(target, thisArg, args); |
| 85 | + } catch (e) { |
| 86 | + captureException(e, { |
| 87 | + mechanism: { |
| 88 | + type: 'cloudflare_durableobject', |
| 89 | + handled: false, |
| 90 | + }, |
| 91 | + }); |
| 92 | + throw e; |
| 93 | + } finally { |
| 94 | + context?.waitUntil(flush(2000)); |
| 95 | + } |
| 96 | + }); |
| 97 | + }); |
| 98 | + }, |
| 99 | + }); |
| 100 | +} |
| 101 | + |
| 102 | +/** |
| 103 | + * Instruments a Durable Object class to capture errors and performance data. |
| 104 | + * |
| 105 | + * Instruments the following methods: |
| 106 | + * - fetch |
| 107 | + * - alarm |
| 108 | + * - webSocketMessage |
| 109 | + * - webSocketClose |
| 110 | + * - webSocketError |
| 111 | + * |
| 112 | + * as well as any other public RPC methods on the Durable Object instance. |
| 113 | + * |
| 114 | + * @param optionsCallback Function that returns the options for the SDK initialization. |
| 115 | + * @param DurableObjectClass The Durable Object class to instrument. |
| 116 | + * @returns The instrumented Durable Object class. |
| 117 | + * |
| 118 | + * @example |
| 119 | + * ```ts |
| 120 | + * class MyDurableObjectBase extends DurableObject { |
| 121 | + * constructor(ctx: DurableObjectState, env: Env) { |
| 122 | + * super(ctx, env); |
| 123 | + * } |
| 124 | + * } |
| 125 | + * |
| 126 | + * export const MyDurableObject = instrumentDurableObjectWithSentry( |
| 127 | + * env => ({ |
| 128 | + * dsn: env.SENTRY_DSN, |
| 129 | + * tracesSampleRate: 1.0, |
| 130 | + * }), |
| 131 | + * MyDurableObjectBase, |
| 132 | + * ); |
| 133 | + * ``` |
| 134 | + */ |
| 135 | +export function instrumentDurableObjectWithSentry<E, T extends DurableObject<E>>( |
| 136 | + optionsCallback: (env: E) => CloudflareOptions, |
| 137 | + DurableObjectClass: new (state: DurableObjectState, env: E) => T, |
| 138 | +): new (state: DurableObjectState, env: E) => T { |
| 139 | + return new Proxy(DurableObjectClass, { |
| 140 | + construct(target, [context, env]) { |
| 141 | + setAsyncLocalStorageAsyncContextStrategy(); |
| 142 | + |
| 143 | + const options = optionsCallback(env); |
| 144 | + |
| 145 | + const obj = new target(context, env); |
| 146 | + |
| 147 | + // These are the methods that are available on a Durable Object |
| 148 | + // ref: https://developers.cloudflare.com/durable-objects/api/base/ |
| 149 | + // obj.alarm |
| 150 | + // obj.fetch |
| 151 | + // obj.webSocketError |
| 152 | + // obj.webSocketClose |
| 153 | + // obj.webSocketMessage |
| 154 | + |
| 155 | + // Any other public methods on the Durable Object instance are RPC calls. |
| 156 | + |
| 157 | + if (obj.fetch && typeof obj.fetch === 'function' && !isInstrumented(obj.fetch)) { |
| 158 | + obj.fetch = new Proxy(obj.fetch, { |
| 159 | + apply(target, thisArg, args) { |
| 160 | + return wrapRequestHandler({ options, request: args[0], context }, () => |
| 161 | + Reflect.apply(target, thisArg, args), |
| 162 | + ); |
| 163 | + }, |
| 164 | + }); |
| 165 | + |
| 166 | + markAsInstrumented(obj.fetch); |
| 167 | + } |
| 168 | + |
| 169 | + if (obj.alarm && typeof obj.alarm === 'function') { |
| 170 | + obj.alarm = wrapMethodWithSentry({ options, context, spanName: 'alarm' }, obj.alarm); |
| 171 | + } |
| 172 | + |
| 173 | + if (obj.webSocketMessage && typeof obj.webSocketMessage === 'function') { |
| 174 | + obj.webSocketMessage = wrapMethodWithSentry( |
| 175 | + { options, context, spanName: 'webSocketMessage' }, |
| 176 | + obj.webSocketMessage, |
| 177 | + ); |
| 178 | + } |
| 179 | + |
| 180 | + if (obj.webSocketClose && typeof obj.webSocketClose === 'function') { |
| 181 | + obj.webSocketClose = wrapMethodWithSentry({ options, context, spanName: 'webSocketClose' }, obj.webSocketClose); |
| 182 | + } |
| 183 | + |
| 184 | + if (obj.webSocketError && typeof obj.webSocketError === 'function') { |
| 185 | + obj.webSocketError = wrapMethodWithSentry( |
| 186 | + { options, context, spanName: 'webSocketError' }, |
| 187 | + obj.webSocketError, |
| 188 | + (_, error) => |
| 189 | + captureException(error, { |
| 190 | + mechanism: { |
| 191 | + type: 'cloudflare_durableobject_websocket', |
| 192 | + handled: false, |
| 193 | + }, |
| 194 | + }), |
| 195 | + ); |
| 196 | + } |
| 197 | + |
| 198 | + for (const method of Object.getOwnPropertyNames(obj)) { |
| 199 | + if ( |
| 200 | + method === 'fetch' || |
| 201 | + method === 'alarm' || |
| 202 | + method === 'webSocketError' || |
| 203 | + method === 'webSocketClose' || |
| 204 | + method === 'webSocketMessage' |
| 205 | + ) { |
| 206 | + continue; |
| 207 | + } |
| 208 | + |
| 209 | + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any |
| 210 | + const value = (obj as any)[method] as unknown; |
| 211 | + if (typeof value === 'function') { |
| 212 | + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any |
| 213 | + (obj as any)[method] = wrapMethodWithSentry( |
| 214 | + { options, context, spanName: method, spanOp: 'rpc' }, |
| 215 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 216 | + value as (...args: any[]) => any, |
| 217 | + ); |
| 218 | + } |
| 219 | + } |
| 220 | + |
| 221 | + return obj; |
| 222 | + }, |
| 223 | + }); |
| 224 | +} |
0 commit comments