Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1.8k
fix(cloudflare): Instrument Durable Object handlers installed as read-only properties#23759
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+229
−106
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
5 changes: 4 additions & 1 deletion
5 dev-packages/e2e-tests/test-applications/cloudflare-agent/package.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
170 changes: 170 additions & 0 deletions
170 packages/cloudflare/src/instrumentations/instrumentDurableObjectHandlers.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,170 @@ | ||
| /* eslint-disable @typescript-eslint/unbound-method */ | ||
| import { FUNCTION, WEBSOCKET } from '@sentry/conventions/op'; | ||
| import { captureException, debug } from '@sentry/core'; | ||
| import type { DurableObject } from 'cloudflare:workers'; | ||
| import type { CloudflareOptions } from '../client'; | ||
| import { DEBUG_BUILD } from '../debug-build'; | ||
| import { ensureInstrumented } from '../instrument'; | ||
| import { init } from '../sdk'; | ||
| import { wrapMethodWithSentry } from '../wrapMethodWithSentry'; | ||
| import { wrapRequestHandlerWithInit } from '../wrapRequestHandlerWithInit'; | ||
| /** | ||
| * The instrumented context of the Durable Object being wrapped. | ||
| * | ||
| * Kept as `any` for the same reason as in `durableobject.ts`: a concrete `DurableObjectState` here | ||
| * makes `tsc` relate its `SqlStorage` graph against the parameter union of `wrapMethodWithSentry`, | ||
| * which hangs the type build. | ||
| */ | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| type InstrumentedDurableObjectContext = any; | ||
| /** | ||
| * Instruments the built-in Durable Object handler methods on a constructed instance. | ||
| * | ||
| * These are the methods that are available on a Durable Object | ||
| * ref: https://developers.cloudflare.com/durable-objects/api/base/ | ||
| * - obj.alarm | ||
| * - obj.fetch | ||
| * - obj.webSocketError | ||
| * - obj.webSocketClose | ||
| * - obj.webSocketMessage | ||
| * | ||
| * Any other public methods on the Durable Object instance are RPC calls. | ||
| * | ||
| * @internal | ||
| */ | ||
| export function instrumentDurableObjectHandlers<E, T extends DurableObject<E>>( | ||
| obj: T, | ||
| options: CloudflareOptions, | ||
| context: InstrumentedDurableObjectContext, | ||
| ): void { | ||
| // Bind each built-in handler to this instance before wrapping. | ||
| // See https://github.com/getsentry/sentry-javascript/issues/22328 | ||
| if (obj.fetch && typeof obj.fetch === 'function') { | ||
| setInstanceHandler( | ||
| obj, | ||
| 'fetch', | ||
| ensureInstrumented( | ||
| obj.fetch.bind(obj), | ||
| original => | ||
| new Proxy(original, { | ||
| apply(target, thisArg, args) { | ||
| return wrapRequestHandlerWithInit( | ||
| { options, request: args[0], context }, | ||
| () => { | ||
| return Reflect.apply(target, thisArg, args); | ||
| }, | ||
| init, | ||
| ); | ||
| }, | ||
| }), | ||
| ), | ||
| ); | ||
| } | ||
| if (obj.alarm && typeof obj.alarm === 'function') { | ||
| // Alarms are independent invocations, so we start a new trace and link to the previous alarm | ||
| setInstanceHandler( | ||
| obj, | ||
| 'alarm', | ||
| wrapMethodWithSentry( | ||
| { | ||
| options, | ||
| context, | ||
| spanName: 'alarm', | ||
| spanOp: FUNCTION, | ||
| startNewTrace: true, | ||
| origin: 'auto.faas.cloudflare.durable_object', | ||
| }, | ||
| obj.alarm.bind(obj), | ||
| ), | ||
| ); | ||
| } | ||
| if (obj.webSocketMessage && typeof obj.webSocketMessage === 'function') { | ||
| setInstanceHandler( | ||
| obj, | ||
| 'webSocketMessage', | ||
| wrapMethodWithSentry( | ||
| { | ||
| options, | ||
| context, | ||
| spanName: 'webSocketMessage', | ||
| spanOp: WEBSOCKET, | ||
| origin: 'auto.faas.cloudflare.durable_object', | ||
| }, | ||
| obj.webSocketMessage.bind(obj), | ||
| ), | ||
| ); | ||
| } | ||
| if (obj.webSocketClose && typeof obj.webSocketClose === 'function') { | ||
| setInstanceHandler( | ||
| obj, | ||
| 'webSocketClose', | ||
| wrapMethodWithSentry( | ||
| { | ||
| options, | ||
| context, | ||
| spanName: 'webSocketClose', | ||
| spanOp: WEBSOCKET, | ||
| origin: 'auto.faas.cloudflare.durable_object', | ||
| }, | ||
| obj.webSocketClose.bind(obj), | ||
| ), | ||
| ); | ||
| } | ||
| if (obj.webSocketError && typeof obj.webSocketError === 'function') { | ||
| setInstanceHandler( | ||
| obj, | ||
| 'webSocketError', | ||
| wrapMethodWithSentry( | ||
| { | ||
| options, | ||
| context, | ||
| spanName: 'webSocketError', | ||
| spanOp: WEBSOCKET, | ||
| origin: 'auto.faas.cloudflare.durable_object', | ||
| }, | ||
| obj.webSocketError.bind(obj), | ||
| (_, error) => | ||
| captureException(error, { | ||
| mechanism: { | ||
| type: 'auto.faas.cloudflare.durable_object_websocket', | ||
| handled: false, | ||
| }, | ||
| }), | ||
| ), | ||
| ); | ||
| } | ||
| } | ||
| /** | ||
| * Installs an instrumented handler as an own property of the Durable Object instance. | ||
| * | ||
| * A plain assignment is not always possible. The `agents` package installs its handlers with | ||
| * `Object.defineProperty(instance, name, { value, configurable: true })`, and `defineProperty` | ||
| * leaves `writable` at `false`. Assigning to such a property throws a `TypeError` in strict mode, | ||
| * so a read-only property is redefined instead. When the property can be neither assigned nor | ||
| * redefined, the handler stays uninstrumented rather than breaking the object. | ||
| */ | ||
| function setInstanceHandler(obj: object, name: string, handler: unknown): void { | ||
| const descriptor = Object.getOwnPropertyDescriptor(obj, name); | ||
| try { | ||
| if (descriptor?.writable === false) { | ||
| Object.defineProperty(obj, name, { | ||
| value: handler, | ||
| writable: true, | ||
| enumerable: descriptor.enumerable, | ||
| configurable: descriptor.configurable, | ||
| }); | ||
| } else { | ||
| (obj as Record<string, unknown>)[name] = handler; | ||
| } | ||
| } catch (error) { | ||
| DEBUG_BUILD && debug.warn(`Failed to instrument Durable Object handler "${name}"`, error); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.