feat(tracker):4.1.6: server-time sync on start

This commit is contained in:
Alex Kaminskii 2022-10-19 18:57:51 +02:00
parent b642e36528
commit a0db19edb0
5 changed files with 19 additions and 10 deletions

View file

@ -40,7 +40,7 @@ func (e *Router) readBody(w http.ResponseWriter, r *http.Request, limit int64) (
func getSessionTimestamp(req *StartSessionRequest, startTimeMili int64) (ts uint64) {
ts = uint64(req.Timestamp)
c, err := semver.NewConstraint(">=4.2.0") // )
c, err := semver.NewConstraint(">=4.1.6")
if err != nil {
return
}

View file

@ -1,7 +1,7 @@
{
"name": "@openreplay/tracker",
"description": "The OpenReplay tracker main package",
"version": "4.1.5",
"version": "4.1.6",
"keywords": [
"logging",
"replay"

View file

@ -1,6 +1,6 @@
import type Message from './messages.gen.js'
import { Timestamp, Metadata, UserID } from './messages.gen.js'
import { timestamp as now, deprecationWarn } from '../utils.js'
import { now, deprecationWarn } from '../utils.js'
import Nodes from './nodes.js'
import Observer from './observer/top_observer.js'
import Sanitizer from './sanitizer.js'
@ -205,22 +205,28 @@ export default class App {
}
this.messages.push(message)
// TODO: commit on start if there were `urgent` sends;
// Clearify where urgent can be used for;
// Clearify workflow for each type of message in case it was sent before start
// Clarify where urgent can be used for;
// Clarify workflow for each type of message in case it was sent before start
// (like Fetch before start; maybe add an option "preCapture: boolean" or sth alike)
// Careful: `this.delay` is equal to zero before start hense all Timestamp-s will have to be updated on start
if (this.activityState === ActivityState.Active && urgent) {
this.commit()
}
}
private commit(): void {
if (this.worker && this.messages.length) {
this.messages.unshift(Timestamp(now()))
this.messages.unshift(Timestamp(this.timestamp()))
this.worker.postMessage(this.messages)
this.commitCallbacks.forEach((cb) => cb(this.messages))
this.messages.length = 0
}
}
private delay = 0
timestamp(): number {
return now() + this.delay
}
safe<T extends (this: any, ...args: any[]) => void>(fn: T): T {
const app = this
return function (this: any, ...args: any[]) {
@ -228,7 +234,7 @@ export default class App {
fn.apply(this, args)
} catch (e) {
app._debug('safe_fn_call', e)
// time: now(),
// time: this.timestamp(),
// name: e.name,
// message: e.message,
// stack: e.stack
@ -444,16 +450,19 @@ export default class App {
projectID,
beaconSizeLimit,
startTimestamp, // real startTS, derived from sessionID
delay,
} = r
if (
typeof token !== 'string' ||
typeof userUUID !== 'string' ||
//typeof startTimestamp !== 'number' ||
//typeof sessionID !== 'string' ||
typeof delay !== 'number' ||
(typeof beaconSizeLimit !== 'number' && typeof beaconSizeLimit !== 'undefined')
) {
return Promise.reject(`Incorrect server response: ${JSON.stringify(r)}`)
}
this.delay = delay
const prevSessionID = this.session.getInfo().sessionID
if (prevSessionID && prevSessionID !== sessionID) {
this.session.reset()

View file

@ -1,5 +1,5 @@
import type App from '../app/index.js'
import { timestamp, isURL, IS_FIREFOX, MAX_STR_LEN } from '../utils.js'
import { isURL, IS_FIREFOX, MAX_STR_LEN } from '../utils.js'
import { ResourceTiming, SetNodeAttributeURLBased, SetNodeAttribute } from '../app/messages.gen.js'
import { hasTag } from '../app/guards.js'
@ -59,7 +59,7 @@ export default function (app: App): void {
const sendImgError = app.safe(function (img: HTMLImageElement): void {
const resolvedSrc = resolveURL(img.src || '') // Src type is null sometimes. - is it true?
if (isURL(resolvedSrc)) {
app.send(ResourceTiming(timestamp(), 0, 0, 0, 0, 0, resolvedSrc, 'img'))
app.send(ResourceTiming(app.timestamp(), 0, 0, 0, 0, 0, resolvedSrc, 'img'))
}
})

View file

@ -9,7 +9,7 @@ export const MAX_STR_LEN = 1e5
const navigationStart: number | false =
IN_BROWSER && (performance.timing.navigationStart || performance.timeOrigin)
// performance.now() is buggy in some browsers
export const timestamp: () => number =
export const now: () => number =
IN_BROWSER && performance.now() && navigationStart
? () => Math.round(performance.now() + navigationStart)
: () => Date.now()