From 88da249172eb0d67f1e3b40a23fef1e533b8f1ec Mon Sep 17 00:00:00 2001 From: Yingchun Lai Date: Thu, 12 Aug 2021 13:28:08 +0800 Subject: [PATCH] Support to hide metrics which hasn't update for a long time --- core/CMakeLists.txt | 1 + core/include/prometheus/counter.h | 3 ++- core/include/prometheus/gauge.h | 3 ++- core/include/prometheus/histogram.h | 3 ++- core/include/prometheus/metric.h | 28 ++++++++++++++++++++++++++++ core/include/prometheus/summary.h | 3 ++- core/src/counter.cc | 6 +++++- core/src/family.cc | 6 +++++- core/src/gauge.cc | 6 +++++- core/src/histogram.cc | 2 ++ core/src/metric.cc | 19 +++++++++++++++++++ core/src/summary.cc | 1 + 12 files changed, 74 insertions(+), 7 deletions(-) create mode 100644 core/include/prometheus/metric.h create mode 100644 core/src/metric.cc diff --git a/core/CMakeLists.txt b/core/CMakeLists.txt index 13b27624..ae7f6ade 100644 --- a/core/CMakeLists.txt +++ b/core/CMakeLists.txt @@ -7,6 +7,7 @@ add_library(core src/detail/time_window_quantiles.cc src/detail/utils.cc src/family.cc + src/metric.cc src/gauge.cc src/histogram.cc src/registry.cc diff --git a/core/include/prometheus/counter.h b/core/include/prometheus/counter.h index c67ddde2..1ab9e182 100644 --- a/core/include/prometheus/counter.h +++ b/core/include/prometheus/counter.h @@ -4,6 +4,7 @@ #include "prometheus/detail/builder.h" // IWYU pragma: export #include "prometheus/detail/core_export.h" #include "prometheus/gauge.h" +#include "prometheus/metric.h" #include "prometheus/metric_type.h" namespace prometheus { @@ -23,7 +24,7 @@ namespace prometheus { /// /// The class is thread-safe. No concurrent call to any API of this type causes /// a data race. -class PROMETHEUS_CPP_CORE_EXPORT Counter { +class PROMETHEUS_CPP_CORE_EXPORT Counter : public Metric { public: static const MetricType metric_type{MetricType::Counter}; diff --git a/core/include/prometheus/gauge.h b/core/include/prometheus/gauge.h index 62498376..fc4a8d3d 100644 --- a/core/include/prometheus/gauge.h +++ b/core/include/prometheus/gauge.h @@ -5,6 +5,7 @@ #include "prometheus/client_metric.h" #include "prometheus/detail/builder.h" // IWYU pragma: export #include "prometheus/detail/core_export.h" +#include "prometheus/metric.h" #include "prometheus/metric_type.h" namespace prometheus { @@ -21,7 +22,7 @@ namespace prometheus { /// /// The class is thread-safe. No concurrent call to any API of this type causes /// a data race. -class PROMETHEUS_CPP_CORE_EXPORT Gauge { +class PROMETHEUS_CPP_CORE_EXPORT Gauge : public Metric { public: static const MetricType metric_type{MetricType::Gauge}; diff --git a/core/include/prometheus/histogram.h b/core/include/prometheus/histogram.h index f8be2435..db138aab 100644 --- a/core/include/prometheus/histogram.h +++ b/core/include/prometheus/histogram.h @@ -7,6 +7,7 @@ #include "prometheus/detail/builder.h" // IWYU pragma: export #include "prometheus/detail/core_export.h" #include "prometheus/gauge.h" +#include "prometheus/metric.h" #include "prometheus/metric_type.h" namespace prometheus { @@ -27,7 +28,7 @@ namespace prometheus { /// /// The class is thread-safe. No concurrent call to any API of this type causes /// a data race. -class PROMETHEUS_CPP_CORE_EXPORT Histogram { +class PROMETHEUS_CPP_CORE_EXPORT Histogram : public Metric { public: using BucketBoundaries = std::vector; diff --git a/core/include/prometheus/metric.h b/core/include/prometheus/metric.h new file mode 100644 index 00000000..fb48efb9 --- /dev/null +++ b/core/include/prometheus/metric.h @@ -0,0 +1,28 @@ +#pragma once + +#include + +#include "prometheus/detail/core_export.h" +#include "prometheus/metric_type.h" + +namespace prometheus { + +/// \brief The base metric. +/// +class PROMETHEUS_CPP_CORE_EXPORT Metric { + public: + static const MetricType metric_type{MetricType::Untyped}; + + Metric(); + virtual ~Metric() = default; + + bool Expired(time_t current_ts) const; + + protected: + void UpdateTS(); + + private: + time_t last_update_ts_; +}; + +} // namespace prometheus diff --git a/core/include/prometheus/summary.h b/core/include/prometheus/summary.h index 87381344..87ba4c5a 100644 --- a/core/include/prometheus/summary.h +++ b/core/include/prometheus/summary.h @@ -10,6 +10,7 @@ #include "prometheus/detail/ckms_quantiles.h" #include "prometheus/detail/core_export.h" #include "prometheus/detail/time_window_quantiles.h" +#include "prometheus/metric.h" #include "prometheus/metric_type.h" namespace prometheus { @@ -38,7 +39,7 @@ namespace prometheus { /// /// The class is thread-safe. No concurrent call to any API of this type causes /// a data race. -class PROMETHEUS_CPP_CORE_EXPORT Summary { +class PROMETHEUS_CPP_CORE_EXPORT Summary : public Metric { public: using Quantiles = std::vector; diff --git a/core/src/counter.cc b/core/src/counter.cc index 2a934635..16e46bca 100644 --- a/core/src/counter.cc +++ b/core/src/counter.cc @@ -2,13 +2,17 @@ namespace prometheus { -void Counter::Increment() { gauge_.Increment(); } +void Counter::Increment() { + gauge_.Increment(); + UpdateTS(); +} void Counter::Increment(const double val) { if (val < 0.0) { return; } gauge_.Increment(val); + UpdateTS(); } double Counter::Value() const { return gauge_.Value(); } diff --git a/core/src/family.cc b/core/src/family.cc index 90314404..ed518f5a 100644 --- a/core/src/family.cc +++ b/core/src/family.cc @@ -96,6 +96,7 @@ const std::map Family::GetConstantLabels() const { template std::vector Family::Collect() const { + auto current_ts = std::time(nullptr); std::lock_guard lock{mutex_}; if (metrics_.empty()) { @@ -108,7 +109,10 @@ std::vector Family::Collect() const { family.type = T::metric_type; family.metric.reserve(metrics_.size()); for (const auto& m : metrics_) { - family.metric.push_back(std::move(CollectMetric(m.first, m.second.get()))); + if (!m.second->Expired(current_ts)) { + family.metric.push_back( + std::move(CollectMetric(m.first, m.second.get()))); + } } return {family}; } diff --git a/core/src/gauge.cc b/core/src/gauge.cc index b3933076..761e9512 100644 --- a/core/src/gauge.cc +++ b/core/src/gauge.cc @@ -14,12 +14,16 @@ void Gauge::Decrement() { Decrement(1.0); } void Gauge::Decrement(const double value) { Change(-1.0 * value); } -void Gauge::Set(const double value) { value_.store(value); } +void Gauge::Set(const double value) { + value_.store(value); + UpdateTS(); +} void Gauge::Change(const double value) { auto current = value_.load(); while (!value_.compare_exchange_weak(current, current + value)) ; + UpdateTS(); } void Gauge::SetToCurrentTime() { diff --git a/core/src/histogram.cc b/core/src/histogram.cc index 78ba3c88..b8f4592f 100644 --- a/core/src/histogram.cc +++ b/core/src/histogram.cc @@ -26,6 +26,7 @@ void Histogram::Observe(const double value) { [value](const double boundary) { return boundary >= value; }))); sum_.Increment(value); bucket_counts_[bucket_index].Increment(); + UpdateTS(); } void Histogram::ObserveMultiple(const std::vector& bucket_increments, @@ -41,6 +42,7 @@ void Histogram::ObserveMultiple(const std::vector& bucket_increments, for (std::size_t i{0}; i < bucket_counts_.size(); ++i) { bucket_counts_[i].Increment(bucket_increments[i]); } + UpdateTS(); } ClientMetric Histogram::Collect() const { diff --git a/core/src/metric.cc b/core/src/metric.cc new file mode 100644 index 00000000..fb68394d --- /dev/null +++ b/core/src/metric.cc @@ -0,0 +1,19 @@ +#include "prometheus/metric.h" + +#include + +namespace prometheus { + +Metric::Metric() : last_update_ts_(std::time(nullptr)) { +} + +bool Metric::Expired(time_t current_ts) const { + static const double retire_time = 600; + return difftime(current_ts, last_update_ts_) > retire_time; +} + +void Metric::UpdateTS() { + last_update_ts_ = std::time(nullptr); +} + +} // namespace prometheus diff --git a/core/src/summary.cc b/core/src/summary.cc index 877c81bf..361a1133 100644 --- a/core/src/summary.cc +++ b/core/src/summary.cc @@ -17,6 +17,7 @@ void Summary::Observe(const double value) { count_ += 1; sum_ += value; quantile_values_.insert(value); + UpdateTS(); } ClientMetric Summary::Collect() const {