opentelemetry-js icon indicating copy to clipboard operation
opentelemetry-js copied to clipboard

Add a way to compensate for clock skew

Open dobesv opened this issue 4 years ago • 5 comments

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.

dobesv avatar Dec 07 '20 06:12 dobesv

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;
}

dobesv avatar Dec 07 '20 06:12 dobesv

Similar discussions have come up here: https://github.com/open-telemetry/opentelemetry-js/issues/852.

mwear avatar Jan 05 '21 01:01 mwear

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).

Screenshot 2022-04-07 at 15 51 47

pranshuchittora avatar Apr 07 '22 14:04 pranshuchittora

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).

Screenshot 2022-04-07 at 15 51 47

Looks like you're running into #852 which is slightly different than this issue.

dyladan avatar Apr 07 '22 18:04 dyladan

Related issue (not specific to JS) https://github.com/open-telemetry/oteps/issues/154

martinkuba avatar Apr 30 '24 16:04 martinkuba