#define METRICS_STORE_ENTRY_PRIVATE
#include <string.h>
#include "orconfig.h"
#include "lib/container/smartlist.h"
#include "lib/log/util_bug.h"
#include "lib/malloc/malloc.h"
#include "lib/metrics/metrics_store_entry.h"
metrics_store_entry_t *
metrics_store_entry_new(const metrics_type_t type, const char *name,
const char *help)
{
metrics_store_entry_t *entry = tor_malloc_zero(sizeof(*entry));
tor_assert(name);
entry->type = type;
entry->name = tor_strdup(name);
entry->labels = smartlist_new();
if (help) {
entry->help = tor_strdup(help);
}
return entry;
}
void
metrics_store_entry_free_(metrics_store_entry_t *entry)
{
if (!entry) {
return;
}
SMARTLIST_FOREACH(entry->labels, char *, l, tor_free(l));
smartlist_free(entry->labels);
tor_free(entry->name);
tor_free(entry->help);
tor_free(entry);
}
void
metrics_store_entry_update(metrics_store_entry_t *entry, const int64_t value)
{
tor_assert(entry);
switch (entry->type) {
case METRICS_TYPE_COUNTER:
if (BUG(value < 0)) {
return;
}
entry->u.counter.value += value;
break;
case METRICS_TYPE_GAUGE:
entry->u.gauge.value += value;
break;
}
}
void
metrics_store_entry_reset(metrics_store_entry_t *entry)
{
tor_assert(entry);
memset(&entry->u, 0, sizeof(entry->u));
}
int64_t
metrics_store_entry_get_value(const metrics_store_entry_t *entry)
{
tor_assert(entry);
switch (entry->type) {
case METRICS_TYPE_COUNTER:
if (entry->u.counter.value > INT64_MAX) {
return INT64_MAX;
}
return entry->u.counter.value;
case METRICS_TYPE_GAUGE:
return entry->u.gauge.value;
}
tor_assert_unreached();
}
void
metrics_store_entry_add_label(metrics_store_entry_t *entry,
const char *label)
{
tor_assert(entry);
tor_assert(label);
smartlist_add(entry->labels, tor_strdup(label));
}
bool
metrics_store_entry_has_label(const metrics_store_entry_t *entry,
const char *label)
{
tor_assert(entry);
tor_assert(label);
return smartlist_contains_string(entry->labels, label);
}