diff --git a/include/MySQL_HostGroups_Manager.h b/include/MySQL_HostGroups_Manager.h index 854198ed9..860d07d11 100644 --- a/include/MySQL_HostGroups_Manager.h +++ b/include/MySQL_HostGroups_Manager.h @@ -389,6 +389,14 @@ class MySQL_HostGroups_Manager { pthread_mutex_t mysql_errors_mutex; umap_mysql_errors mysql_errors_umap; + /** + * @brief Update the prometheus "connection_pool" counters. + */ + void p_update_connection_pool(); + /** + * @brief Update the "stats_mysql_gtid_executed" counters. + */ + void p_update_mysql_gtid_executed(); void p_update_connection_pool_update_counter(std::string& endpoint_id, std::string& endpoint_addr, std::string& endpoint_port, std::string& hostgroup_id, std::map& m_map, unsigned long long value, p_hg_dyn_counter::metric idx); void p_update_connection_pool_update_gauge(std::string& endpoint_id, std::string& endpoint_addr, std::string& endpoint_port, std::string& hostgroup_id, std::map& m_map, unsigned long long value, p_hg_dyn_gauge::metric idx); @@ -462,18 +470,11 @@ class MySQL_HostGroups_Manager { ////////////////////////////////////////////////////// } status; + /** - * @brief Update the prometheus connection_pool counters. - */ - void p_update_connection_pool(); - /** - * @brief Update 'myconnpoll' prometheus counters. - */ - void p_update_myconnpoll(); - /** - * @brief Update the stats_mysql_gtid_executed counters. + * @brief Update the module prometheus metrics. */ - void p_update_mysql_gtid_executed(); + void p_update_metrics(); wqueue queue; MySQL_HostGroups_Manager(); diff --git a/include/prometheus_helpers.h b/include/prometheus_helpers.h index 8ee680bfb..a659a3d85 100644 --- a/include/prometheus_helpers.h +++ b/include/prometheus_helpers.h @@ -210,4 +210,15 @@ void init_prometheus_dyn_gauge_array( } } +/** + * @brief Inline helper function to avoid code duplication while updating prometheus counters. + * + * @param counter The counter to be updated. + * @param new_val The new value to be set in the counter. + */ +inline void p_update_counter(prometheus::Counter* const counter, const double new_val) { + const auto& actual_val = counter->Value(); + counter->Increment(new_val - actual_val); +} + #endif /* __PROXYSQL_PROMETHEUS_HELPERS_H */ \ No newline at end of file diff --git a/lib/MySQL_HostGroups_Manager.cpp b/lib/MySQL_HostGroups_Manager.cpp index 5924b8232..0eef3b5a8 100644 --- a/lib/MySQL_HostGroups_Manager.cpp +++ b/lib/MySQL_HostGroups_Manager.cpp @@ -4091,26 +4091,52 @@ void MySQL_HostGroups_Manager::set_server_current_latency_us(char *hostname, int wrunlock(); } -void MySQL_HostGroups_Manager::p_update_myconnpoll() { - const auto& cur_myconnpoll_get = - this->status.p_counter_array[p_hg_counter::myhgm_myconnpool_get]->Value(); - this->status.p_counter_array[p_hg_counter::myhgm_myconnpool_get]->Increment(status.myconnpoll_get - cur_myconnpoll_get); - - const auto& cur_myconnpoll_get_ok = - this->status.p_counter_array[p_hg_counter::myhgm_myconnpool_get_ok]->Value(); - this->status.p_counter_array[p_hg_counter::myhgm_myconnpool_get_ok]->Increment(status.myconnpoll_get_ok - cur_myconnpoll_get_ok); - - const auto& cur_myconnpoll_push = - this->status.p_counter_array[p_hg_counter::myhgm_myconnpool_push]->Value(); - this->status.p_counter_array[p_hg_counter::myhgm_myconnpool_push]->Increment(status.myconnpoll_push - cur_myconnpoll_push); - - const auto& cur_myconnpoll_reset = - this->status.p_counter_array[p_hg_counter::myhgm_myconnpool_reset]->Value(); - this->status.p_counter_array[p_hg_counter::myhgm_myconnpool_reset]->Increment(status.myconnpoll_reset - cur_myconnpoll_reset); - - const auto& cur_myconnpoll_destroy = - this->status.p_counter_array[p_hg_counter::myhgm_myconnpool_destroy]->Value(); - this->status.p_counter_array[p_hg_counter::myhgm_myconnpool_destroy]->Increment(status.myconnpoll_destroy - cur_myconnpoll_destroy); +void MySQL_HostGroups_Manager::p_update_metrics() { + p_update_counter(status.p_counter_array[p_hg_counter::servers_table_version], status.servers_table_version); + // Update *server_connections* related metrics + status.p_gauge_array[p_hg_gauge::server_connections_connected]->Set(status.server_connections_connected); + p_update_counter(status.p_counter_array[p_hg_counter::server_connections_aborted], status.server_connections_aborted); + p_update_counter(status.p_counter_array[p_hg_counter::server_connections_created], status.server_connections_created); + p_update_counter(status.p_counter_array[p_hg_counter::server_connections_delayed], status.server_connections_delayed); + + // Update *client_connections* related metrics + p_update_counter(status.p_counter_array[p_hg_counter::client_connections_created], status.client_connections_created); + p_update_counter(status.p_counter_array[p_hg_counter::client_connections_aborted], status.client_connections_aborted); + status.p_gauge_array[p_hg_gauge::client_connections_connected]->Set(status.client_connections); + + // Update *acess_denied* related metrics + p_update_counter(status.p_counter_array[p_hg_counter::access_denied_wrong_password], status.access_denied_wrong_password); + p_update_counter(status.p_counter_array[p_hg_counter::access_denied_max_connections], status.access_denied_max_connections); + p_update_counter(status.p_counter_array[p_hg_counter::access_denied_max_user_connections], status.access_denied_max_user_connections); + + p_update_counter(status.p_counter_array[p_hg_counter::selects_for_update__autocommit0], status.select_for_update_or_equivalent); + + // Update *com_* related metrics + p_update_counter(status.p_counter_array[p_hg_counter::com_autocommit], status.autocommit_cnt); + p_update_counter(status.p_counter_array[p_hg_counter::com_autocommit_filtered], status.autocommit_cnt_filtered); + p_update_counter(status.p_counter_array[p_hg_counter::com_commit_cnt], status.commit_cnt); + p_update_counter(status.p_counter_array[p_hg_counter::com_commit_cnt_filtered], status.commit_cnt_filtered); + p_update_counter(status.p_counter_array[p_hg_counter::com_rollback], status.rollback_cnt); + p_update_counter(status.p_counter_array[p_hg_counter::com_rollback_filtered], status.rollback_cnt_filtered); + p_update_counter(status.p_counter_array[p_hg_counter::com_backend_init_db], status.backend_init_db); + p_update_counter(status.p_counter_array[p_hg_counter::com_backend_change_user], status.backend_change_user); + p_update_counter(status.p_counter_array[p_hg_counter::com_backend_set_names], status.backend_set_names); + p_update_counter(status.p_counter_array[p_hg_counter::com_frontend_init_db], status.frontend_init_db); + p_update_counter(status.p_counter_array[p_hg_counter::com_frontend_set_names], status.frontend_set_names); + p_update_counter(status.p_counter_array[p_hg_counter::com_frontend_use_db], status.frontend_use_db); + + // Update *myconnpoll* related metrics + p_update_counter(status.p_counter_array[p_hg_counter::myhgm_myconnpool_get], status.myconnpoll_get); + p_update_counter(status.p_counter_array[p_hg_counter::myhgm_myconnpool_get_ok], status.myconnpoll_get_ok); + p_update_counter(status.p_counter_array[p_hg_counter::myhgm_myconnpool_get_ping], status.myconnpoll_get_ping); + p_update_counter(status.p_counter_array[p_hg_counter::myhgm_myconnpool_push], status.myconnpoll_push); + p_update_counter(status.p_counter_array[p_hg_counter::myhgm_myconnpool_reset], status.myconnpoll_reset); + p_update_counter(status.p_counter_array[p_hg_counter::myhgm_myconnpool_destroy], status.myconnpoll_destroy); + + // Update the *connection_pool* metrics + this->p_update_connection_pool(); + // Update the *gtid_executed* metrics + this->p_update_mysql_gtid_executed(); } SQLite3_result * MySQL_HostGroups_Manager::SQL3_Get_ConnPool_Stats() { diff --git a/lib/MySQL_Monitor.cpp b/lib/MySQL_Monitor.cpp index b2c00905e..4338476b8 100644 --- a/lib/MySQL_Monitor.cpp +++ b/lib/MySQL_Monitor.cpp @@ -755,28 +755,15 @@ void MySQL_Monitor::p_update_metrics() { this->metrics.p_gauge_array[p_mon_gauge::mysql_monitor_workers]->Set(GloMyMon->num_threads); this->metrics.p_gauge_array[p_mon_gauge::mysql_monitor_workers_aux]->Set(GloMyMon->aux_threads); - const auto& cur_started_threads = this->metrics.p_counter_array[p_mon_counter::mysql_monitor_workers_started]->Value(); - this->metrics.p_counter_array[p_mon_counter::mysql_monitor_workers_started]->Increment(GloMyMon->started_threads - cur_started_threads); - - const auto& cur_connect_ok = this->metrics.p_counter_array[p_mon_counter::mysql_monitor_connect_check_ok]->Value(); - this->metrics.p_counter_array[p_mon_counter::mysql_monitor_connect_check_ok]->Increment(GloMyMon->connect_check_OK- cur_connect_ok); - const auto& cur_connect_err = this->metrics.p_counter_array[p_mon_counter::mysql_monitor_connect_check_err]->Value(); - this->metrics.p_counter_array[p_mon_counter::mysql_monitor_connect_check_err]->Increment(GloMyMon->connect_check_ERR - cur_connect_err); - - const auto& cur_ping_check_ok = this->metrics.p_counter_array[p_mon_counter::mysql_monitor_ping_check_ok]->Value(); - this->metrics.p_counter_array[p_mon_counter::mysql_monitor_ping_check_ok]->Increment(GloMyMon->ping_check_OK - cur_ping_check_ok); - const auto& cur_ping_check_err = this->metrics.p_counter_array[p_mon_counter::mysql_monitor_ping_check_err]->Value(); - this->metrics.p_counter_array[p_mon_counter::mysql_monitor_ping_check_err]->Increment(GloMyMon->ping_check_ERR - cur_ping_check_err); - - const auto& cur_read_only_check_ok = this->metrics.p_counter_array[p_mon_counter::mysql_monitor_read_only_check_ok]->Value(); - this->metrics.p_counter_array[p_mon_counter::mysql_monitor_read_only_check_ok]->Increment(GloMyMon->read_only_check_OK - cur_read_only_check_ok); - const auto& cur_read_only_check_err = this->metrics.p_counter_array[p_mon_counter::mysql_monitor_read_only_check_err]->Value(); - this->metrics.p_counter_array[p_mon_counter::mysql_monitor_read_only_check_err]->Increment(GloMyMon->read_only_check_ERR - cur_read_only_check_err); - - const auto& cur_replication_lag_check_ok = this->metrics.p_counter_array[p_mon_counter::mysql_monitor_replication_lag_check_ok]->Value(); - this->metrics.p_counter_array[p_mon_counter::mysql_monitor_replication_lag_check_ok]->Increment(GloMyMon->replication_lag_check_OK - cur_replication_lag_check_ok); - const auto& cur_replication_lag_check_err = this->metrics.p_counter_array[p_mon_counter::mysql_monitor_replication_lag_check_err]->Value(); - this->metrics.p_counter_array[p_mon_counter::mysql_monitor_replication_lag_check_err]->Increment(GloMyMon->replication_lag_check_ERR - cur_replication_lag_check_err); + p_update_counter(this->metrics.p_counter_array[p_mon_counter::mysql_monitor_workers_started], GloMyMon->started_threads); + p_update_counter(this->metrics.p_counter_array[p_mon_counter::mysql_monitor_connect_check_ok], GloMyMon->connect_check_OK); + p_update_counter(this->metrics.p_counter_array[p_mon_counter::mysql_monitor_connect_check_err], GloMyMon->connect_check_ERR); + p_update_counter(this->metrics.p_counter_array[p_mon_counter::mysql_monitor_ping_check_ok], GloMyMon->ping_check_OK); + p_update_counter(this->metrics.p_counter_array[p_mon_counter::mysql_monitor_ping_check_err], GloMyMon->ping_check_ERR ); + p_update_counter(this->metrics.p_counter_array[p_mon_counter::mysql_monitor_read_only_check_ok], GloMyMon->read_only_check_OK); + p_update_counter(this->metrics.p_counter_array[p_mon_counter::mysql_monitor_read_only_check_err], GloMyMon->read_only_check_ERR); + p_update_counter(this->metrics.p_counter_array[p_mon_counter::mysql_monitor_replication_lag_check_ok], GloMyMon->replication_lag_check_OK); + p_update_counter(this->metrics.p_counter_array[p_mon_counter::mysql_monitor_replication_lag_check_err], GloMyMon->replication_lag_check_ERR); } } diff --git a/lib/ProxySQL_Admin.cpp b/lib/ProxySQL_Admin.cpp index f09b76d52..1a72c10db 100644 --- a/lib/ProxySQL_Admin.cpp +++ b/lib/ProxySQL_Admin.cpp @@ -4931,9 +4931,7 @@ void update_modules_metrics() { } // Update mysql_hostgroups_manager metrics if (MyHGM) { - MyHGM->p_update_connection_pool(); - MyHGM->p_update_myconnpoll(); - MyHGM->p_update_mysql_gtid_executed(); + MyHGM->p_update_metrics(); } // Update monitor metrics if (GloMyMon) { diff --git a/lib/Query_Cache.cpp b/lib/Query_Cache.cpp index a83dfa26a..451c6253c 100644 --- a/lib/Query_Cache.cpp +++ b/lib/Query_Cache.cpp @@ -420,27 +420,13 @@ Query_Cache::Query_Cache() { void Query_Cache::p_update_metrics() { 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_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_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_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_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_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_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_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); + p_update_counter(this->metrics.p_counter_array[p_qc_counter::query_cache_count_get], Glo_cntGet); + p_update_counter(this->metrics.p_counter_array[p_qc_counter::query_cache_count_get_ok], Glo_cntGetOK); + p_update_counter(this->metrics.p_counter_array[p_qc_counter::query_cache_count_set], Glo_cntSet); + p_update_counter(this->metrics.p_counter_array[p_qc_counter::query_cache_bytes_in], Glo_dataIN); + p_update_counter(this->metrics.p_counter_array[p_qc_counter::query_cache_bytes_out], Glo_dataOUT); + p_update_counter(this->metrics.p_counter_array[p_qc_counter::query_cache_purged], Glo_cntPurge); + p_update_counter(this->metrics.p_counter_array[p_qc_counter::query_cache_entries], Glo_num_entries); } void Query_Cache::print_version() {