Browse code

xhttp_prom: introduced prom_counter_update

Sergey Safarov authored on 16/09/2021 21:02:21
Showing 4 changed files
... ...
@@ -1155,6 +1155,48 @@ int prom_counter_inc(str *s_name, int number, str *l1, str *l2, str *l3)
1155 1155
 	return 0;
1156 1156
 }
1157 1157
 
1158
+/**
1159
+ * @brief Updates a counter.
1160
+ */
1161
+int prom_counter_update(str *s_name, operation operation, int number, str *l1, str *l2, str *l3)
1162
+{
1163
+	lock_get(prom_lock);
1164
+
1165
+	/* Find a lvalue based on its metric name and labels. */
1166
+	prom_lvalue_t *p = NULL;
1167
+	p = prom_metric_lvalue_get(s_name, M_COUNTER, l1, l2, l3);
1168
+	if (!p) {
1169
+		LM_ERR("Cannot find counter: %.*s\n", s_name->len, s_name->s);
1170
+		lock_release(prom_lock);
1171
+		return -1;
1172
+	}
1173
+
1174
+	if (operation == DECREMENT && p->m.cval < number) {
1175
+		LM_ERR("Counter %.*s cannot have negative value after decrement\n", s_name->len, s_name->s);
1176
+		lock_release(prom_lock);
1177
+		return -1;
1178
+	}
1179
+
1180
+	switch (operation)
1181
+	{
1182
+		case INCREMENT:
1183
+			/* Add to counter value. */
1184
+			p->m.cval += number;
1185
+			break;
1186
+		case DECREMENT:
1187
+			/* Decrement from counter value. */
1188
+			p->m.cval -= number;
1189
+			break;
1190
+		default:
1191
+			LM_ERR("Unknown counter operation\n");
1192
+			lock_release(prom_lock);
1193
+			return -1;
1194
+	}
1195
+
1196
+	lock_release(prom_lock);
1197
+	return 0;
1198
+}
1199
+
1158 1200
 /**
1159 1201
  * @brief Reset a counter.
1160 1202
  */
... ...
@@ -80,6 +80,11 @@ int prom_gauge_reset(str *s_name, str *l1, str *l2, str *l3);
80 80
  */
81 81
 int prom_counter_inc(str *s_name, int number, str *l1, str *l2, str *l3);
82 82
 
83
+/**
84
+ * @brief Updates a counter.
85
+ */
86
+int prom_counter_update(str *s_name, operation operation, int number, str *l1, str *l2, str *l3);
87
+
83 88
 /**
84 89
  * @brief Set a value in a gauge.
85 90
  */
... ...
@@ -1114,9 +1114,9 @@ static int ki_xhttp_prom_counter_inc_l3(struct sip_msg* msg, str *s_name, int nu
1114 1114
 }
1115 1115
 
1116 1116
 /**
1117
- * @brief Add an integer to a counter.
1117
+ * @brief Updates a counter.
1118 1118
  */
1119
-static int w_prom_counter_inc(struct sip_msg* msg, char *pname, char* pnumber,
1119
+static int w_prom_counter_operation(struct sip_msg* msg, operation operation, char *pname, char* pnumber,
1120 1120
 							  char *l1, char *l2, char *l3)
1121 1121
 {
1122 1122
 	int number;
... ...
@@ -1186,16 +1186,16 @@ static int w_prom_counter_inc(struct sip_msg* msg, char *pname, char* pnumber,
1186 1186
 		l3 = NULL;
1187 1187
 	} /* if l1 != NULL */
1188 1188
 
1189
-	if (prom_counter_inc(&s_name, number,
1189
+	if (prom_counter_update(&s_name, operation, number,
1190 1190
 						   (l1!=NULL)?&l1_str:NULL,
1191 1191
 						   (l2!=NULL)?&l2_str:NULL,
1192 1192
 						   (l3!=NULL)?&l3_str:NULL
1193 1193
 						 )) {
1194
-		LM_ERR("Cannot add number: %d to counter: %.*s\n", number, s_name.len, s_name.s);
1194
+		LM_ERR("Cannot %s number: %d %s counter: %.*s\n", operation == INCREMENT? "add" : "decrement", number, operation == INCREMENT? "to" : "from", s_name.len, s_name.s);
1195 1195
 		return -1;
1196 1196
 	}
1197 1197
 
1198
-	LM_DBG("Added %d to counter %.*s\n", number, s_name.len, s_name.s);
1198
+	LM_DBG("%s %d %s counter %.*s\n", operation == INCREMENT? "Added" : "Decremented", number, operation == INCREMENT? "to" : "from", s_name.len, s_name.s);
1199 1199
 	return 1;
1200 1200
 }
1201 1201
 
... ...
@@ -1204,7 +1204,7 @@ static int w_prom_counter_inc(struct sip_msg* msg, char *pname, char* pnumber,
1204 1204
  */
1205 1205
 static int w_prom_counter_inc_l0(struct sip_msg* msg, char *pname, char* pnumber)
1206 1206
 {
1207
-	return w_prom_counter_inc(msg, pname, pnumber, NULL, NULL, NULL);
1207
+	return w_prom_counter_operation(msg, INCREMENT, pname, pnumber, NULL, NULL, NULL);
1208 1208
 }
1209 1209
 
1210 1210
 /**
... ...
@@ -1213,7 +1213,7 @@ static int w_prom_counter_inc_l0(struct sip_msg* msg, char *pname, char* pnumber
1213 1213
 static int w_prom_counter_inc_l1(struct sip_msg* msg, char *pname, char* pnumber,
1214 1214
 								 char *l1)
1215 1215
 {
1216
-	return w_prom_counter_inc(msg, pname, pnumber, l1, NULL, NULL);
1216
+	return w_prom_counter_operation(msg, INCREMENT, pname, pnumber, l1, NULL, NULL);
1217 1217
 }
1218 1218
 
1219 1219
 /**
... ...
@@ -1222,7 +1222,7 @@ static int w_prom_counter_inc_l1(struct sip_msg* msg, char *pname, char* pnumber
1222 1222
 static int w_prom_counter_inc_l2(struct sip_msg* msg, char *pname, char* pnumber,
1223 1223
 								 char *l1, char *l2)
1224 1224
 {
1225
-	return w_prom_counter_inc(msg, pname, pnumber, l1, l2, NULL);
1225
+	return w_prom_counter_operation(msg, INCREMENT, pname, pnumber, l1, l2, NULL);
1226 1226
 }
1227 1227
 
1228 1228
 /**
... ...
@@ -1231,7 +1231,7 @@ static int w_prom_counter_inc_l2(struct sip_msg* msg, char *pname, char* pnumber
1231 1231
 static int w_prom_counter_inc_l3(struct sip_msg* msg, char *pname, char* pnumber,
1232 1232
 								 char *l1, char *l2, char *l3)
1233 1233
 {
1234
-	return w_prom_counter_inc(msg, pname, pnumber, l1, l2, l3);
1234
+	return w_prom_counter_operation(msg, INCREMENT, pname, pnumber, l1, l2, l3);
1235 1235
 }
1236 1236
 
1237 1237
 /**
... ...
@@ -71,6 +71,15 @@ typedef struct prom_ctx {
71 71
 	int reply_sent;
72 72
 } prom_ctx_t;
73 73
 
74
+/**
75
+ * @brief The enum for increment and decrement operations
76
+ *
77
+ */
78
+typedef enum operation {
79
+	INCREMENT,
80
+	DECREMENT
81
+} operation;
82
+
74 83
 /**
75 84
  * @brief string for beginning of metrics.
76 85
  */