-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Compatibility of @sentry/astro with Cloudflare Pages #13217
Comments
Hello, thanks for reaching out. Adding Cloudflare Pages support for Astro is on our roadmap and is actively being worked on. You can follow the progress in #12620. As for making the plugin work, have you already tried it? It's not maintained by us so a bit hard to verify 😅. Maybe my colleague @AbhiPrasad has a bit more insight into this. |
Thank you for coming back to me, @andreiborza. I tried the plugin, but it's not really compatible with the Astro middleware. The sentry plugin provided by cloudflare returns a response format that is not compatible with the response format that the astro middleware needs. But I tried Reproduction: https://stackblitz.com/edit/github-h3zdcy?file=src%2Fmiddleware.ts&on=stackblitz |
@AbhiPrasad seems like astro's middleware does not provide @mariusbolik this might be something to file with Astro as well. |
This should be passed through https://github.com/withastro/adapters/blob/cd4c0842aadc58defc67f4ccf6d6ef6f0401a9ac/packages/cloudflare/src/entrypoints/server.ts#L70 I guess it's called I'll be writing up a guide on this soon! |
Types are still a problem here, but this seems to work: const requestHandlerOptions = {
options: {
dsn: '',
tracesSampleRate: 1.0,
},
request: context.request,
context: (context.locals as any).runtime.ctx,
};
return Sentry.wrapRequestHandler(requestHandlerOptions, () => next()); Thanks @AbhiPrasad! |
@mariusbolik Thanks for the workaround! const sentry = defineMiddleware(async (context, next) => {
return wrapRequestHandler(
{
options: {
dsn: import.meta.env.SENTRY_DSN,
environment: import.meta.env.MODE,
// tracesSampleRate: 1.0,
},
request: context.request,
context: context.locals.runtime.ctx,
},
() => next()
)
}) This works great using However,
Would you happen to have a solution for that too? |
Ok, it looks like adding something like this does the trick: if (typeof context.locals.runtime.ctx?.waitUntil !== 'function') {
return next()
} |
Hmm |
It looks like Another weirdness is that |
We're still looking into this, thanks for the extra info. |
@Lms24 Speaking of Example: Astro.locals.runtime.ctx.waitUntil(
(async () => {
const db = drizzle(Astro.locals.runtime.env.DB, { logger: true })
await db.insert(usersTable).values(...)
.catch((err) => {
console.error(err)
Sentry.captureException(err) // never captured because there's no Sentry client
})
})()
)
Sentry.captureException(...) // works fine outside of waitUntil Logs:
What would be the best way to make the client available? Do I need to to construct a new one inside |
Hmm @AbhiPrasad any ideas? I'm not familiar enough with waitUntil unfortunately |
I would still appreciate any help here. @AbhiPrasad 🙏 Circling back to the original issue, I found out that export default defineConfig({
integrations: [
sentry({
dsn: process.env.SENTRY_DSN,
environment: import.meta.env.MODE,
tracesSampleRate: 1.0,
sourceMapsUploadOptions: {
project: 'myproject',
authToken: process.env.SENTRY_AUTH_TOKEN,
},
debug: true,
}),
],
output: 'hybrid',
adapter: cloudflare({
platformProxy: {
enabled: true,
},
imageService: 'passthrough',
}),
vite: {
ssr: {
external: [
'async_hooks',
'diagnostics_channel',
'events',
'module',
'node:child_process',
'node:fs',
'node:http',
'node:https',
'node:inspector',
'node:net',
'node:os',
'node:path',
'node:readline',
'node:stream',
'node:tls',
'node:util',
'node:worker_threads',
'node:zlib',
'path',
'url',
'util',
'worker_threads',
],
},
build: {
minify: false,
sourcemap: true,
},
},
}) However, I had to patch
This suggests that the Sentry code somehow thinks it's running in a browser and not on the server. Anyway, capturing traces and exceptions works, so proper CF support seems pretty close. |
Last but not least, you can also combine these two options:
Which doesn't involve any hacks at all. |
@mlafeldt thanks for the workarounds/discussion, we'll take a look at get back to you. |
How did you manage to setup App is built and deployed on cloudflare, but sentry doesn't seem to work this way. I've added a Any help is appreciated. |
@rodilo Your middleware looks fine to me. I'd double-check the setup locally using |
In the end it was |
So we do have https://docs.sentry.io/platforms/javascript/guides/cloudflare/frameworks/astro/, which is not ideal because there's a bunch of manual steps but it gets stuff working for you. We'll keep improving this, but closing this for now. |
@AbhiPrasad I tried following the documentation with no success. How are you supposed to get that middleware working? AFAIK, Astro Cloudflare adapter does not support functions. Do you have a working repo? |
@masylum this works for me: // eslint-disable-next-line import/no-extraneous-dependencies
import { wrapRequestHandler } from '@sentry/cloudflare';
import { defineMiddleware } from 'astro/middleware';
export const sentryMiddleware = defineMiddleware(async (context, next) =>
wrapRequestHandler(
{
options: {
dsn: import.meta.env.SENTRY_DSN,
environment: import.meta.env.PUBLIC_ENVIRONMENT,
},
// @ts-expect-error Type for request from Astro and Cloudflare differ slightly as not all cf properties are available in Astro's type
request: context.request,
context: (context.locals as any).runtime.ctx,
},
() => next()
)
); |
Awesome! I tried it and it works. Perhaps it would be worth updating that Sentry documentation since it's misleading. Next step: upload the source maps 😆 |
Problem Statement
I installed the
@sentry/astro
integration in my Astro Project:But on build I receive this error:
So I extendet my astro config:
But then I get the next error:
Solution Brainstorm
Cloudflare provides a sentry plugin: https://developers.cloudflare.com/pages/functions/plugins/sentry/
Is it possible that to use this plugin to make the Sentry/Astro integration compatible with CF Pages?
Maybe releated to: #12620
The text was updated successfully, but these errors were encountered: