Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

feat(cloudflare): instrument scheduled handler #13114

Merged
merged 3 commits into from
Aug 2, 2024

Conversation

AbhiPrasad
Copy link
Member

resolves #13112

This PR adds instrumentation for the scheduled handler in cloudflare workers. This is used for cron triggers.

I elected to not do automatic cron instrumentation for now, this is tracked by #13113. Instead I added manual instrumentation docs to the README, this will get copied to the sentry docs eventually.

ref #12620

@AbhiPrasad AbhiPrasad requested a review from a team July 30, 2024 15:06
@AbhiPrasad AbhiPrasad self-assigned this Jul 30, 2024
@AbhiPrasad AbhiPrasad requested review from mydea and nicohrubec and removed request for a team July 30, 2024 15:06
@AbhiPrasad AbhiPrasad force-pushed the abhi-scheduled-handler branch from 20adfb9 to 1482a37 Compare July 30, 2024 15:52
@AbhiPrasad AbhiPrasad marked this pull request as draft July 30, 2024 21:14
@AbhiPrasad
Copy link
Member Author

Drafting this PR for the time being because #13123 refactors a lot of this code, so this PR needs to be updated.

Hence let's wait for that to get merged in first, and then look at this one in more detail.

@AbhiPrasad AbhiPrasad force-pushed the abhi-scheduled-handler branch 3 times, most recently from 16ddfe8 to 6aa304f Compare July 31, 2024 15:56
@AbhiPrasad AbhiPrasad marked this pull request as ready for review July 31, 2024 15:57
@AbhiPrasad
Copy link
Member Author

This is now ready to review - I refactored this PR to pull in the refactors from #13123

attributes: {
'faas.cron': event.cron,
'faas.time': new Date(event.scheduledTime).toISOString(),
'faas.trigger': 'timer',
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


addCloudResourceContext(isolationScope);

return startSpan(
Copy link
Member Author

Choose a reason for hiding this comment

The 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 withIsolationScope callback.

Copy link
Contributor

@nicohrubec nicohrubec left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good from my pov

handler.scheduled = new Proxy(handler.scheduled, {
apply(target, thisArg, args: Parameters<ExportedHandlerScheduledHandler<ExtractEnv<E>>>) {
const [event, env, context] = args;
return withIsolationScope(isolationScope => {
Copy link
Contributor

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?

Copy link
Member Author

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 scheduled handler and a fetch handler, there's a chance that both happen at the same time, so we need to isolate accordingly.

captureException(e, { mechanism: { handled: false, type: 'cloudflare' } });
throw e;
} finally {
context.waitUntil(flush(2000));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we need to wait here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

waitUntil will execute keep the serverless function alive until we flush out all sentry requests, but it will ensure that this happens after a response is sent. Therefore flushing to sentry does not block sending a response back to whatever sent the request to the cloudflare worker.

@AbhiPrasad AbhiPrasad force-pushed the abhi-scheduled-handler branch from a0a3666 to 459be04 Compare August 2, 2024 14:25
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Instrument Scheduled Handler
2 participants