Skip to content
Open
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;
// Read the request's `http.server` root span, which is the span we rename. The active span is
// usually a framework handler span (e.g. Nitro's h3 `handler` span) without any URL attributes.
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,28 @@ 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 nested handler span', async () => {
const rootSpan = new SentryCore.SentrySpan({
attributes: {
'url.path': '/_server',
[SENTRY_OP]: 'http.server',
},
});
const handlerSpan = new SentryCore.SentrySpan({
attributes: {
[SENTRY_OP]: 'handler',
},
});
SentryCore.addChildSpanToSpan(rootSpan, handlerSpan);
mockGetActiveSpan.mockReturnValue(handlerSpan);
const mockRootSpanSetAttribute = vi.spyOn(rootSpan, 'setAttribute');

await serverActionGetPrefecture();

expect(mockRootSpanSetAttribute).toHaveBeenCalledWith('http.route', 'getPrefecture');
expect(mockRootSpanSetAttribute).toHaveBeenCalledWith(SENTRY_SEGMENT_NAME_SOURCE, 'route');
});

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
Loading