relay: Add connection stats to MetricsPort

This adds the number of created and opened connections to the
MetricsPort for a relay for each connection type and direction.

Output looks like:

  # HELP tor_relay_connections Connections metrics of this relay
  # TYPE tor_relay_connections counter
  tor_relay_connections{type="OR listener",direction="initiated",state="created"} 0
  tor_relay_connections{type="OR listener",direction="received",state="created"} 0
  tor_relay_connections{type="OR listener",direction="initiated",state="opened"} 0
  tor_relay_connections{type="OR listener",direction="received",state="opened"} 0
  tor_relay_connections{type="OR",direction="initiated",state="created"} 5
  tor_relay_connections{type="OR",direction="received",state="created"} 0
  tor_relay_connections{type="OR",direction="initiated",state="opened"} 5
  tor_relay_connections{type="OR",direction="received",state="opened"} 0
  tor_relay_connections{type="Exit",direction="initiated",state="created"} 0
  tor_relay_connections{type="Exit",direction="received",state="created"} 0
  tor_relay_connections{type="Exit",direction="initiated",state="opened"} 0
  tor_relay_connections{type="Exit",direction="received",state="opened"} 0
  tor_relay_connections{type="Socks listener",direction="initiated",state="created"} 0
  tor_relay_connections{type="Socks listener",direction="received",state="created"} 0
  tor_relay_connections{type="Socks listener",direction="initiated",state="opened"} 0
  tor_relay_connections{type="Socks listener",direction="received",state="opened"} 0
  tor_relay_connections{type="Socks",direction="initiated",state="created"} 0
  tor_relay_connections{type="Socks",direction="received",state="created"} 0
  tor_relay_connections{type="Socks",direction="initiated",state="opened"} 0
  tor_relay_connections{type="Socks",direction="received",state="opened"} 0
  tor_relay_connections{type="Directory listener",direction="initiated",state="created"} 0
  tor_relay_connections{type="Directory listener",direction="received",state="created"} 0
  tor_relay_connections{type="Directory listener",direction="initiated",state="opened"} 0
  tor_relay_connections{type="Directory listener",direction="received",state="opened"} 0
  tor_relay_connections{type="Directory",direction="initiated",state="created"} 0
  tor_relay_connections{type="Directory",direction="received",state="created"} 0
  tor_relay_connections{type="Directory",direction="initiated",state="opened"} 0
  tor_relay_connections{type="Directory",direction="received",state="opened"} 0
  tor_relay_connections{type="Control listener",direction="initiated",state="created"} 0
  tor_relay_connections{type="Control listener",direction="received",state="created"} 0
  tor_relay_connections{type="Control listener",direction="initiated",state="opened"} 0
  tor_relay_connections{type="Control listener",direction="received",state="opened"} 0
  tor_relay_connections{type="Control",direction="initiated",state="created"} 0
  tor_relay_connections{type="Control",direction="received",state="created"} 0
  tor_relay_connections{type="Control",direction="initiated",state="opened"} 0
  tor_relay_connections{type="Control",direction="received",state="opened"} 0
  tor_relay_connections{type="Transparent pf/netfilter listener",direction="initiated",state="created"} 0
  tor_relay_connections{type="Transparent pf/netfilter listener",direction="received",state="created"} 0
  tor_relay_connections{type="Transparent pf/netfilter listener",direction="initiated",state="opened"} 0
  tor_relay_connections{type="Transparent pf/netfilter listener",direction="received",state="opened"} 0
  tor_relay_connections{type="Transparent natd listener",direction="initiated",state="created"} 0
  tor_relay_connections{type="Transparent natd listener",direction="received",state="created"} 0
  tor_relay_connections{type="Transparent natd listener",direction="initiated",state="opened"} 0
  tor_relay_connections{type="Transparent natd listener",direction="received",state="opened"} 0
  tor_relay_connections{type="DNS listener",direction="initiated",state="created"} 0
  tor_relay_connections{type="DNS listener",direction="received",state="created"} 0
  tor_relay_connections{type="DNS listener",direction="initiated",state="opened"} 0
  tor_relay_connections{type="DNS listener",direction="received",state="opened"} 0
  tor_relay_connections{type="Extended OR",direction="initiated",state="created"} 0
  tor_relay_connections{type="Extended OR",direction="received",state="created"} 0
  tor_relay_connections{type="Extended OR",direction="initiated",state="opened"} 0
  tor_relay_connections{type="Extended OR",direction="received",state="opened"} 0
  tor_relay_connections{type="Extended OR listener",direction="initiated",state="created"} 0
  tor_relay_connections{type="Extended OR listener",direction="received",state="created"} 0
  tor_relay_connections{type="Extended OR listener",direction="initiated",state="opened"} 0
  tor_relay_connections{type="Extended OR listener",direction="received",state="opened"} 0
  tor_relay_connections{type="HTTP tunnel listener",direction="initiated",state="created"} 0
  tor_relay_connections{type="HTTP tunnel listener",direction="received",state="created"} 0
  tor_relay_connections{type="HTTP tunnel listener",direction="initiated",state="opened"} 0
  tor_relay_connections{type="HTTP tunnel listener",direction="received",state="opened"} 0
  tor_relay_connections{type="Metrics listener",direction="initiated",state="created"} 0
  tor_relay_connections{type="Metrics listener",direction="received",state="created"} 1
  tor_relay_connections{type="Metrics listener",direction="initiated",state="opened"} 0
  tor_relay_connections{type="Metrics listener",direction="received",state="opened"} 1
  tor_relay_connections{type="Metrics",direction="initiated",state="created"} 0
  tor_relay_connections{type="Metrics",direction="received",state="created"} 0
  tor_relay_connections{type="Metrics",direction="initiated",state="opened"} 0
  tor_relay_connections{type="Metrics",direction="received",state="opened"} 0

Related to #40194

Signed-off-by: David Goulet <dgoulet@torproject.org>
This commit is contained in:
David Goulet 2022-10-11 14:03:38 -04:00
parent 5603baf257
commit d543db5ac0
3 changed files with 65 additions and 0 deletions

3
changes/ticket40194 Normal file
View File

@ -0,0 +1,3 @@
o Minor feature (relay, metrics):
- Add counters to the MetricsPort how many connections, per type, are
currently opened and how many were created. Part of ticket 40194.

View File

@ -11,6 +11,7 @@
#include "orconfig.h"
#include "core/or/or.h"
#include "core/mainloop/connection.h"
#include "core/or/relay.h"
#include "lib/malloc/malloc.h"
@ -24,6 +25,7 @@
#include <event2/dns.h>
/** Declarations of each fill function for metrics defined in base_metrics. */
static void fill_connections_values(void);
static void fill_dns_error_values(void);
static void fill_dns_query_values(void);
static void fill_global_bw_limit_values(void);
@ -87,6 +89,13 @@ static const relay_metrics_entry_t base_metrics[] =
.help = "Total number of times we ran out of TCP ports",
.fill_fn = fill_tcp_exhaustion_values,
},
{
.key = RELAY_METRICS_NUM_CONNECTIONS,
.type = METRICS_TYPE_COUNTER,
.name = METRICS_NAME(relay_connections),
.help = "Connections metrics of this relay",
.fill_fn = fill_connections_values,
},
};
static const size_t num_base_metrics = ARRAY_LENGTH(base_metrics);
@ -113,6 +122,57 @@ handshake_type_to_str(const uint16_t type)
}
}
/** Helper: Fill in single connection metrics output. */
static void
fill_single_connection_value(metrics_store_entry_t *sentry,
unsigned int conn_type,
const char* direction,
const char* state,
uint64_t value)
{
metrics_store_entry_add_label(sentry,
metrics_format_label("type", conn_type_to_string(conn_type)));
metrics_store_entry_add_label(sentry,
metrics_format_label("direction", direction));
metrics_store_entry_add_label(sentry,
metrics_format_label("state", state));
metrics_store_entry_update(sentry, value);
}
/** Fill function for the RELAY_METRICS_NUM_CONNECTIONS metric. */
static void
fill_connections_values(void)
{
const relay_metrics_entry_t *rentry =
&base_metrics[RELAY_METRICS_NUM_CONNECTIONS];
for (unsigned int i = CONN_TYPE_MIN_; i < CONN_TYPE_MAX_ ; i++) {
/* Type is unused. Ugly but else we clobber the output. */
if (i == 10) {
continue;
}
metrics_store_entry_t *sentry =
metrics_store_add(the_store, rentry->type, rentry->name, rentry->help);
fill_single_connection_value(sentry, i, "initiated", "created",
rep_hist_get_conn_created(false, i));
sentry = metrics_store_add(the_store, rentry->type, rentry->name,
rentry->help);
fill_single_connection_value(sentry, i, "received", "created",
rep_hist_get_conn_created(true, i));
sentry = metrics_store_add(the_store, rentry->type, rentry->name,
rentry->help);
fill_single_connection_value(sentry, i, "initiated", "opened",
rep_hist_get_conn_opened(false, i));
sentry = metrics_store_add(the_store, rentry->type, rentry->name,
rentry->help);
fill_single_connection_value(sentry, i, "received", "opened",
rep_hist_get_conn_opened(true, i));
}
}
/** Fill function for the RELAY_METRICS_NUM_DNS metrics. */
static void
fill_tcp_exhaustion_values(void)

View File

@ -29,6 +29,8 @@ typedef enum {
RELAY_METRICS_NUM_DNS_ERRORS = 5,
/** Number of TCP exhaustion reached. */
RELAY_METRICS_NUM_TCP_EXHAUSTION = 6,
/** Number of connections. */
RELAY_METRICS_NUM_CONNECTIONS = 7,
} relay_metrics_key_t;
/** The metadata of a relay metric. */