From fc14054c49954308aee5b0b5da275483b467836a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Jaramago=20Fern=C3=A1ndez?= Date: Mon, 8 Mar 2021 18:19:10 +0000 Subject: [PATCH] Added patch to 'prometheus-cpp' adding a counters reset functionality through 'Registry' --- deps/Makefile | 1 + .../registry_counters_reset.patch | 103 ++++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 deps/prometheus-cpp/registry_counters_reset.patch diff --git a/deps/Makefile b/deps/Makefile index 0348ae1b1..6e1c506f2 100644 --- a/deps/Makefile +++ b/deps/Makefile @@ -217,6 +217,7 @@ prometheus-cpp/prometheus-cpp/lib/libprometheus-cpp-core.a: cd prometheus-cpp && tar -zxf v0.9.0.tar.gz cd prometheus-cpp && tar --strip-components=1 -zxf civetweb-v1.11.tar.gz -C prometheus-cpp/3rdparty/civetweb cd prometheus-cpp/prometheus-cpp && patch -p1 < ../serial_exposer.patch + cd prometheus-cpp/prometheus-cpp && patch -p0 < ../registry_counters_reset.patch cd prometheus-cpp/prometheus-cpp && cmake . -DBUILD_SHARED_LIBS=OFF -DENABLE_TESTING=OFF -DENABLE_PUSH=OFF cd prometheus-cpp/prometheus-cpp && CC=${CC} CXX=${CXX} ${MAKE} diff --git a/deps/prometheus-cpp/registry_counters_reset.patch b/deps/prometheus-cpp/registry_counters_reset.patch new file mode 100644 index 000000000..0926c9105 --- /dev/null +++ b/deps/prometheus-cpp/registry_counters_reset.patch @@ -0,0 +1,103 @@ +diff --git core/include/prometheus/counter.h core/include/prometheus/counter.h +index 6ec01dd..6223106 100644 +--- core/include/prometheus/counter.h ++++ core/include/prometheus/counter.h +@@ -46,6 +46,12 @@ class PROMETHEUS_CPP_CORE_EXPORT Counter { + /// Collect is called by the Registry when collecting metrics. + ClientMetric Collect() const; + ++ /// \brief Reset this counter metric to a value of 'zero'. ++ /// ++ /// This function shall only be used in case of 'PROXYSQL STOP' operation ++ /// which invalidates all ProxySQL internal counters for a fresh start. ++ void Reset(); ++ + private: + Gauge gauge_{0.0}; + }; +diff --git core/include/prometheus/family.h core/include/prometheus/family.h +index aa94683..925445a 100644 +--- core/include/prometheus/family.h ++++ core/include/prometheus/family.h +@@ -135,6 +135,12 @@ class PROMETHEUS_CPP_CORE_EXPORT Family : public Collectable { + /// \return Zero or more samples for each dimensional data. + std::vector Collect() const override; + ++ /// \brief Reset all the metrics hold within this specific family. ++ /// ++ /// This function shall only be used in case of 'PROXYSQL STOP' operation ++ /// which invalidates all ProxySQL internal counters for a fresh start. ++ void ResetMetrics(); ++ + private: + std::unordered_map> metrics_; + std::unordered_map> labels_; +diff --git core/include/prometheus/registry.h core/include/prometheus/registry.h +index c8fdeb2..e8a58c5 100644 +--- core/include/prometheus/registry.h ++++ core/include/prometheus/registry.h +@@ -73,6 +73,12 @@ class PROMETHEUS_CPP_CORE_EXPORT Registry : public Collectable { + /// \return Zero or more metrics and their samples. + std::vector Collect() const override; + ++ /// \brief Reset all the counters hold within the registry to a value of zero. ++ /// ++ /// This function shall only be used in case of 'PROXYSQL STOP' operation ++ /// which invalidates all ProxySQL internal counters for a fresh start. ++ void ResetCounters(); ++ + private: + template + friend class detail::Builder; +diff --git core/src/counter.cc core/src/counter.cc +index fc5b6f3..82fb8ae 100644 +--- core/src/counter.cc ++++ core/src/counter.cc +@@ -8,6 +8,8 @@ void Counter::Increment(const double val) { gauge_.Increment(val); } + + double Counter::Value() const { return gauge_.Value(); } + ++void Counter::Reset() { gauge_.Set(0.0); } ++ + ClientMetric Counter::Collect() const { + ClientMetric metric; + metric.counter.value = Value(); +diff --git core/src/family.cc core/src/family.cc +index 83631ab..aa25edb 100644 +--- core/src/family.cc ++++ core/src/family.cc +@@ -97,6 +97,17 @@ ClientMetric Family::CollectMetric(std::size_t hash, T* metric) const { + return collected; + } + ++ ++template <> ++void Family::ResetMetrics() { ++ auto reset_metric = ++ [](std::pair>& metric) { ++ metric.second->Reset(); ++ }; ++ ++ std::for_each(metrics_.begin(), metrics_.end(), reset_metric); ++} ++ + template class PROMETHEUS_CPP_CORE_EXPORT Family; + template class PROMETHEUS_CPP_CORE_EXPORT Family; + template class PROMETHEUS_CPP_CORE_EXPORT Family; +diff --git core/src/registry.cc core/src/registry.cc +index 1a30a23..d3a660d 100644 +--- core/src/registry.cc ++++ core/src/registry.cc +@@ -50,6 +50,12 @@ std::vector Registry::Collect() const { + return results; + } + ++void Registry::ResetCounters(){ ++ for (auto& family : this->counters_) { ++ family->ResetMetrics(); ++ } ++} ++ + template <> + std::vector>>& Registry::GetFamilies() { + return counters_;