commit a882d1bf0abbbcf2bc4f8c039f9b82262462292c
Author: David Goulet <dgou...@torproject.org>
Date:   Mon Oct 19 15:50:45 2020 -0400

    metrics: New feature module to track tor metrics
    
    Related to #40063
    
    Signed-off-by: David Goulet <dgou...@torproject.org>
---
 src/app/main/subsystem_list.c     |  2 ++
 src/feature/metrics/.may_include  |  1 +
 src/feature/metrics/include.am    | 10 ++++++
 src/feature/metrics/metrics.c     | 66 +++++++++++++++++++++++++++++++++++++++
 src/feature/metrics/metrics.h     | 21 +++++++++++++
 src/feature/metrics/metrics_sys.c | 37 ++++++++++++++++++++++
 src/feature/metrics/metrics_sys.h | 22 +++++++++++++
 src/include.am                    |  1 +
 src/lib/subsys/subsys.h           | 12 +++++++
 9 files changed, 172 insertions(+)

diff --git a/src/app/main/subsystem_list.c b/src/app/main/subsystem_list.c
index c6da6f4893..9562b99030 100644
--- a/src/app/main/subsystem_list.c
+++ b/src/app/main/subsystem_list.c
@@ -31,6 +31,7 @@
 #include "lib/evloop/evloop_sys.h"
 
 #include "feature/dirauth/dirauth_sys.h"
+#include "feature/metrics/metrics_sys.h"
 #include "feature/relay/relay_sys.h"
 
 #include <stddef.h>
@@ -68,6 +69,7 @@ const subsys_fns_t *tor_subsystems[] = {
   &sys_btrack,
 
   &sys_dirauth,
+  &sys_metrics,
 };
 
 const unsigned n_tor_subsystems = ARRAY_LENGTH(tor_subsystems);
diff --git a/src/feature/metrics/.may_include b/src/feature/metrics/.may_include
new file mode 100644
index 0000000000..424c745c12
--- /dev/null
+++ b/src/feature/metrics/.may_include
@@ -0,0 +1 @@
+*.h
diff --git a/src/feature/metrics/include.am b/src/feature/metrics/include.am
new file mode 100644
index 0000000000..0e875f43ad
--- /dev/null
+++ b/src/feature/metrics/include.am
@@ -0,0 +1,10 @@
+
+# ADD_C_FILE: INSERT SOURCES HERE.
+LIBTOR_APP_A_SOURCES +=                \
+       src/feature/metrics/metrics.c           \
+       src/feature/metrics/metrics_sys.c
+
+# ADD_C_FILE: INSERT HEADERS HERE.
+noinst_HEADERS +=              \
+       src/feature/metrics/metrics.h           \
+       src/feature/metrics/metrics_sys.h
diff --git a/src/feature/metrics/metrics.c b/src/feature/metrics/metrics.c
new file mode 100644
index 0000000000..5f6fe776b7
--- /dev/null
+++ b/src/feature/metrics/metrics.c
@@ -0,0 +1,66 @@
+/* Copyright (c) 2007-2020, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+/**
+ * @file metrics.c
+ * @brief Metrics subsystem.
+ **/
+
+#include "orconfig.h"
+
+#include "lib/container/smartlist.h"
+#include "lib/log/util_bug.h"
+#include "lib/malloc/malloc.h"
+#include "lib/metrics/metrics_store.h"
+#include "lib/string/printf.h"
+
+#include "feature/metrics/metrics.h"
+
+#include "app/main/subsysmgr.h"
+
+/** Return newly allocated string containing the output of all subsystems
+ * having metrics.
+ *
+ * This is used to output the content on the MetricsPort. */
+char *
+metrics_get_output(const metrics_format_t fmt)
+{
+  char *data;
+  smartlist_t *chunks = smartlist_new();
+
+  /* Go over all subsystems that exposes a metrics store. */
+  for (unsigned i = 0; i < n_tor_subsystems; ++i) {
+    const smartlist_t *stores;
+    const subsys_fns_t *sys = tor_subsystems[i];
+
+    /* Skip unsupported subsystems. */
+    if (!sys->supported) {
+      continue;
+    }
+
+    if (sys->get_metrics && (stores = sys->get_metrics())) {
+      SMARTLIST_FOREACH_BEGIN(stores, const metrics_store_t *, store) {
+        smartlist_add(chunks, metrics_store_get_output(fmt, store));
+      } SMARTLIST_FOREACH_END(store);
+    }
+  }
+
+  data = smartlist_join_strings(chunks, "\n", 0, NULL);
+
+  SMARTLIST_FOREACH(chunks, char *, c, tor_free(c));
+  smartlist_free(chunks);
+
+  return data;
+}
+
+/** Initialize the subsystem. */
+void
+metrics_init(void)
+{
+}
+
+/** Cleanup and free any global memory of this subsystem. */
+void
+metrics_cleanup(void)
+{
+}
diff --git a/src/feature/metrics/metrics.h b/src/feature/metrics/metrics.h
new file mode 100644
index 0000000000..a30c271bea
--- /dev/null
+++ b/src/feature/metrics/metrics.h
@@ -0,0 +1,21 @@
+/* Copyright (c) 2020, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+/**
+ * @file metrics.h
+ * @brief Header for feature/metrics/metrics.c
+ **/
+
+#ifndef TOR_FEATURE_METRICS_METRICS_H
+#define TOR_FEATURE_METRICS_METRICS_H
+
+#include "lib/metrics/metrics_common.h"
+
+/* Initializer / Cleanup. */
+void metrics_init(void);
+void metrics_cleanup(void);
+
+/* Accessors. */
+char *metrics_get_output(const metrics_format_t fmt);
+
+#endif /* !defined(TOR_FEATURE_METRICS_METRICS_H) */
diff --git a/src/feature/metrics/metrics_sys.c 
b/src/feature/metrics/metrics_sys.c
new file mode 100644
index 0000000000..419318068e
--- /dev/null
+++ b/src/feature/metrics/metrics_sys.c
@@ -0,0 +1,37 @@
+/* Copyright (c) 2020, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+/**
+ * @file metrics_sys.c
+ * @brief Setup and tear down the metrics subsystem.
+ **/
+
+#include "lib/subsys/subsys.h"
+
+#include "feature/metrics/metrics.h"
+#include "feature/metrics/metrics_sys.h"
+
+static int
+subsys_metrics_initialize(void)
+{
+  metrics_init();
+  return 0;
+}
+
+static void
+subsys_metrics_shutdown(void)
+{
+  metrics_cleanup();
+}
+
+const subsys_fns_t sys_metrics = {
+  SUBSYS_DECLARE_LOCATION(),
+
+  .name = "metrics",
+  .supported = true,
+  .level = METRICS_SUBSYS_LEVEL,
+
+  .initialize = subsys_metrics_initialize,
+  .shutdown = subsys_metrics_shutdown,
+};
+
diff --git a/src/feature/metrics/metrics_sys.h 
b/src/feature/metrics/metrics_sys.h
new file mode 100644
index 0000000000..30c1b14836
--- /dev/null
+++ b/src/feature/metrics/metrics_sys.h
@@ -0,0 +1,22 @@
+/* Copyright (c) 2020, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+/**
+ * @file metrics_sys.h
+ * @brief Header for feature/metrics/metrics_sys.c
+ **/
+
+#ifndef TOR_FEATURE_METRICS_METRICS_SYS_H
+#define TOR_FEATURE_METRICS_METRICS_SYS_H
+
+extern const struct subsys_fns_t sys_metrics;
+
+/**
+ * Subsystem level for the metrics system.
+ *
+ * Defined here so that it can be shared between the real and stub
+ * definitions.
+ **/
+#define METRICS_SUBSYS_LEVEL (99)
+
+#endif /* !defined(TOR_FEATURE_METRICS_METRICS_SYS_H) */
diff --git a/src/include.am b/src/include.am
index 95acecb057..0826da7548 100644
--- a/src/include.am
+++ b/src/include.am
@@ -73,6 +73,7 @@ include src/feature/hibernate/include.am
 include src/feature/hs_common/include.am
 include src/feature/hs/include.am
 include src/feature/keymgt/include.am
+include src/feature/metrics/include.am
 include src/feature/nodelist/include.am
 include src/feature/relay/include.am
 include src/feature/rend/include.am
diff --git a/src/lib/subsys/subsys.h b/src/lib/subsys/subsys.h
index 62c0de026d..4cff5f0b6e 100644
--- a/src/lib/subsys/subsys.h
+++ b/src/lib/subsys/subsys.h
@@ -15,6 +15,7 @@
 
 struct pubsub_connector_t;
 struct config_format_t;
+struct smartlist_t;
 
 /**
  * A subsystem is a part of Tor that is initialized, shut down, configured,
@@ -190,6 +191,17 @@ typedef struct subsys_fns_t {
    * to disk.
    **/
   int (*flush_state)(void *);
+
+  /**
+   * Return a list of metrics store of this subsystem. This is called
+   * everytime a request arrives on the MetricsPort.
+   *
+   * The list MUST contain metrics_store_t object and contains entries so it
+   * can be formatted for the metrics port.
+   *
+   * This can return NULL or be NULL.
+   **/
+  const struct smartlist_t *(*get_metrics)(void);
 } subsys_fns_t;
 
 #ifndef COCCI



_______________________________________________
tor-commits mailing list
tor-commits@lists.torproject.org
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits

Reply via email to