From 28230499d5f54f4da51a19039cdccea2d31d8bce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Jaramago=20Fern=C3=A1ndez?= Date: Thu, 10 Feb 2022 09:53:31 +0100 Subject: [PATCH] Add new prometheus helper function and improve others definitions - Added new helper function 'p_update_map_gauge' - Improved helpers definitions using references. --- include/prometheus_helpers.h | 56 +++++++++++++++++++++++++----------- 1 file changed, 40 insertions(+), 16 deletions(-) diff --git a/include/prometheus_helpers.h b/include/prometheus_helpers.h index e2d9159fa..41759c8d3 100644 --- a/include/prometheus_helpers.h +++ b/include/prometheus_helpers.h @@ -252,6 +252,36 @@ inline void p_update_counter(prometheus::Counter* const counter, const double ne counter->Increment(new_val - actual_val); } +/** + * @brief Updates the supplied gauge map gauge corresponding with the supplied identifier. + * In case the identifier doesn't exist in the map, a new gauge is created used the supplied + * metric labels and 'gauge family'. + * + * @param gauge_map The gauge map to be updated. + * @param gauge_family The 'gauge family' required to create a new gauge if not present. + * @param m_id The target gauge identifier. + * @param m_labels The labels for creating the new gauge if not present. + * @param new_val The new value to be set in the gauge. + */ +inline void p_update_map_gauge( + std::map& gauge_map, + prometheus::Family* const gauge_family, + const std::string& m_id, + const std::map& m_labels, + const double& new_val +) { + + const auto& id_val = gauge_map.find(m_id); + if (id_val != gauge_map.end()) { + id_val->second->Set(new_val); + } else { + prometheus::Gauge* new_counter = std::addressof(gauge_family->Add(m_labels)); + gauge_map.insert({m_id, new_counter}); + + new_counter->Set(new_val); + } +} + /** * @brief Updates the supplied counter map counter which correspond with the supplied identifier. * In case the identifier doesn't exist in the map, a new counter is created used the supplied @@ -264,20 +294,17 @@ inline void p_update_counter(prometheus::Counter* const counter, const double ne * @param new_val The new value to be set in the counter. */ inline void p_update_map_counter( - std::map counter_map, + std::map& counter_map, prometheus::Family* const counter_family, - const std::string m_id, - const std::map m_labels, - const double new_val + const std::string& m_id, + const std::map& m_labels, + const double& new_val ) { const auto& id_val = counter_map.find(m_id); if (id_val != counter_map.end()) { p_update_counter(id_val->second, new_val); } else { - prometheus::Counter* new_counter = - std::addressof( - counter_family->Add(m_labels) - ); + prometheus::Counter* new_counter = std::addressof(counter_family->Add(m_labels)); counter_map.insert({m_id, new_counter}); p_update_counter(new_counter, new_val); @@ -294,23 +321,20 @@ inline void p_update_map_counter( * @param m_labels The labels for creating a new counter if not present. */ inline void p_inc_map_counter( - std::map counter_map, + std::map& counter_map, prometheus::Family* const counter_family, - const std::string m_id, - const std::map m_labels + const std::string& m_id, + const std::map& m_labels ) { const auto& id_val = counter_map.find(m_id); if (id_val != counter_map.end()) { id_val->second->Increment(); } else { - prometheus::Counter* new_counter = - std::addressof( - counter_family->Add(m_labels) - ); + prometheus::Counter* new_counter = std::addressof(counter_family->Add(m_labels)); counter_map.insert({m_id, new_counter}); new_counter->Increment(); } } -#endif /* __PROXYSQL_PROMETHEUS_HELPERS_H */ \ No newline at end of file +#endif /* __PROXYSQL_PROMETHEUS_HELPERS_H */