-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathregistry_counters_reset.patch
105 lines (97 loc) · 3.62 KB
/
registry_counters_reset.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
diff --git a/core/include/prometheus/counter.h b/core/include/prometheus/counter.h
index 9b2ad19..4fa1fba 100644
--- a/core/include/prometheus/counter.h
+++ b/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 a/core/include/prometheus/family.h b/core/include/prometheus/family.h
index 0bc0723..4f62fda 100644
--- a/core/include/prometheus/family.h
+++ b/core/include/prometheus/family.h
@@ -141,6 +141,12 @@ class PROMETHEUS_CPP_CORE_EXPORT Family : public Collectable {
/// \return Zero or more samples for each dimensional data.
std::vector<MetricFamily> 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<Labels, std::unique_ptr<T>, detail::LabelHasher> metrics_;
diff --git a/core/include/prometheus/registry.h b/core/include/prometheus/registry.h
index 6603f6c..877c085 100644
--- a/core/include/prometheus/registry.h
+++ b/core/include/prometheus/registry.h
@@ -94,6 +94,12 @@ class PROMETHEUS_CPP_CORE_EXPORT Registry : public Collectable {
template <typename T>
bool Remove(const Family<T>& family);
+ /// \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 <typename T>
friend class detail::Builder;
diff --git a/core/src/counter.cc b/core/src/counter.cc
index 2a93463..a12885d 100644
--- a/core/src/counter.cc
+++ b/core/src/counter.cc
@@ -13,6 +13,8 @@ void Counter::Increment(const double 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 a/core/src/family.cc b/core/src/family.cc
index f087586..8f9d276 100644
--- a/core/src/family.cc
+++ b/core/src/family.cc
@@ -122,6 +122,19 @@ ClientMetric Family<T>::CollectMetric(const Labels& metric_labels,
return collected;
}
+template <>
+void Family<Counter>::ResetMetrics() {
+// const auto reset_metric =
+// [](std::pair<const std::size_t, std::unique_ptr<Counter>>& metric) {
+// metric.second->Reset();
+// };
+//
+// std::for_each(metrics_.begin(), metrics_.end(), reset_metric);
+ for (const auto& m : metrics_) {
+ m.second->Reset();
+ }
+}
+
template class PROMETHEUS_CPP_CORE_EXPORT Family<Counter>;
template class PROMETHEUS_CPP_CORE_EXPORT Family<Gauge>;
template class PROMETHEUS_CPP_CORE_EXPORT Family<Histogram>;
diff --git a/core/src/registry.cc b/core/src/registry.cc
index 267bf92..7a365ed 100644
--- a/core/src/registry.cc
+++ b/core/src/registry.cc
@@ -56,6 +56,12 @@ std::vector<MetricFamily> Registry::Collect() const {
return results;
}
+void Registry::ResetCounters(){
+ for (auto& family : this->counters_) {
+ family->ResetMetrics();
+ }
+}
+
template <>
std::vector<std::unique_ptr<Family<Counter>>>& Registry::GetFamilies() {
return counters_;