Change in libosmocore[master]: stat, rate_ctr: Allow setting group name and use it at report time

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

pespin gerrit-no-reply at lists.osmocom.org
Mon May 31 11:43:41 UTC 2021


pespin has uploaded this change for review. ( https://gerrit.osmocom.org/c/libosmocore/+/24467 )


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

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
---
M TODO-RELEASE
M include/osmocom/core/rate_ctr.h
M include/osmocom/core/stat_item.h
M src/rate_ctr.c
M src/stat_item.c
M src/stats_statsd.c
M src/vty/utils.c
7 files changed, 67 insertions(+), 35 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/67/24467/1

diff --git a/TODO-RELEASE b/TODO-RELEASE
index e57ffab..21154b4 100644
--- a/TODO-RELEASE
+++ b/TODO-RELEASE
@@ -12,3 +12,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
+libosmocore     struct rate_ctr_group, osmo_stat_item_group_desc ABI breakage due to new struct members
diff --git a/include/osmocom/core/rate_ctr.h b/include/osmocom/core/rate_ctr.h
index 1669ce4..87895a3 100644
--- a/include/osmocom/core/rate_ctr.h
+++ b/include/osmocom/core/rate_ctr.h
@@ -61,6 +61,8 @@
 	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 */
 	struct rate_ctr ctr[0];
 };
@@ -73,6 +75,7 @@
 {
 	grp->idx = idx;
 }
+void rate_ctr_group_set_name(struct rate_ctr_group *grp, const char *name);
 
 void rate_ctr_group_free(struct rate_ctr_group *grp);
 
diff --git a/include/osmocom/core/stat_item.h b/include/osmocom/core/stat_item.h
index 29e35ef..cccb15a 100644
--- a/include/osmocom/core/stat_item.h
+++ b/include/osmocom/core/stat_item.h
@@ -65,6 +65,8 @@
 	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];
 };
@@ -79,6 +81,7 @@
 {
 	grp->idx = 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);
 
diff --git a/src/rate_ctr.c b/src/rate_ctr.c
index 9043a2c..3384774 100644
--- a/src/rate_ctr.c
+++ b/src/rate_ctr.c
@@ -263,6 +263,16 @@
 	talloc_free(grp);
 }
 
+/*! 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)
 {
diff --git a/src/stat_item.c b/src/stat_item.c
index 40374a7..2a0eb23 100644
--- a/src/stat_item.c
+++ b/src/stat_item.c
@@ -167,6 +167,16 @@
 	talloc_free(grp);
 }
 
+/*! 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.
diff --git a/src/stats_statsd.c b/src/stats_statsd.c
index 99764e6..1acfce8 100644
--- a/src/stats_statsd.c
+++ b/src/stats_statsd.c
@@ -89,7 +89,7 @@
 }
 
 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 @@
 
 	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 @@
 	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 */
 
diff --git a/src/vty/utils.c b/src/vty/utils.c
index 0358d9b..1137f2b 100644
--- a/src/vty/utils.c
+++ b/src/vty/utils.c
@@ -225,12 +225,10 @@
 	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 @@
 	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);
 

-- 
To view, visit https://gerrit.osmocom.org/c/libosmocore/+/24467
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings

Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Change-Id: I0dc510783dd9ae8436dae8005a7b3330e80d36f3
Gerrit-Change-Number: 24467
Gerrit-PatchSet: 1
Gerrit-Owner: pespin <pespin at sysmocom.de>
Gerrit-MessageType: newchange
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20210531/31eb5724/attachment.htm>


More information about the gerrit-log mailing list