diff --git a/source/extensions/stat_sinks/metrics_service/config.cc b/source/extensions/stat_sinks/metrics_service/config.cc index 234b46b37740..22e9e58fad1f 100644 --- a/source/extensions/stat_sinks/metrics_service/config.cc +++ b/source/extensions/stat_sinks/metrics_service/config.cc @@ -29,7 +29,7 @@ Stats::SinkPtr MetricsServiceSinkFactory::createStatsSink(const Protobuf::Messag grpc_service, server.stats(), false), server.threadLocal(), server.localInfo()); - return std::make_unique(grpc_metrics_streamer); + return std::make_unique(grpc_metrics_streamer, server.timeSource()); } ProtobufTypes::MessagePtr MetricsServiceSinkFactory::createEmptyConfigProto() { 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 19263feebb2f..a2ba3c619389 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,15 +16,6 @@ namespace Extensions { namespace StatSinks { namespace MetricsService { -namespace { -// TODO(#4160): use TimeSource::systemTime() rather than directly reading the system-clock. -int64_t getUnixMillisForNow() { - 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) @@ -70,15 +61,19 @@ void GrpcMetricsStreamerImpl::ThreadLocalStreamer::send( } } -MetricsServiceSink::MetricsServiceSink(const GrpcMetricsStreamerSharedPtr& grpc_metrics_streamer) - : grpc_metrics_streamer_(grpc_metrics_streamer) {} +MetricsServiceSink::MetricsServiceSink(const GrpcMetricsStreamerSharedPtr& grpc_metrics_streamer, + TimeSource& time_source) + : grpc_metrics_streamer_(grpc_metrics_streamer), time_source_(time_source) {} void MetricsServiceSink::flushCounter(const Stats::Counter& counter) { io::prometheus::client::MetricFamily* metrics_family = message_.add_envoy_metrics(); 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(getUnixMillisForNow()); + metric->set_timestamp_ms( + std::chrono::duration_cast(time_source_.systemTime().currentTime()) + .time_since_epoch() + .count()); auto* counter_metric = metric->mutable_counter(); counter_metric->set_value(counter.value()); } @@ -88,7 +83,10 @@ 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(getUnixMillisForNow()); + metric->set_timestamp_ms( + std::chrono::duration_cast(time_source_.systemTime().currentTime()) + .time_since_epoch() + .count()); auto* gauage_metric = metric->mutable_gauge(); gauage_metric->set_value(gauge.value()); } @@ -97,7 +95,10 @@ 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(getUnixMillisForNow()); + metric->set_timestamp_ms( + std::chrono::duration_cast(time_source_.systemTime().currentTime()) + .time_since_epoch() + .count()); auto* summary_metric = metric->mutable_summary(); const Stats::HistogramStatistics& hist_stats = histogram.intervalStatistics(); for (size_t i = 0; i < hist_stats.supportedQuantiles().size(); i++) { diff --git a/source/extensions/stat_sinks/metrics_service/grpc_metrics_service_impl.h b/source/extensions/stat_sinks/metrics_service/grpc_metrics_service_impl.h index b27792652d78..b250c9852981 100644 --- a/source/extensions/stat_sinks/metrics_service/grpc_metrics_service_impl.h +++ b/source/extensions/stat_sinks/metrics_service/grpc_metrics_service_impl.h @@ -122,6 +122,7 @@ class MetricsServiceSink : public Stats::Sink { private: GrpcMetricsStreamerSharedPtr grpc_metrics_streamer_; envoy::service::metrics::v2::StreamMetricsMessage message_; + TimeSource& time_source_; }; } // namespace MetricsService