]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: stats: Add the support of float fields in stats
authorChristopher Faulet <cfaulet@haproxy.com>
Tue, 24 Sep 2019 14:35:10 +0000 (16:35 +0200)
committerChristopher Faulet <cfaulet@haproxy.com>
Fri, 27 Sep 2019 06:49:09 +0000 (08:49 +0200)
It is now possible to format stats counters as floats. But the stats applet does
not use it.

This patch is required by the Prometheus exporter to send the time averages in
seconds. If the promex change is backported, this patch must be backported
first.

include/proto/stats.h
include/types/stats.h
src/stats.c

index 01c96cb23a2c257062e1e857fdbe9ec3fa0e0fc2..6144d2f005c6ce70d81128019892c6768a5b8f2e 100644 (file)
@@ -84,6 +84,12 @@ static inline struct field mkf_str(uint32_t type, const char *value)
        return f;
 }
 
+static inline struct field mkf_flt(uint32_t type, double value)
+{
+       struct field f = { .type = FF_FLT | type, .u.flt = value };
+       return f;
+}
+
 extern const char *stat_status_codes[];
 
 /* These two structs contains all field names according with
index d99d51911543ec11234fd356d8a8ebde40465d24..b3a69c61d6346f40bee3bff0cb6678a44ca17ab3 100644 (file)
@@ -198,6 +198,7 @@ enum field_format {
        FF_S64      = 0x00000003,
        FF_U64      = 0x00000004,
        FF_STR      = 0x00000005,
+       FF_FLT      = 0x00000006,
        FF_MASK     = 0x000000FF,
 };
 
@@ -245,6 +246,7 @@ struct field {
                uint32_t    u32; /* FF_U32 */
                int64_t     s64; /* FF_S64 */
                uint64_t    u64; /* FF_U64 */
+               double      flt; /* FF_FLT */
                const char *str; /* FF_STR */
        } u;
 };
index f44f5eef7c40223beab7043df652e0392b79644b..f280e5a7178ed723ea37df00aa13836b8dbe10ae 100644 (file)
@@ -333,6 +333,7 @@ int stats_emit_raw_data_field(struct buffer *out, const struct field *f)
        case FF_U32:   return chunk_appendf(out, "%u", f->u.u32);
        case FF_S64:   return chunk_appendf(out, "%lld", (long long)f->u.s64);
        case FF_U64:   return chunk_appendf(out, "%llu", (unsigned long long)f->u.u64);
+       case FF_FLT:   return chunk_appendf(out, "%f", f->u.flt);
        case FF_STR:   return csv_enc_append(field_str(f, 0), 1, out) != NULL;
        default:       return chunk_appendf(out, "[INCORRECT_FIELD_TYPE_%08x]", f->type);
        }
@@ -350,6 +351,7 @@ int stats_emit_typed_data_field(struct buffer *out, const struct field *f)
        case FF_U32:   return chunk_appendf(out, "u32:%u", f->u.u32);
        case FF_S64:   return chunk_appendf(out, "s64:%lld", (long long)f->u.s64);
        case FF_U64:   return chunk_appendf(out, "u64:%llu", (unsigned long long)f->u.u64);
+       case FF_FLT:   return chunk_appendf(out, "flt:%f", f->u.flt);
        case FF_STR:   return chunk_appendf(out, "str:%s", field_str(f, 0));
        default:       return chunk_appendf(out, "%08x:?", f->type);
        }
@@ -389,6 +391,8 @@ int stats_emit_json_data_field(struct buffer *out, const struct field *f)
                       type = "\"u64\"";
                       snprintf(buf, sizeof(buf), "%llu",
                                (unsigned long long) f->u.u64);
+       case FF_FLT:   type = "\"flt\"";
+                      snprintf(buf, sizeof(buf), "%f", f->u.flt);
                       break;
        case FF_STR:   type = "\"str\"";
                       value = field_str(f, 0);