MEDIUM: promex/stick-table: Dump stick-table metrics via a promex module

Create a promex module to dump stick-table metrics. Thanks to this patch,
all references to stick tables were removed from the promex service.
This commit is contained in:
Christopher Faulet 2024-01-31 10:33:55 +01:00
parent 5f875414eb
commit 3e55b3da30
3 changed files with 93 additions and 53 deletions

View File

@ -37,22 +37,19 @@
#define PROMEX_FL_BACK_METRIC 0x00000008
#define PROMEX_FL_SRV_METRIC 0x00000010
#define PROMEX_FL_LI_METRIC 0x00000020
#define PROMEX_FL_STICKTABLE_METRIC 0x00000040
#define PROMEX_FL_MODULE_METRIC 0x00000080
#define PROMEX_FL_SCOPE_GLOBAL 0x00000100
#define PROMEX_FL_SCOPE_FRONT 0x00000200
#define PROMEX_FL_SCOPE_BACK 0x00000400
#define PROMEX_FL_SCOPE_SERVER 0x00000800
#define PROMEX_FL_SCOPE_LI 0x00001000
#define PROMEX_FL_SCOPE_STICKTABLE 0x00002000
#define PROMEX_FL_SCOPE_MODULE 0x00004000
#define PROMEX_FL_NO_MAINT_SRV 0x00008000
#define PROMEX_FL_EXTRA_COUNTERS 0x00010000
#define PROMEX_FL_MODULE_METRIC 0x00000040
#define PROMEX_FL_SCOPE_GLOBAL 0x00000080
#define PROMEX_FL_SCOPE_FRONT 0x00000100
#define PROMEX_FL_SCOPE_BACK 0x00000200
#define PROMEX_FL_SCOPE_SERVER 0x00000400
#define PROMEX_FL_SCOPE_LI 0x00000800
#define PROMEX_FL_SCOPE_MODULE 0x00001000
#define PROMEX_FL_NO_MAINT_SRV 0x00002000
#define PROMEX_FL_EXTRA_COUNTERS 0x00004000
#define PROMEX_FL_SCOPE_ALL (PROMEX_FL_SCOPE_GLOBAL | PROMEX_FL_SCOPE_FRONT | \
PROMEX_FL_SCOPE_LI | PROMEX_FL_SCOPE_BACK | \
PROMEX_FL_SCOPE_SERVER | PROMEX_FL_SCOPE_MODULE | \
PROMEX_FL_SCOPE_STICKTABLE)
PROMEX_FL_SCOPE_SERVER | PROMEX_FL_SCOPE_MODULE)
/* The max number of labels per metric */
#define PROMEX_MAX_LABELS 8

View File

@ -59,8 +59,7 @@ enum {
PROMEX_DUMPER_BACK, /* dump metrics of backend proxies */
PROMEX_DUMPER_LI, /* dump metrics of listeners */
PROMEX_DUMPER_SRV, /* dump metrics of servers */
PROMEX_DUMPER_STICKTABLE, /* dump metrics of stick tables */
PROMEX_DUMPER_MODULES, /* dump metrics of stick tables */
PROMEX_DUMPER_MODULES, /* dump metrics of modules */
PROMEX_DUMPER_DONE, /* finished */
};
@ -306,25 +305,6 @@ const struct ist promex_st_metric_desc[ST_F_TOTAL_FIELDS] = {
[ST_F_TT_MAX] = IST("Maximum observed total request+response time (request+queue+connect+response+processing)"),
};
/* stick table base fields */
enum sticktable_field {
STICKTABLE_SIZE = 0,
STICKTABLE_USED,
/* must always be the last one */
STICKTABLE_TOTAL_FIELDS
};
const struct promex_metric promex_sticktable_metrics[STICKTABLE_TOTAL_FIELDS] = {
[STICKTABLE_SIZE] = { .n = IST("size"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_STICKTABLE_METRIC },
[STICKTABLE_USED] = { .n = IST("used"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_STICKTABLE_METRIC },
};
/* stick table base description */
const struct ist promex_sticktable_metric_desc[STICKTABLE_TOTAL_FIELDS] = {
[STICKTABLE_SIZE] = IST("Stick table size."),
[STICKTABLE_USED] = IST("Number of entries used in this stick table."),
};
/* Specific labels for all ST_F_HRSP_* fields */
const struct ist promex_hrsp_code[1 + ST_F_HRSP_OTHER - ST_F_HRSP_1XX] = {
[ST_F_HRSP_1XX - ST_F_HRSP_1XX] = IST("1xx"),
@ -1724,23 +1704,6 @@ static int promex_dump_metrics(struct appctx *appctx, struct stconn *sc, struct
}
ctx->flags &= ~(PROMEX_FL_METRIC_HDR|PROMEX_FL_SRV_METRIC);
ctx->flags |= (PROMEX_FL_METRIC_HDR|PROMEX_FL_STICKTABLE_METRIC);
ctx->field_num = STICKTABLE_SIZE;
ctx->mod_field_num = 0;
appctx->st1 = PROMEX_DUMPER_STICKTABLE;
__fallthrough;
case PROMEX_DUMPER_STICKTABLE:
if (ctx->flags & PROMEX_FL_SCOPE_STICKTABLE) {
ret = promex_dump_sticktable_metrics(appctx, htx);
if (ret <= 0) {
if (ret == -1)
goto error;
goto full;
}
}
ctx->flags &= ~(PROMEX_FL_METRIC_HDR|PROMEX_FL_STICKTABLE_METRIC);
ctx->flags |= (PROMEX_FL_METRIC_HDR|PROMEX_FL_MODULE_METRIC);
ctx->field_num = 0;
ctx->mod_field_num = 0;
@ -1872,8 +1835,6 @@ static int promex_parse_uri(struct appctx *appctx, struct stconn *sc)
ctx->flags |= PROMEX_FL_SCOPE_FRONT;
else if (strcmp(value, "listener") == 0)
ctx->flags |= PROMEX_FL_SCOPE_LI;
else if (strcmp(value, "sticktable") == 0)
ctx->flags |= PROMEX_FL_SCOPE_STICKTABLE;
else {
struct promex_module *mod;
struct promex_module_ref *ref;

View File

@ -46,6 +46,18 @@
#include <haproxy/tools.h>
#include <haproxy/xxhash.h>
#if defined(USE_PROMEX)
#include <promex/promex.h>
#endif
/* stick table base fields */
enum sticktable_field {
STICKTABLE_SIZE = 0,
STICKTABLE_USED,
/* must always be the last one */
STICKTABLE_TOTAL_FIELDS
};
/* structure used to return a table key built from a sample */
static THREAD_LOCAL struct stktable_key static_table_key;
@ -5736,3 +5748,73 @@ static struct cfg_kw_list cfg_kws = {{ },{
}};
INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
#if defined(USE_PROMEX)
static int stk_promex_metric_info(unsigned int id, struct promex_metric *metric, struct ist *desc)
{
switch (id) {
case STICKTABLE_SIZE:
*metric = (struct promex_metric){ .n = ist("size"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_MODULE_METRIC };
*desc = ist("Stick table size.");
break;
case STICKTABLE_USED:
*metric = (struct promex_metric){ .n = ist("used"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_MODULE_METRIC };
*desc = ist("Number of entries used in this stick table.");
break;
default:
return -1;
}
return 1;
}
static void *stk_promex_start_ts(void *unused, unsigned int id)
{
return stktables_list;
}
static void *stk_promex_next_ts(void *unsued, void *metric_ctx, unsigned int id)
{
struct stktable *t = metric_ctx;
return t->next;
}
static int stk_promex_fill_ts(void *unused, void *metric_ctx, unsigned int id, struct promex_label *labels, struct field *field)
{
struct stktable *t = metric_ctx;
if (!t->size)
return 0;
labels[0].name = ist("name");
labels[0].value = ist2(t->id, strlen(t->id));
labels[1].name = ist("type");
labels[1].value = ist2(stktable_types[t->type].kw, strlen(stktable_types[t->type].kw));
switch (id) {
case STICKTABLE_SIZE:
*field = mkf_u32(FN_GAUGE, t->size);
break;
case STICKTABLE_USED:
*field = mkf_u32(FN_GAUGE, t->current);
break;
default:
return -1;
}
return 1;
}
static struct promex_module promex_sticktable_module = {
.name = IST("sticktable"),
.metric_info = stk_promex_metric_info,
.start_ts = stk_promex_start_ts,
.next_ts = stk_promex_next_ts,
.fill_ts = stk_promex_fill_ts,
.nb_metrics = STICKTABLE_TOTAL_FIELDS,
};
INITCALL1(STG_REGISTER, promex_register_module, &promex_sticktable_module);
#endif