opentelemetry-js
opentelemetry-js copied to clipboard
Add a way to compensate for clock skew
If the system clock on computer is out of sync from others it can cause problems. This is most likely to be an issue in the browser, where users are less likely to sync with time servers.
Our browser app already has a system to calculate clock skew between client and server using an NTP based system, so simply having an API to report clock skew in milliseconds to the OpenTelemetry client library would work for us.
Other more flexible solutions could work as well. e.g. see https://github.com/open-telemetry/opentelemetry-js/issues/1652
Note however that we do not know the clock skew at the time the telemetry library starts, and the clock skew can vary if the user adjusts their clock or whatever. So we periodically re-calculate clock skew and would want to periodically update the clock skew used in by opentelemetry.
I guess option might be to modify https://github.com/open-telemetry/opentelemetry-js/blob/master/packages/opentelemetry-core/src/common/time.ts along these lines:
// Offset to add to performance.timeOrigin to get a network time (e.g. correct for clock skew)
let timeOriginOffset: number = 0;
/**
* Set the time origin offset to the number of milliseconds difference between the network
* clock and the local system clock. This will be added to the timestamps created by
* opentelemetry to adjust for clock skew between systems.
*
* As for how to calculate clock skew, you can implement a version of NTP to calculate the
* current network clock time and calling setTimeOriginOffset(networkTime - Date.now()).
*/
export function setTimeOriginOffset(offset: number) { timeOriginOffset = offset; }
function getTimeOrigin(): number {
let timeOrigin = performance.timeOrigin;
if (typeof timeOrigin !== 'number') {
const perf: TimeOriginLegacy = (performance as unknown) as TimeOriginLegacy;
timeOrigin = perf.timing && perf.timing.fetchStart;
}
return timeOrigin + timeOriginOffset;
}
Similar discussions have come up here: https://github.com/open-telemetry/opentelemetry-js/issues/852.
Facing same issue while instrumenting my FE app with SigNoz.io. Looks like clock skew is happening only with Chromium based browsers (and not always).
Facing same issue while instrumenting my FE app with SigNoz.io. Looks like clock skew is happening only with Chromium based browsers (and not always).
Looks like you're running into #852 which is slightly different than this issue.
Related issue (not specific to JS) https://github.com/open-telemetry/oteps/issues/154