diff --git a/include/query_cache.hpp b/include/query_cache.hpp index 955a1db0f..f64109520 100644 --- a/include/query_cache.hpp +++ b/include/query_cache.hpp @@ -32,6 +32,111 @@ struct __QC_entry_t { uint32_t ref_count; // reference counter }; +struct p_qc_counter { + enum metric { + query_cache_count_get = 0, + query_cache_count_get_ok, + query_cache_count_set, + query_cache_bytes_in, + query_cache_bytes_out, + query_cache_purged, + query_cache_entries, + __size + }; +}; + +struct p_qc_gauge { + enum metric { + query_cache_memory_bytes = 0, + __size + }; +}; + +struct qc_metrics_map_idx { + enum index { + counters = 0, + gauges + }; +}; + +using active_flag = bool; +using metric_name = std::string; +using metric_help = std::string; +using metric_tags = std::map; + +const static std::tuple< + std::vector< + std::tuple< + p_qc_counter::metric, + metric_name, + metric_help, + metric_tags + > + >, + std::vector< + std::tuple< + p_qc_gauge::metric, + metric_name, + metric_help, + metric_tags + > + > +> +qc_metrics_map { + { + { + p_qc_counter::query_cache_count_get, + "proxysql_query_cache_count_get", + "Number of read requests.", + {} + }, + { + p_qc_counter::query_cache_count_get_ok, + "proxysql_query_cache_count_get_ok", + "Number of successful read requests.", + {} + }, + { + p_qc_counter::query_cache_count_set, + "proxysql_query_cache_count_set", + "Number of write requests.", + {} + }, + { + p_qc_counter::query_cache_bytes_in, + "proxysql_query_cache_bytes_in", + "Number of bytes sent into the Query Cache.", + {} + }, + { + p_qc_counter::query_cache_bytes_out, + "proxysql_query_cache_bytes_out", + "Number of bytes read from the Query Cache.", + {} + }, + { + p_qc_counter::query_cache_purged, + "proxysql_query_cache_purged", + "Number of entries purged by the Query Cache due to TTL expiration.", + {} + }, + { + p_qc_counter::query_cache_entries, + "proxysql_query_cache_entries", + "Number of entries currently stored in the query cache.", + {} + } + }, + { + { + p_qc_gauge::query_cache_memory_bytes, + "proxysql_query_cache_memory_bytes", + "Memory currently used by the query cache (more details later).", + {} + } + } +}; + class KV_BtreeArray; class Query_Cache { private: @@ -39,15 +144,17 @@ class Query_Cache { uint64_t get_data_size_total(); unsigned int current_used_memory_pct(); struct { - prometheus::Gauge* p_query_cache_mem_bytes { nullptr }; - prometheus::Counter* p_query_cache_count_get { nullptr }; - prometheus::Counter* p_query_cache_count_get_ok { nullptr }; - prometheus::Counter* p_query_cache_count_set { nullptr }; - prometheus::Counter* p_query_cache_bytes_in { nullptr }; - prometheus::Counter* p_query_cache_bytes_out { nullptr }; - prometheus::Counter* p_query_cache_purged { nullptr }; - prometheus::Counter* p_query_cache_entries { nullptr }; + std::array p_counter_array {}; + std::array p_gauge_array {}; } metrics; + /** + * @brief Initalizes the prometheus counters specified in th_metrics_map. + */ + void init_prometheus_counters(); + /** + * @brief Initalizes the prometheus gagues specified in th_metrics_map. + */ + void init_prometheus_gauges(); public: void p_update_metrics(); void * purgeHash_thread(void *); diff --git a/lib/Query_Cache.cpp b/lib/Query_Cache.cpp index 7c9f9124f..31b19a376 100644 --- a/lib/Query_Cache.cpp +++ b/lib/Query_Cache.cpp @@ -334,93 +334,93 @@ Query_Cache::Query_Cache() { max_memory_size=DEFAULT_SQC_size; // Initialize prometheus metrics - auto& query_cache_mem_bytes { - prometheus::BuildGauge() - .Name("proxysql_query_cache_memory_bytes") - .Register(*GloVars.prometheus_registry) - }; - this->metrics.p_query_cache_mem_bytes = - std::addressof(query_cache_mem_bytes.Add({})); - - auto& query_cache_count_get { - prometheus::BuildCounter() - .Name("proxysql_query_cache_count_get") - .Register(*GloVars.prometheus_registry) - }; - this->metrics.p_query_cache_count_get = - std::addressof(query_cache_count_get.Add({})); - - auto& query_cache_count_get_ok { - prometheus::BuildCounter() - .Name("proxysql_query_cache_count_get_ok") - .Register(*GloVars.prometheus_registry) - }; - this->metrics.p_query_cache_count_get_ok = - std::addressof(query_cache_count_get_ok.Add({})); - auto& query_cache_count_set { - prometheus::BuildCounter() - .Name("proxysql_query_cache_count_set") - .Register(*GloVars.prometheus_registry) - }; - this->metrics.p_query_cache_count_set = - std::addressof(query_cache_count_set.Add({})); - - auto& query_cache_bytes_in { - prometheus::BuildCounter() - .Name("proxysql_query_cache_bytes_in") - .Register(*GloVars.prometheus_registry) - }; - this->metrics.p_query_cache_bytes_in = - std::addressof(query_cache_bytes_in.Add({})); - - auto& query_cache_bytes_out { - prometheus::BuildCounter() - .Name("proxysql_query_cache_bytes_out") - .Register(*GloVars.prometheus_registry) - }; - this->metrics.p_query_cache_bytes_out = - std::addressof(query_cache_bytes_out.Add({})); - - auto& query_cache_purged { - prometheus::BuildCounter() - .Name("proxysql_query_cache_purged") - .Register(*GloVars.prometheus_registry) - }; - this->metrics.p_query_cache_purged = - std::addressof(query_cache_purged.Add({})); - - auto& query_cache_entries { - prometheus::BuildCounter() - .Name("proxysql_query_cache_entries") - .Register(*GloVars.prometheus_registry) - }; - this->metrics.p_query_cache_entries = - std::addressof(query_cache_entries.Add({})); + init_prometheus_counters(); + init_prometheus_gauges(); }; +void Query_Cache::init_prometheus_counters() { + for (const auto& metric : std::get(qc_metrics_map)) { + const auto& tg_metric { std::get<0>(metric) }; + const auto& metric_name { std::get<1>(metric) }; + const auto& metric_help { std::get<2>(metric) }; + const auto& metric_tags { std::get<3>(metric) }; + prometheus::Family* metric_family { nullptr }; + + if (metric_help.empty()) { + metric_family = + std::addressof( + prometheus::BuildCounter() + .Name(metric_name) + .Register(*GloVars.prometheus_registry) + ); + } else { + metric_family = + std::addressof( + prometheus::BuildCounter() + .Name(metric_name) + .Help(metric_help) + .Register(*GloVars.prometheus_registry) + ); + } + + this->metrics.p_counter_array[tg_metric] = + std::addressof(metric_family->Add(metric_tags)); + } +} + +void Query_Cache::init_prometheus_gauges() { + for (const auto& metric : std::get(qc_metrics_map)) { + const auto& tg_metric { std::get<0>(metric) }; + const auto& metric_name { std::get<1>(metric) }; + const auto& metric_help { std::get<2>(metric) }; + const auto& metric_tags { std::get<3>(metric) }; + prometheus::Family* metric_family { nullptr }; + + if (metric_help.empty()) { + metric_family = + std::addressof( + prometheus::BuildGauge() + .Name(metric_name) + .Register(*GloVars.prometheus_registry) + ); + } else { + metric_family = + std::addressof( + prometheus::BuildGauge() + .Name(metric_name) + .Help(metric_help) + .Register(*GloVars.prometheus_registry) + ); + } + + this->metrics.p_gauge_array[tg_metric] = + std::addressof(metric_family->Add(metric_tags)); + } +} + void Query_Cache::p_update_metrics() { - this->metrics.p_query_cache_mem_bytes->Set(get_data_size_total()); + this->metrics.p_gauge_array[p_qc_gauge::query_cache_memory_bytes]->Set(get_data_size_total()); - const auto& cur_count_get { this->metrics.p_query_cache_count_get->Value() }; - this->metrics.p_query_cache_count_get->Increment(Glo_cntGet - cur_count_get); + const auto& cur_count_get { this->metrics.p_counter_array[p_qc_counter::query_cache_count_get]->Value() }; + this->metrics.p_counter_array[p_qc_counter::query_cache_count_get]->Increment(Glo_cntGet - cur_count_get); - const auto& cur_count_get_ok { this->metrics.p_query_cache_count_get_ok->Value() }; - this->metrics.p_query_cache_count_get_ok->Increment(Glo_cntGetOK - cur_count_get_ok); + const auto& cur_count_get_ok { this->metrics.p_counter_array[p_qc_counter::query_cache_count_get_ok]->Value() }; + this->metrics.p_counter_array[p_qc_counter::query_cache_count_get_ok]->Increment(Glo_cntGetOK - cur_count_get_ok); - const auto& cur_count_set { this->metrics.p_query_cache_count_set->Value() }; - this->metrics.p_query_cache_count_set->Increment(Glo_cntSet - cur_count_set); + const auto& cur_count_set { this->metrics.p_counter_array[p_qc_counter::query_cache_count_set]->Value() }; + this->metrics.p_counter_array[p_qc_counter::query_cache_count_set]->Increment(Glo_cntSet - cur_count_set); - const auto& cur_bytes_in { this->metrics.p_query_cache_bytes_in->Value() }; - this->metrics.p_query_cache_bytes_in->Increment(Glo_dataIN - cur_bytes_in); + const auto& cur_bytes_in { this->metrics.p_counter_array[p_qc_counter::query_cache_bytes_in]->Value() }; + this->metrics.p_counter_array[p_qc_counter::query_cache_bytes_in]->Increment(Glo_dataIN - cur_bytes_in); - const auto& cur_bytes_out { this->metrics.p_query_cache_bytes_out->Value() }; - this->metrics.p_query_cache_bytes_out->Increment(Glo_dataOUT - cur_bytes_out); + const auto& cur_bytes_out { this->metrics.p_counter_array[p_qc_counter::query_cache_bytes_out]->Value() }; + this->metrics.p_counter_array[p_qc_counter::query_cache_bytes_out]->Increment(Glo_dataOUT - cur_bytes_out); - const auto& cur_purged { this->metrics.p_query_cache_purged->Value() }; - this->metrics.p_query_cache_purged->Increment(Glo_cntPurge - cur_purged); + const auto& cur_purged { this->metrics.p_counter_array[p_qc_counter::query_cache_purged]->Value() }; + this->metrics.p_counter_array[p_qc_counter::query_cache_purged]->Increment(Glo_cntPurge - cur_purged); - const auto& cur_entries { this->metrics.p_query_cache_entries->Value() }; - this->metrics.p_query_cache_entries->Increment(Glo_dataIN - cur_entries); + const auto& cur_entries { this->metrics.p_counter_array[p_qc_counter::query_cache_entries]->Value() }; + this->metrics.p_counter_array[p_qc_counter::query_cache_entries]->Increment(Glo_dataIN - cur_entries); } void Query_Cache::print_version() {