[tor-commits] [tor/master] relay: Add the global connection limit metrics

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


commit 897344fddc1ea5ba6dc0db814a703932b05560f4
Author: David Goulet <dgoulet at torproject.org>
Date:   Wed May 5 14:09:07 2021 -0400

    relay: Add the global connection limit metrics
    
    This emits two events (read and write) of the total number that the
    global connection limit was reached.
    
    Related to #40367
    
    Signed-off-by: David Goulet <dgoulet at torproject.org>
---
 src/feature/relay/relay_metrics.c | 29 +++++++++++++++++++++++++++++
 src/feature/relay/relay_metrics.h |  2 ++
 src/feature/stats/rephist.c       | 21 +++++++++++++++++++++
 src/feature/stats/rephist.h       |  3 +++
 4 files changed, 55 insertions(+)

diff --git a/src/feature/relay/relay_metrics.c b/src/feature/relay/relay_metrics.c
index 72030e079b..47d7258239 100644
--- a/src/feature/relay/relay_metrics.c
+++ b/src/feature/relay/relay_metrics.c
@@ -22,6 +22,7 @@
 #include "feature/stats/rephist.h"
 
 /** Declarations of each fill function for metrics defined in base_metrics. */
+static void fill_global_bw_limit_values(void);
 static void fill_socket_values(void);
 static void fill_onionskins_values(void);
 static void fill_oom_values(void);
@@ -53,6 +54,13 @@ static const relay_metrics_entry_t base_metrics[] =
     .help = "Total number of sockets",
     .fill_fn = fill_socket_values,
   },
+  {
+    .key = RELAY_METRICS_NUM_GLOBAL_RW_LIMIT,
+    .type = METRICS_TYPE_COUNTER,
+    .name = METRICS_NAME(relay_load_global_rate_limit_reached_total),
+    .help = "Total number of global connection bucket limit reached",
+    .fill_fn = fill_global_bw_limit_values,
+  },
 };
 static const size_t num_base_metrics = ARRAY_LENGTH(base_metrics);
 
@@ -77,6 +85,27 @@ handshake_type_to_str(const uint16_t type)
   }
 }
 
+/** Fill function for the RELAY_METRICS_NUM_GLOBAL_RW_LIMIT metrics. */
+static void
+fill_global_bw_limit_values(void)
+{
+  metrics_store_entry_t *sentry;
+  const relay_metrics_entry_t *rentry =
+    &base_metrics[RELAY_METRICS_NUM_GLOBAL_RW_LIMIT];
+
+  sentry = metrics_store_add(the_store, rentry->type, rentry->name,
+                             rentry->help);
+  metrics_store_entry_add_label(sentry,
+                                metrics_format_label("side", "read"));
+  metrics_store_entry_update(sentry, rep_hist_get_n_read_limit_reached());
+
+  sentry = metrics_store_add(the_store, rentry->type, rentry->name,
+                             rentry->help);
+  metrics_store_entry_add_label(sentry,
+                                metrics_format_label("side", "write"));
+  metrics_store_entry_update(sentry, rep_hist_get_n_write_limit_reached());
+}
+
 /** Fill function for the RELAY_METRICS_NUM_SOCKETS metrics. */
 static void
 fill_socket_values(void)
diff --git a/src/feature/relay/relay_metrics.h b/src/feature/relay/relay_metrics.h
index fd310ec7ae..0fb7e82fa4 100644
--- a/src/feature/relay/relay_metrics.h
+++ b/src/feature/relay/relay_metrics.h
@@ -21,6 +21,8 @@ typedef enum {
   RELAY_METRICS_NUM_ONIONSKINS = 1,
   /** Number of sockets. */
   RELAY_METRICS_NUM_SOCKETS    = 2,
+  /** Number of global connection rate limit. */
+  RELAY_METRICS_NUM_GLOBAL_RW_LIMIT = 3,
 } relay_metrics_key_t;
 
 /** The metadata of a relay metric. */
diff --git a/src/feature/stats/rephist.c b/src/feature/stats/rephist.c
index 98907055ef..920a96a39a 100644
--- a/src/feature/stats/rephist.c
+++ b/src/feature/stats/rephist.c
@@ -207,6 +207,11 @@ typedef struct {
 /** Current state of overload stats */
 static overload_stats_t overload_stats;
 
+/** Counters to count the number of times we've reached an overload for the
+ * global connection read/write limit. Reported on the MetricsPort. */
+static uint64_t stats_n_read_limit_reached = 0;
+static uint64_t stats_n_write_limit_reached = 0;
+
 /** Return true if this overload happened within the last `n_hours`. */
 static bool
 overload_happened_recently(time_t overload_time, int n_hours)
@@ -221,6 +226,20 @@ overload_happened_recently(time_t overload_time, int n_hours)
 /* The current version of the overload stats version */
 #define OVERLOAD_STATS_VERSION 1
 
+/** Return the stats_n_read_limit_reached counter. */
+uint64_t
+rep_hist_get_n_read_limit_reached(void)
+{
+  return stats_n_read_limit_reached;
+}
+
+/** Return the stats_n_write_limit_reached counter. */
+uint64_t
+rep_hist_get_n_write_limit_reached(void)
+{
+  return stats_n_write_limit_reached;
+}
+
 /** Returns an allocated string for server descriptor for publising information
  * on whether we are overloaded or not. */
 char *
@@ -299,6 +318,7 @@ rep_hist_note_overload(overload_type_t overload)
     SET_TO_START_OF_HOUR(overload_stats.overload_general_time);
     break;
   case OVERLOAD_READ: {
+    stats_n_read_limit_reached++;
     SET_TO_START_OF_HOUR(overload_stats.overload_ratelimits_time);
     if (approx_time() >= last_read_counted + 60) { /* Count once a minute */
         overload_stats.overload_read_count++;
@@ -307,6 +327,7 @@ rep_hist_note_overload(overload_type_t overload)
     break;
   }
   case OVERLOAD_WRITE: {
+    stats_n_write_limit_reached++;
     SET_TO_START_OF_HOUR(overload_stats.overload_ratelimits_time);
     if (approx_time() >= last_write_counted + 60) { /* Count once a minute */
       overload_stats.overload_write_count++;
diff --git a/src/feature/stats/rephist.h b/src/feature/stats/rephist.h
index 0bb9dd4678..b54fc77883 100644
--- a/src/feature/stats/rephist.h
+++ b/src/feature/stats/rephist.h
@@ -162,6 +162,9 @@ void rep_hist_note_overload(overload_type_t overload);
 char *rep_hist_get_overload_general_line(void);
 char *rep_hist_get_overload_stats_lines(void);
 
+uint64_t rep_hist_get_n_read_limit_reached(void);
+uint64_t rep_hist_get_n_write_limit_reached(void);
+
 #ifdef TOR_UNIT_TESTS
 struct hs_v2_stats_t;
 const struct hs_v2_stats_t *rep_hist_get_hs_v2_stats(void);





More information about the tor-commits mailing list