1
- import { captureException , getCurrentHub , runWithAsyncContext , startSpan , Transaction } from '@sentry/core' ;
1
+ import { captureException , continueTrace , runWithAsyncContext , startSpan , Transaction } from '@sentry/core' ;
2
2
import type { Integration } from '@sentry/types' ;
3
- import { addExceptionMechanism , getSanitizedUrlString , parseUrl , tracingContextFromHeaders } from '@sentry/utils' ;
3
+ import { addExceptionMechanism , getSanitizedUrlString , parseUrl } from '@sentry/utils' ;
4
4
5
5
function sendErrorToSentry ( e : unknown ) : unknown {
6
6
captureException ( e , scope => {
@@ -62,22 +62,12 @@ function instrumentBunServeOptions(serveOptions: Parameters<typeof Bun.serve>[0]
62
62
serveOptions . fetch = new Proxy ( serveOptions . fetch , {
63
63
apply ( fetchTarget , fetchThisArg , fetchArgs : Parameters < typeof serveOptions . fetch > ) {
64
64
return runWithAsyncContext ( ( ) => {
65
- const hub = getCurrentHub ( ) ;
66
-
67
65
const request = fetchArgs [ 0 ] ;
68
66
const upperCaseMethod = request . method . toUpperCase ( ) ;
69
67
if ( upperCaseMethod === 'OPTIONS' || upperCaseMethod === 'HEAD' ) {
70
68
return fetchTarget . apply ( fetchThisArg , fetchArgs ) ;
71
69
}
72
70
73
- const sentryTrace = request . headers . get ( 'sentry-trace' ) || '' ;
74
- const baggage = request . headers . get ( 'baggage' ) ;
75
- const { traceparentData, dynamicSamplingContext, propagationContext } = tracingContextFromHeaders (
76
- sentryTrace ,
77
- baggage ,
78
- ) ;
79
- hub . getScope ( ) . setPropagationContext ( propagationContext ) ;
80
-
81
71
const parsedUrl = parseUrl ( request . url ) ;
82
72
const data : Record < string , unknown > = {
83
73
'http.request.method' : request . method || 'GET' ,
@@ -87,43 +77,49 @@ function instrumentBunServeOptions(serveOptions: Parameters<typeof Bun.serve>[0]
87
77
}
88
78
89
79
const url = getSanitizedUrlString ( parsedUrl ) ;
90
- return startSpan (
91
- {
92
- op : 'http.server' ,
93
- name : `${ request . method } ${ parsedUrl . path || '/' } ` ,
94
- origin : 'auto.http.bun.serve' ,
95
- ...traceparentData ,
96
- data,
97
- metadata : {
98
- source : 'url' ,
99
- dynamicSamplingContext : traceparentData && ! dynamicSamplingContext ? { } : dynamicSamplingContext ,
100
- request : {
101
- url,
102
- method : request . method ,
103
- headers : request . headers . toJSON ( ) ,
80
+
81
+ return continueTrace (
82
+ { sentryTrace : request . headers . get ( 'sentry-trace' ) || '' , baggage : request . headers . get ( 'baggage' ) } ,
83
+ ctx => {
84
+ return startSpan (
85
+ {
86
+ op : 'http.server' ,
87
+ name : `${ request . method } ${ parsedUrl . path || '/' } ` ,
88
+ origin : 'auto.http.bun.serve' ,
89
+ ...ctx ,
90
+ data,
91
+ metadata : {
92
+ ...ctx . metadata ,
93
+ source : 'url' ,
94
+ request : {
95
+ url,
96
+ method : request . method ,
97
+ headers : request . headers . toJSON ( ) ,
98
+ } ,
99
+ } ,
104
100
} ,
105
- } ,
106
- } ,
107
- async span => {
108
- try {
109
- const response = await ( fetchTarget . apply ( fetchThisArg , fetchArgs ) as ReturnType <
110
- typeof serveOptions . fetch
111
- > ) ;
112
- if ( response && response . status ) {
113
- span ?. setHttpStatus ( response . status ) ;
114
- span ?. setData ( 'http.response.status_code' , response . status ) ;
115
- if ( span instanceof Transaction ) {
116
- span . setContext ( 'response' , {
117
- headers : response . headers . toJSON ( ) ,
118
- status_code : response . status ,
119
- } ) ;
101
+ async span => {
102
+ try {
103
+ const response = await ( fetchTarget . apply ( fetchThisArg , fetchArgs ) as ReturnType <
104
+ typeof serveOptions . fetch
105
+ > ) ;
106
+ if ( response && response . status ) {
107
+ span ?. setHttpStatus ( response . status ) ;
108
+ span ?. setData ( 'http.response.status_code' , response . status ) ;
109
+ if ( span instanceof Transaction ) {
110
+ span . setContext ( 'response' , {
111
+ headers : response . headers . toJSON ( ) ,
112
+ status_code : response . status ,
113
+ } ) ;
114
+ }
115
+ }
116
+ return response ;
117
+ } catch ( e ) {
118
+ sendErrorToSentry ( e ) ;
119
+ throw e ;
120
120
}
121
- }
122
- return response ;
123
- } catch ( e ) {
124
- sendErrorToSentry ( e ) ;
125
- throw e ;
126
- }
121
+ } ,
122
+ ) ;
127
123
} ,
128
124
) ;
129
125
} ) ;
0 commit comments