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
feat(cloudflare): instrument scheduled handler#13114
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,21 @@ | ||
| import type { ExportedHandler, ExportedHandlerFetchHandler } from '@cloudflare/workers-types'; | ||
| import type { Options } from '@sentry/types'; | ||
| import type { | ||
| ExportedHandler, | ||
| ExportedHandlerFetchHandler, | ||
| ExportedHandlerScheduledHandler, | ||
| } from '@cloudflare/workers-types'; | ||
| import { | ||
| SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, | ||
| SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, | ||
| captureException, | ||
| flush, | ||
| startSpan, | ||
| withIsolationScope, | ||
| } from '@sentry/core'; | ||
| import { setAsyncLocalStorageAsyncContextStrategy } from './async'; | ||
| import type { CloudflareOptions } from './client'; | ||
| import { wrapRequestHandler } from './request'; | ||
| import { addCloudResourceContext } from './scope-utils'; | ||
| import { init } from './sdk'; | ||
| /** | ||
| * Extract environment generic from exported handler. | ||
| @@ -21,7 +35,7 @@ type ExtractEnv<P> = P extends ExportedHandler<infer Env> ? Env : never; | ||
| */ | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| export function withSentry<E extends ExportedHandler<any>>( | ||
| optionsCallback: (env: ExtractEnv<E>) => Options, | ||
| optionsCallback: (env: ExtractEnv<E>) => CloudflareOptions, | ||
| handler: E, | ||
| ): E { | ||
| setAsyncLocalStorageAsyncContextStrategy(); | ||
| @@ -40,5 +54,52 @@ export function withSentry<E extends ExportedHandler<any>>( | ||
| (handler.fetch as any).__SENTRY_INSTRUMENTED__ = true; | ||
| } | ||
| if ( | ||
| 'scheduled' in handler && | ||
| typeof handler.scheduled === 'function' && | ||
| // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any | ||
| !(handler.scheduled as any).__SENTRY_INSTRUMENTED__ | ||
| ) { | ||
| handler.scheduled = new Proxy(handler.scheduled, { | ||
| apply(target, thisArg, args: Parameters<ExportedHandlerScheduledHandler<ExtractEnv<E>>>) { | ||
| const [event, env, context] = args; | ||
| return withIsolationScope(isolationScope => { | ||
| const options = optionsCallback(env); | ||
| const client = init(options); | ||
| isolationScope.setClient(client); | ||
| addCloudResourceContext(isolationScope); | ||
| return startSpan( | ||
ContributorAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This duplication with the request handler is not ideal, but I'd rather refactor this later once I'm more confident in the API design of the | ||
| { | ||
| op: 'faas.cron', | ||
| name: `Scheduled Cron ${event.cron}`, | ||
| attributes: { | ||
| 'faas.cron': event.cron, | ||
| 'faas.time': new Date(event.scheduledTime).toISOString(), | ||
| 'faas.trigger': 'timer', | ||
ContributorAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These are from https://opentelemetry.io/docs/specs/semconv/faas/faas-spans | ||
| [SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.faas.cloudflare', | ||
| [SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'task', | ||
| }, | ||
| }, | ||
| async () => { | ||
| try { | ||
| return await (target.apply(thisArg, args) as ReturnType<typeof target>); | ||
| } catch (e) { | ||
| captureException(e, { mechanism: { handled: false, type: 'cloudflare' } }); | ||
| throw e; | ||
| } finally { | ||
| context.waitUntil(flush(2000)); | ||
Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why do we need to wait here? ContributorAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
| ||
| } | ||
| }, | ||
| ); | ||
| }); | ||
| }, | ||
| }); | ||
| // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any | ||
| (handler.scheduled as any).__SENTRY_INSTRUMENTED__ = true; | ||
| } | ||
| return handler; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| import type { IncomingRequestCfProperties } from '@cloudflare/workers-types'; | ||
| import type { Scope } from '@sentry/types'; | ||
| import { winterCGRequestToRequestData } from '@sentry/utils'; | ||
| /** | ||
| * Set cloud resource context on scope. | ||
| */ | ||
| export function addCloudResourceContext(scope: Scope): void { | ||
| scope.setContext('cloud_resource', { | ||
| 'cloud.provider': 'cloudflare', | ||
| }); | ||
| } | ||
| /** | ||
| * Set culture context on scope | ||
| */ | ||
| export function addCultureContext(scope: Scope, cf: IncomingRequestCfProperties): void { | ||
| scope.setContext('culture', { | ||
| timezone: cf.timezone, | ||
| }); | ||
| } | ||
| /** | ||
| * Set request data on scope | ||
| */ | ||
| export function addRequest(scope: Scope, request: Request): void { | ||
| scope.setSDKProcessingMetadata({ request: winterCGRequestToRequestData(request) }); | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just for my understanding, what do we need this isolation scope for here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To make sure scope bleed doesn't happen. If you define both a
scheduledhandler and afetchhandler, there's a chance that both happen at the same time, so we need to isolate accordingly.