]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: stats: avoid excessive padding of float values with trailing zeroes
authorWilly Tarreau <w@1wt.eu>
Sat, 8 May 2021 08:38:20 +0000 (10:38 +0200)
committerWilly Tarreau <w@1wt.eu>
Sat, 8 May 2021 08:48:17 +0000 (10:48 +0200)
When emitting stats, we don't need to have 6 zeroes after the decimal point
for each value, so let's trim floating point numbers to the longest needed
only.

src/stats.c

index 9862fdd35aca83d3e73af3e8116fe1522c6e3607..df1e9be699d0f004c8cac319ac79210d8769cd65 100644 (file)
@@ -374,7 +374,11 @@ 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_FLT:   {
+               size_t prev_data = out->data;
+               out->data = flt_trim(out->area, prev_data, chunk_appendf(out, "%f", f->u.flt));
+               return out->data;
+       }
        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);
        }
@@ -406,7 +410,11 @@ 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_FLT:   {
+               size_t prev_data = out->data;
+               out->data = flt_trim(out->area, prev_data, chunk_appendf(out, "flt:%f", f->u.flt));
+               return out->data;
+       }
        case FF_STR:   return chunk_appendf(out, "str:%s", field_str(f, 0));
        default:       return chunk_appendf(out, "%08x:?", f->type);
        }
@@ -448,7 +456,7 @@ int stats_emit_json_data_field(struct buffer *out, const struct field *f)
                                (unsigned long long) f->u.u64);
                       break;
        case FF_FLT:   type = "\"flt\"";
-                      snprintf(buf, sizeof(buf), "%f", f->u.flt);
+                      flt_trim(buf, 0, snprintf(buf, sizeof(buf), "%f", f->u.flt));
                       break;
        case FF_STR:   type = "\"str\"";
                       value = field_str(f, 0);