]> git.ipfire.org Git - thirdparty/tor.git/commitdiff
relay: Groundwork for relay metrics support
authorDavid Goulet <dgoulet@torproject.org>
Thu, 15 Apr 2021 12:58:20 +0000 (08:58 -0400)
committerDavid Goulet <dgoulet@torproject.org>
Wed, 12 May 2021 15:58:25 +0000 (11:58 -0400)
The basic functions for the relay subsystem to expose metrics onto the
MetricsPort.

Part of #40367

Signed-off-by: David Goulet <dgoulet@torproject.org>
src/feature/relay/include.am
src/feature/relay/relay_metrics.c [new file with mode: 0644]
src/feature/relay/relay_metrics.h [new file with mode: 0644]
src/feature/relay/relay_sys.c

index 84bb1ff35effb48e689ea0001b4fd3b2ce71f2bd..8a121cef0150875e42616da277e7d7547f5376d4 100644 (file)
@@ -15,6 +15,7 @@ MODULE_RELAY_SOURCES =                                                \
        src/feature/relay/routermode.c                          \
        src/feature/relay/relay_config.c                        \
        src/feature/relay/relay_handshake.c                     \
+       src/feature/relay/relay_metrics.c                       \
        src/feature/relay/relay_periodic.c                      \
        src/feature/relay/relay_sys.c                           \
        src/feature/relay/routerkeys.c                          \
@@ -30,6 +31,7 @@ noinst_HEADERS +=                                     \
        src/feature/relay/onion_queue.h                 \
        src/feature/relay/relay_config.h                \
        src/feature/relay/relay_handshake.h             \
+       src/feature/relay/relay_metrics.h                       \
        src/feature/relay/relay_periodic.h              \
        src/feature/relay/relay_sys.h                   \
        src/feature/relay/relay_find_addr.h             \
diff --git a/src/feature/relay/relay_metrics.c b/src/feature/relay/relay_metrics.c
new file mode 100644 (file)
index 0000000..3364861
--- /dev/null
@@ -0,0 +1,59 @@
+/* Copyright (c) 2021, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+/**
+ * @file relay_metrics.c
+ * @brief Relay metrics exposed through the MetricsPort
+ **/
+
+#define RELAY_METRICS_ENTRY_PRIVATE
+
+#include "orconfig.h"
+
+#include "lib/malloc/malloc.h"
+#include "lib/container/smartlist.h"
+#include "lib/metrics/metrics_store.h"
+#include "lib/log/util_bug.h"
+
+#include "feature/relay/relay_metrics.h"
+
+/** The only and single store of all the relay metrics. */
+static metrics_store_t *the_store;
+
+/** Return a list of all the relay metrics stores. This is the
+ * function attached to the .get_metrics() member of the subsys_t. */
+const smartlist_t *
+relay_metrics_get_stores(void)
+{
+  /* We can't have the caller to free the returned list so keep it static,
+   * simply update it. */
+  static smartlist_t *stores_list = NULL;
+
+  if (!stores_list) {
+    stores_list = smartlist_new();
+    smartlist_add(stores_list, the_store);
+  }
+
+  return stores_list;
+}
+
+/** Initialize the relay metrics. */
+void
+relay_metrics_init(void)
+{
+  if (BUG(the_store)) {
+    return;
+  }
+  the_store = metrics_store_new();
+}
+
+/** Free the relay metrics. */
+void
+relay_metrics_free(void)
+{
+  if (!the_store) {
+    return;
+  }
+  /* NULL is set with this call. */
+  metrics_store_free(the_store);
+}
diff --git a/src/feature/relay/relay_metrics.h b/src/feature/relay/relay_metrics.h
new file mode 100644 (file)
index 0000000..3a13eb2
--- /dev/null
@@ -0,0 +1,21 @@
+/* Copyright (c) 2021, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+/**
+ * @file relay_metrics.h
+ * @brief Header for feature/relay/relay_metrics.c
+ **/
+
+#ifndef TOR_FEATURE_RELAY_RELAY_METRICS_H
+#define TOR_FEATURE_RELAY_RELAY_METRICS_H
+
+#include "lib/container/smartlist.h"
+
+/* Init. */
+void relay_metrics_init(void);
+void relay_metrics_free(void);
+
+/* Accessors. */
+const smartlist_t *relay_metrics_get_stores(void);
+
+#endif /* !defined(TOR_FEATURE_RELAY_RELAY_METRICS_H) */
index 25fc0bbd3276f99597ad292ae5e2dbfd052e095e..9c43734b84c8b50e5a36d79586bf5a6f23a03ac0 100644 (file)
@@ -14,6 +14,7 @@
 
 #include "feature/relay/dns.h"
 #include "feature/relay/ext_orport.h"
+#include "feature/relay/relay_metrics.h"
 #include "feature/relay/onion_queue.h"
 #include "feature/relay/relay_periodic.h"
 #include "feature/relay/relay_sys.h"
@@ -25,6 +26,7 @@
 static int
 subsys_relay_initialize(void)
 {
+  relay_metrics_init();
   relay_register_periodic_events();
   return 0;
 }
@@ -37,6 +39,7 @@ subsys_relay_shutdown(void)
   clear_pending_onions();
   routerkeys_free_all();
   router_free_all();
+  relay_metrics_free();
 }
 
 const struct subsys_fns_t sys_relay = {
@@ -46,4 +49,6 @@ const struct subsys_fns_t sys_relay = {
   .level = RELAY_SUBSYS_LEVEL,
   .initialize = subsys_relay_initialize,
   .shutdown = subsys_relay_shutdown,
+
+  .get_metrics = relay_metrics_get_stores,
 };