Skip to content

Commit d5551aa

Browse files
authored
ref(replay): Capture parametrized route (#8095)
1 parent df5c84a commit d5551aa

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

packages/replay/src/replay.ts

+20-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable max-lines */ // TODO: We might want to split this file up
22
import { EventType, record } from '@sentry-internal/rrweb';
33
import { captureException, getCurrentHub } from '@sentry/core';
4-
import type { Breadcrumb, ReplayRecordingMode } from '@sentry/types';
4+
import type { Breadcrumb, ReplayRecordingMode, Transaction } from '@sentry/types';
55
import { logger } from '@sentry/utils';
66

77
import {
@@ -68,6 +68,12 @@ export class ReplayContainer implements ReplayContainerInterface {
6868
*/
6969
public recordingMode: ReplayRecordingMode = 'session';
7070

71+
/**
72+
* The current or last active transcation.
73+
* This is only available when performance is enabled.
74+
*/
75+
public lastTransaction?: Transaction;
76+
7177
/**
7278
* These are here so we can overwrite them in tests etc.
7379
* @hidden
@@ -614,6 +620,19 @@ export class ReplayContainer implements ReplayContainerInterface {
614620
return res;
615621
}
616622

623+
/**
624+
* This will get the parametrized route name of the current page.
625+
* This is only available if performance is enabled, and if an instrumented router is used.
626+
*/
627+
public getCurrentRoute(): string | undefined {
628+
const lastTransaction = this.lastTransaction || getCurrentHub().getScope().getTransaction();
629+
if (!lastTransaction || !['route', 'custom'].includes(lastTransaction.metadata.source)) {
630+
return undefined;
631+
}
632+
633+
return lastTransaction.name;
634+
}
635+
617636
/**
618637
* Initialize and start all listeners to varying events (DOM,
619638
* Performance Observer, Recording, Sentry SDK, etc)

packages/replay/src/types.ts

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type {
44
ReplayRecordingData,
55
ReplayRecordingMode,
66
SentryWrappedXMLHttpRequest,
7+
Transaction,
78
XhrBreadcrumbHint,
89
} from '@sentry/types';
910

@@ -535,6 +536,7 @@ export interface ReplayContainer {
535536
session: Session | undefined;
536537
recordingMode: ReplayRecordingMode;
537538
timeouts: Timeouts;
539+
lastTransaction?: Transaction;
538540
throttledAddEvent: (
539541
event: RecordingEvent,
540542
isCheckout?: boolean,
@@ -559,6 +561,7 @@ export interface ReplayContainer {
559561
getSessionId(): string | undefined;
560562
checkAndHandleExpiredSession(): boolean | void;
561563
setInitialState(): void;
564+
getCurrentRoute(): string | undefined;
562565
}
563566

564567
export interface ReplayPerformanceEntry<T> {

packages/replay/src/util/addGlobalListeners.ts

+10
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,16 @@ export function addGlobalListeners(replay: ReplayContainer): void {
4040
dsc.replay_id = replayId;
4141
}
4242
});
43+
44+
client.on('startTransaction', transaction => {
45+
replay.lastTransaction = transaction;
46+
});
47+
48+
// We may be missing the initial startTransaction due to timing issues,
49+
// so we capture it on finish again.
50+
client.on('finishTransaction', transaction => {
51+
replay.lastTransaction = transaction;
52+
});
4353
}
4454
}
4555

0 commit comments

Comments
 (0)