Skip to content

ref(bun): Use Sentry.continueTrace in Bun #9606

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 21, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 43 additions & 47 deletions packages/bun/src/integrations/bunserver.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { captureException, getCurrentHub, runWithAsyncContext, startSpan, Transaction } from '@sentry/core';
import { captureException, continueTrace, runWithAsyncContext, startSpan, Transaction } from '@sentry/core';
import type { Integration } from '@sentry/types';
import { addExceptionMechanism, getSanitizedUrlString, parseUrl, tracingContextFromHeaders } from '@sentry/utils';
import { addExceptionMechanism, getSanitizedUrlString, parseUrl } from '@sentry/utils';

function sendErrorToSentry(e: unknown): unknown {
captureException(e, scope => {
Expand Down Expand Up @@ -62,22 +62,12 @@ function instrumentBunServeOptions(serveOptions: Parameters<typeof Bun.serve>[0]
serveOptions.fetch = new Proxy(serveOptions.fetch, {
apply(fetchTarget, fetchThisArg, fetchArgs: Parameters<typeof serveOptions.fetch>) {
return runWithAsyncContext(() => {
const hub = getCurrentHub();

const request = fetchArgs[0];
const upperCaseMethod = request.method.toUpperCase();
if (upperCaseMethod === 'OPTIONS' || upperCaseMethod === 'HEAD') {
return fetchTarget.apply(fetchThisArg, fetchArgs);
}

const sentryTrace = request.headers.get('sentry-trace') || '';
const baggage = request.headers.get('baggage');
const { traceparentData, dynamicSamplingContext, propagationContext } = tracingContextFromHeaders(
sentryTrace,
baggage,
);
hub.getScope().setPropagationContext(propagationContext);

const parsedUrl = parseUrl(request.url);
const data: Record<string, unknown> = {
'http.request.method': request.method || 'GET',
Expand All @@ -87,43 +77,49 @@ function instrumentBunServeOptions(serveOptions: Parameters<typeof Bun.serve>[0]
}

const url = getSanitizedUrlString(parsedUrl);
return startSpan(
{
op: 'http.server',
name: `${request.method} ${parsedUrl.path || '/'}`,
origin: 'auto.http.bun.serve',
...traceparentData,
data,
metadata: {
source: 'url',
dynamicSamplingContext: traceparentData && !dynamicSamplingContext ? {} : dynamicSamplingContext,
request: {
url,
method: request.method,
headers: request.headers.toJSON(),

return continueTrace(
{ sentryTrace: request.headers.get('sentry-trace') || '', baggage: request.headers.get('baggage') },
ctx => {
return startSpan(
{
op: 'http.server',
name: `${request.method} ${parsedUrl.path || '/'}`,
origin: 'auto.http.bun.serve',
...ctx,
data,
metadata: {
...ctx.metadata,
source: 'url',
request: {
url,
method: request.method,
headers: request.headers.toJSON(),
},
},
},
},
},
async span => {
try {
const response = await (fetchTarget.apply(fetchThisArg, fetchArgs) as ReturnType<
typeof serveOptions.fetch
>);
if (response && response.status) {
span?.setHttpStatus(response.status);
span?.setData('http.response.status_code', response.status);
if (span instanceof Transaction) {
span.setContext('response', {
headers: response.headers.toJSON(),
status_code: response.status,
});
async span => {
try {
const response = await (fetchTarget.apply(fetchThisArg, fetchArgs) as ReturnType<
typeof serveOptions.fetch
>);
if (response && response.status) {
span?.setHttpStatus(response.status);
span?.setData('http.response.status_code', response.status);
if (span instanceof Transaction) {
span.setContext('response', {
headers: response.headers.toJSON(),
status_code: response.status,
});
}
}
return response;
} catch (e) {
sendErrorToSentry(e);
throw e;
}
}
return response;
} catch (e) {
sendErrorToSentry(e);
throw e;
}
},
);
},
);
});
Expand Down