]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: promex: Add a param to override the description when a metric is dumped
authorChristopher Faulet <cfaulet@haproxy.com>
Mon, 29 Jan 2024 15:37:35 +0000 (16:37 +0100)
committerChristopher Faulet <cfaulet@haproxy.com>
Thu, 1 Feb 2024 11:00:53 +0000 (12:00 +0100)
When a metric is dumped, it is now possible to specify a custom
description. We will add the support for extra counters. The list of these
counters is retrived dynamically. Thus the description must be dynamic
too. Note it was already possible to customize the metric name.

addons/promex/service-prometheus.c

index 228e0db691d5c00dd86f6a897bf2308aebf7a99a..030d96ebe2164816a85856c5181c61d230878f71 100644 (file)
@@ -458,8 +458,8 @@ static int promex_metric_to_str(struct buffer *out, struct field *f, size_t max)
 /* Dump the header lines for <metric>. It is its #HELP and #TYPE strings. It
  * returns 1 on success. Otherwise, if <out> length exceeds <max>, it returns 0.
  */
-static int promex_dump_metric_header(struct appctx *appctx, struct htx *htx,
-                                    const struct promex_metric *metric, const struct ist name,
+static int promex_dump_metric_header(struct appctx *appctx, struct htx *htx, const struct promex_metric *metric,
+                                    const struct ist name, const struct ist d,
                                     struct ist *out, size_t max)
 {
        struct promex_ctx *ctx = appctx->svcctx;
@@ -479,14 +479,12 @@ static int promex_dump_metric_header(struct appctx *appctx, struct htx *htx,
            istcat(out, ist(" "), max) == -1)
                goto full;
 
-       if (metric->flags & PROMEX_FL_INFO_METRIC)
+       if (isttest(d))
+               desc = d;
+       else if (metric->flags & PROMEX_FL_INFO_METRIC)
                desc = ist(info_fields[ctx->field_num].desc);
-       else if (metric->flags & PROMEX_FL_STICKTABLE_METRIC)
-               desc = promex_sticktable_metric_desc[ctx->field_num];
        else if (!isttest(promex_st_metric_desc[ctx->field_num]))
                desc = ist(stat_fields[ctx->field_num].desc);
-       else
-               desc = promex_st_metric_desc[ctx->field_num];
 
        if (istcat(out, desc, max) == -1 ||
            istcat(out, ist("\n# TYPE "), max) == -1 ||
@@ -508,7 +506,7 @@ static int promex_dump_metric_header(struct appctx *appctx, struct htx *htx,
  * success. Otherwise if <out> length exceeds <max>, it returns 0.
  */
 static int promex_dump_metric(struct appctx *appctx, struct htx *htx, struct ist prefix,
-                             const struct ist n, const  struct promex_metric *metric,
+                             const struct ist n, const  struct ist desc, const struct promex_metric *metric,
                              struct field *val, struct promex_label *labels, struct ist *out, size_t max)
 {
        struct ist name = { .ptr = (char[PROMEX_MAX_NAME_LEN]){ 0 }, .len = 0 };
@@ -522,9 +520,8 @@ static int promex_dump_metric(struct appctx *appctx, struct htx *htx, struct ist
        istcat(&name, prefix, PROMEX_MAX_NAME_LEN);
        istcat(&name, (isttest(n) ? n : metric->n), PROMEX_MAX_NAME_LEN);
 
-
        if ((ctx->flags & PROMEX_FL_METRIC_HDR) &&
-           !promex_dump_metric_header(appctx, htx, metric, name, out, max))
+           !promex_dump_metric_header(appctx, htx, metric, name, desc, out, max))
                goto full;
 
        if (istcat(out, name, max) == -1)
@@ -603,7 +600,8 @@ static int promex_dump_global_metrics(struct appctx *appctx, struct htx *htx)
                                val = info[ctx->field_num];
                }
 
-               if (!promex_dump_metric(appctx, htx, prefix, IST_NULL, &promex_global_metrics[ctx->field_num],
+               if (!promex_dump_metric(appctx, htx, prefix, IST_NULL, IST_NULL,
+                                       &promex_global_metrics[ctx->field_num],
                                        &val, labels, &out, max))
                        goto full;
 
@@ -663,8 +661,10 @@ static int promex_dump_front_metrics(struct appctx *appctx, struct htx *htx)
                                                labels[1].name = ist("state");
                                                labels[1].value = promex_front_st[ctx->obj_state];
                                                val = mkf_u32(FO_STATUS, state == ctx->obj_state);
+
                                                if (!promex_dump_metric(appctx, htx, prefix,
                                                                        promex_st_front_metrics_names[ctx->field_num],
+                                                                       promex_st_metric_desc[ctx->field_num],
                                                                        &promex_st_metrics[ctx->field_num],
                                                                        &val, labels, &out, max))
                                                        goto full;
@@ -705,6 +705,7 @@ static int promex_dump_front_metrics(struct appctx *appctx, struct htx *htx)
 
                        if (!promex_dump_metric(appctx, htx, prefix,
                                                promex_st_front_metrics_names[ctx->field_num],
+                                               promex_st_metric_desc[ctx->field_num],
                                                &promex_st_metrics[ctx->field_num],
                                                &val, labels, &out, max))
                                goto full;
@@ -781,6 +782,7 @@ static int promex_dump_listener_metrics(struct appctx *appctx, struct htx *htx)
                                                        labels[2].value = ist(li_status_st[ctx->obj_state]);
                                                        if (!promex_dump_metric(appctx, htx, prefix,
                                                                                promex_st_li_metrics_names[ctx->field_num],
+                                                                               promex_st_metric_desc[ctx->field_num],
                                                                                &promex_st_metrics[ctx->field_num],
                                                                                &val, labels, &out, max))
                                                                goto full;
@@ -793,6 +795,7 @@ static int promex_dump_listener_metrics(struct appctx *appctx, struct htx *htx)
 
                                if (!promex_dump_metric(appctx, htx, prefix,
                                                        promex_st_li_metrics_names[ctx->field_num],
+                                                       promex_st_metric_desc[ctx->field_num],
                                                        &promex_st_metrics[ctx->field_num],
                                                        &val, labels, &out, max))
                                        goto full;
@@ -879,6 +882,7 @@ static int promex_dump_back_metrics(struct appctx *appctx, struct htx *htx)
                                                labels[1].value = promex_srv_st[ctx->obj_state];
                                                if (!promex_dump_metric(appctx, htx, prefix,
                                                                        promex_st_back_metrics_names[ctx->field_num],
+                                                                       promex_st_metric_desc[ctx->field_num],
                                                                        &promex_st_metrics[ctx->field_num],
                                                                        &val, labels, &out, max))
                                                        goto full;
@@ -905,6 +909,7 @@ static int promex_dump_back_metrics(struct appctx *appctx, struct htx *htx)
                                                labels[1].value = ist(check_state);
                                                if (!promex_dump_metric(appctx, htx, prefix,
                                                                        promex_st_back_metrics_names[ctx->field_num],
+                                                                       promex_st_metric_desc[ctx->field_num],
                                                                        &promex_st_metrics[ctx->field_num],
                                                                        &val, labels, &out, max))
                                                        goto full;
@@ -919,6 +924,7 @@ static int promex_dump_back_metrics(struct appctx *appctx, struct htx *htx)
                                                val = mkf_u32(FO_STATUS, bkd_state == ctx->obj_state);
                                                if (!promex_dump_metric(appctx, htx, prefix,
                                                                        promex_st_back_metrics_names[ctx->field_num],
+                                                                       promex_st_metric_desc[ctx->field_num],
                                                                        &promex_st_metrics[ctx->field_num],
                                                                        &val, labels, &out, max))
                                                        goto full;
@@ -989,6 +995,7 @@ static int promex_dump_back_metrics(struct appctx *appctx, struct htx *htx)
 
                        if (!promex_dump_metric(appctx, htx, prefix,
                                                promex_st_back_metrics_names[ctx->field_num],
+                                               promex_st_metric_desc[ctx->field_num],
                                                &promex_st_metrics[ctx->field_num],
                                                &val, labels, &out, max))
                                goto full;
@@ -1066,6 +1073,7 @@ static int promex_dump_srv_metrics(struct appctx *appctx, struct htx *htx)
                                                        labels[2].value = promex_srv_st[ctx->obj_state];
                                                        if (!promex_dump_metric(appctx, htx, prefix,
                                                                                promex_st_srv_metrics_names[ctx->field_num],
+                                                                               promex_st_metric_desc[ctx->field_num],
                                                                                &promex_st_metrics[ctx->field_num],
                                                                                &val, labels, &out, max))
                                                                goto full;
@@ -1117,6 +1125,7 @@ static int promex_dump_srv_metrics(struct appctx *appctx, struct htx *htx)
                                                        labels[2].value = ist(check_state);
                                                        if (!promex_dump_metric(appctx, htx, prefix,
                                                                                promex_st_srv_metrics_names[ctx->field_num],
+                                                                               promex_st_metric_desc[ctx->field_num],
                                                                                &promex_st_metrics[ctx->field_num],
                                                                                &val, labels, &out, max))
                                                                goto full;
@@ -1160,6 +1169,7 @@ static int promex_dump_srv_metrics(struct appctx *appctx, struct htx *htx)
 
                                if (!promex_dump_metric(appctx, htx, prefix,
                                                        promex_st_srv_metrics_names[ctx->field_num],
+                                                       promex_st_metric_desc[ctx->field_num],
                                                        &promex_st_metrics[ctx->field_num],
                                                        &val, labels, &out, max))
                                        goto full;
@@ -1229,6 +1239,7 @@ static int promex_dump_sticktable_metrics(struct appctx *appctx, struct htx *htx
                        }
 
                        if (!promex_dump_metric(appctx, htx, prefix, IST_NULL,
+                                               promex_sticktable_metric_desc[ctx->field_num],
                                                &promex_sticktable_metrics[ctx->field_num],
                                                &val, labels, &out, max))
                                goto full;