]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: stats: add stats size as a parameter for csv/json dump
authorAmaury Denoyelle <adenoyelle@haproxy.com>
Fri, 2 Oct 2020 16:32:01 +0000 (18:32 +0200)
committerChristopher Faulet <cfaulet@haproxy.com>
Mon, 5 Oct 2020 07:06:10 +0000 (09:06 +0200)
Render the stats size parametric in csv/json dump functions. This is
needed for the future patch which provides dynamic stats. For now the
static value ST_F_TOTAL_FIELDS is provided.

Remove unused parameter px on stats_dump_one_line.

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

include/haproxy/stats.h
src/stats.c

index 3c53486d08e6bff7438f94028b2ac5b6e946ed34..4fe96b592f7b674a31e9441beef71a365b24b84b 100644 (file)
@@ -42,7 +42,7 @@ extern struct applet http_stats_applet;
 struct htx;
 int stats_putchk(struct channel *chn, struct htx *htx, struct buffer *chk);
 
-int stats_dump_one_line(const struct field *stats, struct proxy *px, struct appctx *appctx);
+int stats_dump_one_line(const struct field *stats, size_t stats_count, struct appctx *appctx);
 
 int stats_fill_info(struct field *info, int len);
 int stats_fill_fe_stats(struct proxy *px, struct field *stats, int len);
index 9b2e9117bfd51128d6a9832cbc67c3d979baec1a..6ba3f62cde98107c36572acff87d0794ee133624 100644 (file)
@@ -512,11 +512,12 @@ int stats_emit_json_field_tags(struct buffer *out, const struct field *f)
 
 /* Dump all fields from <stats> into <out> using CSV format */
 static int stats_dump_fields_csv(struct buffer *out,
-                                const struct field *stats, unsigned int flags)
+                                 const struct field *stats, size_t stats_count,
+                                 unsigned int flags)
 {
        int field;
 
-       for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
+       for (field = 0; field < stats_count; field++) {
                if (!stats_emit_raw_data_field(out, &stats[field]))
                        return 0;
                if (!chunk_strcat(out, ","))
@@ -528,11 +529,13 @@ static int stats_dump_fields_csv(struct buffer *out,
 
 /* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
 static int stats_dump_fields_typed(struct buffer *out,
-                                  const struct field *stats, unsigned int flags)
+                                   const struct field *stats,
+                                   size_t stats_count,
+                                   unsigned int flags)
 {
        int field;
 
-       for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
+       for (field = 0; field < stats_count; ++field) {
                if (!stats[field].type)
                        continue;
 
@@ -608,8 +611,8 @@ err:
 
 /* 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,
-                                 unsigned int flags)
+                                  const struct field *stats, size_t stats_count,
+                                  unsigned int flags)
 {
        int field;
        int started = 0;
@@ -619,7 +622,7 @@ static int stats_dump_fields_json(struct buffer *out,
        if (!chunk_strcat(out, "["))
                return 0;
 
-       for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
+       for (field = 0; field < stats_count; field++) {
                const char *obj_type;
                int old_len;
 
@@ -1381,18 +1384,19 @@ static int stats_dump_fields_html(struct buffer *out,
        return 1;
 }
 
-int stats_dump_one_line(const struct field *stats, struct proxy *px, struct appctx *appctx)
+int stats_dump_one_line(const struct field *stats, size_t stats_count,
+                        struct appctx *appctx)
 {
        int ret;
 
        if (appctx->ctx.stats.flags & STAT_FMT_HTML)
                ret = stats_dump_fields_html(&trash, stats, appctx->ctx.stats.flags);
        else if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
-               ret = stats_dump_fields_typed(&trash, stats, appctx->ctx.stats.flags);
+               ret = stats_dump_fields_typed(&trash, stats, stats_count, appctx->ctx.stats.flags);
        else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
-               ret = stats_dump_fields_json(&trash, stats, appctx->ctx.stats.flags);
+               ret = stats_dump_fields_json(&trash, stats, stats_count, appctx->ctx.stats.flags);
        else
-               ret = stats_dump_fields_csv(&trash, stats, appctx->ctx.stats.flags);
+               ret = stats_dump_fields_csv(&trash, stats, stats_count, appctx->ctx.stats.flags);
 
        if (ret)
                appctx->ctx.stats.flags |= STAT_STARTED;
@@ -1486,7 +1490,7 @@ static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
        if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS))
                return 0;
 
-       return stats_dump_one_line(stats, px, appctx);
+       return stats_dump_one_line(stats, ST_F_TOTAL_FIELDS, appctx);
 }
 
 /* Fill <stats> with the listener statistics. <stats> is
@@ -1571,7 +1575,7 @@ static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, st
        if (!stats_fill_li_stats(px, l, appctx->ctx.stats.flags, stats, ST_F_TOTAL_FIELDS))
                return 0;
 
-       return stats_dump_one_line(stats, px, appctx);
+       return stats_dump_one_line(stats, ST_F_TOTAL_FIELDS, appctx);
 }
 
 enum srv_stats_state {
@@ -1878,7 +1882,7 @@ static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, st
        if (!stats_fill_sv_stats(px, sv, appctx->ctx.stats.flags, stats, ST_F_TOTAL_FIELDS))
                return 0;
 
-       return stats_dump_one_line(stats, px, appctx);
+       return stats_dump_one_line(stats, ST_F_TOTAL_FIELDS, appctx);
 }
 
 /* Fill <stats> with the backend statistics. <stats> is
@@ -1998,7 +2002,7 @@ static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px)
        if (!stats_fill_be_stats(px, appctx->ctx.stats.flags, stats, ST_F_TOTAL_FIELDS))
                return 0;
 
-       return stats_dump_one_line(stats, px, appctx);
+       return stats_dump_one_line(stats, ST_F_TOTAL_FIELDS, appctx);
 }
 
 /* Dumps the HTML table header for proxy <px> to the trash for and uses the state from