Tor  0.4.7.0-alpha-dev
prometheus.c
Go to the documentation of this file.
1 /* Copyright (c) 2020-2021, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
3 
4 /**
5  * @file prometheus.c
6  * @brief Metrics format driver for Prometheus data model.
7  **/
8 
9 #define METRICS_STORE_ENTRY_PRIVATE
10 
11 #include "orconfig.h"
12 
14 #include "lib/log/util_bug.h"
15 #include "lib/malloc/malloc.h"
16 #include "lib/string/printf.h"
17 
18 #include "lib/metrics/prometheus.h"
19 
20 /** Return a static buffer containing all the labels properly formatted
21  * for the output as a string.
22  *
23  * Subsequent calls to this invalidates the previous result. */
24 static const char *
26 {
27  static char buf[1024];
28  char *line = NULL;
29 
30  if (smartlist_len(labels) == 0) {
31  buf[0] = '\0';
32  goto end;
33  }
34 
35  line = smartlist_join_strings(labels, ",", 0, NULL);
36  tor_snprintf(buf, sizeof(buf), "{%s}", line);
37 
38  end:
39  tor_free(line);
40  return buf;
41 }
42 
43 /** Format the given entry in to the buffer data. */
44 void
45 prometheus_format_store_entry(const metrics_store_entry_t *entry, buf_t *data,
46  bool no_comment)
47 {
48  tor_assert(entry);
49  tor_assert(data);
50 
51  if (!no_comment) {
52  buf_add_printf(data, "# HELP %s %s\n", entry->name, entry->help);
53  buf_add_printf(data, "# TYPE %s %s\n", entry->name,
54  metrics_type_to_str(entry->type));
55  }
56  buf_add_printf(data, "%s%s %" PRIi64 "\n", entry->name,
57  format_labels(entry->labels),
59 }
void buf_add_printf(buf_t *buf, const char *format,...)
Definition: buffers.c:568
Headers for util_malloc.c.
#define tor_free(p)
Definition: malloc.h:52
const char * metrics_type_to_str(const metrics_type_t type)
int64_t metrics_store_entry_get_value(const metrics_store_entry_t *entry)
int tor_snprintf(char *str, size_t size, const char *format,...)
Definition: printf.c:27
Header for printf.c.
void prometheus_format_store_entry(const metrics_store_entry_t *entry, buf_t *data, bool no_comment)
Definition: prometheus.c:45
static const char * format_labels(smartlist_t *labels)
Definition: prometheus.c:25
Header for feature/metrics/prometheus.c.
char * smartlist_join_strings(smartlist_t *sl, const char *join, int terminate, size_t *len_out)
Definition: smartlist.c:279
Header for smartlist.c.
Macros to manage assertions, fatal and non-fatal.
#define tor_assert(expr)
Definition: util_bug.h:102