Skip to content
Closed
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
52 changes: 35 additions & 17 deletions packages/bun/src/integrations/bunserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import {
captureException,
continueTrace,
defineIntegration,
extractQueryParamsFromUrl,
getSanitizedUrlString,
parseUrl,
isURLObjectRelative,
parseStringToURLObject,
getSanitizedUrlStringFromUrlObject,
setHttpStatus,
startSpan,
withIsolationScope,
Expand Down Expand Up @@ -76,24 +76,15 @@ function instrumentBunServeOptions(serveOptions: Parameters<typeof Bun.serve>[0]
return fetchTarget.apply(fetchThisArg, fetchArgs);
}

const parsedUrl = parseUrl(request.url);
const attributes: SpanAttributes = {
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.http.bun.serve',
[SEMANTIC_ATTRIBUTE_HTTP_REQUEST_METHOD]: request.method || 'GET',
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'url',
};
if (parsedUrl.search) {
attributes['http.query'] = parsedUrl.search;
}

const url = getSanitizedUrlString(parsedUrl);
const parsedUrl = parseStringToURLObject(request.url);
const attributes = getBunServerSpanAttributes(request, parsedUrl);

isolationScope.setSDKProcessingMetadata({
normalizedRequest: {
url,
url: request.url,
method: request.method,
headers: request.headers.toJSON(),
query_string: extractQueryParamsFromUrl(url),
query_string: parsedUrl?.search,
} satisfies RequestEventData,
});

Expand All @@ -104,7 +95,9 @@ function instrumentBunServeOptions(serveOptions: Parameters<typeof Bun.serve>[0]
{
attributes,
op: 'http.server',
name: `${request.method} ${parsedUrl.path || '/'}`,
name: parsedUrl
? `${request.method} ${getSanitizedUrlStringFromUrlObject(parsedUrl)}`
: `${request.method} /`,
},
async span => {
try {
Expand Down Expand Up @@ -139,3 +132,28 @@ function instrumentBunServeOptions(serveOptions: Parameters<typeof Bun.serve>[0]
},
});
}

function getBunServerSpanAttributes(
request: Request,
parsedUrl: ReturnType<typeof parseStringToURLObject>,
): SpanAttributes {
const attributes: SpanAttributes = {
url: request.url,
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.http.bun.serve',
[SEMANTIC_ATTRIBUTE_HTTP_REQUEST_METHOD]: request.method || 'GET',
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'url',
};
if (parsedUrl) {
if (!isURLObjectRelative(parsedUrl)) {
attributes['http.url'] = parsedUrl.href;
attributes['server.address'] = parsedUrl.host;
}
if (parsedUrl.search) {
attributes['http.query'] = parsedUrl.search;
}
if (parsedUrl.hash) {
attributes['http.fragment'] = parsedUrl.hash;
}
}
return attributes;
}
Loading