Skip to content

Bun.serve Inbound Instrumentation #4817

@DanielSuhett

Description

@DanielSuhett

Is your feature request related to a problem? Please describe.
When running apps under Bun, inbound HTTP transactions are not captured. Bun exposes a global Fetch-style API via Bun.serve, which bypasses Node’s http/https hooks and the module-load patch points the agent relies on. Users can see errors (via captureError) but no request transactions in APM.

Describe the solution you'd like
Add inbound support for Bun.serve that:

  • Detects Bun at runtime and idempotently wraps Bun.serve.
  • Starts one transaction per request (name: METHOD /path, type: request).
  • Extracts distributed tracing headers (traceparent/elastic-apm-traceparent, tracestate).
  • Runs the handler under the agent run-context so child spans parent to the transaction.
  • Sets result/outcome from response status and captures handler errors.
  • Respects existing config (e.g., instrumentIncomingHTTPRequests) and is a no‑op outside Bun.

This should “just work” when the agent is started before Bun.serve, matching the experience with common Node frameworks.

Describe alternatives you've considered

  • Preloading the agent’s start entrypoint to wrap Bun.serve early (works, but adds a preload step).
  • Preloading a separate shim/module (works for local dev; not ideal as a user experience).
  • Doing nothing and waiting for Bun diagnostic hooks (leaves users without inbound transactions now).

Additional context

  • I use this agent extensively in my Node services and want to help Bun‑based services keep using it too. I’m not fully familiar with the agent internals, so my prototype may not follow all conventions, but I’m keen to adjust it with your guidance. I’ve also opened a discussion on the Bun side about possible hooks; given Bun.serve’s different architecture, this likely still needs an agent‑side wrapper.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions