Skip to content

Commit 2f9d355

Browse files
committed
feat: ensure trace channel spans has correct origins
1 parent 2381e3b commit 2f9d355

File tree

4 files changed

+38
-8
lines changed

4 files changed

+38
-8
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { defineHandler, setResponseHeader } from 'nitro/h3';
2+
3+
export default defineHandler(event => {
4+
// Simple middleware that adds a custom header to verify it ran
5+
setResponseHeader(event, 'x-sentry-test-middleware', 'executed');
6+
});
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { expect, test } from '@playwright/test';
2+
import { waitForTransaction } from '@sentry-internal/test-utils';
3+
4+
test('Creates middleware spans for requests', async ({ request }) => {
5+
const transactionEventPromise = waitForTransaction('nitro-3', event => {
6+
return event?.transaction === 'GET /api/test-transaction';
7+
});
8+
9+
const response = await request.get('/api/test-transaction');
10+
11+
expect(response.headers()['x-sentry-test-middleware']).toBe('executed');
12+
13+
const transactionEvent = await transactionEventPromise;
14+
15+
// h3 middleware spans have origin auto.http.nitro.h3 and op middleware.nitro
16+
const h3MiddlewareSpans = transactionEvent.spans?.filter(
17+
span => span.origin === 'auto.http.nitro.h3' && span.op === 'middleware.nitro',
18+
);
19+
expect(h3MiddlewareSpans?.length).toBeGreaterThanOrEqual(1);
20+
});

dev-packages/e2e-tests/test-applications/nitro-3/tests/transactions.test.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ test('Sends a transaction event for a successful route', async ({ request }) =>
1717
}),
1818
);
1919

20-
expect(transactionEvent.contexts?.trace).toEqual(
21-
expect.objectContaining({
22-
op: expect.stringContaining('http'),
23-
span_id: expect.stringMatching(/[a-f0-9]{16}/),
24-
trace_id: expect.stringMatching(/[a-f0-9]{32}/),
25-
}),
26-
);
20+
// srvx.request creates a span for the request
21+
const srvxSpans = transactionEvent.spans?.filter(span => span.origin === 'auto.http.nitro.srvx');
22+
expect(srvxSpans?.length).toBeGreaterThanOrEqual(1);
23+
24+
// h3 creates a child span for the route handler
25+
const h3Spans = transactionEvent.spans?.filter(span => span.origin === 'auto.http.nitro.h3');
26+
expect(h3Spans?.length).toBeGreaterThanOrEqual(1);
2727
});
2828

2929
test('Sets correct HTTP status code on transaction', async ({ request }) => {

packages/nitro/src/runtime/hooks/captureTracingEvents.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
httpHeadersToSpanAttributes,
88
parseStringToURLObject,
99
SEMANTIC_ATTRIBUTE_SENTRY_OP,
10+
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
1011
setHttpStatus,
1112
type Span,
1213
SPAN_STATUS_ERROR,
@@ -80,6 +81,7 @@ function setupH3TracingChannels(): void {
8081
name: spanName,
8182
attributes: {
8283
...urlAttributes,
84+
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.http.nitro.h3',
8385
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: data?.type === 'middleware' ? 'middleware.nitro' : 'http.server',
8486
},
8587
},
@@ -119,6 +121,7 @@ function setupSrvxTracingChannels(): void {
119121
attributes: {
120122
...urlAttributes,
121123
...headerAttributes,
124+
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.http.nitro.srvx',
122125
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: data.middleware ? 'middleware.nitro' : 'http.server',
123126
'server.port': data.server.options.port,
124127
},
@@ -155,7 +158,7 @@ function setupSrvxTracingChannels(): void {
155158
}
156159

157160
const parsedUrl = data.request._url ? parseStringToURLObject(data.request._url.href) : undefined;
158-
const [, urlAttributes] = getHttpSpanDetailsFromUrlObject(parsedUrl, 'server', 'auto.http.nitro.srvx.middleware', {
161+
const [, urlAttributes] = getHttpSpanDetailsFromUrlObject(parsedUrl, 'server', 'auto.http.nitro.srvx', {
159162
method: data.request.method,
160163
});
161164

@@ -165,6 +168,7 @@ function setupSrvxTracingChannels(): void {
165168
name: `${data.middleware?.handler.name ?? 'unknown'} - ${data.request.method} ${data.request._url?.pathname}`,
166169
attributes: {
167170
...urlAttributes,
171+
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.http.nitro.srvx',
168172
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'middleware.nitro',
169173
},
170174
parentSpan: requestParentSpan || undefined,

0 commit comments

Comments
 (0)