Browse code

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

These errors are caused by lost connectivity to the server.

Marius Zbihlei authored on 12/08/2010 15:11:16
Showing 4 changed files
... ...
@@ -81,6 +81,7 @@ static int db_mysql_submit_query(const db1_con_t* _h, const str* _s)
81 81
 		if ((t - CON_TIMESTAMP(_h)) > my_ping_interval) {
82 82
 			if (mysql_ping(CON_CONNECTION(_h))) {
83 83
 				LM_WARN("driver error on ping: %s\n", mysql_error(CON_CONNECTION(_h)));
84
+				counter_inc(mysql_cnts_h.driver_err);
84 85
 			}
85 86
 		}
86 87
 		/*
... ...
@@ -115,6 +116,10 @@ static int db_mysql_submit_query(const db1_con_t* _h, const str* _s)
115 116
 		}
116 117
 	}
117 118
 	LM_ERR("driver error on query: %s\n", mysql_error(CON_CONNECTION(_h)));
119
+	/* Bad queries don't count */
120
+	if(code == CR_SERVER_GONE_ERROR || code == CR_SERVER_LOST) {
121
+		counter_inc(mysql_cnts_h.driver_err);
122
+	}
118 123
 	return -2;
119 124
 }
120 125
 
... ...
@@ -34,7 +34,7 @@
34 34
 #include "../../mem/mem.h"
35 35
 #include "../../dprint.h"
36 36
 #include "../../ut.h"
37
-
37
+#include "mysql_mod.h"
38 38
 
39 39
 /*! \brief
40 40
  * Create a new connection structure,
... ...
@@ -107,6 +107,8 @@ struct my_con* db_mysql_new_connection(const struct db_id* id)
107 107
 				id->database, id->port, 0, 0)) {
108 108
 #endif
109 109
 		LM_ERR("driver error: %s\n", mysql_error(ptr->con));
110
+		/* increase error counter */
111
+		counter_inc(mysql_cnts_h.driver_err);
110 112
 		mysql_close(ptr->con);
111 113
 		goto err;
112 114
 	}
... ...
@@ -57,6 +57,12 @@ unsigned int my_retries = 1;    /* Number of retries when command fails */
57 57
 
58 58
 unsigned long my_client_ver = 0;
59 59
 
60
+struct mysql_counters_h mysql_cnts_h;
61
+counter_def_t mysql_cnt_defs[] =  {
62
+	{&mysql_cnts_h.driver_err, "Mysql driver erros", 0, 0, 0,
63
+		"incremented each time a Mysql error happened because the server/connection has failed."},
64
+	{0, 0, 0, 0, 0, 0 }
65
+};
60 66
 #define DEFAULT_MY_SEND_TO  2   /* in seconds */
61 67
 #define DEFAULT_MY_RECV_TO  4   /* in seconds */
62 68
 
... ...
@@ -145,8 +151,12 @@ static int mysql_mod_init(void)
145 151
 			" compiled against %ld)\n", MYSQL_VERSION_ID);
146 152
 	}
147 153
 #endif
154
+	if (counter_register_array("mysql", mysql_cnt_defs) < 0)
155
+		goto error;
148 156
 
149 157
 	return kam_mysql_mod_init();
158
+error:
159
+	return -1;
150 160
 }
151 161
 
152 162
 /** @} */
... ...
@@ -36,10 +36,21 @@
36 36
 #ifndef _MYSQL_MOD_H
37 37
 #define _MYSQL_MOD_H
38 38
 
39
+#include "../../counters.h"
40
+
41
+/* counter struct
42
+*/
43
+struct mysql_counters_h {
44
+    counter_handle_t driver_err;
45
+};
46
+/* defined in km_dbase.c */
47
+extern struct mysql_counters_h mysql_cnts_h;
48
+
39 49
 /** @defgroup mysql MySQL db driver
40 50
  *  @ingroup DB_API
41 51
  */
42 52
 /** @{ */
53
+
43 54
 extern int my_ping_interval;
44 55
 extern unsigned int my_connect_to;
45 56
 extern unsigned int my_send_to;