11import http from 'node:http' ;
2- import { getCurrentScope } from '@sentry/core' ;
2+ import { getTraceData } from '@sentry/core' ;
33import { beforeAll , describe , expect , test } from 'bun:test' ;
4- import { init } from '../../src' ;
5- import { instrumentBunHttpServer } from '../../src/integrations/bunHttpServer' ;
4+ import { bunHttpServerIntegration , init } from '../../src' ;
65
76async function startServer ( handler : http . RequestListener ) : Promise < { port : number ; close : ( ) => Promise < void > } > {
87 const server = http . createServer ( handler ) ;
@@ -15,23 +14,27 @@ async function startServer(handler: http.RequestListener): Promise<{ port: numbe
1514 } ;
1615}
1716
17+ /** Read the trace id the SDK would propagate for the current request. Works in both OTel and non-OTel modes. */
18+ function currentTraceId ( ) : string | undefined {
19+ return getTraceData ( ) [ 'sentry-trace' ] ?. split ( '-' ) [ 0 ] ;
20+ }
21+
1822describe ( 'Bun HTTP Server Integration' , ( ) => {
1923 beforeAll ( ( ) => {
2024 init ( {
2125 dsn : 'https://public@dsn.ingest.sentry.io/1337' ,
2226 tracesSampleRate : 0 ,
2327 // Avoid sending anything to Sentry
2428 transport : ( ) => ( { send : async ( ) => ( { } ) , flush : async ( ) => true } ) ,
29+ integrations : [ bunHttpServerIntegration ( { spans : false } ) ] ,
2530 } ) ;
26- // Only performs isolation + trace reset, no span creation (Next.js emits its own spans).
27- instrumentBunHttpServer ( { spans : false } ) ;
2831 } ) ;
2932
3033 test ( 'isolates each incoming request with a distinct trace id' , async ( ) => {
31- const traceIds : string [ ] = [ ] ;
34+ const traceIds : Array < string | undefined > = [ ] ;
3235
3336 const { port, close } = await startServer ( ( _req , res ) => {
34- traceIds . push ( getCurrentScope ( ) . getPropagationContext ( ) . traceId ) ;
37+ traceIds . push ( currentTraceId ( ) ) ;
3538 res . end ( 'ok' ) ;
3639 } ) ;
3740
@@ -51,7 +54,7 @@ describe('Bun HTTP Server Integration', () => {
5154 let observedTraceId : string | undefined ;
5255
5356 const { port, close } = await startServer ( ( _req , res ) => {
54- observedTraceId = getCurrentScope ( ) . getPropagationContext ( ) . traceId ;
57+ observedTraceId = currentTraceId ( ) ;
5558 res . end ( 'ok' ) ;
5659 } ) ;
5760
0 commit comments