Skip to content
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

Sentry Cloudflare Workers SDK #12620

Closed
10 of 27 tasks
AbhiPrasad opened this issue Jun 24, 2024 · 22 comments
Closed
10 of 27 tasks

Sentry Cloudflare Workers SDK #12620

AbhiPrasad opened this issue Jun 24, 2024 · 22 comments
Assignees
Labels
Package: cloudflare Issues related to the Sentry Cloudflare Workers SDK

Comments

@AbhiPrasad
Copy link
Member

AbhiPrasad commented Jun 24, 2024

ref #2484
ref #5611

Let's add some 1st class cloudflare workers support (finally)!

We've had https://github.com/robertcepa/toucan-js for a while that has worked great, but doesn't match our unified API and doesn't have full support for performance, cron monitoring, and more.

First we can start off by making a basic package, @sentry/cloudflare-workers

Initial

Preview Give feedback
  1. Package: cloudflare
    AbhiPrasad
  2. AbhiPrasad
  3. AbhiPrasad
  4. AbhiPrasad
  5. AbhiPrasad

After First Release

Preview Give feedback
  1. Package: cloudflare
  2. AbhiPrasad
  3. Package: cloudflare
    AbhiPrasad

Once the SDK is in a good state we can think about better instrumentation for the different bindings that cloudflare workers exposes. Top of mind is:

Bindings support

Preview Give feedback
  1. Feature: Spans Package: cloudflare
    AbhiPrasad

At the same time we should make sure that we add support for our metaframeworks to use the Cloudflare workers SDK

Support metaframeworks

Preview Give feedback
  1. AbhiPrasad
  2. Package: cloudflare Package: remix Type: Improvement
  3. Package: sveltekit Type: Bug
  4. Package: nextjs Type: Bug
@AbhiPrasad AbhiPrasad self-assigned this Jun 24, 2024
@AbhiPrasad AbhiPrasad added the Package: cloudflare Issues related to the Sentry Cloudflare Workers SDK label Jun 24, 2024
@AbhiPrasad
Copy link
Member Author

AbhiPrasad commented Jun 24, 2024

Initial thinking of the general API.

We're probably only going to support ES modules syntax.

Current toucan requires you to always instantiate new Toucan. Ideally we can avoid this by exposing a helper that wraps export default obj from the entry point.

It would probably automatically try to read env.SENTRY_DSN to get the sentry dsn.

export default withSentry({ beforeSend }, {
  async fetch(request, env, ctx) {
    return new Response('Hello World!');
  },
});

For further customization, you can take a function withSentryHandler that isolates sentry to the specific handler you care about. This allows you to override how sentry is initialized for that specific handler without

export default withSentry({ tracesSampleRate: 1.0 }, {
  async fetch: withSentryFetchHandler((init) => (request, env, ctx) => {
    const client = init({ tracesSampleRate: 0.5, request, env, ctx });
    // do whatever you want with client
    return new Response('Hello World!');
  }),
});

With the withSentry API's you'll need to have compatibility_flags = [ "nodejs_als" ] on in your wrangler.toml, otherwise you risk request bleeding (we require AsyncLocalStorage to do isolation).

If you don't want any node_compat, we can't do the global withSentry magic function, so you'll need to manually create a client.

import * as Sentry from '@sentry/cloudflare-workers';

export default {
  async fetch(request, env, ctx) {
    const client = new Sentry.CloudflareWorkersClient();
    const scope = new Sentry.Scope();
    scope.setClient(client);

    try {
      handler();
      return new Response('Hello!');
    } catch (e) {
      scope.captureException(e);

      return new Response('Something went wrong! Team has been notified.', {
        status: 500,
      });
    }
  },
};

Unfortunately performance is not active here because the parent child relationship is wrong, so maybe we gate performance behind requiring compatibility_flags = [ "nodejs_als" ]. The same also applies for automatic instrumentation adding breadcrumbs, we risk attaching to the wrong request. All automatic instrumentation (like automatically instrumenting d1 or r2 bindings) might be turned off if we cannot access asynclocalstorage.

We also need to add support for Version metadata binding - I'm imagining this is easiest to expose as an init config where you tell us what environmental variable to access for this info, and we automatically update releases and such.

@AbhiPrasad
Copy link
Member Author

I'm throwing around some experiments in https://github.com/AbhiPrasad/sentry-cloudflare-test/tree/main/examples

@ChristianJacobsen
Copy link

Hey @AbhiPrasad,

by adding support for Cloudflare Workers, do you also plan to add support for Cloudflare Pages? I know CF are working on unifying the two platforms, but it's still "a bit out there" (at least according to their Discord) and there are some subtle differences to how they work.

Anyways, awesome that you are doing this! 😄 I've been looking forward to official support for a long time!

@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 3 Jun 26, 2024
@AbhiPrasad
Copy link
Member Author

by adding support for Cloudflare Workers, do you also plan to add support for Cloudflare Pages

Yes for sure, this is def part of it because this is how all the metaframework integrations work. Let me make it more clear in the tracking issue, and also add some example of API design for that too.

@F0rce
Copy link

F0rce commented Jun 27, 2024

[...] how all the Metaframework integrations work

Does this include Astro? 👀 I know there is already an @sentry/astro integration (which is great), but the Cloudflare adapter (and Astro in SSR mode) is still not supported with it.

Like everyone else said, im also super happy that this is being worked on :)

@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 3 Jun 27, 2024
@AbhiPrasad
Copy link
Member Author

Does this include Astro

Yes! At the beginning you might have to manually add @sentry/cloudflare-workers yourself (and wrap some methods accordingly), but we'll work toward it all being automatically setup.

@timfish
Copy link
Collaborator

timfish commented Jul 29, 2024

  • Add durable objects instrumentation

Durable objects run in a separate isolate with their own fetch function. Would you use withSentry here?

Cloudflare workers also has:

It would be great to have tracing work across all these boundaries too!

@AbhiPrasad
Copy link
Member Author

Durable objects run in a separate isolate with their own fetch function. Would you use withSentry here?

We need a different exported method.

It would be great to have tracing work across all these boundaries too!

Adding to the todo!

@KyGuy2002
Copy link
Contributor

Hey @KyGuy2002, we're currently on company-wide hackweek and thus on limited support. We'll take a look at this next week.

Hey, I'm sure you are very busy but let me know if you need anything else from me to help with this issue. Thanks!

@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 3 Sep 3, 2024
@andreiborza
Copy link
Member

Hey, sorry there hasn't been an update yet. We're having some people out of office, but this should get picked up soon/this week.

@KyGuy2002
Copy link
Contributor

No worries, thanks for the update 👍

@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 3 Sep 3, 2024
@getsantry getsantry bot removed the status in GitHub Issues with 👀 3 Sep 3, 2024
AbhiPrasad added a commit to getsentry/sentry that referenced this issue Sep 19, 2024
harshithadurai pushed a commit to getsentry/sentry that referenced this issue Sep 19, 2024
harshithadurai pushed a commit to getsentry/sentry that referenced this issue Sep 19, 2024
AbhiPrasad added a commit that referenced this issue Sep 20, 2024
Removes alpha note from cloudflare sdk readme.

ref #12620
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Package: cloudflare Issues related to the Sentry Cloudflare Workers SDK
Projects
Archived in project
Development

No branches or pull requests