metrics: Move helper function to lib/metrics

It is a common function that a lot of subsystem can use which is to
format a label so move it out of the HS subsystem into the more generic
metrics library.

Signed-off-by: David Goulet <dgoulet@torproject.org>
This commit is contained in:
David Goulet 2021-04-15 09:05:55 -04:00
parent cc2947c007
commit 455471835d
3 changed files with 19 additions and 15 deletions

View File

@ -29,18 +29,6 @@ port_to_str(const uint16_t port)
return buf;
}
/** Return a static buffer pointer that contains a formatted label on the form
* of key=value.
*
* Subsequent call to this function invalidates the previous buffer. */
static const char *
format_label(const char *key, const char *value)
{
static char buf[128];
tor_snprintf(buf, sizeof(buf), "%s=%s", key, value);
return buf;
}
/** Initialize a metrics store for the given service.
*
* Essentially, this goes over the base_metrics array and adds them all to the
@ -61,12 +49,12 @@ init_store(hs_service_t *service)
/* Add labels to the entry. */
metrics_store_entry_add_label(entry,
format_label("onion", service->onion_address));
metrics_format_label("onion", service->onion_address));
if (base_metrics[i].port_as_label && service->config.ports) {
SMARTLIST_FOREACH_BEGIN(service->config.ports,
const hs_port_config_t *, p) {
metrics_store_entry_add_label(entry,
format_label("port", port_to_str(p->virtual_port)));
metrics_format_label("port", port_to_str(p->virtual_port)));
} SMARTLIST_FOREACH_END(p);
}
}
@ -96,7 +84,7 @@ hs_metrics_update_by_service(const hs_metrics_key_t key,
SMARTLIST_FOREACH_BEGIN(entries, metrics_store_entry_t *, entry) {
if (port == 0 ||
metrics_store_entry_has_label(entry,
format_label("port", port_to_str(port)))) {
metrics_format_label("port", port_to_str(port)))) {
metrics_store_entry_update(entry, n);
break;
}

View File

@ -11,6 +11,7 @@
#include "orconfig.h"
#include "lib/log/util_bug.h"
#include "lib/string/printf.h"
#include "lib/metrics/metrics_common.h"
@ -27,3 +28,15 @@ metrics_type_to_str(const metrics_type_t type)
tor_assert_unreached();
}
}
/** Return a static buffer pointer that contains a formatted label on the form
* of key=value.
*
* Subsequent call to this function invalidates the previous buffer. */
const char *
metrics_format_label(const char *key, const char *value)
{
static char buf[128];
tor_snprintf(buf, sizeof(buf), "%s=%s", key, value);
return buf;
}

View File

@ -42,4 +42,7 @@ typedef struct metrics_gauge_t {
const char *metrics_type_to_str(const metrics_type_t type);
/* Helpers. */
const char *metrics_format_label(const char *key, const char *value);
#endif /* !defined(TOR_LIB_METRICS_METRICS_COMMON_H) */