Skip to content

Commit 00fa011

Browse files
committed
Update activeSpan instead of rootSpan
1 parent 39f469a commit 00fa011

File tree

2 files changed

+8
-26
lines changed

2 files changed

+8
-26
lines changed

packages/react/src/reactrouterv6-compat-utils.tsx

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -518,27 +518,28 @@ export function handleNavigation(opts: {
518518
}
519519

520520
const activeSpan = getActiveSpan();
521-
const rootSpan = activeSpan ? getRootSpan(activeSpan) : undefined;
522-
const isAlreadyInNavigationSpan = rootSpan && spanToJSON(rootSpan).op === 'navigation';
521+
const isAlreadyInNavigationSpan = activeSpan && spanToJSON(activeSpan).op === 'navigation';
523522

524523
// Cross usage can result in multiple navigation spans being created without this check
525524
if (isAlreadyInNavigationSpan) {
526525
// Check if we've already set the name for this span using a custom property
527526
const hasBeenNamed = (
528-
rootSpan as {
527+
activeSpan as {
529528
__sentry_navigation_name_set__?: boolean;
530529
}
531530
)?.__sentry_navigation_name_set__;
532531
if (!hasBeenNamed) {
533532
// This is the first time we're setting the name for this span
534-
const spanJson = spanToJSON(rootSpan);
533+
const spanJson = spanToJSON(activeSpan);
535534
if (!spanJson.timestamp) {
536-
rootSpan?.updateName(name);
537-
rootSpan?.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, source);
535+
activeSpan?.updateName(name);
538536
}
537+
539538
// Mark this span as having its name set to prevent future updates
540-
addNonEnumerableProperty(rootSpan, '__sentry_navigation_name_set__', true);
539+
addNonEnumerableProperty(activeSpan, '__sentry_navigation_name_set__', true);
541540
}
541+
542+
activeSpan?.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, source);
542543
// If we already have a name for this span, don't update it
543544
} else {
544545
const span = startBrowserTracingNavigationSpan(client, {

packages/react/test/reactrouter-cross-usage.test.tsx

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -288,11 +288,6 @@ describe('React Router cross usage of wrappers', () => {
288288

289289
// It's called 1 time from the wrapped `MemoryRouter`
290290
expect(mockStartBrowserTracingNavigationSpan).toHaveBeenCalledTimes(1);
291-
292-
// It's called 3 times from the 3 `useRoutes` components
293-
expect(mockNavigationSpan.updateName).toHaveBeenCalledTimes(3);
294-
expect(mockNavigationSpan.updateName).toHaveBeenLastCalledWith('/second-level/:id/third-level/:id');
295-
296291
expect(mockStartBrowserTracingNavigationSpan).toHaveBeenLastCalledWith(expect.any(BrowserClient), {
297292
name: '/second-level/:id/third-level/:id',
298293
attributes: {
@@ -459,11 +454,6 @@ describe('React Router cross usage of wrappers', () => {
459454

460455
// It's called 1 time from the wrapped `MemoryRouter`
461456
expect(mockStartBrowserTracingNavigationSpan).toHaveBeenCalledTimes(1);
462-
463-
// It's called 3 times from the 3 `useRoutes` components
464-
expect(mockNavigationSpan.updateName).toHaveBeenCalledTimes(3);
465-
expect(mockNavigationSpan.updateName).toHaveBeenLastCalledWith('/second-level/:id/third-level/:id');
466-
expect(mockNavigationSpan.setAttribute).toHaveBeenLastCalledWith(SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, 'route');
467457
});
468458
});
469459

@@ -611,10 +601,6 @@ describe('React Router cross usage of wrappers', () => {
611601
// It's called 1 time from the wrapped `createMemoryRouter`
612602
expect(mockStartBrowserTracingNavigationSpan).toHaveBeenCalledTimes(1);
613603

614-
// It's called 3 times from the 3 `SentryRoutes` components
615-
expect(mockNavigationSpan.updateName).toHaveBeenCalledTimes(3);
616-
expect(mockNavigationSpan.updateName).toHaveBeenLastCalledWith('/second-level/:id/third-level/:id');
617-
618604
expect(mockStartBrowserTracingNavigationSpan).toHaveBeenLastCalledWith(expect.any(BrowserClient), {
619605
name: '/second-level/:id/third-level/:id',
620606
attributes: {
@@ -790,11 +776,6 @@ describe('React Router cross usage of wrappers', () => {
790776

791777
// It's called 1 time from the wrapped `MemoryRouter`
792778
expect(mockStartBrowserTracingNavigationSpan).toHaveBeenCalledTimes(1);
793-
794-
// It's called 3 times from the 2 `useRoutes` components and 1 <SentryRoutes> component
795-
expect(mockNavigationSpan.updateName).toHaveBeenCalledTimes(3);
796-
797-
expect(mockNavigationSpan.updateName).toHaveBeenLastCalledWith('/second-level/:id/third-level/:id');
798779
expect(mockNavigationSpan.setAttribute).toHaveBeenLastCalledWith(SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, 'route');
799780
});
800781
});

0 commit comments

Comments
 (0)