]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
REORG: stats: extract proxy json dump
authorAmaury Denoyelle <adenoyelle@haproxy.com>
Fri, 2 Oct 2020 16:32:03 +0000 (18:32 +0200)
committerChristopher Faulet <cfaulet@haproxy.com>
Mon, 5 Oct 2020 08:53:50 +0000 (10:53 +0200)
Create a dedicated function to dump a proxy as a json content. This
patch will be needed when other types of objects will be available for
json dump.

This patch is needed to extend stat support to components other than
proxies objects.

src/stats.c

index 4334c5860636c0e0703f6fbd097c084a55b17504..e2cf174e0068d718c021e9488d316d8008e0eaaf 100644 (file)
@@ -609,6 +609,34 @@ err:
        return 0;
 }
 
+static void stats_print_proxy_field_json(struct buffer *out,
+                                         const struct field *stat,
+                                         const char *name,
+                                         int pos,
+                                         uint32_t field_type,
+                                         uint32_t iid,
+                                         uint32_t sid,
+                                         uint32_t pid)
+{
+       const char *obj_type;
+       switch (field_type) {
+               case STATS_TYPE_FE: obj_type = "Frontend"; break;
+               case STATS_TYPE_BE: obj_type = "Backend";  break;
+               case STATS_TYPE_SO: obj_type = "Listener"; break;
+               case STATS_TYPE_SV: obj_type = "Server";   break;
+               default:            obj_type = "Unknown";  break;
+       }
+
+       chunk_appendf(out,
+                     "{"
+                     "\"objType\":\"%s\","
+                     "\"proxyId\":%u,"
+                     "\"id\":%u,"
+                     "\"field\":{\"pos\":%d,\"name\":\"%s\"},"
+                     "\"processNum\":%u,",
+                     obj_type, iid, sid, pos, name, pid);
+}
+
 /* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
 static int stats_dump_fields_json(struct buffer *out,
                                   const struct field *stats, size_t stats_count,
@@ -623,7 +651,6 @@ static int stats_dump_fields_json(struct buffer *out,
                return 0;
 
        for (field = 0; field < stats_count; field++) {
-               const char *obj_type;
                int old_len;
 
                if (!stats[field].type)
@@ -633,25 +660,14 @@ static int stats_dump_fields_json(struct buffer *out,
                        goto err;
                started = 1;
 
-               switch (stats[ST_F_TYPE].u.u32) {
-               case STATS_TYPE_FE: obj_type = "Frontend"; break;
-               case STATS_TYPE_BE: obj_type = "Backend";  break;
-               case STATS_TYPE_SO: obj_type = "Listener"; break;
-               case STATS_TYPE_SV: obj_type = "Server";   break;
-               default:            obj_type = "Unknown";  break;
-               }
-
                old_len = out->data;
-               chunk_appendf(out,
-                             "{"
-                               "\"objType\":\"%s\","
-                               "\"proxyId\":%d,"
-                               "\"id\":%d,"
-                               "\"field\":{\"pos\":%d,\"name\":\"%s\"},"
-                               "\"processNum\":%u,",
-                              obj_type, stats[ST_F_IID].u.u32,
-                              stats[ST_F_SID].u.u32, field,
-                              stat_fields[field].name, stats[ST_F_PID].u.u32);
+               stats_print_proxy_field_json(out, &stats[field],
+                                            stat_fields[field].name, field,
+                                            stats[ST_F_TYPE].u.u32,
+                                            stats[ST_F_IID].u.u32,
+                                            stats[ST_F_SID].u.u32,
+                                            stats[ST_F_PID].u.u32);
+
                if (old_len == out->data)
                        goto err;