From a42e4edef4d4464218e762442e5ed19e70eebdd0 Mon Sep 17 00:00:00 2001 From: Pete Davids <5599894+MisterSquishy@users.noreply.github.com> Date: Sat, 14 Jan 2023 07:36:37 -0500 Subject: [PATCH] fix(sdk-metrics): use Date.now() for instrument recording timestamps --- CHANGELOG.md | 1 + .../opentelemetry-exporter-prometheus/test/util.ts | 6 +----- packages/sdk-metrics/src/Instruments.ts | 9 +++++++-- packages/sdk-metrics/src/aggregator/LastValue.ts | 4 ++-- packages/sdk-metrics/src/state/MetricCollector.ts | 4 ++-- 5 files changed, 13 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4a09363008..40647a188a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -31,6 +31,7 @@ For experimental package changes, see the [experimental CHANGELOG](experimental/ * `telemetry.sdk.name` * `telemetry.sdk.language` * `telemetry.sdk.version` +* fix(sdk-metrics): use Date.now() for instrument recording timestamps [#3514](https://github.com/open-telemetry/opentelemetry-js/pull/3514) @MisterSquishy * fix(sdk-trace): make spans resilient to clock drift [#3434](https://github.com/open-telemetry/opentelemetry-js/pull/3434) @dyladan * fix(selenium-tests): updated webpack version for selenium test issue [#3456](https://github.com/open-telemetry/opentelemetry-js/issues/3456) @SaumyaBhushan * fix(sdk-metrics): collect metrics when periodic exporting metric reader flushes [#3517](https://github.com/open-telemetry/opentelemetry-js/pull/3517) @legendecas diff --git a/experimental/packages/opentelemetry-exporter-prometheus/test/util.ts b/experimental/packages/opentelemetry-exporter-prometheus/test/util.ts index be07dd3d8c..2af8aa7f56 100644 --- a/experimental/packages/opentelemetry-exporter-prometheus/test/util.ts +++ b/experimental/packages/opentelemetry-exporter-prometheus/test/util.ts @@ -15,17 +15,13 @@ */ import * as sinon from 'sinon'; -import * as perf_hooks from 'perf_hooks'; import { Resource } from '@opentelemetry/resources'; import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions'; export const mockedHrTimeMs = 1586347902211; export function mockHrTime() { - // We cannot stub core.now or core.timeOrigin since a property of - // ModuleNamespace can not be reconfigured.ß - sinon.stub(perf_hooks.performance, 'timeOrigin').value(0); - sinon.stub(perf_hooks.performance, 'now').returns(mockedHrTimeMs); + sinon.useFakeTimers(mockedHrTimeMs); } export const serviceName = Resource.default() diff --git a/packages/sdk-metrics/src/Instruments.ts b/packages/sdk-metrics/src/Instruments.ts index 09e849cc32..d99c621c0b 100644 --- a/packages/sdk-metrics/src/Instruments.ts +++ b/packages/sdk-metrics/src/Instruments.ts @@ -29,7 +29,7 @@ import { ObservableGauge, ObservableUpDownCounter, } from '@opentelemetry/api'; -import { hrTime } from '@opentelemetry/core'; +import { millisToHrTime } from '@opentelemetry/core'; import { InstrumentDescriptor } from './InstrumentDescriptor'; import { ObservableRegistry } from './state/ObservableRegistry'; import { @@ -57,7 +57,12 @@ export class SyncInstrument { ); value = Math.trunc(value); } - this._writableMetricStorage.record(value, attributes, context, hrTime()); + this._writableMetricStorage.record( + value, + attributes, + context, + millisToHrTime(Date.now()) + ); } } diff --git a/packages/sdk-metrics/src/aggregator/LastValue.ts b/packages/sdk-metrics/src/aggregator/LastValue.ts index e9e85b36bd..9c627130f6 100644 --- a/packages/sdk-metrics/src/aggregator/LastValue.ts +++ b/packages/sdk-metrics/src/aggregator/LastValue.ts @@ -22,7 +22,7 @@ import { LastValue, } from './types'; import { HrTime } from '@opentelemetry/api'; -import { hrTime, hrTimeToMicroseconds } from '@opentelemetry/core'; +import { millisToHrTime, hrTimeToMicroseconds } from '@opentelemetry/core'; import { DataPointType, GaugeMetricData } from '../export/MetricData'; import { InstrumentDescriptor } from '../InstrumentDescriptor'; import { Maybe } from '../utils'; @@ -37,7 +37,7 @@ export class LastValueAccumulation implements Accumulation { record(value: number): void { this._current = value; - this.sampleTime = hrTime(); + this.sampleTime = millisToHrTime(Date.now()); } setStartTime(startTime: HrTime): void { diff --git a/packages/sdk-metrics/src/state/MetricCollector.ts b/packages/sdk-metrics/src/state/MetricCollector.ts index 95fe8d4e14..3f17a0d5b9 100644 --- a/packages/sdk-metrics/src/state/MetricCollector.ts +++ b/packages/sdk-metrics/src/state/MetricCollector.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -import { hrTime } from '@opentelemetry/core'; +import { millisToHrTime } from '@opentelemetry/core'; import { AggregationTemporalitySelector } from '../export/AggregationSelector'; import { CollectionResult } from '../export/MetricData'; import { MetricProducer, MetricCollectOptions } from '../export/MetricProducer'; @@ -36,7 +36,7 @@ export class MetricCollector implements MetricProducer { ) {} async collect(options?: MetricCollectOptions): Promise { - const collectionTime = hrTime(); + const collectionTime = millisToHrTime(Date.now()); const meterCollectionPromises = Array.from( this._sharedState.meterSharedStates.values() ).map(meterSharedState =>