Skip to content

Commit 51576c2

Browse files
authored
ref(bun): Use Sentry.continueTrace in Bun (#9606)
1 parent 83eaf9e commit 51576c2

File tree

1 file changed

+43
-47
lines changed

1 file changed

+43
-47
lines changed

packages/bun/src/integrations/bunserver.ts

Lines changed: 43 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { captureException, getCurrentHub, runWithAsyncContext, startSpan, Transaction } from '@sentry/core';
1+
import { captureException, continueTrace, runWithAsyncContext, startSpan, Transaction } from '@sentry/core';
22
import type { Integration } from '@sentry/types';
3-
import { addExceptionMechanism, getSanitizedUrlString, parseUrl, tracingContextFromHeaders } from '@sentry/utils';
3+
import { addExceptionMechanism, getSanitizedUrlString, parseUrl } from '@sentry/utils';
44

55
function sendErrorToSentry(e: unknown): unknown {
66
captureException(e, scope => {
@@ -62,22 +62,12 @@ function instrumentBunServeOptions(serveOptions: Parameters<typeof Bun.serve>[0]
6262
serveOptions.fetch = new Proxy(serveOptions.fetch, {
6363
apply(fetchTarget, fetchThisArg, fetchArgs: Parameters<typeof serveOptions.fetch>) {
6464
return runWithAsyncContext(() => {
65-
const hub = getCurrentHub();
66-
6765
const request = fetchArgs[0];
6866
const upperCaseMethod = request.method.toUpperCase();
6967
if (upperCaseMethod === 'OPTIONS' || upperCaseMethod === 'HEAD') {
7068
return fetchTarget.apply(fetchThisArg, fetchArgs);
7169
}
7270

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-
8171
const parsedUrl = parseUrl(request.url);
8272
const data: Record<string, unknown> = {
8373
'http.request.method': request.method || 'GET',
@@ -87,43 +77,49 @@ function instrumentBunServeOptions(serveOptions: Parameters<typeof Bun.serve>[0]
8777
}
8878

8979
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+
},
104100
},
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;
120120
}
121-
}
122-
return response;
123-
} catch (e) {
124-
sendErrorToSentry(e);
125-
throw e;
126-
}
121+
},
122+
);
127123
},
128124
);
129125
});

0 commit comments

Comments
 (0)