Skip to content

Commit

Permalink
Use TimeSource replace std::chrono
Browse files Browse the repository at this point in the history
Signed-off-by: tianqian.zyf <[email protected]>
  • Loading branch information
zyfjeff committed Sep 5, 2018
1 parent 51f3cb4 commit a4e61f9
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 15 deletions.
2 changes: 1 addition & 1 deletion source/extensions/stat_sinks/metrics_service/config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Stats::SinkPtr MetricsServiceSinkFactory::createStatsSink(const Protobuf::Messag
grpc_service, server.stats(), false),
server.threadLocal(), server.localInfo());

return std::make_unique<MetricsServiceSink>(grpc_metrics_streamer);
return std::make_unique<MetricsServiceSink>(grpc_metrics_streamer, server.timeSource());
}

ProtobufTypes::MessagePtr MetricsServiceSinkFactory::createEmptyConfigProto() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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::milliseconds>(
std::chrono::system_clock::now().time_since_epoch())
.count();
}
} // namespace

GrpcMetricsStreamerImpl::GrpcMetricsStreamerImpl(Grpc::AsyncClientFactoryPtr&& factory,
ThreadLocal::SlotAllocator& tls,
const LocalInfo::LocalInfo& local_info)
Expand Down Expand Up @@ -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<std::chrono::milliseconds>(time_source_.systemTime().currentTime())
.time_since_epoch()
.count());
auto* counter_metric = metric->mutable_counter();
counter_metric->set_value(counter.value());
}
Expand All @@ -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<std::chrono::milliseconds>(time_source_.systemTime().currentTime())
.time_since_epoch()
.count());
auto* gauage_metric = metric->mutable_gauge();
gauage_metric->set_value(gauge.value());
}
Expand All @@ -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<std::chrono::milliseconds>(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++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a4e61f9

Please sign in to comment.