Skip to content

Commit

Permalink
fix tracker: improve logging, add batch num to network ingest (openre…
Browse files Browse the repository at this point in the history
  • Loading branch information
nick-delirium authored Apr 4, 2024
1 parent c8f4d9d commit 3733dac
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 18 deletions.
2 changes: 1 addition & 1 deletion tracker/tracker/package.json
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
4 changes: 2 additions & 2 deletions tracker/tracker/src/main/app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}

Expand Down
6 changes: 2 additions & 4 deletions tracker/tracker/src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,17 +186,16 @@ 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)
}
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(() => {
Expand Down Expand Up @@ -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,
}),
)
}
Expand Down
33 changes: 22 additions & 11 deletions tracker/tracker/src/webworker/QueueSender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export default class QueueSender {
// its actually on #24
// eslint-disable-next-line
private isCompressing
private lastBatchNum = 0

constructor(
ingestBaseURL: string,
Expand All @@ -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) {
Expand Down Expand Up @@ -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)
}
}
}
Expand All @@ -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) {
Expand All @@ -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
}
Expand All @@ -111,26 +120,28 @@ 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
}

// Success
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() {
Expand Down
1 change: 1 addition & 0 deletions tracker/tracker/src/webworker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ self.onmessage = ({ data }: { data: ToWorkerData }): any => {
(batch) => {
postMessage({ type: 'compress', batch }, [batch.buffer])
},
data.pageNo,
)
writer = new BatchWriter(
data.pageNo,
Expand Down

0 comments on commit 3733dac

Please sign in to comment.