-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(replay): Keep 30-60s instead of 0-60s of recording in replay errror mode #6924
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
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,54 +1,59 @@ | ||
import type { AddEventResult, EventBuffer, RecordingEvent } from '../types'; | ||
import type { ReplayRecordingData } from '@sentry/types'; | ||
|
||
import type { 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 { | ||
private _events: RecordingEvent[]; | ||
private _events: PartitionedQueue<RecordingEvent>; | ||
|
||
public constructor() { | ||
this._events = []; | ||
this._events = new PartitionedQueue<RecordingEvent>(); | ||
} | ||
|
||
/** @inheritdoc */ | ||
public get pendingLength(): number { | ||
return this._events.length; | ||
return this._events.getLength(); | ||
} | ||
|
||
/** | ||
* Returns the raw events that are buffered. In `EventBufferArray`, this is the | ||
* same as `this._events`. | ||
*/ | ||
/** @inheritdoc */ | ||
public get pendingEvents(): RecordingEvent[] { | ||
return this._events; | ||
return this._events.getItems(); | ||
} | ||
|
||
/** @inheritdoc */ | ||
public getEarliestTimestamp(): number | null { | ||
return this.pendingEvents.map(event => event.timestamp).sort()[0] || null; | ||
} | ||
|
||
/** @inheritdoc */ | ||
public destroy(): void { | ||
this._events = []; | ||
this.clear(); | ||
} | ||
|
||
/** @inheritdoc */ | ||
public async addEvent(event: RecordingEvent, isCheckout?: boolean): Promise<AddEventResult> { | ||
if (isCheckout) { | ||
this._events = [event]; | ||
return; | ||
} | ||
public addEvent(event: RecordingEvent, isCheckout?: boolean): void { | ||
this._events.add(event, isCheckout); | ||
} | ||
|
||
this._events.push(event); | ||
return; | ||
/** @inheritdoc */ | ||
public clear(keepLastCheckout?: boolean): void { | ||
this._events.clear(keepLastCheckout); | ||
} | ||
|
||
/** @inheritdoc */ | ||
public finish(): Promise<string> { | ||
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 = []; | ||
resolve(JSON.stringify(eventsRet)); | ||
}); | ||
public finish(): Promise<ReplayRecordingData> { | ||
const { pendingEvents } = this; | ||
this.clear(); | ||
|
||
return Promise.resolve(this._finishRecording(pendingEvents)); | ||
} | ||
|
||
/** Finish in a sync manner. */ | ||
protected _finishRecording(events: RecordingEvent[]): ReplayRecordingData { | ||
return 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,23 @@ | ||
import type { ReplayRecordingData } from '@sentry/types'; | ||
import { logger } from '@sentry/utils'; | ||
|
||
import type { AddEventResult, EventBuffer, RecordingEvent, WorkerRequest, WorkerResponse } from '../types'; | ||
import type { RecordingEvent, WorkerRequest, WorkerResponse } from '../types'; | ||
import { EventBufferArray } from './EventBufferArray'; | ||
|
||
/** | ||
* Event buffer that uses a web worker to compress events. | ||
* Exported only for testing. | ||
*/ | ||
export class EventBufferCompressionWorker implements EventBuffer { | ||
/** | ||
* Keeps track of the list of events since the last flush that have not been compressed. | ||
* For example, page is reloaded and a flush attempt is made, but | ||
* `finish()` (and thus the flush), does not complete. | ||
*/ | ||
public _pendingEvents: RecordingEvent[] = []; | ||
|
||
export class EventBufferCompressionWorker extends EventBufferArray { | ||
private _worker: Worker; | ||
private _eventBufferItemLength: number = 0; | ||
|
||
private _id: number = 0; | ||
private _ensureReadyPromise?: Promise<void>; | ||
|
||
public constructor(worker: Worker) { | ||
super(); | ||
this._worker = worker; | ||
} | ||
|
||
/** | ||
* The number of raw events that are buffered. This may not be the same as | ||
* the number of events that have been compresed in the worker because | ||
* `addEvent` is async. | ||
*/ | ||
public get pendingLength(): number { | ||
return this._eventBufferItemLength; | ||
} | ||
|
||
/** | ||
* Returns a list of the raw recording events that are being compressed. | ||
*/ | ||
public get pendingEvents(): RecordingEvent[] { | ||
return this._pendingEvents; | ||
} | ||
|
||
/** | ||
* Ensure the worker is ready (or not). | ||
* This will either resolve when the worker is ready, or reject if an error occured. | ||
|
@@ -75,56 +53,34 @@ export class EventBufferCompressionWorker implements EventBuffer { | |
return this._ensureReadyPromise; | ||
} | ||
|
||
/** | ||
* Destroy the event buffer. | ||
*/ | ||
/** @inheritdoc */ | ||
public destroy(): void { | ||
__DEBUG_BUILD__ && logger.log('[Replay] Destroying compression worker'); | ||
this._worker.terminate(); | ||
} | ||
|
||
/** | ||
* Add an event to the event buffer. | ||
* | ||
* Returns true if event was successfuly received and processed by worker. | ||
*/ | ||
public async addEvent(event: RecordingEvent, isCheckout?: boolean): Promise<AddEventResult> { | ||
if (isCheckout) { | ||
// This event is a checkout, make sure worker buffer is cleared before | ||
// proceeding. | ||
await this._postMessage({ | ||
id: this._getAndIncrementId(), | ||
method: 'init', | ||
args: [], | ||
}); | ||
} | ||
|
||
// Don't store checkout events in `_pendingEvents` because they are too large | ||
if (!isCheckout) { | ||
this._pendingEvents.push(event); | ||
} | ||
|
||
return this._sendEventToWorker(event); | ||
super.destroy(); | ||
} | ||
|
||
/** | ||
* Finish the event buffer and return the compressed data. | ||
*/ | ||
public async finish(): Promise<ReplayRecordingData> { | ||
const pendingEvents = this.pendingEvents.slice(); | ||
|
||
this.clear(); | ||
|
||
try { | ||
return await this._finishRequest(this._getAndIncrementId()); | ||
return await this._compressEvents(this._getAndIncrementId(), pendingEvents); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change means that if something goes wrong when processing, we don't error out but just send the events uncompressed. |
||
} catch (error) { | ||
__DEBUG_BUILD__ && logger.error('[Replay] Error when trying to compress events', error); | ||
// fall back to uncompressed | ||
const events = this.pendingEvents; | ||
return JSON.stringify(events); | ||
return this._finishRecording(pendingEvents); | ||
} | ||
} | ||
|
||
/** | ||
* Post message to worker and wait for response before resolving promise. | ||
*/ | ||
private _postMessage<T>({ id, method, args }: WorkerRequest): Promise<T> { | ||
private _postMessage<T>({ id, method, arg }: WorkerRequest): Promise<T> { | ||
return new Promise((resolve, reject) => { | ||
const listener = ({ data }: MessageEvent): void => { | ||
const response = data as WorkerResponse; | ||
|
@@ -152,51 +108,18 @@ export class EventBufferCompressionWorker implements EventBuffer { | |
resolve(response.response as T); | ||
}; | ||
|
||
let stringifiedArgs; | ||
try { | ||
stringifiedArgs = JSON.stringify(args); | ||
} catch (err) { | ||
__DEBUG_BUILD__ && logger.error('[Replay] Error when trying to stringify args', err); | ||
stringifiedArgs = '[]'; | ||
} | ||
|
||
// Note: we can't use `once` option because it's possible it needs to | ||
// listen to multiple messages | ||
this._worker.addEventListener('message', listener); | ||
this._worker.postMessage({ id, method, args: stringifiedArgs }); | ||
}); | ||
} | ||
|
||
/** | ||
* Send the event to the worker. | ||
*/ | ||
private async _sendEventToWorker(event: RecordingEvent): Promise<AddEventResult> { | ||
const promise = this._postMessage<void>({ | ||
id: this._getAndIncrementId(), | ||
method: 'addEvent', | ||
args: [event], | ||
this._worker.postMessage({ id, method, arg }); | ||
}); | ||
|
||
// XXX: See note in `get length()` | ||
this._eventBufferItemLength++; | ||
|
||
return promise; | ||
} | ||
|
||
/** | ||
* Finish the request and return the compressed data from the worker. | ||
*/ | ||
private async _finishRequest(id: number): Promise<Uint8Array> { | ||
const promise = this._postMessage<Uint8Array>({ id, method: 'finish', args: [] }); | ||
|
||
// XXX: See note in `get length()` | ||
this._eventBufferItemLength = 0; | ||
|
||
await promise; | ||
|
||
this._pendingEvents = []; | ||
|
||
return promise; | ||
private async _compressEvents(id: number, events: RecordingEvent[]): Promise<Uint8Array> { | ||
return this._postMessage<Uint8Array>({ id, method: 'compress', arg: JSON.stringify(events) }); | ||
} | ||
|
||
/** Get the current ID and increment it for the next call. */ | ||
|
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.
make function async?
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 need to return a promise here to satisfy the interface, so need to wrap 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.
if it's async, it will return an promise in any case :)
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.
it's currently not async, though!