[sr-dev] git:master: modules/db_mysql Added statistics (via counter framework) for mysql driver error

Marius Zbihlei marius.zbihlei at 1and1.ro
Fri Aug 13 13:44:00 CEST 2010


Module: sip-router
Branch: master
Commit: 7ca3941c12b8a78d58642241c3dab9621ff0f2e1
URL:    http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=7ca3941c12b8a78d58642241c3dab9621ff0f2e1

Author: Marius Zbihlei <marius.zbihlei at 1and1.ro>
Committer: Marius Zbihlei <marius.zbihlei at 1and1.ro>
Date:   Thu Aug 12 18:11:16 2010 +0300

modules/db_mysql Added statistics (via counter framework) for mysql driver error

These errors are caused by lost connectivity to the server.

---

 modules/db_mysql/km_dbase.c  |    5 +++++
 modules/db_mysql/km_my_con.c |    4 +++-
 modules/db_mysql/mysql_mod.c |   10 ++++++++++
 modules/db_mysql/mysql_mod.h |   11 +++++++++++
 4 files changed, 29 insertions(+), 1 deletions(-)

diff --git a/modules/db_mysql/km_dbase.c b/modules/db_mysql/km_dbase.c
index 49ab55a..b73e73a 100644
--- a/modules/db_mysql/km_dbase.c
+++ b/modules/db_mysql/km_dbase.c
@@ -81,6 +81,7 @@ static int db_mysql_submit_query(const db1_con_t* _h, const str* _s)
 		if ((t - CON_TIMESTAMP(_h)) > my_ping_interval) {
 			if (mysql_ping(CON_CONNECTION(_h))) {
 				LM_WARN("driver error on ping: %s\n", mysql_error(CON_CONNECTION(_h)));
+				counter_inc(mysql_cnts_h.driver_err);
 			}
 		}
 		/*
@@ -115,6 +116,10 @@ static int db_mysql_submit_query(const db1_con_t* _h, const str* _s)
 		}
 	}
 	LM_ERR("driver error on query: %s\n", mysql_error(CON_CONNECTION(_h)));
+	/* Bad queries don't count */
+	if(code == CR_SERVER_GONE_ERROR || code == CR_SERVER_LOST) {
+		counter_inc(mysql_cnts_h.driver_err);
+	}
 	return -2;
 }
 
diff --git a/modules/db_mysql/km_my_con.c b/modules/db_mysql/km_my_con.c
index 953c574..ce20be2 100644
--- a/modules/db_mysql/km_my_con.c
+++ b/modules/db_mysql/km_my_con.c
@@ -34,7 +34,7 @@
 #include "../../mem/mem.h"
 #include "../../dprint.h"
 #include "../../ut.h"
-
+#include "mysql_mod.h"
 
 /*! \brief
  * Create a new connection structure,
@@ -107,6 +107,8 @@ struct my_con* db_mysql_new_connection(const struct db_id* id)
 				id->database, id->port, 0, 0)) {
 #endif
 		LM_ERR("driver error: %s\n", mysql_error(ptr->con));
+		/* increase error counter */
+		counter_inc(mysql_cnts_h.driver_err);
 		mysql_close(ptr->con);
 		goto err;
 	}
diff --git a/modules/db_mysql/mysql_mod.c b/modules/db_mysql/mysql_mod.c
index e0ebe7f..014e396 100644
--- a/modules/db_mysql/mysql_mod.c
+++ b/modules/db_mysql/mysql_mod.c
@@ -57,6 +57,12 @@ unsigned int my_retries = 1;    /* Number of retries when command fails */
 
 unsigned long my_client_ver = 0;
 
+struct mysql_counters_h mysql_cnts_h;
+counter_def_t mysql_cnt_defs[] =  {
+	{&mysql_cnts_h.driver_err, "Mysql driver erros", 0, 0, 0,
+		"incremented each time a Mysql error happened because the server/connection has failed."},
+	{0, 0, 0, 0, 0, 0 }
+};
 #define DEFAULT_MY_SEND_TO  2   /* in seconds */
 #define DEFAULT_MY_RECV_TO  4   /* in seconds */
 
@@ -145,8 +151,12 @@ static int mysql_mod_init(void)
 			" compiled against %ld)\n", MYSQL_VERSION_ID);
 	}
 #endif
+	if (counter_register_array("mysql", mysql_cnt_defs) < 0)
+		goto error;
 
 	return kam_mysql_mod_init();
+error:
+	return -1;
 }
 
 /** @} */
diff --git a/modules/db_mysql/mysql_mod.h b/modules/db_mysql/mysql_mod.h
index 3a6f470..0d98334 100644
--- a/modules/db_mysql/mysql_mod.h
+++ b/modules/db_mysql/mysql_mod.h
@@ -36,10 +36,21 @@
 #ifndef _MYSQL_MOD_H
 #define _MYSQL_MOD_H
 
+#include "../../counters.h"
+
+/* counter struct
+*/
+struct mysql_counters_h {
+    counter_handle_t driver_err;
+};
+/* defined in km_dbase.c */
+extern struct mysql_counters_h mysql_cnts_h;
+
 /** @defgroup mysql MySQL db driver
  *  @ingroup DB_API
  */
 /** @{ */
+
 extern int my_ping_interval;
 extern unsigned int my_connect_to;
 extern unsigned int my_send_to;




More information about the sr-dev mailing list