Browse code

xhttp_prom: added rpc_prom_counter_dec

Sergey Safarov authored on 16/09/2021 21:02:21
Showing 1 changed files
... ...
@@ -2108,7 +2108,7 @@ static void rpc_prom_counter_reset(rpc_t *rpc, void *ct)
2108 2108
 	return;
2109 2109
 }
2110 2110
 
2111
-static void rpc_prom_counter_inc(rpc_t *rpc, void *ct)
2111
+static void rpc_prom_counter_update(rpc_t *rpc, void *ct, operation operation)
2112 2112
 {
2113 2113
 	str s_name;
2114 2114
 
... ...
@@ -2137,60 +2137,102 @@ static void rpc_prom_counter_inc(rpc_t *rpc, void *ct)
2137 2137
 	res = rpc->scan(ct, "*SSS", &l1, &l2, &l3);
2138 2138
 	if (res == 0) {
2139 2139
 		/* No labels */
2140
-		if (prom_counter_update(&s_name, INCREMENT, number, NULL, NULL, NULL)) {
2141
-			LM_ERR("Cannot add %d to counter: %.*s\n", number, s_name.len, s_name.s);
2142
-			rpc->fault(ct, 500, "Failed to add %d to counter: %.*s", number,
2140
+		if (prom_counter_update(&s_name, operation, number, NULL, NULL, NULL)) {
2141
+			LM_ERR("Cannot %s %d %s counter: %.*s\n",
2142
+				   operation == INCREMENT? "add" : "decrement",
2143
+				   number,
2144
+				   operation == INCREMENT? "to" : "from",
2145
+				   s_name.len, s_name.s);
2146
+			rpc->fault(ct, 500, "Failed to %s %d %s counter: %.*s",
2147
+					   operation == INCREMENT? "add" : "decrement",
2148
+					   number,
2149
+					   operation == INCREMENT? "to" : "from",
2143 2150
 					   s_name.len, s_name.s);
2144 2151
 			return;
2145 2152
 		}
2146
-		LM_DBG("Added %d to counter: (%.*s)\n", number, s_name.len, s_name.s);
2153
+		LM_DBG("%s %d %s counter: (%.*s)\n",
2154
+			   operation == INCREMENT? "Added" : "Decremented",
2155
+			   number,
2156
+			   operation == INCREMENT? "to" : "from",
2157
+			   s_name.len, s_name.s);
2147 2158
 		
2148 2159
 	} else if (res == 1) {
2149
-		if (prom_counter_update(&s_name, INCREMENT, number, &l1, NULL, NULL)) {
2150
-			LM_ERR("Cannot add %d to counter: %.*s (%.*s)\n", number, s_name.len, s_name.s,
2160
+		if (prom_counter_update(&s_name, operation, number, &l1, NULL, NULL)) {
2161
+			LM_ERR("Cannot %s %d %s counter: %.*s (%.*s)\n",
2162
+				   operation == INCREMENT? "add" : "decrement",
2163
+				   number,
2164
+				   operation == INCREMENT? "to" : "from",
2165
+				   s_name.len, s_name.s,
2151 2166
 				   l1.len, l1.s);
2152
-			rpc->fault(ct, 500, "Failed to add %d to counter: %.*s (%.*s)",
2153
-					   number, s_name.len, s_name.s,
2167
+			rpc->fault(ct, 500, "Failed to %s %d %s counter: %.*s (%.*s)",
2168
+					   operation == INCREMENT? "add" : "decrement",
2169
+					   number,
2170
+					   operation == INCREMENT? "to" : "from",
2171
+					   s_name.len, s_name.s,
2154 2172
 					   l1.len, l1.s);
2155 2173
 			return;
2156 2174
 		}
2157
-		LM_DBG("Added %d to counter: %.*s (%.*s)\n", number, s_name.len, s_name.s,
2175
+		LM_DBG("%s %d %s counter: %.*s (%.*s)\n",
2176
+			   operation == INCREMENT? "Added" : "Decremented",
2177
+			   number,
2178
+			   operation == INCREMENT? "to" : "from",
2179
+			   s_name.len, s_name.s,
2158 2180
 			   l1.len, l1.s);
2159 2181
 
2160 2182
 	} else if (res == 2) {
2161
-		if (prom_counter_update(&s_name, INCREMENT, number, &l1, &l2, NULL)) {
2162
-			LM_ERR("Cannot add %d to counter: %.*s (%.*s, %.*s)\n", number,
2183
+		if (prom_counter_update(&s_name, operation, number, &l1, &l2, NULL)) {
2184
+			LM_ERR("Cannot %s %d %s counter: %.*s (%.*s, %.*s)\n",
2185
+				   operation == INCREMENT? "add" : "decrement",
2186
+				   number,
2187
+				   operation == INCREMENT? "to" : "from",
2163 2188
 				   s_name.len, s_name.s,
2164 2189
 				   l1.len, l1.s,
2165 2190
 				   l2.len, l2.s);
2166
-			rpc->fault(ct, 500, "Failed to add %d to counter: %.*s (%.*s, %.*s)",
2167
-					   number, s_name.len, s_name.s,
2191
+			rpc->fault(ct, 500, "Failed to %s %d %s counter: %.*s (%.*s, %.*s)",
2192
+					   operation == INCREMENT? "add" : "decrement",
2193
+					   number,
2194
+					   operation == INCREMENT? "to" : "from",
2195
+					   s_name.len, s_name.s,
2168 2196
 					   l1.len, l1.s,
2169 2197
 					   l2.len, l2.s
2170 2198
 				);
2171 2199
 			return;
2172 2200
 		}
2173
-		LM_DBG("Added %d to counter: %.*s (%.*s, %.*s)\n", number, s_name.len, s_name.s,
2201
+		LM_DBG("%s %d %s counter: %.*s (%.*s, %.*s)\n",
2202
+			   operation == INCREMENT? "Added" : "Decremented",
2203
+			   number,
2204
+			   operation == INCREMENT? "to" : "from",
2205
+			   s_name.len, s_name.s,
2174 2206
 			   l1.len, l1.s,
2175 2207
 			   l2.len, l2.s);
2176 2208
 
2177 2209
 	} else if (res == 3) {
2178
-		if (prom_counter_update(&s_name, INCREMENT, number, &l1, &l2, &l3)) {
2179
-			LM_ERR("Cannot add %d to counter: %.*s (%.*s, %.*s, %.*s)\n",
2180
-				   number, s_name.len, s_name.s,
2210
+		if (prom_counter_update(&s_name, operation, number, &l1, &l2, &l3)) {
2211
+			LM_ERR("Cannot %s %d %s counter: %.*s (%.*s, %.*s, %.*s)\n",
2212
+				   operation == INCREMENT? "add" : "decrement",
2213
+				   number,
2214
+				   operation == INCREMENT? "to" : "from",
2215
+				   s_name.len, s_name.s,
2181 2216
 				   l1.len, l1.s,
2182 2217
 				   l2.len, l2.s,
2183 2218
 				   l3.len, l3.s
2184 2219
 				);
2185
-			rpc->fault(ct, 500, "Failed to add %d to counter: %.*s (%.*s, %.*s, %.*s)",
2186
-					   number, s_name.len, s_name.s,
2220
+			rpc->fault(ct, 500, "Failed to $s %d %s counter: %.*s (%.*s, %.*s, %.*s)",
2221
+					   operation == INCREMENT? "add" : "decrement",
2222
+					   number,
2223
+					   operation == INCREMENT? "to" : "from",
2224
+					   s_name.len, s_name.s,
2187 2225
 					   l1.len, l1.s,
2188 2226
 					   l2.len, l2.s,
2189 2227
 					   l3.len, l3.s
2190 2228
 				);
2191 2229
 			return;
2192 2230
 		}
2193
-		LM_DBG("Added %d to counter: %.*s (%.*s, %.*s, %.*s)\n", number, s_name.len, s_name.s,
2231
+		LM_DBG("%s %d %s counter: %.*s (%.*s, %.*s, %.*s)\n",
2232
+			   operation == INCREMENT? "Added" : "Decremented",
2233
+			   number,
2234
+			   operation == INCREMENT? "to" : "from",
2235
+			   s_name.len, s_name.s,
2194 2236
 			   l1.len, l1.s,
2195 2237
 			   l2.len, l2.s,
2196 2238
 			   l3.len, l3.s
... ...
@@ -2205,6 +2247,16 @@ static void rpc_prom_counter_inc(rpc_t *rpc, void *ct)
2205 2247
 	return;
2206 2248
 }
2207 2249
 
2250
+static void rpc_prom_counter_inc(rpc_t *rpc, void *ct)
2251
+{
2252
+    rpc_prom_counter_update(rpc, ct, INCREMENT);
2253
+}
2254
+
2255
+static void rpc_prom_counter_dec(rpc_t *rpc, void *ct)
2256
+{
2257
+    rpc_prom_counter_update(rpc, ct, DECREMENT);
2258
+}
2259
+
2208 2260
 static void rpc_prom_gauge_reset(rpc_t *rpc, void *ct)
2209 2261
 {
2210 2262
 	str s_name;
... ...
@@ -2520,6 +2572,10 @@ static const char* rpc_prom_counter_inc_doc[2] = {
2520 2572
 	0
2521 2573
 };
2522 2574
 
2575
+static const char* rpc_prom_counter_dec_doc[2] = {
2576
+	"Decrement a number (greater or equal to zero) from a counter based on its identifier",
2577
+	0
2578
+};
2523 2579
 static const char* rpc_prom_gauge_reset_doc[2] = {
2524 2580
 	"Reset a gauge based on its identifier",
2525 2581
 	0
... ...
@@ -2543,6 +2599,7 @@ static const char* rpc_prom_metric_list_print_doc[2] = {
2543 2599
 static rpc_export_t rpc_cmds[] = {
2544 2600
 	{"xhttp_prom.counter_reset", rpc_prom_counter_reset, rpc_prom_counter_reset_doc, 0},
2545 2601
 	{"xhttp_prom.counter_inc", rpc_prom_counter_inc, rpc_prom_counter_inc_doc, 0},
2602
+	{"xhttp_prom.counter_dec", rpc_prom_counter_dec, rpc_prom_counter_dec_doc, 0},
2546 2603
 	{"xhttp_prom.gauge_reset", rpc_prom_gauge_reset, rpc_prom_gauge_reset_doc, 0},
2547 2604
 	{"xhttp_prom.gauge_set", rpc_prom_gauge_set, rpc_prom_gauge_set_doc, 0},
2548 2605
 	{"xhttp_prom.histogram_observe", rpc_prom_histogram_observe, rpc_prom_histogram_observe_doc, 0},