Skip to content

Commit 2d86760

Browse files
authored
recorder maintains a ready flag (#1330)
1 parent b99de9b commit 2d86760

File tree

4 files changed

+45
-0
lines changed

4 files changed

+45
-0
lines changed

src/browser/replay/recorder.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,17 @@ export default class Recorder {
2727

2828
this.options = options;
2929
this._recordFn = recordFn;
30+
this._isReady = false;
3031
}
3132

3233
get isRecording() {
3334
return this._stopFn !== null;
3435
}
3536

37+
get isReady() {
38+
return this._isReady;
39+
}
40+
3641
get options() {
3742
return this._options;
3843
}
@@ -127,6 +132,9 @@ export default class Recorder {
127132

128133
this._stopFn = this._recordFn({
129134
emit: (event, isCheckout) => {
135+
if (!this._ready && event.type === EventType.FullSnapshot) {
136+
this._isReady = true;
137+
}
130138
if (this.options.debug?.logEmits) {
131139
this._logEvent(event, isCheckout);
132140
}
@@ -158,6 +166,7 @@ export default class Recorder {
158166

159167
this._stopFn();
160168
this._stopFn = null;
169+
this._isReady = false;
161170

162171
return this;
163172
}
@@ -167,6 +176,7 @@ export default class Recorder {
167176
previous: [],
168177
current: [],
169178
};
179+
this._isReady = false;
170180
}
171181

172182
_collectEvents() {

src/browser/replay/replayManager.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ export default class ReplayManager {
7979
* @returns {string} A unique identifier for this replay
8080
*/
8181
add(replayId, occurrenceUuid) {
82+
if (!this._recorder.isReady) {
83+
logger.warn('ReplayManager.add: Recorder is not ready, cannot export replay');
84+
return null;
85+
}
8286
replayId = replayId || id.gen(8);
8387

8488
// Start processing the replay in the background

test/browser.replay.recorder.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ describe('Recorder', function () {
4040
const recorder = new Recorder({}, recordFnStub);
4141

4242
expect(recorder.isRecording).to.be.false;
43+
expect(recorder.isReady).to.be.false;
4344
expect(recorder.options).to.deep.equal({
4445
enabled: undefined,
4546
autoStart: undefined,
@@ -162,6 +163,21 @@ describe('Recorder', function () {
162163
});
163164
});
164165

166+
it('should be ready after first full snapshot', function () {
167+
const recorder = new Recorder({}, recordFnStub);
168+
recorder.start();
169+
170+
// First checkout
171+
emitCallback({ timestamp: 0, type: EventType.Meta, data: {} }, false);
172+
expect(recorder.isReady).to.be.false;
173+
174+
emitCallback(
175+
{ timestamp: 10, type: EventType.FullSnapshot, data: {} },
176+
false,
177+
);
178+
expect(recorder.isReady).to.be.true;
179+
});
180+
165181
it('should handle checkout events correctly', function () {
166182
const recorder = new Recorder({}, recordFnStub);
167183
recorder.start();

test/replay/unit/replayManager.test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import id from '../../../src/tracing/id.js';
1111
class MockRecorder {
1212
constructor() {
1313
this.exportRecordingSpan = sinon.stub();
14+
this.isReady = true;
1415
}
1516
}
1617

@@ -220,6 +221,20 @@ describe('ReplayManager', function () {
220221
expect(id.gen.calledWith(8)).to.be.true;
221222
expect(processStub.calledWith('1234567890abcdef', uuid)).to.be.true;
222223
});
224+
225+
it('should return without replayId when recorder is not ready', function () {
226+
const uuid = '12345678-1234-5678-1234-1234567890ab';
227+
const processStub = sinon
228+
.stub(replayManager, '_exportSpansAndAddTracingPayload')
229+
.resolves();
230+
mockRecorder.isReady = false;
231+
232+
const replayId = replayManager.add(null, uuid);
233+
234+
expect(replayId).to.be.null;
235+
expect(id.gen.called).to.be.false;
236+
expect(processStub.called).to.be.false;
237+
});
223238
});
224239

225240
describe('send', function () {

0 commit comments

Comments
 (0)