Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
The diff you're trying to view is too large. We only load the first 3000 changed files.
87 changes: 87 additions & 0 deletions sentry-javascript/19013/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# Reproduction for sentry-javascript#19013

**Issue:** https://github.com/getsentry/sentry-javascript/issues/19013

## Description

When using `@sentry/bun` with Express, incoming HTTP requests don't get properly scoped isolation. Two distinct requests end up sharing the same trace ID instead of each having their own unique trace ID.

## Prerequisites

- [Bun](https://bun.sh/) installed (tested with v1.3.4)

## Steps to Reproduce

1. Install dependencies:
```bash
bun install
```

2. (Optional) Set your Sentry DSN:
```bash
export SENTRY_DSN="your-dsn-here"
```

3. Start the server:
```bash
bun run start
```

4. In another terminal, run the test script:
```bash
bun run test
```

Or manually make requests:
```bash
curl http://localhost:3000/endpoint1
curl http://localhost:3000/endpoint2
curl http://localhost:3000/endpoint1
```

## Expected Behavior

Each incoming HTTP request should have its own unique trace ID. The automatic instrumentation should capture inbound requests and create new root spans for each request.

## Actual Behavior

Multiple distinct web requests are bundled under the same trace ID. The scope isolation is not working correctly, causing requests to share the same trace context.

Example output showing the bug (server logs):
```
--- Request: GET /endpoint1 ---
Trace ID: NO TRACE ID
Span ID: NO SPAN ID

--- Request: GET /endpoint2 ---
Trace ID: NO TRACE ID
Span ID: NO SPAN ID
```

Test script output:
```
=== Results ===

Request 1: endpoint1 -> Trace ID: NO TRACE ID
Request 2: endpoint2 -> Trace ID: NO TRACE ID
Request 3: endpoint1 -> Trace ID: NO TRACE ID
...

=== Summary ===

Total requests: 6
Unique trace IDs: 0

❌ BUG: No trace IDs were generated at all
```

## Environment

- Bun: 1.3.4
- @sentry/bun: 10.37.0
- express: ^4.21.0
- OS: Any (tested on macOS)

## Notes

The issue report mentions that the only workaround is to manually start a new span for each incoming request, but this should be handled automatically by the SDK's instrumentation.
297 changes: 297 additions & 0 deletions sentry-javascript/19013/bun.lock

Large diffs are not rendered by default.

72 changes: 72 additions & 0 deletions sentry-javascript/19013/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import express from "express";
import * as SentryBun from "@sentry/bun";

import { subscribe } from "node:diagnostics_channel";
import http from "node:http";

const app = express();

// Middleware to log trace information for each request
app.use((req, res, next) => {
const traceId = SentryBun.getActiveSpan()?.spanContext().traceId;
const spanId = SentryBun.getActiveSpan()?.spanContext().spanId;

console.log(`\n--- Request: ${req.method} ${req.path} ---`);
console.log(`Trace ID: ${traceId || "NO TRACE ID"}`);
console.log(`Span ID: ${spanId || "NO SPAN ID"}`);

// Store trace ID in response header for easy verification
if (traceId) {
res.setHeader("X-Trace-Id", traceId);
}

next();
});

// Simple endpoint 1
app.get("/endpoint1", (req, res) => {
SentryBun.startSpan(
{
name: "endpoint1",
},
() => {
const traceId = SentryBun.getActiveSpan()?.spanContext().traceId;
res.json({
endpoint: "endpoint1",
traceId: traceId || "NO TRACE ID",
message: "This is endpoint 1",
});
}
);
});

// Simple endpoint 2
app.get("/endpoint2", (req, res) => {
const traceId = SentryBun.getActiveSpan()?.spanContext().traceId;
res.json({
endpoint: "endpoint2",
traceId: traceId || "NO TRACE ID",
message: "This is endpoint 2",
});
});

subscribe("http.server.request.start", (message, name) => {
console.warn("http.server.request: ", message, name);
});

subscribe("http.client.request.start", (message, name) => {
console.warn("http.client.request: ", message, name);
});

const PORT = process.env.PORT || 3001;

app.use(SentryBun.expressErrorHandler());

app.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
console.log("\nTest the issue by making multiple requests:");
console.log(` curl http://localhost:${PORT}/endpoint1`);
console.log(` curl http://localhost:${PORT}/endpoint2`);
console.log("\nExpected: Each request should have a DIFFERENT trace ID");
console.log("Actual (bug): All requests share the SAME trace ID");
});
17 changes: 17 additions & 0 deletions sentry-javascript/19013/instrument.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import * as SentryBun from "@sentry/bun";

// Initialize Sentry - set SENTRY_DSN environment variable before running
SentryBun.init({
dsn: process.env.SENTRY_DSN || "",

tracesSampleRate: 1.0,

debug: true, // Enable debug to see what's happening

integrations: [
SentryBun.httpIntegration({disableIncomingRequestSpans: false})
],

});

console.log("✅ Sentry initialized");
1 change: 1 addition & 0 deletions sentry-javascript/19013/node_modules/.bin/acorn

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions sentry-javascript/19013/node_modules/.bin/mime

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading