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

Add tunneling option to @sentry/astro #10309

Open
florian-lefebvre opened this issue Jan 24, 2024 · 4 comments · May be fixed by #10334
Open

Add tunneling option to @sentry/astro #10309

florian-lefebvre opened this issue Jan 24, 2024 · 4 comments · May be fixed by #10334
Assignees
Labels
Package: astro Issues related to the Sentry Astro SDK

Comments

@florian-lefebvre
Copy link

Problem Statement

I think the Next package has an option for tunneling, it would be great to have the same for Astro

Solution Brainstorm

Back when I had a look at the docs, there was a snippet with how to create a tunnel endpoint in PHP. Closest thing I find now is https://docs.sentry.io/platforms/javascript/troubleshooting/#using-the-tunnel-option.

Anyway, it seems fairly easy with the injectRoute utility. Here is what I've done for a project of mine:

// src/pages/sy/[...params].ts

import type { APIContext } from "astro";

export const prerender = false;

export async function POST({ request }: APIContext) {
  const body = await request.text();

  let payload: {
    sent_at: string;
    sdk: { name: string; version: string };
    dsn: string;
  };

  try {
    payload = JSON.parse(body.split("\n")[0]);
  } catch (err) {
    console.error(err);
    return new Response(null, { status: 400 });
  }

  if (!payload.dsn) {
    return new Response("Invalid request", { status: 400 });
  }

  const dsn = new URL(payload.dsn);
  const projectID = dsn.pathname.replace(/\//g, "");

  if (projectID !== import.meta.env.SECRET_SENTRY_PROJECT_ID) {
    return new Response("Invalid request", { status: 400 });
  }

  await fetch(`https://sentry.io/api/${projectID}/envelope/`, {
    method: "POST",
    headers: {
      "Content-type": "application/x-sentry-envelope",
    },
    body: JSON.stringify(payload),
  });

  return new Response(null, { status: 200 });
}

It's not always working so something is probably off. I can contribute this if you can help fix the endpoint logic itself

@Lms24
Copy link
Member

Lms24 commented Jan 24, 2024

Hi @florian-lefebvre thanks for writing in!

So to confirm, the tunnel option in the SDK works for you if you set it in an external sentry.client|server.config.js file? And your request is that the @sentry/astro package exposes a built-in proxy/forwarding functionality, correct?

It sounds like a viable option to me and I like the idea of simply injecting the route. However, right now we don't have the capacity to work on this. I'll backlog it for later. If you want to contribute this feature, feel free to open a PR. I'd recommend taking a look at this PR which was supposed to add guidance on how to implement such a proxy server in Node. Unfortunately this PR got a bit stale which is why it didn't make it to docs yet (I'll follow up on this).

Long story short, we'll need to parse the envelope payload and read the DSN from the envelope headers.

@Lms24 Lms24 added Package: astro Issues related to the Sentry Astro SDK Type: Feature and removed Type: Improvement labels Jan 24, 2024
@florian-lefebvre
Copy link
Author

Yes that's it! Assign me, I'll submit a PR today

@florian-lefebvre
Copy link
Author

Question about the linked PR: what data is sent as body? Since it has to be forwarded with the fetch call, there is no req.body in astro. Should I just forward the body as text?

@Lms24
Copy link
Member

Lms24 commented Jan 24, 2024

IIRC you should forward the body as application/x-sentry-envelope. The envelope itself is text but some items could be binary. This should work though because the header you need to parse to get the DSN is always text. I think we should avoid parsing the entire envelope but only look at the header, get the DSN and forward the original body.

If you're curious, here's our envelope payload specification.

Hope this answers your question.

florian-lefebvre added a commit to florian-lefebvre/sentry-javascript that referenced this issue Jan 25, 2024
@florian-lefebvre florian-lefebvre linked a pull request Jan 25, 2024 that will close this issue
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Package: astro Issues related to the Sentry Astro SDK
Projects
Status: No status
Development

Successfully merging a pull request may close this issue.

3 participants