|
| 1 | +import type { EmailMessage, ExportedHandler } from '@cloudflare/workers-types'; |
| 2 | +import { |
| 3 | + captureException, |
| 4 | + SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, |
| 5 | + SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, |
| 6 | + startSpan, |
| 7 | + withIsolationScope, |
| 8 | +} from '@sentry/core'; |
| 9 | +import type { CloudflareOptions } from '../../client'; |
| 10 | +import { flushAndDispose } from '../../flush'; |
| 11 | +import { isInstrumented, markAsInstrumented } from '../../instrument'; |
| 12 | +import { getFinalOptions } from '../../options'; |
| 13 | +import { addCloudResourceContext } from '../../scope-utils'; |
| 14 | +import { init } from '../../sdk'; |
| 15 | +import { instrumentContext } from '../../utils/instrumentContext'; |
| 16 | + |
| 17 | +/** |
| 18 | + * Core email handler logic - wraps execution with Sentry instrumentation. |
| 19 | + */ |
| 20 | +function wrapEmailHandler( |
| 21 | + emailMessage: EmailMessage, |
| 22 | + options: CloudflareOptions, |
| 23 | + context: ExecutionContext, |
| 24 | + fn: () => unknown, |
| 25 | +): unknown { |
| 26 | + return withIsolationScope(isolationScope => { |
| 27 | + const waitUntil = context.waitUntil.bind(context); |
| 28 | + |
| 29 | + const client = init({ ...options, ctx: context }); |
| 30 | + isolationScope.setClient(client); |
| 31 | + |
| 32 | + addCloudResourceContext(isolationScope); |
| 33 | + |
| 34 | + return startSpan( |
| 35 | + { |
| 36 | + op: 'faas.email', |
| 37 | + name: `Handle Email ${emailMessage.to}`, |
| 38 | + attributes: { |
| 39 | + 'faas.trigger': 'email', |
| 40 | + [SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.faas.cloudflare.email', |
| 41 | + [SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'task', |
| 42 | + }, |
| 43 | + }, |
| 44 | + async () => { |
| 45 | + try { |
| 46 | + return await fn(); |
| 47 | + } catch (e) { |
| 48 | + captureException(e, { mechanism: { handled: false, type: 'auto.faas.cloudflare.email' } }); |
| 49 | + throw e; |
| 50 | + } finally { |
| 51 | + waitUntil(flushAndDispose(client)); |
| 52 | + } |
| 53 | + }, |
| 54 | + ); |
| 55 | + }); |
| 56 | +} |
| 57 | + |
| 58 | +/** |
| 59 | + * Instruments an email handler for ExportedHandler (env/ctx come from args). |
| 60 | + */ |
| 61 | +// eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 62 | +export function instrumentExportedHandlerEmail<T extends ExportedHandler<any, any, any>>( |
| 63 | + handler: T, |
| 64 | + optionsCallback: (env: Parameters<NonNullable<T['email']>>[1]) => CloudflareOptions | undefined, |
| 65 | +): void { |
| 66 | + if (!('email' in handler) || typeof handler.email !== 'function' || isInstrumented(handler.email)) { |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + handler.email = new Proxy(handler.email, { |
| 71 | + apply(target, thisArg, args: Parameters<NonNullable<T['email']>>) { |
| 72 | + const [emailMessage, env, ctx] = args; |
| 73 | + const context = instrumentContext(ctx); |
| 74 | + args[2] = context; |
| 75 | + |
| 76 | + const options = getFinalOptions(optionsCallback(env), env); |
| 77 | + |
| 78 | + return wrapEmailHandler(emailMessage, options, context, () => target.apply(thisArg, args)); |
| 79 | + }, |
| 80 | + }); |
| 81 | + |
| 82 | + markAsInstrumented(handler.email); |
| 83 | +} |
0 commit comments