]> git.ipfire.org Git - thirdparty/tor.git/commitdiff
metrics: Move helper function to lib/metrics
authorDavid Goulet <dgoulet@torproject.org>
Thu, 15 Apr 2021 13:05:55 +0000 (09:05 -0400)
committerDavid Goulet <dgoulet@torproject.org>
Wed, 12 May 2021 15:58:25 +0000 (11:58 -0400)
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>
src/feature/hs/hs_metrics.c
src/lib/metrics/metrics_common.c
src/lib/metrics/metrics_common.h

index e023eab90c1dae337230adba4647f2c41aac85aa..0f1824c51cf83f9a25ee55cf5f3edc7439ad934d 100644 (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;
     }
index 5941a4d8923afe1a999b09e88864cfd76b666e56..c0352b66469c3d89bdcd87b85a7f667906a5bb9e 100644 (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;
+}
index 59aa9c0e902304eca95c3cba71326d2b1ced4c54..3644ad3d50c703ebdabc796241600ae70dda1b60 100644 (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) */