diff --git a/include/prometheus_helpers.h b/include/prometheus_helpers.h index a659a3d85..b19772773 100644 --- a/include/prometheus_helpers.h +++ b/include/prometheus_helpers.h @@ -4,6 +4,7 @@ #include #include #include +#include #include "proxysql.h" @@ -221,4 +222,65 @@ inline void p_update_counter(prometheus::Counter* const counter, const double ne counter->Increment(new_val - actual_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 + * metric labels and 'counter family'. + * + * @param counter_map The counter map to be updated. + * @param counter_family The 'counter family' required to create a new counter if not present. + * @param m_id The target counter identifier. + * @param m_labels The labels for creating a new counter if not present. + * @param new_val The new value to be set in the counter. + */ +inline void p_update_map_counter( + std::map counter_map, + prometheus::Family* const counter_family, + 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) + ); + counter_map.insert({m_id, new_counter}); + + p_update_counter(new_counter, new_val); + } +} + +/** + * @brief Updates the supplied counter map counter incrementing the counter that correspond + * with the supplied identifier. In case of non existing, it creates a new one and increment it. + * + * @param counter_map The counter map to be updated. + * @param counter_family The 'counter family' required to create a new counter if not present. + * @param m_id The target counter identifier. + * @param m_labels The labels for creating a new counter if not present. + */ +inline void p_inc_map_counter( + std::map counter_map, + prometheus::Family* const counter_family, + 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) + ); + counter_map.insert({m_id, new_counter}); + + new_counter->Increment(); + } +} + #endif /* __PROXYSQL_PROMETHEUS_HELPERS_H */ \ No newline at end of file