|
| 1 | +import * as Sentry from '@sentry/cloudflare'; |
| 2 | +import { DurableObject } from 'cloudflare:workers'; |
| 3 | + |
| 4 | +interface Env { |
| 5 | + SENTRY_DSN: string; |
| 6 | + MY_DURABLE_OBJECT: DurableObjectNamespace<MyDurableObjectBase>; |
| 7 | + SVC_ALPHA: DurableObjectNamespace<MyDurableObjectBase>; |
| 8 | + SVC_BETA: DurableObjectNamespace<MyDurableObjectBase>; |
| 9 | +} |
| 10 | + |
| 11 | +class MyDurableObjectBase extends DurableObject<Env> { |
| 12 | + async sayHello(name: string): Promise<string> { |
| 13 | + return `Hello, ${name}!`; |
| 14 | + } |
| 15 | + |
| 16 | + async alpha(): Promise<string> { |
| 17 | + return 'alpha'; |
| 18 | + } |
| 19 | + |
| 20 | + async beta(): Promise<string> { |
| 21 | + return 'beta'; |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +export const MyDurableObject = Sentry.instrumentDurableObjectWithSentry( |
| 26 | + (env: Env) => ({ |
| 27 | + dsn: env.SENTRY_DSN, |
| 28 | + traceLifecycle: 'static', |
| 29 | + tracesSampleRate: 1.0, |
| 30 | + }), |
| 31 | + MyDurableObjectBase, |
| 32 | +); |
| 33 | + |
| 34 | +export default Sentry.withSentry( |
| 35 | + (env: Env) => ({ |
| 36 | + dsn: env.SENTRY_DSN, |
| 37 | + traceLifecycle: 'static', |
| 38 | + tracesSampleRate: 1.0, |
| 39 | + // Both targets are written in a casing the bindings do not use, and the regex carries the `g` |
| 40 | + // flag, which makes `test()` stateful unless the SDK normalizes it away. |
| 41 | + rpcTracePropagationBindings: ['my_durable_object', /^svc_/g], |
| 42 | + }), |
| 43 | + { |
| 44 | + async fetch(request, env) { |
| 45 | + const url = new URL(request.url); |
| 46 | + |
| 47 | + if (url.pathname === '/rpc/all') { |
| 48 | + const results = [ |
| 49 | + await env.MY_DURABLE_OBJECT.get(env.MY_DURABLE_OBJECT.idFromName('test')).sayHello('World'), |
| 50 | + await env.SVC_ALPHA.get(env.SVC_ALPHA.idFromName('test')).alpha(), |
| 51 | + await env.SVC_BETA.get(env.SVC_BETA.idFromName('test')).beta(), |
| 52 | + ]; |
| 53 | + |
| 54 | + return new Response(results.join(',')); |
| 55 | + } |
| 56 | + |
| 57 | + return new Response('Not found', { status: 404 }); |
| 58 | + }, |
| 59 | + } satisfies ExportedHandler<Env>, |
| 60 | +); |
0 commit comments