relay: Groundwork for relay metrics support

The basic functions for the relay subsystem to expose metrics onto the
MetricsPort.

Part of #40367

Signed-off-by: David Goulet <dgoulet@torproject.org>
This commit is contained in:
David Goulet 2021-04-15 08:58:20 -04:00
parent 3164e55f01
commit 0cf25fd3a5
4 changed files with 87 additions and 0 deletions

View File

@ -15,6 +15,7 @@ MODULE_RELAY_SOURCES = \
src/feature/relay/routermode.c \
src/feature/relay/relay_config.c \
src/feature/relay/relay_handshake.c \
src/feature/relay/relay_metrics.c \
src/feature/relay/relay_periodic.c \
src/feature/relay/relay_sys.c \
src/feature/relay/routerkeys.c \
@ -30,6 +31,7 @@ noinst_HEADERS += \
src/feature/relay/onion_queue.h \
src/feature/relay/relay_config.h \
src/feature/relay/relay_handshake.h \
src/feature/relay/relay_metrics.h \
src/feature/relay/relay_periodic.h \
src/feature/relay/relay_sys.h \
src/feature/relay/relay_find_addr.h \

View File

@ -0,0 +1,59 @@
/* Copyright (c) 2021, The Tor Project, Inc. */
/* See LICENSE for licensing information */
/**
* @file relay_metrics.c
* @brief Relay metrics exposed through the MetricsPort
**/
#define RELAY_METRICS_ENTRY_PRIVATE
#include "orconfig.h"
#include "lib/malloc/malloc.h"
#include "lib/container/smartlist.h"
#include "lib/metrics/metrics_store.h"
#include "lib/log/util_bug.h"
#include "feature/relay/relay_metrics.h"
/** The only and single store of all the relay metrics. */
static metrics_store_t *the_store;
/** Return a list of all the relay metrics stores. This is the
* function attached to the .get_metrics() member of the subsys_t. */
const smartlist_t *
relay_metrics_get_stores(void)
{
/* We can't have the caller to free the returned list so keep it static,
* simply update it. */
static smartlist_t *stores_list = NULL;
if (!stores_list) {
stores_list = smartlist_new();
smartlist_add(stores_list, the_store);
}
return stores_list;
}
/** Initialize the relay metrics. */
void
relay_metrics_init(void)
{
if (BUG(the_store)) {
return;
}
the_store = metrics_store_new();
}
/** Free the relay metrics. */
void
relay_metrics_free(void)
{
if (!the_store) {
return;
}
/* NULL is set with this call. */
metrics_store_free(the_store);
}

View File

@ -0,0 +1,21 @@
/* Copyright (c) 2021, The Tor Project, Inc. */
/* See LICENSE for licensing information */
/**
* @file relay_metrics.h
* @brief Header for feature/relay/relay_metrics.c
**/
#ifndef TOR_FEATURE_RELAY_RELAY_METRICS_H
#define TOR_FEATURE_RELAY_RELAY_METRICS_H
#include "lib/container/smartlist.h"
/* Init. */
void relay_metrics_init(void);
void relay_metrics_free(void);
/* Accessors. */
const smartlist_t *relay_metrics_get_stores(void);
#endif /* !defined(TOR_FEATURE_RELAY_RELAY_METRICS_H) */

View File

@ -14,6 +14,7 @@
#include "feature/relay/dns.h"
#include "feature/relay/ext_orport.h"
#include "feature/relay/relay_metrics.h"
#include "feature/relay/onion_queue.h"
#include "feature/relay/relay_periodic.h"
#include "feature/relay/relay_sys.h"
@ -25,6 +26,7 @@
static int
subsys_relay_initialize(void)
{
relay_metrics_init();
relay_register_periodic_events();
return 0;
}
@ -37,6 +39,7 @@ subsys_relay_shutdown(void)
clear_pending_onions();
routerkeys_free_all();
router_free_all();
relay_metrics_free();
}
const struct subsys_fns_t sys_relay = {
@ -46,4 +49,6 @@ const struct subsys_fns_t sys_relay = {
.level = RELAY_SUBSYS_LEVEL,
.initialize = subsys_relay_initialize,
.shutdown = subsys_relay_shutdown,
.get_metrics = relay_metrics_get_stores,
};