-
Notifications
You must be signed in to change notification settings - Fork 55
fix(session-replay-browser): Optimize function restart logic #1310
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice! Thanks for adding this!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
|
|
||
| // NOTE: If there is already an existing active recording, exit early unless forceRestart is true | ||
| if (this.recordCancelCallback && !forceRestart) { | ||
| this.loggerProvider.debug(`A capture is already in progress and forceRestart is false. Exiting.`); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Maybe instead of "Exiting" we could say "not restarting". I wouldn't want this to leak to users who made get the wrong impression.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the feedback - c81dab9
| async recordEvents(recordEventsConfig?: { shouldLogMetadata?: boolean; forceRestart?: boolean }) { | ||
| const { shouldLogMetadata = true, forceRestart = true } = recordEventsConfig || {}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking these could be non null since their defaults may not be obvious and maybe it's better to be more explicit here. It also saves a null check.
| async recordEvents(recordEventsConfig?: { shouldLogMetadata?: boolean; forceRestart?: boolean }) { | |
| const { shouldLogMetadata = true, forceRestart = true } = recordEventsConfig || {}; | |
| async recordEvents({shouldLogMetadata, forceRestart}: { shouldLogMetadata: boolean; forceRestart: boolean }) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great call - f75a512
| this.loggerProvider.debug(`A capture is already in progress and forceRestart is false. Exiting.`); | ||
| return; | ||
| } | ||
| this.loggerProvider.debug(`Starting new capture for session.`); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor nit: Maybe we should include the session replay ID or session ID? I think it wouldn't hurt and would potentially help with debugging.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done! c68312f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
- @amplitude/[email protected] - @amplitude/[email protected] - @amplitude/[email protected]
Summary
This PR optimizes some of the logic around starting/restarting replay capture.
Previously, the following three actions would cause the replay to start or restart:
setSessionId())This PR stops the replay capture from being restarted when an event is emitted from the SDK.
How does this fix work?
The primary change is in
recordEvents():We will check for an existing callback and if the user does not pass forceRestart (default is
forceRestart=false), we will exit early and not re-create the SDK.In the case of "focus" or "session ID changes", we pass
forceRestart=true, to ensure that a new capture is created.Verification
Beyond unit tests, I created a test app and installed this version of the SDK. I was able to observe the behavior of the SDK on a focus event and then when sending 5 events via a button click:
The first 3 logs represent the "focus" event. The fourth log represents the button click.
Notes:
Given a call to
track()in your application, it goes through the Amplitude Analytics SDK's pipeline:For us, that means the plugin execution pipeline. So it goes through timeline.ts, which executes the various types of plugins:
Our plugin (the SR plugin) is an enrichment plugin (source), so its execute() method is called for every single event (source)
The execute() method calls:
The recordEvents() method:
Checklist