Skip to content

Commit 08b0433

Browse files
isaacsclaude
andauthored
fix(bun): Apply httpIntegration incoming-request options to node:http servers (#26611)
## What On Bun, the incoming-request options passed to `httpIntegration` (such as `maxRequestBodySize: 'none'`, `ignoreIncomingRequests`, `onSpanCreated`, `sessions`) now apply to `node:http` servers. Options set on `bunHttpServerIntegration` still win. ## Why Bun does not publish `http.server.request.start`, so `bunHttpServerIntegration` creates the `http.server` spans, and it read only its own options. The `httpIntegration` options type-checked but did nothing, so request bodies were still sent after a user turned them off. `bun test` now runs with `--isolate`, because the `http.Server` patch happens once per process and the first test file's `init()` fixed the options for all files. That exposed a `perf_hooks` mock in `bunRuntimeMetrics.test.ts` that failed when the file ran alone. Closes: #26609 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 1216ab4 commit 08b0433

7 files changed

Lines changed: 124 additions & 33 deletions

File tree

‎dev-packages/bun-integration-tests/node-suites/excludes.ts‎

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -169,22 +169,18 @@ const FETCH_INTEGRATION_DIFFERS = [
169169
'suites/tracing/requests/fetch-sampled-no-active-span/test.ts',
170170
];
171171

172-
// `@sentry/bun` creates `http.server` spans with `bunHttpServerIntegration`, not `httpIntegration`,
173-
// so the incoming-request options that the scenario passes to `httpIntegration` (for example
174-
// `sessionFlushingDelayMS`, `onSpanCreated`, `ignoreIncomingRequests`, `ignoreStaticAssets`) have
175-
// no effect. Its spans also differ: response headers become attributes, and there is no
176-
// `http.response.status_text`. And a server
177-
// that sets `emit` back to the `emit` it had before its first request gets no spans after that.
178-
// See https://github.com/getsentry/sentry-javascript/issues/26609
179-
const HTTP_SERVER_OPTIONS_IGNORED = [
180-
'suites/sessions/exited-session-aggregate/test.ts',
172+
// `@sentry/bun` creates `http.server` spans with `bunHttpServerIntegration`, not `httpIntegration`.
173+
// Its spans differ: response headers become attributes, there is no `http.response.status_text`,
174+
// and the span name has no route. A server that sets `emit` back to the `emit` it had before its
175+
// first request gets no spans after that. And an error that reaches the server does not mark
176+
// the request session as crashed.
177+
const HTTP_SERVER_SPANS_DIFFER = [
181178
'suites/sessions/server-without-process-session/test.ts',
182179
'suites/tracing/httpIntegration/test.ts',
183180
];
184181

185182
// With `@sentry/bun`, these suites fail only because they need Express spans or route names,
186-
// which `bun run` does not create. The crashed and errored session suites also need
187-
// `sessionFlushingDelayMS` (see above).
183+
// which `bun run` does not create.
188184
// See https://github.com/getsentry/sentry-javascript/issues/23882
189185
const NO_EXPRESS_INSTRUMENTATION = [
190186
'suites/sessions/crashed-session-aggregate/test.ts',
@@ -211,7 +207,7 @@ const BUN_DROPS_PIPELINED_REQUESTS = ['suites/tunnel/hardening/test.ts'];
211207

212208
export const SENTRY_BUN_EXCLUDE = [
213209
...BUN_DROPS_PIPELINED_REQUESTS,
214-
...HTTP_SERVER_OPTIONS_IGNORED,
210+
...HTTP_SERVER_SPANS_DIFFER,
215211
...NO_EXPRESS_INSTRUMENTATION,
216212
...NO_NATIVE_NODE_FETCH_INTEGRATION,
217213
...FETCH_INTEGRATION_DIFFERS,
@@ -237,14 +233,10 @@ const BUN_BUILD_NO_HTTP_SERVER_INTEGRATION = ['suites/express/multiple-init/test
237233
// `require is not defined`. See https://github.com/oven-sh/bun/issues/14891
238234
const BUN_BUILD_UNBUNDLEABLE_DEPENDENCY = ['suites/tracing/mistral/test.ts', 'suites/tracing/mongodb/test.ts'];
239235

240-
// Some or all tests fail with the bundled scenarios. The cause is not investigated yet. In
241-
// `express/tracing` only the request data tests fail: they set `httpIntegration` options, and with
242-
// `@sentry/bun` the request body comes from `bunHttpServerIntegration`.
236+
// Some or all tests fail with the bundled scenarios. The cause is not investigated yet.
243237
const BUN_BUILD_NOT_TRIAGED = [
244238
'suites/express/sentry-trace/test.ts',
245239
'suites/express/tracing/routing/test.ts',
246-
'suites/express/tracing/request-data/test.ts',
247-
'suites/express/with-http/**',
248240
'suites/hono-sdk/test.ts',
249241
'suites/tracing/google-genai-v2/test.ts',
250242
'suites/tracing/google-genai-v3/test.ts',
@@ -269,6 +261,15 @@ const BUN_BUILD_NOT_TRIAGED = [
269261
'suites/tracing/vercelai/v6_v7/**',
270262
];
271263

264+
// Before Bun 1.4, Bun does not publish `http.client.request.created`, and `@sentry/node` picks the
265+
// channel from the Node version that Bun reports. So outgoing `node:http` requests get no
266+
// `sentry-trace` header, and the `/test2` request starts its own trace.
267+
// See https://github.com/getsentry/sentry-javascript/issues/26614
268+
const BUN_1_3_NO_HTTP_CLIENT_CHANNEL = ['suites/express/with-http/base/test.ts'];
269+
270+
// Excluded in `node-suites-bun-build` only before Bun 1.4.
271+
export const BUN_BUILD_BEFORE_1_4_EXCLUDE = [...BUN_1_3_NO_HTTP_CLIENT_CHANNEL];
272+
272273
export const BUN_BUILD_EXCLUDE = [
273274
'**/node_modules/**',
274275
// SQL Server runs in the dedicated Node/x86 CI job.
@@ -282,15 +283,6 @@ export const BUN_BUILD_EXCLUDE = [
282283
// The `bun run` project (`node-suites-bun-run`) runs the suites of `NO_AUTO_INSTRUMENTATION` with
283284
// `@sentry/bun/import` preloaded, on Bun 1.4.6 or later. These do not run there.
284285

285-
// The scenarios pass the incoming-request options (`maxRequestBodySize`, `ignoreIncomingRequestBody`)
286-
// to `httpIntegration`. With `@sentry/bun`, `bunHttpServerIntegration` creates the `http.server` spans
287-
// and takes these options itself, so the tests that set them fail.
288-
// See https://github.com/getsentry/sentry-javascript/issues/26609
289-
const BUN_RUN_HTTP_INTEGRATION_OPTIONS_IGNORED = [
290-
'suites/express/tracing/request-data/test.ts',
291-
'suites/express/with-http/maxIncomingRequestBodySize/test.ts',
292-
];
293-
294286
// The suite tests `@sentry/hono/node`, which builds on `@sentry/node` and creates no `http.server`
295287
// spans on Bun. `@sentry/hono/bun` takes its options in `sentry(app, options)` instead of a separate
296288
// `init()`, so the scenario cannot use it. `suites/hono-sdk` in this package tests `@sentry/hono/bun`.
@@ -303,6 +295,5 @@ export const BUN_RUN_EXCLUDE = [
303295
// These fail for the same reasons as with the bundled scenarios.
304296
...BUN_BUILD_EAGER_SUBSCRIPTION,
305297
...BUN_BUILD_NO_HTTP_SERVER_INTEGRATION,
306-
...BUN_RUN_HTTP_INTEGRATION_OPTIONS_IGNORED,
307298
...BUN_RUN_HONO_NODE_SDK,
308299
];

‎dev-packages/bun-integration-tests/vite.config.mts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { fileURLToPath } from 'node:url';
33
import { defineConfig } from 'vitest/config';
44
import baseConfig from '../../vite/vite.config';
55
import {
6+
BUN_BUILD_BEFORE_1_4_EXCLUDE,
67
BUN_BUILD_EXCLUDE,
78
BUN_RUN_EXCLUDE,
89
NO_AUTO_INSTRUMENTATION,
@@ -21,6 +22,7 @@ const [bunMajor = 0, bunMinor = 0, bunPatch = 0] = execFileSync('bun', ['--versi
2122
.trim()
2223
.split(/[.-]/)
2324
.map(Number);
25+
const BUN_BEFORE_1_4 = bunMajor < 1 || (bunMajor === 1 && bunMinor < 4);
2426
const BUN_RUN_INSTRUMENTS = bunMajor > 1 || (bunMajor === 1 && (bunMinor > 4 || (bunMinor === 4 && bunPatch >= 6)));
2527

2628
const AUTO_INSTRUMENTATION_SUITES = NO_AUTO_INSTRUMENTATION.map(glob =>
@@ -106,7 +108,7 @@ export default defineConfig({
106108
// See https://github.com/getsentry/sentry-javascript/issues/23882
107109
name: 'node-suites-bun-build',
108110
include: AUTO_INSTRUMENTATION_SUITES,
109-
exclude: BUN_BUILD_EXCLUDE,
111+
exclude: [...BUN_BUILD_EXCLUDE, ...(BUN_BEFORE_1_4 ? BUN_BUILD_BEFORE_1_4_EXCLUDE : [])],
110112
env: {
111113
RUNTIME: 'bun',
112114
RUNTIME_PRELOAD: fileURLToPath(new URL('./node-suites/alias-sentry-bun.ts', import.meta.url)),

‎packages/bun/package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
"lint:es-compatibility": "node ../../scripts/es-check.mjs es2022",
7171
"install:bun": "node ./scripts/install-bun.js",
7272
"test": "pnpm run install:bun && pnpm run test:bun",
73-
"test:bun": "bun test",
73+
"test:bun": "bun test --isolate",
7474
"test:watch": "bun test --watch",
7575
"yalc:publish": "pnpm dlx yalc publish --push --sig"
7676
},

‎packages/bun/src/integrations/bunHttpServer.ts‎

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import { errorMonitor } from 'node:events';
22
import * as http from 'node:http';
33
import * as https from 'node:https';
4-
import type { IntegrationFn, Span } from '@sentry/core';
4+
import type { Client, Integration, IntegrationFn, Span } from '@sentry/core';
55
import { defineIntegration } from '@sentry/core';
66
import type {
77
AssertTrue,
88
CheckServerRequestOptions,
99
HttpIncomingMessage,
1010
HttpServerResponse,
11+
ServerRequestOptions,
1112
} from '@sentry/core/server';
1213
import { getHttpServerSubscriptions, HTTP_ON_SERVER_REQUEST } from '@sentry/core/server';
1314
import { isBun } from '@sentry/server-utils';
@@ -90,13 +91,44 @@ type _ServerRequestOptionsCheck = AssertTrue<
9091
CheckServerRequestOptions<BunHttpServerOptions, HttpIncomingMessage, HttpServerResponse>
9192
>;
9293

94+
// Every shared option is a required key, so a new one fails to compile
95+
// below until the integration passes it on.
96+
type BunHttpServerRequestOptions = {
97+
[K in keyof Required<ServerRequestOptions<HttpIncomingMessage, HttpServerResponse>>]: BunHttpServerOptions[K];
98+
};
99+
100+
interface HttpIntegration extends Integration {
101+
serverRequestOptions: BunHttpServerRequestOptions;
102+
}
103+
93104
let hasPatched = false;
94105

106+
/**
107+
* Merges the incoming-request options of the client's `httpIntegration`
108+
* under the given options. Bun does not publish
109+
* `http.server.request.start`, so `httpIntegration` never applies them.
110+
*/
111+
function resolveOptions(options: BunHttpServerOptions, client: Client): BunHttpServerRequestOptions {
112+
const httpOptions: Partial<BunHttpServerRequestOptions> =
113+
client.getIntegrationByName<HttpIntegration>('Http')?.serverRequestOptions ?? {};
114+
return {
115+
spans: options.spans ?? httpOptions.spans,
116+
sessions: options.sessions ?? httpOptions.sessions,
117+
sessionFlushingDelayMS: options.sessionFlushingDelayMS ?? httpOptions.sessionFlushingDelayMS,
118+
maxRequestBodySize: options.maxRequestBodySize ?? httpOptions.maxRequestBodySize,
119+
ignoreRequestBody: options.ignoreRequestBody ?? httpOptions.ignoreRequestBody,
120+
ignoreIncomingRequests: options.ignoreIncomingRequests ?? httpOptions.ignoreIncomingRequests,
121+
ignoreStaticAssets: options.ignoreStaticAssets ?? httpOptions.ignoreStaticAssets,
122+
onSpanCreated: options.onSpanCreated ?? httpOptions.onSpanCreated,
123+
onSpanEnd: options.onSpanEnd ?? httpOptions.onSpanEnd,
124+
};
125+
}
126+
95127
const _bunHttpServerIntegration = ((options: BunHttpServerOptions = {}) => {
96128
return {
97129
name: INTEGRATION_NAME,
98-
setupOnce() {
99-
instrumentBunHttpServer(options);
130+
afterAllSetup(client) {
131+
instrumentBunHttpServer(resolveOptions(options, client));
100132
},
101133
};
102134
}) satisfies IntegrationFn;

‎packages/bun/test/integrations/bunHttpServer.test.ts‎

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import http from 'node:http';
22
import { HTTP_REQUEST_METHOD, SENTRY_OP } from '@sentry/conventions/attributes';
33
import { getActiveSpan, getCurrentScope, getTraceData, spanToJSON } from '@sentry/core';
44
import { afterAll, beforeAll, describe, expect, test } from 'bun:test';
5-
import { init } from '../../src';
5+
import { bunHttpServerIntegration, httpIntegration, init } from '../../src';
66

77
async function startServer(handler: http.RequestListener): Promise<{ port: number; close: () => Promise<void> }> {
88
const server = http.createServer(handler);
@@ -31,6 +31,15 @@ describe('Bun HTTP Server Integration', () => {
3131
tracePropagationTargets: [],
3232
// Avoid sending anything to Sentry
3333
transport: () => ({ send: async () => ({}), flush: async () => true }),
34+
integrations: [
35+
httpIntegration({
36+
ignoreIncomingRequests: urlPath => urlPath.startsWith('/ignored'),
37+
onSpanCreated: span => span.setAttribute('created.by', 'http'),
38+
}),
39+
bunHttpServerIntegration({
40+
onSpanCreated: span => span.setAttribute('created.by', 'bunHttpServer'),
41+
}),
42+
],
3443
});
3544
});
3645

@@ -125,4 +134,36 @@ describe('Bun HTTP Server Integration', () => {
125134

126135
expect(observedTraceId).toBe(incomingTraceId);
127136
});
137+
138+
test('applies the httpIntegration options that bunHttpServerIntegration does not set', async () => {
139+
let span: ReturnType<typeof spanToJSON> | undefined;
140+
141+
const { port, close } = await startServer((_req, res) => {
142+
const activeSpan = getActiveSpan();
143+
span = activeSpan ? spanToJSON(activeSpan) : undefined;
144+
res.end('ok');
145+
});
146+
147+
await fetch(`http://localhost:${port}/ignored`).then(res => res.text());
148+
149+
await close();
150+
151+
expect(span).toBeUndefined();
152+
});
153+
154+
test('prefers the bunHttpServerIntegration options over the httpIntegration options', async () => {
155+
let span: ReturnType<typeof spanToJSON> | undefined;
156+
157+
const { port, close } = await startServer((_req, res) => {
158+
const activeSpan = getActiveSpan();
159+
span = activeSpan ? spanToJSON(activeSpan) : undefined;
160+
res.end('ok');
161+
});
162+
163+
await fetch(`http://localhost:${port}/`).then(res => res.text());
164+
165+
await close();
166+
167+
expect(span?.attributes['created.by']).toBe('bunHttpServer');
168+
});
128169
});

‎packages/bun/test/integrations/bunRuntimeMetrics.test.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ const mockEventLoopUtilization = jest.fn((curr?: object, _prev?: object) => {
88
return mockElu;
99
});
1010

11+
const realPerfHooks = await import('perf_hooks');
1112
mock.module('perf_hooks', () => ({
13+
...realPerfHooks,
1214
performance: { eventLoopUtilization: mockEventLoopUtilization },
1315
}));
1416

‎packages/node/src/integrations/http/index.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import type {
1313
CheckServerRequestOptions,
1414
HttpIncomingMessage,
1515
HttpServerResponse,
16+
ServerRequestOptions,
1617
} from '@sentry/core/server';
1718
import { getRequestUrlFromClientRequest } from '@sentry/core/server';
1819
import type { NodeClient } from '../../sdk/client';
@@ -96,6 +97,12 @@ type _ServerRequestOptionsCheck = AssertTrue<
9697
CheckServerRequestOptions<HttpOptions, HttpIncomingMessage, HttpServerResponse>
9798
>;
9899

100+
// Every shared option is a required key, so a new one fails to compile
101+
// below until the integration passes it on.
102+
type HttpServerRequestOptions = {
103+
[K in keyof Required<ServerRequestOptions<HttpIncomingMessage, HttpServerResponse>>]: HttpOptions[K];
104+
};
105+
99106
/**
100107
* The http integration instruments Node's internal http and https modules.
101108
* It creates breadcrumbs and spans for outgoing HTTP requests which will be attached to the currently active span.
@@ -107,8 +114,24 @@ export const httpIntegration = defineIntegration((options: HttpOptions = {}) =>
107114
const server = httpServerIntegration(options);
108115
const serverSpans = httpServerSpansIntegration(options);
109116

117+
// Runtimes that do not publish `http.server.request.start` (such as
118+
// Bun) create incoming-request spans in another integration, which
119+
// reads these options.
120+
const serverRequestOptions: HttpServerRequestOptions = {
121+
spans: enableServerSpans ? undefined : false,
122+
sessions: options.sessions,
123+
sessionFlushingDelayMS: options.sessionFlushingDelayMS,
124+
maxRequestBodySize: options.maxRequestBodySize,
125+
ignoreRequestBody: options.ignoreRequestBody,
126+
ignoreIncomingRequests: options.ignoreIncomingRequests,
127+
ignoreStaticAssets: options.ignoreStaticAssets,
128+
onSpanCreated: options.onSpanCreated,
129+
onSpanEnd: options.onSpanEnd,
130+
};
131+
110132
return {
111133
name: INTEGRATION_NAME,
134+
serverRequestOptions,
112135
setup(client: NodeClient) {
113136
const clientOptions = client.getOptions();
114137

0 commit comments

Comments
 (0)