Skip to content

feat(bun): Update Bun integration to support routes & optional fetch #15978

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

Closed

Conversation

Jarred-Sumner
Copy link

@Jarred-Sumner Jarred-Sumner commented Apr 4, 2025

resolves #15941

This updates the @sentry/bun package to support instrumenting routes and to no longer throw an error when omitting fetch. This also updates bun-types from 1.0.1 to 1.2.8 (which includes the types for routes).

This does not include the code for updating node:http to support our migration away from wrapping Request & Response to support node:http servers better. It was less clear how to handle that, but it will likely involve wrapping node:http instead of Bun.serve.

Also, it seems that the tests did not pass previously when running bun test due to shared global state between files (probably the sdk test file). It still does not pass when run on multiple files. It does pass individually though.

  • If you've added code that should be tested, please add tests.
  • Ensure your code lints and the test suite passes (yarn lint) & (yarn test).

Additional notes:

  • The import-in-the-middle usage needs in @sentry/node needs to be disabled in Bun for now or else it will throw an error at runtime
  • A bunch of this PR was generated by Claude, so review carefully

Feel free to use this PR as a starting point (or discard) and close if you think that's better

@AbhiPrasad AbhiPrasad self-assigned this Apr 4, 2025
@AbhiPrasad
Copy link
Member

Hey @Jarred-Sumner thanks for the PR! I put this on my todo to review and merge, we'll try to get this out with the next release!

@s1gr1d s1gr1d mentioned this pull request Apr 7, 2025
3 tasks
@mydea mydea changed the title Update Bun integration to support routes & optional fetch feat(bun): Update Bun integration to support routes & optional fetch Apr 7, 2025
@AbhiPrasad
Copy link
Member

I've opened #16035 based on this PR, I think we can close this now.

@AbhiPrasad AbhiPrasad closed this Apr 11, 2025
AbhiPrasad added a commit that referenced this pull request Apr 11, 2025
Supercedes #15978

resolves #15941
resolves #15827
resolves #15816

Bun recently updated their `Bun.serve` API with new functionality, which
unfortunately broke our existing instrumentation. This is detailed with
https://bun.sh/docs/api/http#bun-serve. Specifically, they added a new
routing API that looks like so:

```ts
Bun.serve({
  // `routes` requires Bun v1.2.3+
  routes: {
    // Dynamic routes
    "/users/:id": req => {
      return new Response(`Hello User ${req.params.id}!`);
    },

    // Per-HTTP method handlers
    "/api/posts": {
      GET: () => new Response("List posts"),
      POST: async req => {
        const body = await req.json();
        return Response.json({ created: true, ...body });
      },
    },

    // Wildcard route for all routes that start with "/api/" and aren't otherwise matched
    "/api/*": Response.json({ message: "Not found" }, { status: 404 }),

    // Redirect from /blog/hello to /blog/hello/world
    "/blog/hello": Response.redirect("/blog/hello/world"),

    // Serve a file by buffering it in memory
    "/favicon.ico": new Response(await Bun.file("./favicon.ico").bytes(), {
      headers: {
        "Content-Type": "image/x-icon",
      },
    }),
  },

  // (optional) fallback for unmatched routes:
  // Required if Bun's version < 1.2.3
  fetch(req) {
    return new Response("Not Found", { status: 404 });
  },
});
``` 

Because there are now dynamic routes and wildcard routes, we can
actually generate `route` transaction source and send parameterized
routes to Sentry. The `fetch` API is still supported.

The only API we don't support is [static
routes/responses](https://bun.sh/docs/api/http#static-responses). This
is because these are optimized by Bun itself, and if we turn it into a
function (which we need to do to time it), we'll lose out on the
optimization. For now they aren't instrumented.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fix bun server integration from latest bun changes
2 participants