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

fix time_since_epoch different in different os default return precision #4288

Merged
merged 12 commits into from
Sep 6, 2018
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 @@ -61,15 +61,18 @@ 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(std::chrono::system_clock::now().time_since_epoch().count());
metric->set_timestamp_ms(std::chrono::duration_cast<std::chrono::milliseconds>(
time_source_.systemTime().time_since_epoch())
.count());
auto* counter_metric = metric->mutable_counter();
counter_metric->set_value(counter.value());
}
Expand All @@ -79,7 +82,9 @@ 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::system_clock::now().time_since_epoch().count());
metric->set_timestamp_ms(std::chrono::duration_cast<std::chrono::milliseconds>(
time_source_.systemTime().time_since_epoch())
.count());
auto* gauage_metric = metric->mutable_gauge();
gauage_metric->set_value(gauge.value());
}
Expand All @@ -88,7 +93,9 @@ 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::system_clock::now().time_since_epoch().count());
metric->set_timestamp_ms(std::chrono::duration_cast<std::chrono::milliseconds>(
time_source_.systemTime().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 @@ -111,7 +111,8 @@ class GrpcMetricsStreamerImpl : public Singleton::Instance, public GrpcMetricsSt
class MetricsServiceSink : public Stats::Sink {
public:
// MetricsService::Sink
MetricsServiceSink(const GrpcMetricsStreamerSharedPtr& grpc_metrics_streamer);
MetricsServiceSink(const GrpcMetricsStreamerSharedPtr& grpc_metrics_streamer,
TimeSource& time_source);
void flush(Stats::Source& source) override;
void onHistogramComplete(const Stats::Histogram&, uint64_t) override {}

Expand All @@ -122,6 +123,7 @@ class MetricsServiceSink : public Stats::Sink {
private:
GrpcMetricsStreamerSharedPtr grpc_metrics_streamer_;
envoy::service::metrics::v2::StreamMetricsMessage message_;
TimeSource& time_source_;
};

} // namespace MetricsService
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "extensions/stat_sinks/metrics_service/grpc_metrics_service_impl.h"

#include "test/mocks/common.h"
#include "test/mocks/grpc/mocks.h"
#include "test/mocks/local_info/mocks.h"
#include "test/mocks/stats/mocks.h"
Expand Down Expand Up @@ -98,9 +99,10 @@ class MetricsServiceSinkTest : public testing::Test {};

TEST(MetricsServiceSinkTest, CheckSendCall) {
NiceMock<Stats::MockSource> source;
NiceMock<MockTimeSource> mock_time;
std::shared_ptr<MockGrpcMetricsStreamer> streamer_{new MockGrpcMetricsStreamer()};

MetricsServiceSink sink(streamer_);
MetricsServiceSink sink(streamer_, mock_time);

auto counter = std::make_shared<NiceMock<Stats::MockCounter>>();
counter->name_ = "test_counter";
Expand All @@ -125,9 +127,10 @@ TEST(MetricsServiceSinkTest, CheckSendCall) {

TEST(MetricsServiceSinkTest, CheckStatsCount) {
NiceMock<Stats::MockSource> source;
NiceMock<MockTimeSource> mock_time;
std::shared_ptr<TestGrpcMetricsStreamer> streamer_{new TestGrpcMetricsStreamer()};

MetricsServiceSink sink(streamer_);
MetricsServiceSink sink(streamer_, mock_time);

auto counter = std::make_shared<NiceMock<Stats::MockCounter>>();
counter->name_ = "test_counter";
Expand Down