stat,rate_ctr: Allow setting group name and use it at report time

This patch adds a new field "name" to the rate_ctr and osmo_stat_item_group
structs, together with an API to set it. This new field allows for easy
identification of specific group instances when several of them exists,
rather than using a sometimes random/increasing index value.

If set, this name (string) is used instead of the index by the stats
reporter.

The name, if set, is also printed during "show stats" VTY commands.

It's up to the user or application to set up unique or meaningful names
to fullfill one's needs.

WARNING: this commit breaks ABI and possibly creates unexpected behavior
when run with non-rebuilt apps which use the modified structs directly
to get the coutners, or if use the static inline API rate_ctr_inc2().
Existing users of these structs should migrate to use new APIs
introduced in follow-up commits instead of accessing the field directly.

Related: SYS#5456
Change-Id: I0dc510783dd9ae8436dae8005a7b3330e80d36f3
This commit is contained in:
Pau Espin 2021-05-31 13:10:24 +02:00 committed by laforge
parent d794806719
commit 09f075fad6
7 changed files with 67 additions and 35 deletions

View File

@ -13,3 +13,4 @@ libosmosim osim_card_hdl ABI + API breakage due to new struct members
libosmocore osmo_tdef_fsm_inst_state_chg change default_timeout arg from unsigned long to long type (API breakage, not ABI)
libosmovty vty_read_config_filep New API
libosmosim osim_card_{reset,close} New API
libosmocore struct rate_ctr_group, osmo_stat_item_group_desc ABI breakage due to new struct members

View File

@ -61,6 +61,8 @@ struct rate_ctr_group {
const struct rate_ctr_group_desc *desc;
/*! The index of this ctr_group within its class */
unsigned int idx;
/*! Optional string-based identifier to be used instead of index at report time */
char *name;
/*! Actual counter structures below. Don't access it directly, use APIs below! */
struct rate_ctr ctr[0];
};
@ -73,6 +75,7 @@ static inline void rate_ctr_group_upd_idx(struct rate_ctr_group *grp, unsigned i
{
grp->idx = idx;
}
void rate_ctr_group_set_name(struct rate_ctr_group *grp, const char *name);
struct rate_ctr *rate_ctr_group_get_ctr(struct rate_ctr_group *grp, unsigned int idx);

View File

@ -65,6 +65,8 @@ struct osmo_stat_item_group {
const struct osmo_stat_item_group_desc *desc;
/*! The index of this value group within its class */
unsigned int idx;
/*! Optional string-based identifier to be used instead of index at report time */
char *name;
/*! Actual counter structures below */
struct osmo_stat_item *items[0];
};
@ -80,6 +82,7 @@ static inline void osmo_stat_item_group_udp_idx(
grp->idx = idx;
}
struct osmo_stat_item *osmo_stat_item_group_get_item(struct osmo_stat_item_group *grp, unsigned int idx);
void osmo_stat_item_group_set_name(struct osmo_stat_item_group *statg, const char *name);
void osmo_stat_item_group_free(struct osmo_stat_item_group *statg);
void osmo_stat_item_inc(struct osmo_stat_item *item, int32_t value);

View File

@ -273,6 +273,16 @@ struct rate_ctr *rate_ctr_group_get_ctr(struct rate_ctr_group *grp, unsigned int
return &grp->ctr[idx];
}
/*! Set a name for the group of counters be used instead of index value
at report time.
* \param[in] grp Rate counter group
* \param[in] name Name identifier to assign to the rate counter group
*/
void rate_ctr_group_set_name(struct rate_ctr_group *grp, const char *name)
{
osmo_talloc_replace_string(grp, &grp->name, name);
}
/*! Add a number to the counter */
void rate_ctr_add(struct rate_ctr *ctr, int inc)
{

View File

@ -177,6 +177,16 @@ struct osmo_stat_item *osmo_stat_item_group_get_item(struct osmo_stat_item_group
return grp->items[idx];
}
/*! Set a name for the statistics item group to be used instead of index value
at report time.
* \param[in] statg Statistics item group
* \param[in] name Name identifier to assign to the statistics item group
*/
void osmo_stat_item_group_set_name(struct osmo_stat_item_group *statg, const char *name)
{
osmo_talloc_replace_string(statg, &statg->name, name);
}
/*! Increase the stat_item to the given value.
* This function adds a new value for the given stat_item at the end of
* the FIFO.

View File

@ -89,7 +89,7 @@ static void osmo_stats_reporter_sanitize_name(char *buf)
}
static int osmo_stats_reporter_statsd_send(struct osmo_stats_reporter *srep,
const char *name1, unsigned int index1, const char *name2, int64_t value,
const char *name1, const char *index1, const char *name2, int64_t value,
const char *unit)
{
char *buf;
@ -101,13 +101,13 @@ static int osmo_stats_reporter_statsd_send(struct osmo_stats_reporter *srep,
if (prefix) {
if (name1)
fmt = "%1$s.%2$s.%6$u.%3$s:%4$" PRId64 "|%5$s";
fmt = "%1$s.%2$s.%6$s.%3$s:%4$" PRId64 "|%5$s";
else
fmt = "%1$s.%2$0.0s%3$s:%4$" PRId64 "|%5$s";
} else {
prefix = "";
if (name1)
fmt = "%1$s%2$s.%6$u.%3$s:%4$" PRId64 "|%5$s";
fmt = "%1$s%2$s.%6$s.%3$s:%4$" PRId64 "|%5$s";
else
fmt = "%1$s%2$0.0s%3$s:%4$" PRId64 "|%5$s";
}
@ -162,32 +162,42 @@ static int osmo_stats_reporter_statsd_send_counter(struct osmo_stats_reporter *s
const struct rate_ctr_desc *desc,
int64_t value, int64_t delta)
{
if (ctrg)
return osmo_stats_reporter_statsd_send(srep,
ctrg->desc->group_name_prefix,
ctrg->idx,
desc->name, delta, "c");
else
return osmo_stats_reporter_statsd_send(srep,
NULL, 0,
desc->name, delta, "c");
char buf_idx[64];
const char *idx_name = buf_idx;
const char *prefix;
if (ctrg) {
prefix = ctrg->desc->group_name_prefix;
if (ctrg->name)
idx_name = ctrg->name;
else
snprintf(buf_idx, sizeof(buf_idx), "%u", ctrg->idx);
} else {
prefix = NULL;
buf_idx[0] = '0';
buf_idx[1] = '\n';
}
return osmo_stats_reporter_statsd_send(srep, prefix, idx_name, desc->name, delta, "c");
}
static int osmo_stats_reporter_statsd_send_item(struct osmo_stats_reporter *srep,
const struct osmo_stat_item_group *statg,
const struct osmo_stat_item_desc *desc, int64_t value)
{
if (value < 0) {
return osmo_stats_reporter_statsd_send(srep,
statg->desc->group_name_prefix,
statg->idx,
desc->name, 0, "g");
} else {
return osmo_stats_reporter_statsd_send(srep,
statg->desc->group_name_prefix,
statg->idx,
desc->name, value, "g");
char buf_idx[64];
char *idx_name;
if (statg->name)
idx_name = statg->name;
else {
snprintf(buf_idx, sizeof(buf_idx), "%u", statg->idx);
idx_name = buf_idx;
}
if (value < 0)
value = 0;
return osmo_stats_reporter_statsd_send(srep, statg->desc->group_name_prefix,
idx_name, desc->name, value, "g");
}
#endif /* !EMBEDDED */

View File

@ -225,12 +225,10 @@ static int rate_ctr_group_handler(struct rate_ctr_group *ctrg, void *vctx_)
if (ctrg->desc->class_id > vctx->max_level)
return 0;
if (ctrg->idx)
vty_out(vty, "%s%s (%d):%s", vctx->prefix,
ctrg->desc->group_description, ctrg->idx, VTY_NEWLINE);
else
vty_out(vty, "%s%s:%s", vctx->prefix,
ctrg->desc->group_description, VTY_NEWLINE);
vty_out(vty, "%s%s (%d)", vctx->prefix, ctrg->desc->group_description, ctrg->idx);
if (ctrg->name)
vty_out(vty, "('%s')", ctrg->name);
vty_out(vty, ":%s", VTY_NEWLINE);
rate_ctr_for_each_counter(ctrg, rate_ctr_handler, vctx);
@ -284,13 +282,10 @@ static int osmo_stat_item_group_handler(struct osmo_stat_item_group *statg, void
if (statg->desc->class_id > vctx->max_level)
return 0;
if (statg->idx)
vty_out(vty, "%s%s (%d):%s", vctx->prefix,
statg->desc->group_description, statg->idx,
VTY_NEWLINE);
else
vty_out(vty, "%s%s:%s", vctx->prefix,
statg->desc->group_description, VTY_NEWLINE);
vty_out(vty, "%s%s (%d)", vctx->prefix, statg->desc->group_description, statg->idx);
if (statg->name)
vty_out(vty, "('%s')", statg->name);
vty_out(vty, ":%s", VTY_NEWLINE);
osmo_stat_item_for_each_item(statg, osmo_stat_item_handler, vctx);