Tor  0.4.6.0-alpha-dev
metrics_store_entry.c
Go to the documentation of this file.
1 /* Copyright (c) 2020, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
3 
4 /**
5  * @file metrics_store_entry.c
6  * @brief Metrics store entry which contains the gathered data.
7  **/
8 
9 #define METRICS_STORE_ENTRY_PRIVATE
10 
11 #include <string.h>
12 
13 #include "orconfig.h"
14 
16 #include "lib/log/util_bug.h"
17 #include "lib/malloc/malloc.h"
18 
20 
21 /*
22  * Public API.
23  */
24 
25 /** Return newly allocated store entry of type COUNTER. */
26 metrics_store_entry_t *
28  const char *help)
29 {
30  metrics_store_entry_t *entry = tor_malloc_zero(sizeof(*entry));
31 
33 
34  entry->type = type;
35  entry->name = tor_strdup(name);
36  entry->labels = smartlist_new();
37  if (help) {
38  entry->help = tor_strdup(help);
39  }
40 
41  return entry;
42 }
43 
44 /** Free a store entry. */
45 void
46 metrics_store_entry_free_(metrics_store_entry_t *entry)
47 {
48  if (!entry) {
49  return;
50  }
51  SMARTLIST_FOREACH(entry->labels, char *, l, tor_free(l));
52  smartlist_free(entry->labels);
53  tor_free(entry->name);
54  tor_free(entry->help);
55  tor_free(entry);
56 }
57 
58 /** Update a store entry with value. */
59 void
60 metrics_store_entry_update(metrics_store_entry_t *entry, const int64_t value)
61 {
62  tor_assert(entry);
63 
64  switch (entry->type) {
65  case METRICS_TYPE_COUNTER:
66  /* Counter can ONLY be positive. */
67  if (BUG(value < 0)) {
68  return;
69  }
70  entry->u.counter.value += value;
71  break;
72  case METRICS_TYPE_GAUGE:
73  /* Gauge can increment or decrement. And can be positive or negative. */
74  entry->u.gauge.value += value;
75  break;
76  }
77 }
78 
79 /** Reset a store entry that is set its metric data to 0. */
80 void
81 metrics_store_entry_reset(metrics_store_entry_t *entry)
82 {
83  tor_assert(entry);
84  /* Everything back to 0. */
85  memset(&entry->u, 0, sizeof(entry->u));
86 }
87 
88 /** Return store entry value. */
89 int64_t
90 metrics_store_entry_get_value(const metrics_store_entry_t *entry)
91 {
92  tor_assert(entry);
93 
94  switch (entry->type) {
95  case METRICS_TYPE_COUNTER:
96  if (entry->u.counter.value > INT64_MAX) {
97  return INT64_MAX;
98  }
99  return entry->u.counter.value;
100  case METRICS_TYPE_GAUGE:
101  return entry->u.gauge.value;
102  }
103 
104  // LCOV_EXCL_START
105  tor_assert_unreached();
106  // LCOV_EXCL_STOP
107 }
108 
109 /** Add a label into the given entry.*/
110 void
111 metrics_store_entry_add_label(metrics_store_entry_t *entry,
112  const char *label)
113 {
114  tor_assert(entry);
115  tor_assert(label);
116 
117  smartlist_add(entry->labels, tor_strdup(label));
118 }
119 
120 /** Return true iff the given entry has the given label. */
121 bool
122 metrics_store_entry_has_label(const metrics_store_entry_t *entry,
123  const char *label)
124 {
125  tor_assert(entry);
126  tor_assert(label);
127 
128  return smartlist_contains_string(entry->labels, label);
129 }
tor_free
#define tor_free(p)
Definition: malloc.h:52
name
const char * name
Definition: config.c:2464
smartlist.h
Header for smartlist.c.
metrics_store_entry_add_label
void metrics_store_entry_add_label(metrics_store_entry_t *entry, const char *label)
Definition: metrics_store_entry.c:111
tor_assert
#define tor_assert(expr)
Definition: util_bug.h:102
smartlist_add
void smartlist_add(smartlist_t *sl, void *element)
Definition: smartlist_core.c:117
smartlist_new
smartlist_t * smartlist_new(void)
Definition: smartlist_core.c:26
util_bug.h
Macros to manage assertions, fatal and non-fatal.
SMARTLIST_FOREACH
#define SMARTLIST_FOREACH(sl, type, var, cmd)
Definition: smartlist_foreach.h:112
metrics_store_entry_new
metrics_store_entry_t * metrics_store_entry_new(const metrics_type_t type, const char *name, const char *help)
Definition: metrics_store_entry.c:27
metrics_type_t
metrics_type_t
Definition: metrics_common.h:26
smartlist_contains_string
int smartlist_contains_string(const smartlist_t *sl, const char *element)
Definition: smartlist.c:93
malloc.h
Headers for util_malloc.c.
metrics_store_entry.h
Header for lib/metrics/metrics_store_entry.c.
metrics_store_entry_reset
void metrics_store_entry_reset(metrics_store_entry_t *entry)
Definition: metrics_store_entry.c:81
metrics_store_entry_get_value
int64_t metrics_store_entry_get_value(const metrics_store_entry_t *entry)
Definition: metrics_store_entry.c:90
metrics_store_entry_free_
void metrics_store_entry_free_(metrics_store_entry_t *entry)
Definition: metrics_store_entry.c:46
metrics_store_entry_update
void metrics_store_entry_update(metrics_store_entry_t *entry, const int64_t value)
Definition: metrics_store_entry.c:60
metrics_store_entry_has_label
bool metrics_store_entry_has_label(const metrics_store_entry_t *entry, const char *label)
Definition: metrics_store_entry.c:122