[tor-commits] [tor/master] metrics: Move helper function to lib/metrics

dgoulet at torproject.org dgoulet at torproject.org
Wed May 12 16:03:05 UTC 2021


commit 455471835da35d8ee64e6a2c0a70acb89a003bf4
Author: David Goulet <dgoulet at torproject.org>
Date:   Thu Apr 15 09:05:55 2021 -0400

    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 at torproject.org>
---
 src/feature/hs/hs_metrics.c      | 18 +++---------------
 src/lib/metrics/metrics_common.c | 13 +++++++++++++
 src/lib/metrics/metrics_common.h |  3 +++
 3 files changed, 19 insertions(+), 15 deletions(-)

diff --git a/src/feature/hs/hs_metrics.c b/src/feature/hs/hs_metrics.c
index e023eab90c..0f1824c51c 100644
--- a/src/feature/hs/hs_metrics.c
+++ b/src/feature/hs/hs_metrics.c
@@ -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;
     }
diff --git a/src/lib/metrics/metrics_common.c b/src/lib/metrics/metrics_common.c
index 5941a4d892..c0352b6646 100644
--- a/src/lib/metrics/metrics_common.c
+++ b/src/lib/metrics/metrics_common.c
@@ -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;
+}
diff --git a/src/lib/metrics/metrics_common.h b/src/lib/metrics/metrics_common.h
index 59aa9c0e90..3644ad3d50 100644
--- a/src/lib/metrics/metrics_common.h
+++ b/src/lib/metrics/metrics_common.h
@@ -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) */





More information about the tor-commits mailing list