-
-
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
RemixJS - Cloudflare Server Adapter - Sentry Wrapper #5610
Comments
Hey, There is currently no support for cloudflare workers since the Sentry SDK does not officially support the cloudflare worker runtime. Backlogging for the team, but PRs welcome in the meantime! |
Related: #2484 |
Spent the morning on a workaround until we have proper WinterCG support. It’s a first pass, largely hacky, and relying on what we have in Toucan today. First, copy-paste this into import { type EntryContext, type HandleDocumentRequestFunction, type ServerBuild } from "@remix-run/cloudflare";
import { extractData, type AppData } from "@remix-run/react/dist/data";
import { isResponse } from "@remix-run/server-runtime/dist/responses";
import { type ActionFunction, type LoaderFunction } from "@remix-run/server-runtime/dist/routeModules";
import { type Exception, type WrappedFunction } from "@sentry/types";
import { fill, normalize } from "@sentry/utils";
import * as cookie from "cookie";
import type Toucan from "toucan-js";
import { type Options } from "toucan-js";
const extractResponseError = async (response: Response): Promise<unknown> => {
const responseData = (await extractData(response)) as unknown;
if (typeof responseData === "string") {
return responseData;
}
if (response.statusText) {
return response.statusText;
}
return responseData;
};
const captureRemixServerException = async (
sentry: Toucan,
error: unknown,
name: string,
request: Request
): Promise<void> => {
// Skip capturing if the thrown error is not a 5xx response
// https://remix.run/docs/en/v1/api/conventions#throwing-responses-in-loaders
if (isResponse(error) && error.status < 500) {
return;
}
const exception = isResponse(error) ? await extractResponseError(error) : error;
sentry.withScope((scope) => {
// Transformed & handled later in `beforeSend`
// Attempts to match https://github.com/getsentry/sentry-javascript/blob/b49082e9aa0f0c1d7ffff5116fdf0412269e0204/packages/remix/src/utils/instrumentServer.ts#L107
scope.setExtra("request", request);
scope.setExtra("exceptionMechanism", {
type: "instrument",
handled: true,
data: {
function: name,
},
});
sentry.captureException(exception);
});
};
const makeWrappedDocumentRequestFunction =
(sentry: Toucan) =>
(origDocumentRequestFunction: HandleDocumentRequestFunction): HandleDocumentRequestFunction => {
return async function (
this: unknown,
request: Request,
responseStatusCode: number,
responseHeaders: Headers,
context: EntryContext
): Promise<Response> {
let response: Response;
try {
// eslint-disable-next-line @babel/no-invalid-this -- We need to be able to pass `this` to the original function to wrap it correctly.
response = await origDocumentRequestFunction.call(this, request, responseStatusCode, responseHeaders, context);
} catch (error) {
await captureRemixServerException(sentry, error, "documentRequest", request);
throw error;
}
return response;
};
};
interface MakeWrappedDataFunction {
(sentry: Toucan, dataFunctionType: "action", origFunction: ActionFunction): ActionFunction;
(sentry: Toucan, dataFunctionType: "loader", origFunction: LoaderFunction): LoaderFunction;
}
const makeWrappedDataFunction: MakeWrappedDataFunction = (sentry: Toucan, dataFunctionType, origFunction) => {
return async function (this: unknown, args: Parameters<typeof origFunction>[0]): Promise<Response | AppData> {
let response: unknown;
try {
// eslint-disable-next-line @babel/no-invalid-this -- We need to be able to pass `this` to the original function to wrap it correctly.
response = (await origFunction.call(this, args)) as unknown;
} catch (error) {
await captureRemixServerException(sentry, error, dataFunctionType, args.request);
throw error;
}
return response;
};
};
const makeWrappedAction =
(sentry: Toucan) =>
(origAction: ActionFunction): ActionFunction => {
return makeWrappedDataFunction(sentry, "action", origAction);
};
const makeWrappedLoader =
(sentry: Toucan) =>
(origLoader: LoaderFunction): LoaderFunction => {
return makeWrappedDataFunction(sentry, "loader", origLoader);
};
export const beforeSend: Options["beforeSend"] = (event) => {
// Add data from the `request` extra to the event request object.
// Mostly adapted from https://github.com/getsentry/sentry-javascript/blob/b49082e9aa0f0c1d7ffff5116fdf0412269e0204/packages/node/src/requestdata.ts#L140
if (event.extra?.request) {
const request = event.extra.request as Request;
const headers = Object.fromEntries(request.headers.entries());
const cookieHeader = headers.cookie;
delete headers.cookie;
event.request = {
...event.request,
url: request.url,
method: request.method,
data: normalize(request.body) as unknown,
query_string: new URL(request.url).search.replace("?", ""),
cookies: (cookieHeader && cookie.parse(cookieHeader)) || {},
// Add more to the allowlist later if you'd like.
headers: { user_agent: headers["user-agent"] },
};
delete event.extra.request;
}
// Add the exception mechanism from extra to the event object.
// Adapted from https://github.com/getsentry/sentry-javascript/blob/b49082e9aa0f0c1d7ffff5116fdf0412269e0204/packages/utils/src/misc.ts#L93
if (event.extra?.exceptionMechanism) {
const firstException = event.exception?.values?.[0];
const newMechanism = event.extra?.exceptionMechanism as Exception["mechanism"];
if (firstException) {
const defaultMechanism = { type: "generic", handled: true };
const currentMechanism = firstException.mechanism;
firstException.mechanism = { ...defaultMechanism, ...currentMechanism, ...newMechanism };
if (newMechanism && "data" in newMechanism) {
const mergedData = { ...currentMechanism?.data, ...newMechanism?.data };
firstException.mechanism.data = mergedData;
}
}
delete event.extra.exceptionMechanism;
}
return event;
};
/**
* Instruments `remix` ServerBuild for performance tracing and error tracking.
*/
export const instrumentBuild = (sentry: Toucan, build: ServerBuild): ServerBuild => {
const routes: ServerBuild["routes"] = {};
const wrappedEntry = { ...build.entry, module: { ...build.entry.module } };
// Not keeping boolean flags like it's done for `requestHandler` functions,
// Because the build can change between build and runtime.
// So if there is a new `loader` or`action` or `documentRequest` after build.
// We should be able to wrap them, as they may not be wrapped before.
if (!(wrappedEntry.module.default as WrappedFunction).__sentry_original__) {
fill(wrappedEntry.module, "default", makeWrappedDocumentRequestFunction(sentry));
}
for (const [id, route] of Object.entries(build.routes)) {
const wrappedRoute = { ...route, module: { ...route.module } };
if (wrappedRoute.module.action && !(wrappedRoute.module.action as WrappedFunction).__sentry_original__) {
fill(wrappedRoute.module, "action", makeWrappedAction(sentry));
}
if (wrappedRoute.module.loader && !(wrappedRoute.module.loader as WrappedFunction).__sentry_original__) {
fill(wrappedRoute.module, "loader", makeWrappedLoader(sentry));
}
routes[id] = wrappedRoute;
}
return { ...build, routes, entry: wrappedEntry };
};
export default instrumentBuild; If you’re keeping score at home, this modifies Then modify import { createEventHandler } from "@remix-run/cloudflare-workers";
import * as build from "@remix-run/dev/server-build";
import Toucan from "toucan-js";
import instrumentBuild, { beforeSend } from "./instrumentBuild";
addEventListener("fetch", (event: FetchEvent) => {
const sentry = new Toucan({
dsn: SENTRY_DSN,
context: event,
beforeSend,
// `allowedHeaders` and `allowedSearchParams` are ignored, see `beforeSend` in `instrumentBuild.ts`
// allowedHeaders: ["user-agent"],
// allowedSearchParams: /(.*)/u,
});
createEventHandler({
build: instrumentBuild(sentry, build),
mode: process.env.NODE_ENV,
})(event);
}); Hope this helps anyone stuck on this one ^_^ I’ll try and patch it up a bit further to get metadata & tracing in. |
I finally got around to updating the above thanks to Toucan v3. Everything’s in this gist, but essentially you need to:
One major caveat is that server-side timing will be extremely inaccurate. Cloudflare freeze If anyone stumbles across this in the future and is having trouble with the code gimme a yell. (And if Sentry are interested I’ll happily modify the existing code to support Workers, but it’ll be a decent refactor because of |
I’ve updated my Gist for Toucan v3.2.0 and Remix v2. It’s a lot more complicated & hacky now thanks to Remix v2, but still very possible. Please reply there if you have any questions :) |
@AbhiPrasad (or others) I’d like to start work on contributing back some stuff that should make non-Node runtime support a bit easier, but I wanted to run it by the team first in case you have better ideas than me :) (I know you’re working on Deno next—unsure how much of that would be reusable here) The main change I’d like to start with would be (1) exporting the utility functions such as const hub = new Toucan({ … });
const getCurrentHub = () => hub;
const newBuild = instrumentBuild(getCurrentHub, build); This would go 90% of the way to preventing me from having to vendor & maintain your code in my repo while remaining fully backwards-compatible with the existing implementation. Would you be open to this? |
We have #9225 - feel free to take a look at that.
What might be the easiest is just to change the behaviour of |
To confirm I understood things correctly:
If so, is there a way you can add one of our plugins to that wrangler build? I know next to nothing about wrangler though 😬 If I understand things correctly, the source maps emitted by the wrangler build don't point back to the original source code but to the initial Vite build, correct? In that case you might need to use a tool like sorcery to "flatten" the source map chain. I guess this needs to happen post build but pre sentry CLI. |
Hey @andreiborza - wondering whether your team made any progress on investigating this issue? Our team are still majorly hindered by vague "Unexpected Server Errors" being reported when unhandled server errors are thrown. Would be great to understand why this is a little better and if there's anything we can do in the meantime to mitigate 🙏 |
@charlie-bud are you not able to set up and use |
@AbhiPrasad In |
@AbhiPrasad We have a basic reproduction of the issue here. If you build and start this app, you'll see the issue. |
Just to add some meat onto @charlie-bud's great example from some first hand experience and following this issue for a some time 👀 .. I wonder if this should be split into two issues as there are two different problems. The second one is a bit of a wild ride so do strap in!! Problem 1 - Remix deployed onto a Cloudflare worker results in incorrect sourcemap mapping
import * as build from "../build/server"; I wonder if this might help when it becomes more widely supported..
Problem 2 - Making Remix I focussed on the loader specifically and here there are two things happening in @charlie-bud's example:
^^ Hopefully the wording makes sense but heres a picture that might help if not.. I raised a PR to show the changes made From the image above that PR does the following to the image: You will always get 2 errors (unless you add a custom error boundary)
Something of note is if you try and call the They're both pretty complex issues that are funnily not Cloudflare's, Remix's or Sentry's fault - just a side effect of using them all together 🙏 Unless the original |
@geemanjs amazing writeup! We probably will go ahead an add workers export condition to the remix sdk to make this work. @onurtemizkan I know you touched upon this before, now that we have |
@AbhiPrasad Would this also cover fixing the sourcemaps, mentioned in @geemanjs's write up? |
Nope, but that requires help from cloudflare, there's not much we can do alone here. |
Is there an existing issue for this?
How do you use Sentry?
Sentry Saas (sentry.io)
Which package are you using?
@sentry/remix
SDK Version
7.11.0
Framework Version
Remix-Run: 1.6.7 & React 17.0.2
Link to Sentry event
No response
Steps to Reproduce
Sentry.captureException(new Error("Error)
is only being reported to Sentry if in primary tsx component, Not in the loader / action functions.It looks like if Express is used as the server adapter as opposed to cloudflare-workers then there exists the
wrapExpressCreateRequestHandler
to handle it but nothing exists for the cloudflare-workers.See current
server.js
below utilizing cloudflare-workers:Current Sentry.init in
app/entry.server.tsx
:Expected Result
Sentry capture exceptions should be reported in the server code as well.
Actual Result
No sentry event is reported from the server, only client.
The text was updated successfully, but these errors were encountered: