10#define METRICS_STORE_ENTRY_PRIVATE
27metrics_store_entry_t *
29 const char *help,
size_t bucket_count,
30 const int64_t *buckets)
32 metrics_store_entry_t *entry = tor_malloc_zero(
sizeof(*entry));
37 entry->name = tor_strdup(
name);
40 entry->help = tor_strdup(help);
43 if (type == METRICS_TYPE_HISTOGRAM && bucket_count > 0) {
46 entry->u.histogram.bucket_count = bucket_count;
47 entry->u.histogram.buckets =
50 for (
size_t i = 0; i < bucket_count; ++i) {
51 entry->u.histogram.buckets[i].bucket = buckets[i];
66 smartlist_free(entry->labels);
70 if (entry->type == METRICS_TYPE_HISTOGRAM) {
71 tor_free(entry->u.histogram.buckets);
84 if (BUG(entry->type == METRICS_TYPE_HISTOGRAM)) {
88 switch (entry->type) {
89 case METRICS_TYPE_COUNTER:
94 entry->u.counter.value += value;
96 case METRICS_TYPE_GAUGE:
98 entry->u.gauge.value += value;
100 case METRICS_TYPE_HISTOGRAM:
101 tor_assert_unreached();
110 const int64_t value,
const int64_t obs)
112 if (BUG(entry->type != METRICS_TYPE_HISTOGRAM)) {
117 if (BUG(value < 0)) {
123 if (PREDICT_UNLIKELY(
124 (obs > 0 && entry->u.histogram.sum > INT64_MAX - obs) ||
125 (obs < 0 && entry->u.histogram.sum < INT64_MIN - obs))) {
129 entry->u.histogram.count += value;
130 entry->u.histogram.sum += obs;
132 for (
size_t i = 0; i < entry->u.histogram.bucket_count; ++i) {
134 if (obs <= hb->bucket) {
146 switch (entry->type) {
147 case METRICS_TYPE_COUNTER: FALLTHROUGH;
148 case METRICS_TYPE_GAUGE:
150 memset(&entry->u, 0,
sizeof(entry->u));
152 case METRICS_TYPE_HISTOGRAM:
153 for (
size_t i = 0; i < entry->u.histogram.bucket_count; ++i) {
157 entry->u.histogram.sum = 0;
158 entry->u.histogram.count = 0;
170 if (BUG(entry->type == METRICS_TYPE_HISTOGRAM)) {
174 switch (entry->type) {
175 case METRICS_TYPE_COUNTER:
176 if (entry->u.counter.value > INT64_MAX) {
179 return entry->u.counter.value;
180 case METRICS_TYPE_GAUGE:
181 return entry->u.gauge.value;
182 case METRICS_TYPE_HISTOGRAM:
183 tor_assert_unreached();
188 tor_assert_unreached();
197 const int64_t bucket)
201 if (BUG(entry->type != METRICS_TYPE_HISTOGRAM)) {
205 for (
size_t i = 0; i <= entry->u.histogram.bucket_count; ++i) {
207 if (bucket == hb.bucket) {
208 if (hb.value > INT64_MAX) {
216 tor_assertf_nonfatal(
false,
"attempted to get the value of non-existent "
217 "bucket %" PRId64, bucket);
245metrics_store_entry_t *
258 } SMARTLIST_FOREACH_END(entry);
267 if (entry->type == METRICS_TYPE_HISTOGRAM) {
280 if (BUG(entry->type != METRICS_TYPE_HISTOGRAM)) {
284 return entry->u.histogram.count;
293 if (BUG(entry->type != METRICS_TYPE_HISTOGRAM)) {
297 return entry->u.histogram.sum;
Headers for util_malloc.c.
Header for lib/metrics/metrics_common.c.
uint64_t metrics_store_hist_entry_get_count(const metrics_store_entry_t *entry)
void metrics_store_entry_update(metrics_store_entry_t *entry, const int64_t value)
bool metrics_store_entry_is_histogram(const metrics_store_entry_t *entry)
uint64_t metrics_store_hist_entry_get_value(const metrics_store_entry_t *entry, const int64_t bucket)
void metrics_store_entry_add_label(metrics_store_entry_t *entry, const char *label)
int64_t metrics_store_hist_entry_get_sum(const metrics_store_entry_t *entry)
metrics_store_entry_t * metrics_store_entry_new(const metrics_type_t type, const char *name, const char *help, size_t bucket_count, const int64_t *buckets)
metrics_store_entry_t * metrics_store_find_entry_with_label(const smartlist_t *entries, const char *label)
void metrics_store_entry_free_(metrics_store_entry_t *entry)
void metrics_store_hist_entry_update(metrics_store_entry_t *entry, const int64_t value, const int64_t obs)
int64_t metrics_store_entry_get_value(const metrics_store_entry_t *entry)
void metrics_store_entry_reset(metrics_store_entry_t *entry)
bool metrics_store_entry_has_label(const metrics_store_entry_t *entry, const char *label)
Header for lib/metrics/metrics_store_entry.c.
int smartlist_contains_string(const smartlist_t *sl, const char *element)
smartlist_t * smartlist_new(void)
void smartlist_add(smartlist_t *sl, void *element)
#define SMARTLIST_FOREACH_BEGIN(sl, type, var)
#define SMARTLIST_FOREACH(sl, type, var, cmd)
Macros to manage assertions, fatal and non-fatal.