Tor  0.4.6.0-alpha-dev
metrics_store.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.c
6  * @brief Metrics interface to store them based on specific store type and get
7  * their MetricsPort output.
8  **/
9 
10 #define METRICS_STORE_ENTRY_PRIVATE
11 
12 #include "orconfig.h"
13 
14 #include "lib/container/map.h"
15 #include "lib/log/util_bug.h"
16 #include "lib/malloc/malloc.h"
17 
20 
21 /* Format Drivers. */
22 #include "lib/metrics/prometheus.h"
23 
24 /** A metric store which contains a map of entries. */
26  /** Indexed by metrics entry name. An entry is a smartlist_t of one or more
27  * metrics_store_entry_t allowing for multiple metrics of the same name.
28  *
29  * The reason we allow multiple entries is because there are cases where one
30  * metrics can be used twice by the same entity but with different labels.
31  * One example is an onion service with multiple ports, the port specific
32  * metrics will have a port value as a label. */
33  strmap_t *entries;
34 };
35 
36 /** Function pointer to the format function of a specific driver. */
37 typedef void (fmt_driver_fn_t)(const metrics_store_entry_t *, buf_t *);
38 
39 /** Helper: Free a single entry in a metrics_store_t taking a void pointer
40  * parameter. */
41 static void
43 {
44  smartlist_t *list = p;
45  SMARTLIST_FOREACH(list, metrics_store_entry_t *, entry,
46  metrics_store_entry_free(entry));
47  smartlist_free(list);
48 }
49 
50 /** Put the given store output in the buffer data and use the format function
51  * given in fmt to get it for each entry. */
52 static void
53 get_output(const metrics_store_t *store, buf_t *data, fmt_driver_fn_t fmt)
54 {
55  tor_assert(store);
56  tor_assert(data);
57  tor_assert(fmt);
58 
59  STRMAP_FOREACH(store->entries, key, const smartlist_t *, entries) {
60  SMARTLIST_FOREACH_BEGIN(entries, const metrics_store_entry_t *, entry) {
61  fmt(entry, data);
62  } SMARTLIST_FOREACH_END(entry);
63  } STRMAP_FOREACH_END;
64 }
65 
66 /** Return a newly allocated and initialized store of the given type. */
69 {
70  metrics_store_t *store = tor_malloc_zero(sizeof(*store));
71 
72  store->entries = strmap_new();
73 
74  return store;
75 }
76 
77 /** Free the given store including all its entries. */
78 void
80 {
81  if (store == NULL) {
82  return;
83  }
84 
85  strmap_free(store->entries, metrics_store_free_void);
86  tor_free(store);
87 }
88 
89 /** Find all metrics entry in the given store identified by name. If not found,
90  * NULL is returned. */
92 metrics_store_get_all(const metrics_store_t *store, const char *name)
93 {
94  tor_assert(store);
96 
97  return strmap_get(store->entries, name);
98 }
99 
100 /** Add a new metrics entry to the given store and type. The name MUST be the
101  * unique identifier. The help string can be omitted. */
102 metrics_store_entry_t *
104  const char *name, const char *help)
105 {
106  smartlist_t *entries;
107  metrics_store_entry_t *entry;
108 
109  tor_assert(store);
110  tor_assert(name);
111 
112  entries = metrics_store_get_all(store, name);
113  if (!entries) {
114  entries = smartlist_new();
115  strmap_set(store->entries, name, entries);
116  }
117  entry = metrics_store_entry_new(type, name, help);
118  smartlist_add(entries, entry);
119 
120  return entry;
121 }
122 
123 /** Set the output of the given store of the format fmt into the given buffer
124  * data. */
125 void
127  const metrics_store_t *store, buf_t *data)
128 {
129  tor_assert(store);
130 
131  switch (fmt) {
134  break;
135  default:
136  // LCOV_EXCL_START
137  tor_assert_unreached();
138  // LCOV_EXCL_STOP
139  }
140 }
tor_free
#define tor_free(p)
Definition: malloc.h:52
metrics_store_free_
void metrics_store_free_(metrics_store_t *store)
Definition: metrics_store.c:79
name
const char * name
Definition: config.c:2447
metrics_store_t
Definition: metrics_store.c:25
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
metrics_store_t::entries
strmap_t * entries
Definition: metrics_store.c:33
smartlist_new
smartlist_t * smartlist_new(void)
Definition: smartlist_core.c:26
metrics_store_new
metrics_store_t * metrics_store_new(void)
Definition: metrics_store.c:68
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_FORMAT_PROMETHEUS
@ METRICS_FORMAT_PROMETHEUS
Definition: metrics_common.h:22
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
fmt_driver_fn_t
void() fmt_driver_fn_t(const metrics_store_entry_t *, buf_t *)
Definition: metrics_store.c:37
metrics_store_add
metrics_store_entry_t * metrics_store_add(metrics_store_t *store, metrics_type_t type, const char *name, const char *help)
Definition: metrics_store.c:103
malloc.h
Headers for util_malloc.c.
map.h
Headers for map.c.
metrics_store_entry.h
Header for lib/metrics/metrics_store_entry.c.
prometheus.h
Header for feature/metrics/prometheus.c.
SMARTLIST_FOREACH_BEGIN
#define SMARTLIST_FOREACH_BEGIN(sl, type, var)
Definition: smartlist_foreach.h:78
metrics_store_free_void
static void metrics_store_free_void(void *p)
Definition: metrics_store.c:42
get_output
static void get_output(const metrics_store_t *store, buf_t *data, fmt_driver_fn_t fmt)
Definition: metrics_store.c:53
metrics_store_get_output
void metrics_store_get_output(const metrics_format_t fmt, const metrics_store_t *store, buf_t *data)
Definition: metrics_store.c:126
metrics_store.h
Header for lib/metrics/metrics_store.c.
metrics_store_get_all
smartlist_t * metrics_store_get_all(const metrics_store_t *store, const char *name)
Definition: metrics_store.c:92
metrics_format_t
metrics_format_t
Definition: metrics_common.h:20
prometheus_format_store_entry
void prometheus_format_store_entry(const metrics_store_entry_t *entry, buf_t *data)
Definition: prometheus.c:45
smartlist_t
Definition: smartlist_core.h:26