relay: Add number of open and max sockets metrics

With this commit, a relay will emit metrics that give the total number
of sockets and total number of opened sockets.

Related to #40367

Signed-off-by: David Goulet <dgoulet@torproject.org>
This commit is contained in:
David Goulet 2021-05-05 13:57:19 -04:00
parent 8bb1874f1e
commit 9040a5475d
2 changed files with 30 additions and 1 deletions

View File

@ -22,8 +22,9 @@
#include "feature/stats/rephist.h" #include "feature/stats/rephist.h"
/** Declarations of each fill function for metrics defined in base_metrics. */ /** Declarations of each fill function for metrics defined in base_metrics. */
static void fill_oom_values(void); static void fill_socket_values(void);
static void fill_onionskins_values(void); static void fill_onionskins_values(void);
static void fill_oom_values(void);
/** The base metrics that is a static array of metrics added to the metrics /** The base metrics that is a static array of metrics added to the metrics
* store. * store.
@ -45,6 +46,13 @@ static const relay_metrics_entry_t base_metrics[] =
.help = "Total number of onionskins handled", .help = "Total number of onionskins handled",
.fill_fn = fill_onionskins_values, .fill_fn = fill_onionskins_values,
}, },
{
.key = RELAY_METRICS_NUM_SOCKETS,
.type = METRICS_TYPE_GAUGE,
.name = METRICS_NAME(relay_load_socket_total),
.help = "Total number of sockets",
.fill_fn = fill_socket_values,
},
}; };
static const size_t num_base_metrics = ARRAY_LENGTH(base_metrics); static const size_t num_base_metrics = ARRAY_LENGTH(base_metrics);
@ -69,6 +77,25 @@ handshake_type_to_str(const uint16_t type)
} }
} }
/** Fill function for the RELAY_METRICS_NUM_SOCKETS metrics. */
static void
fill_socket_values(void)
{
metrics_store_entry_t *sentry;
const relay_metrics_entry_t *rentry =
&base_metrics[RELAY_METRICS_NUM_SOCKETS];
sentry = metrics_store_add(the_store, rentry->type, rentry->name,
rentry->help);
metrics_store_entry_add_label(sentry,
metrics_format_label("state", "opened"));
metrics_store_entry_update(sentry, get_n_open_sockets());
sentry = metrics_store_add(the_store, rentry->type, rentry->name,
rentry->help);
metrics_store_entry_update(sentry, get_max_sockets());
}
/** Fill function for the RELAY_METRICS_NUM_ONIONSKINS metrics. */ /** Fill function for the RELAY_METRICS_NUM_ONIONSKINS metrics. */
static void static void
fill_onionskins_values(void) fill_onionskins_values(void)

View File

@ -19,6 +19,8 @@ typedef enum {
RELAY_METRICS_NUM_OOM_BYTES = 0, RELAY_METRICS_NUM_OOM_BYTES = 0,
/** Number of onionskines handled. */ /** Number of onionskines handled. */
RELAY_METRICS_NUM_ONIONSKINS = 1, RELAY_METRICS_NUM_ONIONSKINS = 1,
/** Number of sockets. */
RELAY_METRICS_NUM_SOCKETS = 2,
} relay_metrics_key_t; } relay_metrics_key_t;
/** The metadata of a relay metric. */ /** The metadata of a relay metric. */