#define METRICS_STORE_ENTRY_PRIVATE
#include "orconfig.h"
#include "lib/container/map.h"
#include "lib/log/util_bug.h"
#include "lib/malloc/malloc.h"
#include "lib/metrics/metrics_store.h"
#include "lib/metrics/metrics_store_entry.h"
#include "lib/metrics/prometheus.h"
struct metrics_store_t {
strmap_t *entries;
};
typedef void (fmt_driver_fn_t)(const metrics_store_entry_t *, buf_t *);
static void
metrics_store_free_void(void *p)
{
smartlist_t *list = p;
SMARTLIST_FOREACH(list, metrics_store_entry_t *, entry,
metrics_store_entry_free(entry));
smartlist_free(list);
}
static void
get_output(const metrics_store_t *store, buf_t *data, fmt_driver_fn_t fmt)
{
tor_assert(store);
tor_assert(data);
tor_assert(fmt);
STRMAP_FOREACH(store->entries, key, const smartlist_t *, entries) {
SMARTLIST_FOREACH_BEGIN(entries, const metrics_store_entry_t *, entry) {
fmt(entry, data);
} SMARTLIST_FOREACH_END(entry);
} STRMAP_FOREACH_END;
}
metrics_store_t *
metrics_store_new(void)
{
metrics_store_t *store = tor_malloc_zero(sizeof(*store));
store->entries = strmap_new();
return store;
}
void
metrics_store_free_(metrics_store_t *store)
{
if (store == NULL) {
return;
}
strmap_free(store->entries, metrics_store_free_void);
tor_free(store);
}
smartlist_t *
metrics_store_get_all(const metrics_store_t *store, const char *name)
{
tor_assert(store);
tor_assert(name);
return strmap_get(store->entries, name);
}
metrics_store_entry_t *
metrics_store_add(metrics_store_t *store, metrics_type_t type,
const char *name, const char *help)
{
smartlist_t *entries;
metrics_store_entry_t *entry;
tor_assert(store);
tor_assert(name);
entries = metrics_store_get_all(store, name);
if (!entries) {
entries = smartlist_new();
strmap_set(store->entries, name, entries);
}
entry = metrics_store_entry_new(type, name, help);
smartlist_add(entries, entry);
return entry;
}
void
metrics_store_get_output(const metrics_format_t fmt,
const metrics_store_t *store, buf_t *data)
{
tor_assert(store);
switch (fmt) {
case METRICS_FORMAT_PROMETHEUS:
get_output(store, data, prometheus_format_store_entry);
break;
default:
tor_assert_unreached();
}
}