-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(replay): Keep min. 30 seconds of data in error mode #7025
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,59 @@ | ||
import type { ReplayRecordingData } from '@sentry/types'; | ||
|
||
import type { AddEventResult, EventBuffer, RecordingEvent } from '../types'; | ||
import { PartitionedQueue } from './PartitionedQueue'; | ||
|
||
/** | ||
* A basic event buffer that does not do any compression. | ||
* Used as fallback if the compression worker cannot be loaded or is disabled. | ||
*/ | ||
export class EventBufferArray implements EventBuffer { | ||
/** All the events that are buffered to be sent. */ | ||
public events: RecordingEvent[]; | ||
protected _events: PartitionedQueue<RecordingEvent>; | ||
|
||
public constructor() { | ||
this.events = []; | ||
this._events = new PartitionedQueue<RecordingEvent>(); | ||
} | ||
|
||
/** @inheritdoc */ | ||
public get events(): RecordingEvent[] { | ||
return this._events.getItems(); | ||
} | ||
|
||
/** @inheritdoc */ | ||
public get hasEvents(): boolean { | ||
return this.events.length > 0; | ||
return this._events.getLength() > 0; | ||
} | ||
|
||
/** @inheritdoc */ | ||
public destroy(): void { | ||
this.events = []; | ||
this._events.clear(); | ||
} | ||
|
||
/** @inheritdoc */ | ||
public async addEvent(event: RecordingEvent, isCheckout?: boolean): Promise<AddEventResult> { | ||
if (isCheckout) { | ||
this.events = [event]; | ||
return; | ||
} | ||
public async clear(keepLastCheckout?: boolean): Promise<void> { | ||
this._events.clear(keepLastCheckout); | ||
} | ||
|
||
this.events.push(event); | ||
return; | ||
/** @inheritdoc */ | ||
public async addEvent(event: RecordingEvent, isCheckout?: boolean): Promise<AddEventResult> { | ||
this._events.add(event, isCheckout); | ||
} | ||
|
||
/** @inheritdoc */ | ||
public finish(): Promise<string> { | ||
public finish(): Promise<ReplayRecordingData> { | ||
return new Promise<string>(resolve => { | ||
// Make a copy of the events array reference and immediately clear the | ||
// events member so that we do not lose new events while uploading | ||
// attachment. | ||
const eventsRet = this.events; | ||
this.events = []; | ||
this._events.clear(); | ||
resolve(JSON.stringify(eventsRet)); | ||
}); | ||
} | ||
|
||
/** @inheritdoc */ | ||
public getEarliestTimestamp(): number | null { | ||
return this.events.map(event => event.timestamp).sort()[0] || null; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
packages/replay/src/eventBuffer/EventBufferPartitionedCompressionWorker.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import type { ReplayRecordingData } from '@sentry/types'; | ||
|
||
import type { RecordingEvent } from '../types'; | ||
import { EventBufferArray } from './EventBufferArray'; | ||
import { WorkerHandler } from './WorkerHandler'; | ||
|
||
/** | ||
* Event buffer that uses a web worker to compress events. | ||
* Exported only for testing. | ||
*/ | ||
export class EventBufferPartitionedCompressionWorker extends EventBufferArray { | ||
private _worker: WorkerHandler; | ||
|
||
public constructor(worker: Worker) { | ||
super(); | ||
this._worker = new WorkerHandler(worker); | ||
} | ||
/** | ||
* Ensure the worker is ready (or not). | ||
* This will either resolve when the worker is ready, or reject if an error occured. | ||
*/ | ||
public ensureReady(): Promise<void> { | ||
return this._worker.ensureReady(); | ||
} | ||
|
||
/** @inheritdoc */ | ||
public destroy(): void { | ||
this._worker.destroy(); | ||
super.destroy(); | ||
} | ||
|
||
/** | ||
* Finish the event buffer and return the compressed data. | ||
*/ | ||
public finish(): Promise<ReplayRecordingData> { | ||
const { events } = this; | ||
this._events.clear(); | ||
|
||
return this._compressEvents(events); | ||
} | ||
|
||
/** Compress a given array of events at once. */ | ||
private _compressEvents(events: RecordingEvent[]): Promise<Uint8Array> { | ||
return this._worker.postMessage<Uint8Array>('compress', JSON.stringify(events)); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/** | ||
* A queue with partitions for each checkout. | ||
*/ | ||
export class PartitionedQueue<T> { | ||
private _items: T[]; | ||
private _lastCheckoutPos?: number; | ||
|
||
public constructor() { | ||
this._items = []; | ||
} | ||
|
||
/** Add an item to the queue. */ | ||
public add(record: T, isCheckout?: boolean): void { | ||
this._items.push(record); | ||
|
||
if (isCheckout) { | ||
this._lastCheckoutPos = this._items.length - 1; | ||
} | ||
} | ||
|
||
/** | ||
* Clear items from the queue. | ||
* If `keepLastCheckout` is given, all items after the last checkout will be kept. | ||
*/ | ||
public clear(keepLastCheckout?: boolean): void { | ||
if (!keepLastCheckout) { | ||
this._items = []; | ||
this._lastCheckoutPos = undefined; | ||
return; | ||
} | ||
|
||
if (this._lastCheckoutPos) { | ||
this._items = this._items.splice(this._lastCheckoutPos); | ||
this._lastCheckoutPos = undefined; | ||
} | ||
|
||
// Else, there is only a single checkout recorded yet, which we don't want to clear out | ||
} | ||
|
||
/** Get all items */ | ||
public getItems(): T[] { | ||
return this._items; | ||
} | ||
|
||
/** Get the number of items that are queued. */ | ||
public getLength(): number { | ||
return this._items.length; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Just to clarify for myself, we examine the buffer for the earliest event because we want the replay "start time" to be when the 1st checkout in the buffer happens.
So if session has been running for 2.5 minutes, we would want the replay to "start" at ~2 minutes. And
replay.context.earliestEvent
(before this block of code), would represent the earliest event of the entire "session".So an issue I can think of is with the performance observer. I believe we only add them when we flush, so those events will end up becoming the earliest events anyway (which might be fine).
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.
Yeah, to be honest all of this is a bit brittle to me 😅 but this was the best I could come up with to handle this in a somewhat reasonable way. I wonder if it makes sense to maybe compute this on the server? Or generally change how we keep this a bit, not sure...
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.
semi-related to this PR but I was also wondering about that - do we discard events with timestamps before the initial full checkout on the server? Or would sending such events cause problems?
Uh oh!
There was an error while loading. Please reload this page.
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.
We discard events that are older than SESSION_IDLE_TIMEOUT minutes ago. Because we get buffered data from performance observer, those occur before initial checkout.
I wonder if we need to change/move this logic as well
sentry-javascript/packages/replay/src/replay.ts
Lines 552 to 557 in c3dd355
I think this is to meant to update session start time so we know when to correctly timeout a session.