|
1 | 1 | /* eslint-disable max-lines */ // TODO: We might want to split this file up |
2 | 2 | import { EventType, record } from '@sentry-internal/rrweb'; |
3 | | -import { SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, getActiveSpan, getClient, getRootSpan, spanToJSON } from '@sentry/core'; |
| 3 | +import { |
| 4 | + SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, |
| 5 | + getActiveSpan, |
| 6 | + getClient, |
| 7 | + getRootSpan, |
| 8 | + setTag, |
| 9 | + spanToJSON, |
| 10 | +} from '@sentry/core'; |
4 | 11 | import type { ReplayRecordingMode, Span } from '@sentry/types'; |
5 | 12 | import { logger } from './util/logger'; |
6 | 13 |
|
@@ -1215,28 +1222,14 @@ export class ReplayContainer implements ReplayContainerInterface { |
1215 | 1222 | return; |
1216 | 1223 | } |
1217 | 1224 |
|
1218 | | - const start = this.session.started; |
1219 | | - const now = Date.now(); |
1220 | | - const duration = now - start; |
1221 | | - |
1222 | 1225 | // A flush is about to happen, cancel any queued flushes |
1223 | 1226 | this._debouncedFlush.cancel(); |
1224 | 1227 |
|
1225 | | - // If session is too short, or too long (allow some wiggle room over maxReplayDuration), do not send it |
1226 | | - // This _should_ not happen, but it may happen if flush is triggered due to a page activity change or similar |
1227 | | - const tooShort = duration < this._options.minReplayDuration; |
1228 | | - const tooLong = duration > this._options.maxReplayDuration + 5_000; |
1229 | | - if (tooShort || tooLong) { |
1230 | | - DEBUG_BUILD && |
1231 | | - logger.info( |
1232 | | - `Session duration (${Math.floor(duration / 1000)}s) is too ${ |
1233 | | - tooShort ? 'short' : 'long' |
1234 | | - }, not sending replay.`, |
1235 | | - ); |
| 1228 | + const isValidDuration = this._checkReplayDurationDuringFlush(); |
1236 | 1229 |
|
1237 | | - if (tooShort) { |
1238 | | - this._debouncedFlush(); |
1239 | | - } |
| 1230 | + // XXX: disregard durations for buffer mode for debug purposes |
| 1231 | + if (!isValidDuration && this.recordingMode !== 'buffer') { |
| 1232 | + DEBUG_BUILD && logger.info('Invalid duration while buffering'); |
1240 | 1233 | return; |
1241 | 1234 | } |
1242 | 1235 |
|
@@ -1272,6 +1265,47 @@ export class ReplayContainer implements ReplayContainerInterface { |
1272 | 1265 | } |
1273 | 1266 | }; |
1274 | 1267 |
|
| 1268 | + /** |
| 1269 | + * Checks to see if replay duration is within bounds during a flush. If it is |
| 1270 | + * too short, will queue up a new flush to prevent short replays. |
| 1271 | + * |
| 1272 | + * Returns true if duration is ok, false otherwise |
| 1273 | + */ |
| 1274 | + private _checkReplayDurationDuringFlush(): boolean { |
| 1275 | + if (!this.session) { |
| 1276 | + return false; |
| 1277 | + } |
| 1278 | + |
| 1279 | + const start = this.session.started; |
| 1280 | + const now = Date.now(); |
| 1281 | + const duration = now - start; |
| 1282 | + |
| 1283 | + // If session is too short, or too long (allow some wiggle room over maxReplayDuration), do not send it |
| 1284 | + // This _should_ not happen, but it may happen if flush is triggered due to a page activity change or similar |
| 1285 | + const tooShort = duration < this._options.minReplayDuration; |
| 1286 | + const tooLong = duration > this._options.maxReplayDuration + 5_000; |
| 1287 | + if (tooShort || tooLong) { |
| 1288 | + DEBUG_BUILD && |
| 1289 | + logger.info( |
| 1290 | + `Session duration (${Math.floor(duration / 1000)}s) is too ${ |
| 1291 | + tooShort ? 'short' : 'long' |
| 1292 | + }, not sending replay.`, |
| 1293 | + ); |
| 1294 | + |
| 1295 | + if (tooShort) { |
| 1296 | + this._debouncedFlush(); |
| 1297 | + } |
| 1298 | + |
| 1299 | + // XXX: disregard durations for buffer mode for debug purposes |
| 1300 | + if (this.recordingMode === 'buffer') { |
| 1301 | + setTag(`replay.${tooShort ? 'tooShort' : 'tooLong'}`, true); |
| 1302 | + } |
| 1303 | + return false; |
| 1304 | + } |
| 1305 | + |
| 1306 | + return true; |
| 1307 | + } |
| 1308 | + |
1275 | 1309 | /** Save the session, if it is sticky */ |
1276 | 1310 | private _maybeSaveSession(): void { |
1277 | 1311 | if (this.session && this._options.stickySession) { |
|
0 commit comments