Tor  0.4.6.0-alpha-dev
prometheus.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 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 {
47  tor_assert(entry);
48  tor_assert(data);
49 
50  buf_add_printf(data, "# HELP %s %s\n", entry->name, entry->help);
51  buf_add_printf(data, "# TYPE %s %s\n", entry->name,
52  metrics_type_to_str(entry->type));
53  buf_add_printf(data, "%s%s %" PRIi64 "\n", entry->name,
54  format_labels(entry->labels),
56 }
tor_free
#define tor_free(p)
Definition: malloc.h:52
smartlist.h
Header for smartlist.c.
tor_assert
#define tor_assert(expr)
Definition: util_bug.h:102
metrics_type_to_str
const char * metrics_type_to_str(const metrics_type_t type)
Definition: metrics_common.c:19
util_bug.h
Macros to manage assertions, fatal and non-fatal.
tor_snprintf
int tor_snprintf(char *str, size_t size, const char *format,...)
Definition: printf.c:27
format_labels
static const char * format_labels(smartlist_t *labels)
Definition: prometheus.c:25
printf.h
Header for printf.c.
malloc.h
Headers for util_malloc.c.
metrics_store_entry_get_value
int64_t metrics_store_entry_get_value(const metrics_store_entry_t *entry)
Definition: metrics_store_entry.c:90
prometheus.h
Header for feature/metrics/prometheus.c.
buf_add_printf
void buf_add_printf(buf_t *buf, const char *format,...)
Definition: buffers.c:568
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
smartlist_join_strings
char * smartlist_join_strings(smartlist_t *sl, const char *join, int terminate, size_t *len_out)
Definition: smartlist.c:279