Skip to content

Commit c475437

Browse files
committed
ref(replay): Capture parametrized route
1 parent 6be91da commit c475437

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

packages/replay/src/replay.ts

Lines changed: 20 additions & 1 deletion
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 {
@@ -65,6 +65,12 @@ export class ReplayContainer implements ReplayContainerInterface {
6565
*/
6666
public recordingMode: ReplayRecordingMode = 'session';
6767

68+
/**
69+
* The current or last active transcation.
70+
* This is only available when performance is enabled.
71+
*/
72+
public lastTransaction?: Transaction;
73+
6874
/**
6975
* These are here so we can overwrite them in tests etc.
7076
* @hidden
@@ -546,6 +552,19 @@ export class ReplayContainer implements ReplayContainerInterface {
546552
this._context.urls.push(url);
547553
}
548554

555+
/**
556+
* This will get the parametrized route name of the current page.
557+
* This is only available if performance is enabled, and if an instrumented router is used.
558+
*/
559+
public getCurrentRoute(): string | undefined {
560+
const lastTransaction = this.lastTransaction || getCurrentHub().getScope().getTransaction();
561+
if (!lastTransaction || ['route', 'custom'].includes(lastTransaction.metadata.source)) {
562+
return undefined;
563+
}
564+
565+
return lastTransaction.name;
566+
}
567+
549568
/**
550569
* Initialize and start all listeners to varying events (DOM,
551570
* Performance Observer, Recording, Sentry SDK, etc)

packages/replay/src/types.ts

Lines changed: 3 additions & 0 deletions
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

@@ -498,6 +499,7 @@ export interface ReplayContainer {
498499
session: Session | undefined;
499500
recordingMode: ReplayRecordingMode;
500501
timeouts: Timeouts;
502+
lastTransaction?: Transaction;
501503
isEnabled(): boolean;
502504
isPaused(): boolean;
503505
getContext(): InternalEventContext;
@@ -516,6 +518,7 @@ export interface ReplayContainer {
516518
getSessionId(): string | undefined;
517519
checkAndHandleExpiredSession(): boolean | void;
518520
setInitialState(): void;
521+
getCurrentRoute(): string | undefined;
519522
}
520523

521524
export interface ReplayPerformanceEntry<T> {

packages/replay/src/util/addGlobalListeners.ts

Lines changed: 10 additions & 0 deletions
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)