diff options
author | David Goulet <dgoulet@torproject.org> | 2021-04-15 14:23:47 -0400 |
---|---|---|
committer | David Goulet <dgoulet@torproject.org> | 2021-05-12 11:58:25 -0400 |
commit | 9c2fa3498233bbb5f347a03188433c6c5f6de24f (patch) | |
tree | f3dc5b9d500198c147ae9e414b7f97abe8c4685b /src/feature | |
parent | bdde4579c2fcb43cf5a32d617b3a5fe72314adf8 (diff) | |
download | tor-9c2fa3498233bbb5f347a03188433c6c5f6de24f.tar.gz tor-9c2fa3498233bbb5f347a03188433c6c5f6de24f.zip |
relay: Add the OOM invocation metrics
With this commit, a relay now emits metrics event on the MetricsPort
related to the OOM invocation for:
- DNS cache
- GeoIP database
- Cell queues
- HSDir caches
Everytime the OOM is invoked, the number of bytes is added to the
metrics counter for that specific type of invocation.
Related to #40367
Signed-off-by: David Goulet <dgoulet@torproject.org>
Diffstat (limited to 'src/feature')
-rw-r--r-- | src/feature/relay/relay_metrics.c | 74 | ||||
-rw-r--r-- | src/feature/relay/relay_metrics.h | 10 |
2 files changed, 77 insertions, 7 deletions
diff --git a/src/feature/relay/relay_metrics.c b/src/feature/relay/relay_metrics.c index 3d392847e1..2686249548 100644 --- a/src/feature/relay/relay_metrics.c +++ b/src/feature/relay/relay_metrics.c @@ -10,6 +10,9 @@ #include "orconfig.h" +#include "core/or/or.h" +#include "core/or/relay.h" + #include "lib/malloc/malloc.h" #include "lib/container/smartlist.h" #include "lib/metrics/metrics_store.h" @@ -17,16 +20,78 @@ #include "feature/relay/relay_metrics.h" +/** Declarations of each fill function for metrics defined in base_metrics. */ +static void fill_oom_values(void); + /** The base metrics that is a static array of metrics added to the metrics * store. * * The key member MUST be also the index of the entry in the array. */ -static const relay_metrics_entry_t base_metrics[] = {}; +static const relay_metrics_entry_t base_metrics[] = +{ + { + .key = RELAY_METRICS_NUM_OOM_BYTES, + .type = METRICS_TYPE_COUNTER, + .name = METRICS_NAME(relay_load_oom_bytes_total), + .help = "Total number of bytes the OOM has freed by subsystem", + .fill_fn = fill_oom_values, + }, +}; static const size_t num_base_metrics = ARRAY_LENGTH(base_metrics); /** The only and single store of all the relay metrics. */ static metrics_store_t *the_store; +/** Fill function for the RELAY_METRICS_NUM_OOM_BYTES metrics. */ +static void +fill_oom_values(void) +{ + metrics_store_entry_t *sentry; + const relay_metrics_entry_t *rentry = + &base_metrics[RELAY_METRICS_NUM_OOM_BYTES]; + + sentry = metrics_store_add(the_store, rentry->type, rentry->name, + rentry->help); + metrics_store_entry_add_label(sentry, "subsys=cell"); + metrics_store_entry_update(sentry, oom_stats_n_bytes_removed_cell); + + sentry = metrics_store_add(the_store, rentry->type, rentry->name, + rentry->help); + metrics_store_entry_add_label(sentry, "subsys=dns"); + metrics_store_entry_update(sentry, oom_stats_n_bytes_removed_dns); + + sentry = metrics_store_add(the_store, rentry->type, rentry->name, + rentry->help); + metrics_store_entry_add_label(sentry, "subsys=geoip"); + metrics_store_entry_update(sentry, oom_stats_n_bytes_removed_geoip); + + sentry = metrics_store_add(the_store, rentry->type, rentry->name, + rentry->help); + metrics_store_entry_add_label(sentry, "subsys=hsdir"); + metrics_store_entry_update(sentry, oom_stats_n_bytes_removed_hsdir); +} + +/** Reset the global store and fill it with all the metrics from base_metrics + * and their associated values. + * + * To pull this off, every metrics has a "fill" function that is called and in + * charge of adding the metrics to the store, appropriate labels and finally + * updating the value to report. */ +static void +fill_store(void) +{ + /* Reset the current store, we are about to fill it with all the things. */ + metrics_store_reset(the_store); + + /* Call the fill function for each metrics. */ + for (size_t i = 0; i < num_base_metrics; i++) { + if (BUG(!base_metrics[i].fill_fn)) { + continue; + } + base_metrics[i].fill_fn(); + } +} + /** 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 * @@ -36,6 +101,13 @@ relay_metrics_get_stores(void) * simply update it. */ static smartlist_t *stores_list = NULL; + /* We dynamically fill the store with all the metrics upon a request. The + * reason for this is because the exposed metrics of a relay are often + * internal counters in the fast path and thus we fetch the value when a + * metrics port request arrives instead of keeping a local metrics store of + * those values. */ + fill_store(); + if (!stores_list) { stores_list = smartlist_new(); smartlist_add(stores_list, the_store); diff --git a/src/feature/relay/relay_metrics.h b/src/feature/relay/relay_metrics.h index 7bc4760916..7d644badd3 100644 --- a/src/feature/relay/relay_metrics.h +++ b/src/feature/relay/relay_metrics.h @@ -12,13 +12,11 @@ #include "lib/container/smartlist.h" #include "lib/metrics/metrics_common.h" -#ifdef RELAY_METRICS_ENTRY_PRIVATE - /** Metrics key for each reported metrics. This key is also used as an index in * the base_metrics array. */ typedef enum { - /* XXX So code compiles. */ - PLACEHOLDER = 0, + /** Number of OOM invocation. */ + RELAY_METRICS_NUM_OOM_BYTES = 0, } relay_metrics_key_t; /** The metadata of a relay metric. */ @@ -31,10 +29,10 @@ typedef struct relay_metrics_entry_t { const char *name; /* Metrics output help comment. */ const char *help; + /* Update value function. */ + void (*fill_fn)(void); } relay_metrics_entry_t; -#endif /* RELAY_METRICS_ENTRY_PRIVATE */ - /* Init. */ void relay_metrics_init(void); void relay_metrics_free(void); |