GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — feat(cloudflare): Add `withSentry` method by AbhiPrasad · Pull Request #13025 · getsentry/sentry-javascript

Conversation

@AbhiPrasad

@AbhiPrasad AbhiPrasad commented Jul 23, 2024

Copy link
Copy Markdown
Contributor

ref #12620

before reviewing this PR, I recommend reading through a writeup I did: #13007

This PR adds withSentry, a method that wraps your cloudflare worker handler to add Sentry instrumentation. The writeup above explains why we need to do this over just a regular Sentry.init call.

The implementation of withSentry is fairly straightforward, wrapping the fetch handler in the cloudflare worker with:

  1. withIsolationScope to isolate it from other concurrent requests
  2. helpers to update scope with relevant contexts/request
  3. continueTrace to continue distributed tracing
  4. startSpan to track spans

Usage looks something like so:

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

export default withSentry(
  (env) => ({
    dsn: env.SENTRY_DSN,
    tracesSampleRate: 1.0,
  }),
  {
    async fetch(request, env, ctx) {
      return new Response('Hello World!');
    },
  } satisfies ExportedHandler<Env>,
);

image

image

Next step here is to add more robust e2e tests, and then release an initial version!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@AbhiPrasad AbhiPrasad added this to the Sentry Cloudflare Workers SDK milestone Jul 23, 2024
@AbhiPrasad
AbhiPrasad requested a review from a team July 23, 2024 22:12
@AbhiPrasad AbhiPrasad self-assigned this Jul 23, 2024
@AbhiPrasad
AbhiPrasad requested review from andreiborza and lforst and removed request for a team July 23, 2024 22:12
@AbhiPrasad AbhiPrasad mentioned this pull request Jul 23, 2024
Sentry Cloudflare Workers SDK #12620
Closed
@github-actions

github-actions Bot commented Jul 23, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

size-limit report 📦

Path Size
@sentry/browser 22.45 KB (+0.06% 🔺)
@sentry/browser (incl. Tracing) 34.22 KB (+0.02% 🔺)
@sentry/browser (incl. Tracing, Replay) 70.26 KB (+0.02% 🔺)
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 63.59 KB (+0.02% 🔺)
@sentry/browser (incl. Tracing, Replay with Canvas) 74.66 KB (+0.02% 🔺)
@sentry/browser (incl. Tracing, Replay, Feedback) 87.24 KB (+0.02% 🔺)
@sentry/browser (incl. Tracing, Replay, Feedback, metrics) 89.08 KB (+0.02% 🔺)
@sentry/browser (incl. metrics) 26.75 KB (+0.06% 🔺)
@sentry/browser (incl. Feedback) 39.37 KB (+0.03% 🔺)
@sentry/browser (incl. sendFeedback) 27.06 KB (+0.04% 🔺)
@sentry/browser (incl. FeedbackAsync) 31.7 KB (+0.04% 🔺)
@sentry/react 25.22 KB (+0.05% 🔺)
@sentry/react (incl. Tracing) 37.22 KB (+0.03% 🔺)
@sentry/vue 26.6 KB (+0.05% 🔺)
@sentry/vue (incl. Tracing) 36.06 KB (+0.04% 🔺)
@sentry/svelte 22.58 KB (+0.05% 🔺)
CDN Bundle 23.64 KB (+0.14% 🔺)
CDN Bundle (incl. Tracing) 35.88 KB (+0.03% 🔺)
CDN Bundle (incl. Tracing, Replay) 70.26 KB (+0.02% 🔺)
CDN Bundle (incl. Tracing, Replay, Feedback) 75.53 KB (+0.02% 🔺)
CDN Bundle - uncompressed 69.37 KB (+0.07% 🔺)
CDN Bundle (incl. Tracing) - uncompressed 106.31 KB (+0.02% 🔺)
CDN Bundle (incl. Tracing, Replay) - uncompressed 217.95 KB (+0.01% 🔺)
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 230.78 KB (+0.01% 🔺)
@sentry/nextjs (client) 37.08 KB (+0.02% 🔺)
@sentry/sveltekit (client) 34.81 KB (+0.03% 🔺)
@sentry/node 111.9 KB (0%)
@sentry/node - without tracing 89.33 KB (-0.01% 🔽)
@sentry/aws-serverless 98.49 KB (0%)
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@AbhiPrasad
AbhiPrasad requested a review from mydea July 24, 2024 13:44
andreiborza
andreiborza reviewed Jul 25, 2024
View reviewed changes
Comment thread packages/cloudflare/README.md Outdated
npm install @sentry/cloudflare
```

Then set either the `nodejs_compat` or `nodejs_als` compatibility flags in your `wrangler.toml`:

@andreiborza andreiborza Jul 25, 2024

Copy link
Copy Markdown
Member

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.

l: Could we add a sentence of why this is needed? I saw Because we require tracing support as top-of-mind priority, AsyncLocalStorage support is a requirement of the SDK. in the issue you made.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

AbhiPrasad reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

@AbhiPrasad AbhiPrasad Jul 29, 2024

Copy link
Copy Markdown
Contributor Author

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.

done in 5671b7b

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread packages/cloudflare/README.md
Comment on lines +77 to +98
// Set user information, as well as tags and further extras
Sentry.setExtra('battery', 0.7);
Sentry.setTag('user_mode', 'admin');
Sentry.setUser({ id: '4711' });

// Add a breadcrumb for future events
Sentry.addBreadcrumb({
message: 'My Breadcrumb',
// ...
});

// Capture exceptions, messages or manual events
Sentry.captureMessage('Hello, world!');
Sentry.captureException(new Error('Good bye'));
Sentry.captureEvent({
message: 'Manual',
stacktrace: [
// ...
],
});

@andreiborza andreiborza Jul 25, 2024

Copy link
Copy Markdown
Member

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.

q: From your issue:

This needs to be done because workers can change their bindings (env) without re-deploying the entire worker, so a top-level Sentry.init call would become stale.

Wouldn't these suffer from the same issue of going stale?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@AbhiPrasad AbhiPrasad Jul 29, 2024

Copy link
Copy Markdown
Contributor Author

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.

Nope, because under the hood they call getClient() which should always have the current client in the handler execution context, therefore will stay up-to-date.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread packages/cloudflare/src/handler.ts Outdated
setHttpStatus(span, res.status);
return res;
} catch (e) {
captureException(e, { mechanism: { handled: false } });

@andreiborza andreiborza Jul 25, 2024

Copy link
Copy Markdown
Member

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.

q: I've seen some sdks add additional keys to mechanism, e.g. type: 'bun', do we want that here too? why or why not?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@AbhiPrasad AbhiPrasad Jul 29, 2024

Copy link
Copy Markdown
Contributor Author

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.

As per the develop docs

Required unique identifier of this mechanism determining rendering and processing of the mechanism data.

In the Python SDK this is merely the name of the framework integration that produced the exception, while for native it is e.g. "minidump" or "applecrashreport".

Changed with 309c05a

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread packages/cloudflare/test/handler.test.ts Outdated
@@ -0,0 +1,300 @@
// Note: These tests run the handler in Node.js, which is has some differences to the cloudflare workers runtime.

@andreiborza andreiborza Jul 25, 2024

Copy link
Copy Markdown
Member

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.

Suggested change
// Note: These tests run the handler in Node.js, which is has some differences to the cloudflare workers runtime.
// Note: These tests run the handler in Node.js, which has some differences to the cloudflare workers runtime.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@AbhiPrasad AbhiPrasad Jul 29, 2024

Copy link
Copy Markdown
Contributor Author

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.

Changed with 61ad09f

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread packages/cloudflare/test/handler.test.ts Outdated
Comment on lines +72 to +83
const handler = {
async fetch(_request, _env, _context) {
expect(SentryCore.getClient() instanceof CloudflareClient).toBe(true);
return new Response('test');
},
} satisfies ExportedHandler;

const context = createMockExecutionContext();
const wrappedHandler = withSentry(() => ({}), handler);
await wrappedHandler.fetch(new Request('https://example.com'), MOCK_ENV, context);

expect.assertions(1);

@andreiborza andreiborza Jul 25, 2024

Copy link
Copy Markdown
Member

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.

m: I think a robuster way to write this is to create a vi.fn that gets called with SentryCore.getClient() and assert that it has been called with that.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

AbhiPrasad reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

@AbhiPrasad AbhiPrasad Jul 29, 2024

Copy link
Copy Markdown
Contributor Author

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.

Good call!

Changed with 61ad09f

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread packages/cloudflare/test/handler.test.ts
Comment on lines +88 to +106
const handler = {
async fetch(_request, _env, _context) {
SentryCore.captureMessage('test');
return new Response('test');
},
} satisfies ExportedHandler;

let sentryEvent: Event = {};
const wrappedHandler = withSentry(
(env: any) => ({
dsn: env.MOCK_DSN,
beforeSend(event) {
sentryEvent = event;
return null;
},
}),
handler,
);

@andreiborza andreiborza Jul 25, 2024

Copy link
Copy Markdown
Member

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.

l: can we extract this into a helper just call that in the next few tests?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@AbhiPrasad AbhiPrasad Jul 29, 2024

Copy link
Copy Markdown
Contributor Author

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.

I'll come back to take a look at this after this PR merges, good suggestion.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread packages/cloudflare/test/sdk.test.ts
import * as SentryCore from '@sentry/core';
import { init } from '../src/sdk';

describe('init', () => {

@andreiborza andreiborza Jul 25, 2024

Copy link
Copy Markdown
Member

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.

m: could we add a test for init returning a client?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@AbhiPrasad AbhiPrasad Jul 29, 2024

Copy link
Copy Markdown
Contributor Author

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.

Changed with 87b4fa1

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
chargome
chargome reviewed Jul 26, 2024
View reviewed changes
Comment thread packages/cloudflare/README.md
export default withSentry(
(env) => ({
dsn: env.SENTRY_DSN,
tracesSampleRate: 1.0,

@chargome chargome Jul 26, 2024

Copy link
Copy Markdown
Member

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.

Maybe add a comment explaining this value?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

AbhiPrasad reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

@AbhiPrasad AbhiPrasad Jul 29, 2024

Copy link
Copy Markdown
Contributor Author

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.

done in 5671b7b

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
andreiborza
andreiborza approved these changes Jul 29, 2024
View reviewed changes

@andreiborza andreiborza left a comment

Copy link
Copy Markdown
Member

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.

very nice!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

AbhiPrasad reacted with heart emoji
All reactions
  • ❤️ 1 reaction
AbhiPrasad added 5 commits July 29, 2024 11:03
@AbhiPrasad
feat(cloudflare): Add withSentry method
3e424c7
@AbhiPrasad
add extra readme context
e10cbe3
@AbhiPrasad
update capture exception mechanism
e95408c
@AbhiPrasad
test refactors
ed92ca7
@AbhiPrasad
test: Update init tests
e02e283
@AbhiPrasad
AbhiPrasad force-pushed the abhi-cloudflare-with-sentry branch from 87b4fa1 to e02e283 Compare July 29, 2024 15:04
@AbhiPrasad
AbhiPrasad enabled auto-merge (squash) July 29, 2024 15:26
@AbhiPrasad
AbhiPrasad merged commit d2ab51c into develop Jul 29, 2024
@AbhiPrasad
AbhiPrasad deleted the abhi-cloudflare-with-sentry branch July 29, 2024 15:31
@AbhiPrasad AbhiPrasad mentioned this pull request Jul 30, 2024
feat(cloudflare): Add plugin for cloudflare pages #13123
Merged
@sleidig sleidig mentioned this pull request Aug 21, 2024
[Snyk] Upgrade @sentry/angular from 8.20.0 to 8.21.0 Aam-Digital/ndb-core#2517
Closed
@q1blue q1blue mentioned this pull request Aug 22, 2024
[Snyk] Upgrade @sentry/react from 7.16.0 to 8.21.0 https-quantumblockchainai-atlassian-net/ossinsight#814
Open
@sleidig sleidig mentioned this pull request Aug 22, 2024
[Snyk] Upgrade @sentry/angular from 8.20.0 to 8.22.0 Aam-Digital/ndb-core#2519
Closed
@angeloreale angeloreale mentioned this pull request Aug 22, 2024
[Snyk] Upgrade @sentry/nextjs from 8.20.0 to 8.22.0 dupipcom/hypnos#32
Merged
@q1blue q1blue mentioned this pull request Aug 23, 2024
[Snyk] Upgrade @sentry/react from 7.16.0 to 8.22.0 https-quantumblockchainai-atlassian-net/ossinsight#816
Open
@gokulk16 gokulk16 mentioned this pull request Aug 23, 2024
[Snyk] Upgrade @sentry/browser from 8.20.0 to 8.22.0 gokulk16/type-to-calculate-web#33
Closed
@FengShangWuQi FengShangWuQi mentioned this pull request Aug 25, 2024
[Snyk] Upgrade @sentry/gatsby from 7.20.0 to 8.22.0 FengShangWuQi/fengshangwuqi.github.io#1916
Closed
@angeloreale angeloreale mentioned this pull request Aug 25, 2024
[Snyk] Upgrade @sentry/nextjs from 8.20.0 to 8.22.0 dupipcom/hypnos#46
Closed
@q1blue q1blue mentioned this pull request Aug 27, 2024
[Snyk] Upgrade @sentry/react from 7.16.0 to 8.23.0 https-quantumblockchainai-atlassian-net/ossinsight#820
Open
@gokulk16 gokulk16 mentioned this pull request Aug 27, 2024
[Snyk] Upgrade @sentry/browser from 8.20.0 to 8.23.0 gokulk16/type-to-calculate-web#35
Closed
@angeloreale angeloreale mentioned this pull request Aug 27, 2024
[Snyk] Upgrade @sentry/nextjs from 8.20.0 to 8.23.0 dupipcom/hygieia#1
Merged
@tomwwinter tomwwinter mentioned this pull request Aug 27, 2024
[Snyk] Upgrade @sentry/angular from 8.20.0 to 8.23.0 Aam-Digital/ndb-core#2537
Closed
@gokulk16 gokulk16 mentioned this pull request Aug 28, 2024
[Snyk] Upgrade @sentry/browser from 8.20.0 to 8.24.0 gokulk16/type-to-calculate-web#36
Closed
@angeloreale angeloreale mentioned this pull request Aug 28, 2024
[Snyk] Upgrade @sentry/nextjs from 8.20.0 to 8.24.0 dupipcom/hygieia#3
Open
@q1blue q1blue mentioned this pull request Aug 28, 2024
[Snyk] Upgrade @sentry/react from 7.16.0 to 8.24.0 https-quantumblockchainai-atlassian-net/ossinsight#833
Open
@angeloreale angeloreale mentioned this pull request Aug 29, 2024
[Snyk] Upgrade @sentry/nextjs from 8.20.0 to 8.24.0 dupipcom/hygieia#4
Open
@gokulk16 gokulk16 mentioned this pull request Aug 30, 2024
[Snyk] Upgrade @sentry/browser from 8.20.0 to 8.25.0 gokulk16/type-to-calculate-web#38
Merged
@angeloreale angeloreale mentioned this pull request Aug 30, 2024
[Snyk] Upgrade @sentry/nextjs from 8.20.0 to 8.25.0 dupipcom/hygieia#5
Open
@q1blue q1blue mentioned this pull request Aug 30, 2024
[Snyk] Upgrade @sentry/react from 7.16.0 to 8.25.0 https-quantumblockchainai-atlassian-net/ossinsight#835
Open
@FengShangWuQi FengShangWuQi mentioned this pull request Sep 1, 2024
[Snyk] Upgrade @sentry/gatsby from 7.20.0 to 8.25.0 FengShangWuQi/fengshangwuqi.github.io#1918
Closed
@PierreGode PierreGode mentioned this pull request Sep 4, 2024
[Snyk] Upgrade @sentry/node from 8.9.2 to 8.25.0 PierreGode/shields#1
Closed
@q1blue q1blue mentioned this pull request Sep 7, 2024
[Snyk] Upgrade @sentry/react from 7.16.0 to 8.26.0 https-quantumblockchainai-atlassian-net/ossinsight#846
Open
@FengShangWuQi FengShangWuQi mentioned this pull request Sep 8, 2024
[Snyk] Upgrade @sentry/gatsby from 7.20.0 to 8.26.0 FengShangWuQi/fengshangwuqi.github.io#1919
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@chargome chargome chargome left review comments
@andreiborza andreiborza andreiborza approved these changes
@lforst lforst Awaiting requested review from lforst lforst was automatically assigned from getsentry/team-javascript-sdks
@mydea mydea Awaiting requested review from mydea

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@AbhiPrasad AbhiPrasad

Labels

None yet

Projects

None yet

Milestone

Sentry Cloudflare Workers SDK

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@AbhiPrasad @chargome @andreiborza
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.