diff --git a/src/core/or/relay.c b/src/core/or/relay.c index 01a377b6f5..d77c47100a 100644 --- a/src/core/or/relay.c +++ b/src/core/or/relay.c @@ -502,7 +502,7 @@ relay_header_unpack(relay_header_t *dest, const uint8_t *src) } /** Convert the relay command into a human-readable string. */ -static const char * +const char * relay_command_to_string(uint8_t command) { static char buf[64]; diff --git a/src/core/or/relay.h b/src/core/or/relay.h index 71e07562cd..24466bccd0 100644 --- a/src/core/or/relay.h +++ b/src/core/or/relay.h @@ -16,6 +16,8 @@ extern uint64_t stats_n_relay_cells_relayed; extern uint64_t stats_n_relay_cells_delivered; extern uint64_t stats_n_circ_max_cell_reached; +const char *relay_command_to_string(uint8_t command); + void relay_consensus_has_changed(const networkstatus_t *ns); uint32_t relay_get_param_max_circuit_cell_queue_size( const networkstatus_t *ns); diff --git a/src/feature/relay/relay_metrics.c b/src/feature/relay/relay_metrics.c index 8d0fef86b3..e48e211ba7 100644 --- a/src/feature/relay/relay_metrics.c +++ b/src/feature/relay/relay_metrics.c @@ -32,6 +32,7 @@ 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); +static void fill_streams_values(void); static void fill_tcp_exhaustion_values(void); /** The base metrics that is a static array of metrics added to the metrics @@ -96,6 +97,13 @@ static const relay_metrics_entry_t base_metrics[] = .help = "Connections metrics of this relay", .fill_fn = fill_connections_values, }, + { + .key = RELAY_METRICS_NUM_STREAMS, + .type = METRICS_TYPE_COUNTER, + .name = METRICS_NAME(relay_streams_total), + .help = "Total number of streams", + .fill_fn = fill_streams_values, + }, }; static const size_t num_base_metrics = ARRAY_LENGTH(base_metrics); @@ -122,6 +130,34 @@ handshake_type_to_str(const uint16_t type) } } +/** Helper: Fill in single stream metrics output. */ +static void +fill_single_stream_value(metrics_store_entry_t *sentry, uint8_t cmd) +{ + metrics_store_entry_add_label(sentry, + metrics_format_label("type", relay_command_to_string(cmd))); + metrics_store_entry_update(sentry, rep_hist_get_stream_seen(cmd)); +} + +/** Fill function for the RELAY_METRICS_NUM_STREAMS metric. */ +static void +fill_streams_values(void) +{ + const relay_metrics_entry_t *rentry = + &base_metrics[RELAY_METRICS_NUM_STREAMS]; + metrics_store_entry_t *sentry = + metrics_store_add(the_store, rentry->type, rentry->name, rentry->help); + fill_single_stream_value(sentry, RELAY_COMMAND_BEGIN); + + sentry = metrics_store_add(the_store, rentry->type, rentry->name, + rentry->help); + fill_single_stream_value(sentry, RELAY_COMMAND_BEGIN_DIR); + + sentry = metrics_store_add(the_store, rentry->type, rentry->name, + rentry->help); + fill_single_stream_value(sentry, RELAY_COMMAND_RESOLVE); +} + /** Helper: Fill in single connection metrics output. */ static void fill_single_connection_value(metrics_store_entry_t *sentry, diff --git a/src/feature/relay/relay_metrics.h b/src/feature/relay/relay_metrics.h index 02b92cd043..17f9c9f195 100644 --- a/src/feature/relay/relay_metrics.h +++ b/src/feature/relay/relay_metrics.h @@ -31,6 +31,8 @@ typedef enum { RELAY_METRICS_NUM_TCP_EXHAUSTION = 6, /** Number of connections. */ RELAY_METRICS_NUM_CONNECTIONS = 7, + /** Number of streams. */ + RELAY_METRICS_NUM_STREAMS = 8, } relay_metrics_key_t; /** The metadata of a relay metric. */