Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use Date.now() for instrument recording timestamps #3514

Merged
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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): fix duplicated registration of metrics for collectors [#3488](https://github.com/open-telemetry/opentelemetry-js/pull/3488) @legendecas
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
pichlermarc marked this conversation as resolved.
Show resolved Hide resolved
}

export const serviceName = Resource.default()
Expand Down
9 changes: 7 additions & 2 deletions packages/sdk-metrics/src/Instruments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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())
);
}
}

Expand Down
4 changes: 2 additions & 2 deletions packages/sdk-metrics/src/aggregator/LastValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions packages/sdk-metrics/src/state/MetricCollector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -36,7 +36,7 @@ export class MetricCollector implements MetricProducer {
) {}

async collect(options?: MetricCollectOptions): Promise<CollectionResult> {
const collectionTime = hrTime();
const collectionTime = millisToHrTime(Date.now());
const meterCollectionPromises = Array.from(
this._sharedState.meterSharedStates.values()
).map(meterSharedState =>
Expand Down