elastic / elastic/apm-agent-nodejs

Bun.serve Inbound Instrumentation

Open
#4,817 1 comment 0 reactions 0 assignees View on GitHub
community triage
Dominant language
JavaScript
Stars
594
Forks
244
Avg merge
1d 8h
Merged PRs (30d)
16

Description

**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.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.