Skip to content
Closed
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { handleCallbackErrors, SPAN_STATUS_ERROR } from '@sentry/core';
import { flushIfServerless } from '@sentry/core/server';
import { captureException, getActiveSpan, spanToJSON, startSpan } from '@sentry/node';
import { captureException, getActiveSpan, getRootSpan, spanToJSON, startSpan } from '@sentry/node';
import { isRedirect } from './utils';
import {
SENTRY_SEGMENT_NAME_SOURCE,
Expand All @@ -25,7 +25,9 @@ export async function withServerActionInstrumentation<A extends (...args: unknow
const activeSpan = getActiveSpan();

if (activeSpan) {
const spanData = spanToJSON(activeSpan).attributes;
// The request attributes live on the root `http.server` span. The active span can be a child of
// it, e.g. the Nitro route handler span.
const spanData = spanToJSON(getRootSpan(activeSpan)).attributes;

// In solid start, server function calls are made to `/_server` which doesn't tell us
// a lot. We rewrite the span's route to be that of the sever action name but only
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,37 @@ describe('withServerActionInstrumentation', () => {
expect(mockSpanSetAttribute).to.toHaveBeenCalledWith(SENTRY_SEGMENT_NAME_SOURCE, 'route');
});

it('sets a server action name on the root span when the active span is a child span', async () => {
setCurrentClient(createMockNodeClient());

const rootSpan = SentryCore.startInactiveSpan({
name: 'POST',
parentSpan: null,
attributes: {
'url.path': '/_server',
'http.request.method': 'POST',
[SENTRY_OP]: 'http.server',
},
});
const handlerSpan = SentryCore.startInactiveSpan({
name: 'POST /**',
parentSpan: rootSpan,
attributes: { [SENTRY_OP]: 'handler' },
});
mockGetActiveSpan.mockReturnValue(handlerSpan);

await serverActionGetPrefecture();

expect(spanToJSON(rootSpan)).toMatchObject({
name: 'POST getPrefecture',
attributes: expect.objectContaining({
'http.route': 'getPrefecture',
[SENTRY_SEGMENT_NAME_SOURCE]: 'route',
}),
});
expect(spanToJSON(handlerSpan).name).toBe('POST /**');
});

it('does not set a server action name if the active span had a non `/_server` `url.path`', async () => {
const span = new SentryCore.SentrySpan();
span.setAttribute('url.path', '/users/5');
Expand Down