MINOR: promex: Always pass the final name and description to promex_dmp_ts()

It is easier this way, especially for promex modules. And because name and
description are now explicitly passed to this function, there is no reason
to still pass the metric, its type is enough. The function is easier to read
this way.
This commit is contained in:
Christopher Faulet 2024-01-31 11:54:58 +01:00
parent c4b723df99
commit 1e13796792

View File

@ -412,24 +412,21 @@ static int promex_ts_val_to_str(struct buffer *out, struct field *f, size_t max)
return 1;
}
/* Dump the time series header lines for <metric>. It is its #HELP and #TYPE
/* Dump the time series header lines for the metric <name>. 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_ts_header(struct appctx *appctx, const struct promex_metric *metric,
const struct ist name, const struct ist d,
static int promex_dump_ts_header(const struct ist name, const struct ist desc, enum promex_mt_type type,
struct ist *out, size_t max)
{
struct promex_ctx *ctx = appctx->svcctx;
struct ist type;
struct ist desc;
struct ist t;
switch (metric->type) {
switch (type) {
case PROMEX_MT_COUNTER:
type = ist("counter");
t = ist("counter");
break;
default:
type = ist("gauge");
t = ist("gauge");
}
if (istcat(out, ist("# HELP "), max) == -1 ||
@ -437,18 +434,16 @@ static int promex_dump_ts_header(struct appctx *appctx, const struct promex_metr
istcat(out, ist(" "), max) == -1)
goto full;
if (isttest(d))
desc = d;
else if (metric->flags & PROMEX_FL_INFO_METRIC)
desc = ist(info_fields[ctx->field_num].desc);
else if (!isttest(promex_st_metric_desc[ctx->field_num]))
desc = ist(stat_fields[ctx->field_num].desc);
if (istcat(out, desc, max) == -1 ||
istcat(out, ist("\n# TYPE "), max) == -1 ||
if (istcat(out, ist("# HELP "), max) == -1 ||
istcat(out, name, max) == -1 ||
istcat(out, ist(" "), max) == -1 ||
istcat(out, type, max) == -1 ||
istcat(out, desc, max) == -1)
goto full;
if (istcat(out, ist("\n# TYPE "), max) == -1 ||
istcat(out, name, max) == -1 ||
istcat(out, ist(" "), max) == -1 ||
istcat(out, t, max) == -1 ||
istcat(out, ist("\n"), max) == -1)
goto full;
@ -458,31 +453,32 @@ static int promex_dump_ts_header(struct appctx *appctx, const struct promex_metr
return 0;
}
/* Dump the time series for <metric>. It starts by the metric name followed by
/* Dump the time series for the metric <name>. It starts by the metric name followed by
* its labels (proxy name, server name...) between braces and finally its
* value. If not already done, the header lines are dumped first. It returns 1
* on success. Otherwise if <out> length exceeds <max>, it returns 0.
*/
static int promex_dump_ts(struct appctx *appctx, struct ist prefix,
const struct ist n, const struct ist desc, const struct promex_metric *metric,
const struct ist name, const struct ist desc, enum promex_mt_type type,
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 };
struct ist n = { .ptr = (char[PROMEX_MAX_NAME_LEN]){ 0 }, .len = 0 };
struct promex_ctx *ctx = appctx->svcctx;
size_t len = out->len;
if (out->len + PROMEX_MAX_METRIC_LENGTH > max)
return 0;
/* Fill the metric name */
istcat(&name, prefix, PROMEX_MAX_NAME_LEN);
istcat(&name, (isttest(n) ? n : metric->n), PROMEX_MAX_NAME_LEN);
istcat(&n, prefix, PROMEX_MAX_NAME_LEN);
istcat(&n, name, PROMEX_MAX_NAME_LEN);
if ((ctx->flags & PROMEX_FL_METRIC_HDR) &&
!promex_dump_ts_header(appctx, metric, name, desc, out, max))
!promex_dump_ts_header(n, desc, type, out, max))
goto full;
if (istcat(out, name, max) == -1)
if (istcat(out, n, max) == -1)
goto full;
if (isttest(labels[0].name)) {
@ -534,7 +530,7 @@ static int promex_dump_global_metrics(struct appctx *appctx, struct htx *htx)
struct promex_ctx *ctx = appctx->svcctx;
struct field val;
struct channel *chn = sc_ic(appctx_sc(appctx));
struct ist out = ist2(trash.area, 0);
struct ist name, desc, out = ist2(trash.area, 0);
size_t max = htx_get_max_blksz(htx, channel_htx_recv_max(chn, htx));
int ret = 1;
@ -547,6 +543,9 @@ static int promex_dump_global_metrics(struct appctx *appctx, struct htx *htx)
if (!(promex_global_metrics[ctx->field_num].flags & ctx->flags))
continue;
name = promex_global_metrics[ctx->field_num].n;
desc = ist(info_fields[ctx->field_num].desc);
switch (ctx->field_num) {
case INF_BUILD_INFO:
labels[0].name = ist("version");
@ -558,8 +557,8 @@ static int promex_dump_global_metrics(struct appctx *appctx, struct htx *htx)
val = info[ctx->field_num];
}
if (!promex_dump_ts(appctx, prefix, IST_NULL, IST_NULL,
&promex_global_metrics[ctx->field_num],
if (!promex_dump_ts(appctx, prefix, name, desc,
promex_global_metrics[ctx->field_num].type,
&val, labels, &out, max))
goto full;
@ -588,7 +587,7 @@ static int promex_dump_front_metrics(struct appctx *appctx, struct htx *htx)
struct stats_module *mod = ctx->p[1];
struct field val;
struct channel *chn = sc_ic(appctx_sc(appctx));
struct ist out = ist2(trash.area, 0);
struct ist name, desc, out = ist2(trash.area, 0);
size_t max = htx_get_max_blksz(htx, channel_htx_recv_max(chn, htx));
struct field *stats = stat_l[STATS_DOMAIN_PROXY];
int ret = 1;
@ -598,6 +597,14 @@ static int promex_dump_front_metrics(struct appctx *appctx, struct htx *htx)
if (!(promex_st_metrics[ctx->field_num].flags & ctx->flags))
continue;
name = promex_st_front_metrics_names[ctx->field_num];
desc = promex_st_metric_desc[ctx->field_num];
if (!isttest(name))
name = promex_st_metrics[ctx->field_num].n;
if (!isttest(desc))
desc = ist(stat_fields[ctx->field_num].desc);
if (!px)
px = proxies_list;
@ -622,10 +629,8 @@ static int promex_dump_front_metrics(struct appctx *appctx, struct htx *htx)
labels[1].value = promex_front_st[ctx->obj_state];
val = mkf_u32(FO_STATUS, state == ctx->obj_state);
if (!promex_dump_ts(appctx, prefix,
promex_st_front_metrics_names[ctx->field_num],
promex_st_metric_desc[ctx->field_num],
&promex_st_metrics[ctx->field_num],
if (!promex_dump_ts(appctx, prefix, name, desc,
promex_st_metrics[ctx->field_num].type,
&val, labels, &out, max))
goto full;
}
@ -663,10 +668,8 @@ static int promex_dump_front_metrics(struct appctx *appctx, struct htx *htx)
val = stats[ctx->field_num];
}
if (!promex_dump_ts(appctx, prefix,
promex_st_front_metrics_names[ctx->field_num],
promex_st_metric_desc[ctx->field_num],
&promex_st_metrics[ctx->field_num],
if (!promex_dump_ts(appctx, prefix, name, desc,
promex_st_metrics[ctx->field_num].type,
&val, labels, &out, max))
goto full;
next_px:
@ -694,6 +697,9 @@ static int promex_dump_front_metrics(struct appctx *appctx, struct htx *htx)
}
for (;ctx->mod_field_num < mod->stats_count; ctx->mod_field_num++) {
name = ist2(mod->stats[ctx->mod_field_num].name, strlen(mod->stats[ctx->mod_field_num].name));
desc = ist2(mod->stats[ctx->mod_field_num].desc, strlen(mod->stats[ctx->mod_field_num].desc));
if (!px)
px = proxies_list;
@ -718,10 +724,8 @@ static int promex_dump_front_metrics(struct appctx *appctx, struct htx *htx)
val = stats[ctx->field_num + ctx->mod_field_num];
metric.type = ((val.type == FN_GAUGE) ? PROMEX_MT_GAUGE : PROMEX_MT_COUNTER);
if (!promex_dump_ts(appctx, prefix,
ist2(mod->stats[ctx->mod_field_num].name, strlen(mod->stats[ctx->mod_field_num].name)),
ist2(mod->stats[ctx->mod_field_num].desc, strlen(mod->stats[ctx->mod_field_num].desc)),
&metric, &val, labels, &out, max))
if (!promex_dump_ts(appctx, prefix, name, desc, metric.type,
&val, labels, &out, max))
goto full;
next_px2:
@ -764,7 +768,7 @@ static int promex_dump_listener_metrics(struct appctx *appctx, struct htx *htx)
struct stats_module *mod = ctx->p[2];
struct field val;
struct channel *chn = sc_ic(appctx_sc(appctx));
struct ist out = ist2(trash.area, 0);
struct ist name, desc, out = ist2(trash.area, 0);
size_t max = htx_get_max_blksz(htx, channel_htx_recv_max(chn, htx));
struct field *stats = stat_l[STATS_DOMAIN_PROXY];
int ret = 1;
@ -774,6 +778,14 @@ static int promex_dump_listener_metrics(struct appctx *appctx, struct htx *htx)
if (!(promex_st_metrics[ctx->field_num].flags & ctx->flags))
continue;
name = promex_st_li_metrics_names[ctx->field_num];
desc = promex_st_metric_desc[ctx->field_num];
if (!isttest(name))
name = promex_st_metrics[ctx->field_num].n;
if (!isttest(desc))
desc = ist(stat_fields[ctx->field_num].desc);
if (!px)
px = proxies_list;
@ -808,10 +820,8 @@ static int promex_dump_listener_metrics(struct appctx *appctx, struct htx *htx)
val = mkf_u32(FO_STATUS, status == ctx->obj_state);
labels[2].name = ist("state");
labels[2].value = ist(li_status_st[ctx->obj_state]);
if (!promex_dump_ts(appctx, prefix,
promex_st_li_metrics_names[ctx->field_num],
promex_st_metric_desc[ctx->field_num],
&promex_st_metrics[ctx->field_num],
if (!promex_dump_ts(appctx, prefix, name, desc,
promex_st_metrics[ctx->field_num].type,
&val, labels, &out, max))
goto full;
}
@ -821,10 +831,8 @@ static int promex_dump_listener_metrics(struct appctx *appctx, struct htx *htx)
val = stats[ctx->field_num];
}
if (!promex_dump_ts(appctx, prefix,
promex_st_li_metrics_names[ctx->field_num],
promex_st_metric_desc[ctx->field_num],
&promex_st_metrics[ctx->field_num],
if (!promex_dump_ts(appctx, prefix, name, desc,
promex_st_metrics[ctx->field_num].type,
&val, labels, &out, max))
goto full;
}
@ -855,6 +863,9 @@ static int promex_dump_listener_metrics(struct appctx *appctx, struct htx *htx)
}
for (;ctx->mod_field_num < mod->stats_count; ctx->mod_field_num++) {
name = ist2(mod->stats[ctx->mod_field_num].name, strlen(mod->stats[ctx->mod_field_num].name));
desc = ist2(mod->stats[ctx->mod_field_num].desc, strlen(mod->stats[ctx->mod_field_num].desc));
if (!px)
px = proxies_list;
@ -889,10 +900,8 @@ static int promex_dump_listener_metrics(struct appctx *appctx, struct htx *htx)
val = stats[ctx->field_num + ctx->mod_field_num];
metric.type = ((val.type == FN_GAUGE) ? PROMEX_MT_GAUGE : PROMEX_MT_COUNTER);
if (!promex_dump_ts(appctx, prefix,
ist2(mod->stats[ctx->mod_field_num].name, strlen(mod->stats[ctx->mod_field_num].name)),
ist2(mod->stats[ctx->mod_field_num].desc, strlen(mod->stats[ctx->mod_field_num].desc)),
&metric, &val, labels, &out, max))
if (!promex_dump_ts(appctx, prefix, name, desc, metric.type,
&val, labels, &out, max))
goto full;
}
li = NULL;
@ -938,7 +947,7 @@ static int promex_dump_back_metrics(struct appctx *appctx, struct htx *htx)
struct server *sv;
struct field val;
struct channel *chn = sc_ic(appctx_sc(appctx));
struct ist out = ist2(trash.area, 0);
struct ist name, desc, out = ist2(trash.area, 0);
size_t max = htx_get_max_blksz(htx, channel_htx_recv_max(chn, htx));
struct field *stats = stat_l[STATS_DOMAIN_PROXY];
int ret = 1;
@ -951,6 +960,14 @@ static int promex_dump_back_metrics(struct appctx *appctx, struct htx *htx)
if (!(promex_st_metrics[ctx->field_num].flags & ctx->flags))
continue;
name = promex_st_back_metrics_names[ctx->field_num];
desc = promex_st_metric_desc[ctx->field_num];
if (!isttest(name))
name = promex_st_metrics[ctx->field_num].n;
if (!isttest(desc))
desc = ist(stat_fields[ctx->field_num].desc);
if (!px)
px = proxies_list;
@ -985,10 +1002,8 @@ static int promex_dump_back_metrics(struct appctx *appctx, struct htx *htx)
val = mkf_u32(FN_GAUGE, srv_state_count[ctx->obj_state]);
labels[1].name = ist("state");
labels[1].value = promex_srv_st[ctx->obj_state];
if (!promex_dump_ts(appctx, prefix,
promex_st_back_metrics_names[ctx->field_num],
promex_st_metric_desc[ctx->field_num],
&promex_st_metrics[ctx->field_num],
if (!promex_dump_ts(appctx, prefix, name, desc,
promex_st_metrics[ctx->field_num].type,
&val, labels, &out, max))
goto full;
}
@ -1012,10 +1027,8 @@ static int promex_dump_back_metrics(struct appctx *appctx, struct htx *htx)
check_state = get_check_status_info(ctx->obj_state);
labels[1].name = ist("state");
labels[1].value = ist(check_state);
if (!promex_dump_ts(appctx, prefix,
promex_st_back_metrics_names[ctx->field_num],
promex_st_metric_desc[ctx->field_num],
&promex_st_metrics[ctx->field_num],
if (!promex_dump_ts(appctx, prefix, name, desc,
promex_st_metrics[ctx->field_num].type,
&val, labels, &out, max))
goto full;
}
@ -1027,10 +1040,8 @@ static int promex_dump_back_metrics(struct appctx *appctx, struct htx *htx)
labels[1].name = ist("state");
labels[1].value = promex_back_st[ctx->obj_state];
val = mkf_u32(FO_STATUS, bkd_state == ctx->obj_state);
if (!promex_dump_ts(appctx, prefix,
promex_st_back_metrics_names[ctx->field_num],
promex_st_metric_desc[ctx->field_num],
&promex_st_metrics[ctx->field_num],
if (!promex_dump_ts(appctx, prefix, name, desc,
promex_st_metrics[ctx->field_num].type,
&val, labels, &out, max))
goto full;
}
@ -1098,10 +1109,8 @@ static int promex_dump_back_metrics(struct appctx *appctx, struct htx *htx)
val = stats[ctx->field_num];
}
if (!promex_dump_ts(appctx, prefix,
promex_st_back_metrics_names[ctx->field_num],
promex_st_metric_desc[ctx->field_num],
&promex_st_metrics[ctx->field_num],
if (!promex_dump_ts(appctx, prefix, name, desc,
promex_st_metrics[ctx->field_num].type,
&val, labels, &out, max))
goto full;
next_px:
@ -1129,6 +1138,9 @@ static int promex_dump_back_metrics(struct appctx *appctx, struct htx *htx)
}
for (;ctx->mod_field_num < mod->stats_count; ctx->mod_field_num++) {
name = ist2(mod->stats[ctx->mod_field_num].name, strlen(mod->stats[ctx->mod_field_num].name));
desc = ist2(mod->stats[ctx->mod_field_num].desc, strlen(mod->stats[ctx->mod_field_num].desc));
if (!px)
px = proxies_list;
@ -1153,10 +1165,8 @@ static int promex_dump_back_metrics(struct appctx *appctx, struct htx *htx)
val = stats[ctx->field_num + ctx->mod_field_num];
metric.type = ((val.type == FN_GAUGE) ? PROMEX_MT_GAUGE : PROMEX_MT_COUNTER);
if (!promex_dump_ts(appctx, prefix,
ist2(mod->stats[ctx->mod_field_num].name, strlen(mod->stats[ctx->mod_field_num].name)),
ist2(mod->stats[ctx->mod_field_num].desc, strlen(mod->stats[ctx->mod_field_num].desc)),
&metric, &val, labels, &out, max))
if (!promex_dump_ts(appctx, prefix, name, desc, metric.type,
&val, labels, &out, max))
goto full;
next_px2:
@ -1198,7 +1208,7 @@ static int promex_dump_srv_metrics(struct appctx *appctx, struct htx *htx)
struct stats_module *mod = ctx->p[2];
struct field val;
struct channel *chn = sc_ic(appctx_sc(appctx));
struct ist out = ist2(trash.area, 0);
struct ist name, desc, out = ist2(trash.area, 0);
size_t max = htx_get_max_blksz(htx, channel_htx_recv_max(chn, htx));
struct field *stats = stat_l[STATS_DOMAIN_PROXY];
int ret = 1;
@ -1210,6 +1220,14 @@ static int promex_dump_srv_metrics(struct appctx *appctx, struct htx *htx)
if (!(promex_st_metrics[ctx->field_num].flags & ctx->flags))
continue;
name = promex_st_srv_metrics_names[ctx->field_num];
desc = promex_st_metric_desc[ctx->field_num];
if (!isttest(name))
name = promex_st_metrics[ctx->field_num].n;
if (!isttest(desc))
desc = ist(stat_fields[ctx->field_num].desc);
if (!px)
px = proxies_list;
@ -1243,10 +1261,8 @@ static int promex_dump_srv_metrics(struct appctx *appctx, struct htx *htx)
val = mkf_u32(FO_STATUS, state == ctx->obj_state);
labels[2].name = ist("state");
labels[2].value = promex_srv_st[ctx->obj_state];
if (!promex_dump_ts(appctx, prefix,
promex_st_srv_metrics_names[ctx->field_num],
promex_st_metric_desc[ctx->field_num],
&promex_st_metrics[ctx->field_num],
if (!promex_dump_ts(appctx, prefix, name, desc,
promex_st_metrics[ctx->field_num].type,
&val, labels, &out, max))
goto full;
}
@ -1295,10 +1311,8 @@ static int promex_dump_srv_metrics(struct appctx *appctx, struct htx *htx)
check_state = get_check_status_info(ctx->obj_state);
labels[2].name = ist("state");
labels[2].value = ist(check_state);
if (!promex_dump_ts(appctx, prefix,
promex_st_srv_metrics_names[ctx->field_num],
promex_st_metric_desc[ctx->field_num],
&promex_st_metrics[ctx->field_num],
if (!promex_dump_ts(appctx, prefix, name, desc,
promex_st_metrics[ctx->field_num].type,
&val, labels, &out, max))
goto full;
}
@ -1343,10 +1357,8 @@ static int promex_dump_srv_metrics(struct appctx *appctx, struct htx *htx)
val = stats[ctx->field_num];
}
if (!promex_dump_ts(appctx, prefix,
promex_st_srv_metrics_names[ctx->field_num],
promex_st_metric_desc[ctx->field_num],
&promex_st_metrics[ctx->field_num],
if (!promex_dump_ts(appctx, prefix, name, desc,
promex_st_metrics[ctx->field_num].type,
&val, labels, &out, max))
goto full;
next_sv:
@ -1378,6 +1390,9 @@ static int promex_dump_srv_metrics(struct appctx *appctx, struct htx *htx)
}
for (;ctx->mod_field_num < mod->stats_count; ctx->mod_field_num++) {
name = ist2(mod->stats[ctx->mod_field_num].name, strlen(mod->stats[ctx->mod_field_num].name));
desc = ist2(mod->stats[ctx->mod_field_num].desc, strlen(mod->stats[ctx->mod_field_num].desc));
if (!px)
px = proxies_list;
@ -1413,10 +1428,8 @@ static int promex_dump_srv_metrics(struct appctx *appctx, struct htx *htx)
val = stats[ctx->field_num + ctx->mod_field_num];
metric.type = ((val.type == FN_GAUGE) ? PROMEX_MT_GAUGE : PROMEX_MT_COUNTER);
if (!promex_dump_ts(appctx, prefix,
ist2(mod->stats[ctx->mod_field_num].name, strlen(mod->stats[ctx->mod_field_num].name)),
ist2(mod->stats[ctx->mod_field_num].desc, strlen(mod->stats[ctx->mod_field_num].desc)),
&metric, &val, labels, &out, max))
if (!promex_dump_ts(appctx, prefix, name, desc, metric.type,
&val, labels, &out, max))
goto full;
next_sv2:
@ -1495,7 +1508,7 @@ static int promex_dump_module_metrics(struct appctx *appctx, struct promex_modul
if (ret < 0)
goto error;
if (!promex_dump_ts(appctx, prefix, IST_NULL, desc, &metric,
if (!promex_dump_ts(appctx, prefix, metric.n, desc, metric.type,
&val, labels, out, max))
goto full;