diff --git a/source/extensions/stat_sinks/metrics_service/grpc_metrics_service_impl.cc b/source/extensions/stat_sinks/metrics_service/grpc_metrics_service_impl.cc index c6629d3614c1..ad32ce56d6f8 100644 --- a/source/extensions/stat_sinks/metrics_service/grpc_metrics_service_impl.cc +++ b/source/extensions/stat_sinks/metrics_service/grpc_metrics_service_impl.cc @@ -16,6 +16,15 @@ namespace Extensions { namespace StatSinks { namespace MetricsService { +namespace { +// TODO(#4160): use TimeSource::systemTime() rather than directly reading the system-clock. +int64_t getUnixMicrosForNow() { + return std::chrono::duration_cast( + std::chrono::system_clock::now().time_since_epoch()) + .count(); +} +} // namespace + GrpcMetricsStreamerImpl::GrpcMetricsStreamerImpl(Grpc::AsyncClientFactoryPtr&& factory, ThreadLocal::SlotAllocator& tls, const LocalInfo::LocalInfo& local_info) @@ -69,9 +78,7 @@ void MetricsServiceSink::flushCounter(const Stats::Counter& counter) { metrics_family->set_type(io::prometheus::client::MetricType::COUNTER); metrics_family->set_name(counter.name()); auto* metric = metrics_family->add_metric(); - metric->set_timestamp_ms(std::chrono::duration_cast( - std::chrono::system_clock::now().time_since_epoch()) - .count()); + metric->set_timestamp_ms(getUnixMicrosForNow()); auto* counter_metric = metric->mutable_counter(); counter_metric->set_value(counter.value()); } @@ -81,9 +88,7 @@ void MetricsServiceSink::flushGauge(const Stats::Gauge& gauge) { metrics_family->set_type(io::prometheus::client::MetricType::GAUGE); metrics_family->set_name(gauge.name()); auto* metric = metrics_family->add_metric(); - metric->set_timestamp_ms(std::chrono::duration_cast( - std::chrono::system_clock::now().time_since_epoch()) - .count()); + metric->set_timestamp_ms(getUnixMicrosForNow()); auto* gauage_metric = metric->mutable_gauge(); gauage_metric->set_value(gauge.value()); } @@ -92,9 +97,7 @@ void MetricsServiceSink::flushHistogram(const Stats::ParentHistogram& histogram) metrics_family->set_type(io::prometheus::client::MetricType::SUMMARY); metrics_family->set_name(histogram.name()); auto* metric = metrics_family->add_metric(); - metric->set_timestamp_ms(std::chrono::duration_cast( - std::chrono::system_clock::now().time_since_epoch()) - .count()); + metric->set_timestamp_ms(getUnixMicrosForNow()); auto* summary_metric = metric->mutable_summary(); const Stats::HistogramStatistics& hist_stats = histogram.intervalStatistics(); for (size_t i = 0; i < hist_stats.supportedQuantiles().size(); i++) {