Skip to content

Commit c32e738

Browse files
committed
feat: added http status code handling
1 parent fb8e4d5 commit c32e738

1 file changed

Lines changed: 19 additions & 3 deletions

File tree

packages/nitro/src/runtime/plugins/server.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
httpHeadersToSpanAttributes,
88
parseStringToURLObject,
99
SEMANTIC_ATTRIBUTE_SENTRY_OP,
10+
setHttpStatus,
1011
type Span,
1112
SPAN_STATUS_ERROR,
1213
startSpanManual,
@@ -43,8 +44,24 @@ export default definePlugin(nitroApp => {
4344
globalWithTraceChannels.__SENTRY_NITRO_HTTP_CHANNELS_INSTRUMENTED__ = true;
4445
});
4546

46-
function onTraceEnd(data: { span?: Span }): void {
47-
data.span?.end();
47+
/**
48+
* Extracts the HTTP status code from a tracing channel result.
49+
* The result is the return value of the traced handler, which is a Response for srvx
50+
* and may or may not be a Response for h3.
51+
*/
52+
function getResponseStatusCode(result: unknown): number | undefined {
53+
if (result && typeof result === 'object' && 'status' in result && typeof result.status === 'number') {
54+
return result.status;
55+
}
56+
return undefined;
57+
}
58+
59+
function onTraceEnd(data: { span?: Span; result?: unknown }): void {
60+
const statusCode = getResponseStatusCode(data.result);
61+
if (data.span && statusCode !== undefined) {
62+
setHttpStatus(data.span, statusCode);
63+
data.span.end();
64+
}
4865
}
4966

5067
function onTraceError(data: { span?: Span; error: unknown }): void {
@@ -120,7 +137,6 @@ function setupSrvxTracingChannels(): void {
120137
asyncStart: () => {},
121138
end: () => {},
122139
asyncEnd: data => {
123-
// data.span?.setAttribute('http.response.status_code', data.result.);
124140
onTraceEnd(data);
125141

126142
// Reset parent span reference after the fetch handler completes

0 commit comments

Comments
 (0)