Skip to content

Commit

Permalink
fix metrics timestamp using monotonic clock
Browse files Browse the repository at this point in the history
The utc_timestamp_ms field that is reported by the metrics
uses a CLOCK_MONOTONIC timestamp. The POSIX standard defines
the monotonic clock as a clock with a fixed but undefined
starting point.
For linux, the starting point is usually set at boot.

This commit replaces the clock with a CLOCK_REALTIME, which
gives the interval from the UTC UNIX epoch. It also updates
the changelog and adds a regression test.

Signed-off-by: alindima <alindima@amazon.com>
  • Loading branch information
alindima committed Jul 8, 2021
1 parent 24cc8d9 commit c339167
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@
- Fixed the SSBD mitigation not being enabled on `aarch64` with the provided
`prod-host-setup.md`.
- Fixed the balloon statistics not working after a snapshot restore event.
- The `utc_timestamp_ms` now reports the timestamp in ms from the UTC UNIX
Epoch, as the name suggests. It was previously using a monotonic clock with
an undefined starting point.

## [0.24.0]

Expand Down
2 changes: 1 addition & 1 deletion src/logger/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -769,7 +769,7 @@ struct SerializeToUtcTimestampMs;
impl Serialize for SerializeToUtcTimestampMs {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_i64(
utils::time::get_time_ns(utils::time::ClockType::Monotonic) as i64 / 1_000_000,
utils::time::get_time_ns(utils::time::ClockType::Real) as i64 / 1_000_000,
)
}
}
Expand Down
11 changes: 11 additions & 0 deletions tests/integration_tests/functional/test_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
# SPDX-License-Identifier: Apache-2.0
"""Tests the metrics system."""

import datetime
import os
import json
import math
import platform
import host_tools.logging as log_tools

Expand Down Expand Up @@ -54,4 +56,13 @@ def test_flush_metrics(test_microvm_with_api):

assert set(metrics.keys()) == set(exp_keys)

utc_time = datetime.datetime.now(datetime.timezone.utc)
utc_timestamp_ms = math.floor(utc_time.timestamp() * 1000)

# Assert that the absolute difference is less than 1 second, to check that
# the reported utc_timestamp_ms is actually a UTC timestamp from the Unix
# Epoch.Regression test for:
# https://github.com/firecracker-microvm/firecracker/issues/2639
assert abs(utc_timestamp_ms - metrics['utc_timestamp_ms']) < 1000

microvm.flush_metrics(metrics_fifo)

0 comments on commit c339167

Please sign in to comment.