Commit 45547183 authored by David Goulet's avatar David Goulet 🆘
Browse files

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's avatarDavid Goulet <dgoulet@torproject.org>
parent cc2947c0
......@@ -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;
}
......
......@@ -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;
}
......@@ -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) */
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment