fix tracker: improve logging, add batch num to network ingest (#2033)

This commit is contained in:
Delirium 2024-04-04 10:20:16 +02:00 committed by GitHub
parent c8f4d9d060
commit 3733dac781
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 28 additions and 18 deletions

View file

@ -1,7 +1,7 @@
{
"name": "@openreplay/tracker",
"description": "The OpenReplay tracker main package",
"version": "12.0.7-beta.1",
"version": "12.0.8-2",
"keywords": [
"logging",
"replay"

View file

@ -1132,8 +1132,8 @@ export default class App {
}
this._debug('session_start', reason)
this.signalError(START_ERROR, [])
return UnsuccessfulStart(START_ERROR)
this.signalError(reason.toString?.() || reason, [])
return UnsuccessfulStart(reason.toString?.() || reason)
})
}

View file

@ -186,7 +186,6 @@ export default class API {
app.attachStartCallback(() => {
const tabId = app.getTabId()
const sessStorage = app.sessionStorage ?? window.sessionStorage
// @ts-ignore ?
window.open = function (...args) {
if (options.autoResetOnWindowOpen) {
app.resetNextPageSession(true)
@ -194,9 +193,9 @@ export default class API {
if (options.resetTabOnWindowOpen) {
sessStorage.removeItem(options.session_tabid_key || '__openreplay_tabid')
}
wOpen.call(window, ...args)
app.resetNextPageSession(false)
sessStorage.setItem(options.session_tabid_key || '__openreplay_tabid', tabId)
return wOpen.call(window, ...args)
}
})
app.attachStopCallback(() => {
@ -224,8 +223,7 @@ export default class API {
trackerVersion: 'TRACKER_VERSION',
projectKey: this.options.projectKey,
doNotTrack,
reason,
missingApi,
reason: missingApi.length ? `missing api: ${missingApi.join(',')}` : reason,
}),
)
}

View file

@ -10,6 +10,7 @@ export default class QueueSender {
// its actually on #24
// eslint-disable-next-line
private isCompressing
private lastBatchNum = 0
constructor(
ingestBaseURL: string,
@ -18,6 +19,7 @@ export default class QueueSender {
private readonly MAX_ATTEMPTS_COUNT = 10,
private readonly ATTEMPT_TIMEOUT = 250,
private readonly onCompress?: (batch: Uint8Array) => any,
private readonly pageNo?: number,
) {
this.ingestURL = ingestBaseURL + INGEST_PATH
if (onCompress !== undefined) {
@ -47,7 +49,8 @@ export default class QueueSender {
if (this.isCompressing && this.onCompress) {
this.onCompress(batch)
} else {
this.sendBatch(batch)
const batchNum = ++this.lastBatchNum
this.sendBatch(batch, false, batchNum)
}
}
}
@ -59,29 +62,35 @@ export default class QueueSender {
if (this.isCompressing && this.onCompress) {
this.onCompress(nextBatch)
} else {
this.sendBatch(nextBatch)
const batchNum = ++this.lastBatchNum
this.sendBatch(nextBatch, false, batchNum)
}
} else {
this.busy = false
}
}
private retry(batch: Uint8Array, isCompressed?: boolean): void {
private retry(batch: Uint8Array, isCompressed?: boolean, batchNum?: string | number): void {
if (this.attemptsCount >= this.MAX_ATTEMPTS_COUNT) {
this.onFailure(`Failed to send batch after ${this.attemptsCount} attempts.`)
// remains this.busy === true
return
}
this.attemptsCount++
setTimeout(() => this.sendBatch(batch, isCompressed), this.ATTEMPT_TIMEOUT * this.attemptsCount)
setTimeout(
() => this.sendBatch(batch, isCompressed, batchNum),
this.ATTEMPT_TIMEOUT * this.attemptsCount,
)
}
// would be nice to use Beacon API, but it is not available in WebWorker
private sendBatch(batch: Uint8Array, isCompressed?: boolean): void {
private sendBatch(batch: Uint8Array, isCompressed?: boolean, batchNum?: string | number): void {
const batchNumStr = batchNum?.toString().replace(/^([^_]+)_([^_]+).*/, '$1_$2_$3')
this.busy = true
const headers = {
Authorization: `Bearer ${this.token as string}`,
'X-Openreplay-Batch': `${this.pageNo ?? 'noPageNum'}_${batchNumStr ?? 'noBatchNum'}`,
} as Record<string, string>
if (isCompressed) {
@ -93,7 +102,7 @@ export default class QueueSender {
* */
if (this.token === null) {
setTimeout(() => {
this.sendBatch(batch, isCompressed)
this.sendBatch(batch, isCompressed, `${batchNum ?? 'noBatchNum'}_newToken`)
}, 500)
return
}
@ -111,7 +120,7 @@ export default class QueueSender {
this.onUnauthorised()
return
} else if (r.status >= 400) {
this.retry(batch, isCompressed)
this.retry(batch, isCompressed, `${batchNum ?? 'noBatchNum'}_network:${r.status}`)
return
}
@ -119,18 +128,20 @@ export default class QueueSender {
this.attemptsCount = 0
this.sendNext()
})
.catch((e: any) => {
.catch((e: Error) => {
console.warn('OpenReplay:', e)
this.retry(batch, isCompressed)
this.retry(batch, isCompressed, `${batchNum ?? 'noBatchNum'}_reject:${e.message}`)
})
}
sendCompressed(batch: Uint8Array) {
this.sendBatch(batch, true)
const batchNum = ++this.lastBatchNum
this.sendBatch(batch, true, batchNum)
}
sendUncompressed(batch: Uint8Array) {
this.sendBatch(batch, false)
const batchNum = ++this.lastBatchNum
this.sendBatch(batch, false, batchNum)
}
clean() {

View file

@ -147,6 +147,7 @@ self.onmessage = ({ data }: { data: ToWorkerData }): any => {
(batch) => {
postMessage({ type: 'compress', batch }, [batch.buffer])
},
data.pageNo,
)
writer = new BatchWriter(
data.pageNo,