Tor 0.4.9.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
19
20#include <string.h>
21
22/** Return a static buffer containing all the labels properly formatted
23 * for the output as a string.
24 *
25 * Subsequent calls to this invalidates the previous result. */
26static const char *
28{
29 static char buf[1024];
30 char *line = NULL;
31
32 if (smartlist_len(labels) == 0) {
33 buf[0] = '\0';
34 goto end;
35 }
36
37 line = smartlist_join_strings(labels, ",", 0, NULL);
38 tor_snprintf(buf, sizeof(buf), "%s", line);
39
40 end:
41 tor_free(line);
42 return buf;
43}
44
45/** Write the string representation of the histogram entry to the specified
46 * buffer.
47 *
48 * Note: entry **must** be a histogram.
49 */
50static void
51format_histogram(const metrics_store_entry_t *entry, buf_t *data)
52{
53 tor_assert(entry->type == METRICS_TYPE_HISTOGRAM);
54
55 const char *labels = format_labels(entry->labels);
56
57 for (size_t i = 0; i < entry->u.histogram.bucket_count; ++i) {
58 metrics_histogram_bucket_t hb = entry->u.histogram.buckets[i];
59 if (strlen(labels) > 0) {
60 buf_add_printf(data, "%s_bucket{%s,le=\"%.2f\"} %" PRIi64 "\n",
61 entry->name, labels, (double)hb.bucket, hb.value);
62 } else {
63 buf_add_printf(data, "%s_bucket{le=\"%.2f\"} %" PRIi64 "\n",
64 entry->name, (double)hb.bucket, hb.value);
65 }
66 }
67
68 if (strlen(labels) > 0) {
69 buf_add_printf(data, "%s_bucket{%s,le=\"+Inf\"} %" PRIi64 "\n",
70 entry->name, labels,
72 buf_add_printf(data, "%s_sum{%s} %" PRIi64 "\n", entry->name, labels,
74 buf_add_printf(data, "%s_count{%s} %" PRIi64 "\n", entry->name, labels,
76 } else {
77 buf_add_printf(data, "%s_bucket{le=\"+Inf\"} %" PRIi64 "\n", entry->name,
79 buf_add_printf(data, "%s_sum %" PRIi64 "\n", entry->name,
81 buf_add_printf(data, "%s_count %" PRIi64 "\n", entry->name,
83 }
84}
85
86/** Format the given entry in to the buffer data. */
87void
88prometheus_format_store_entry(const metrics_store_entry_t *entry, buf_t *data,
89 bool no_comment)
90{
91 tor_assert(entry);
92 tor_assert(data);
93
94 if (!no_comment) {
95 buf_add_printf(data, "# HELP %s %s\n", entry->name, entry->help);
96 buf_add_printf(data, "# TYPE %s %s\n", entry->name,
97 metrics_type_to_str(entry->type));
98 }
99
100 switch (entry->type) {
101 case METRICS_TYPE_COUNTER: FALLTHROUGH;
102 case METRICS_TYPE_GAUGE:
103 {
104 const char *labels = format_labels(entry->labels);
105 if (strlen(labels) > 0) {
106 buf_add_printf(data, "%s{%s} %" PRIi64 "\n", entry->name,
107 labels,
109 } else {
110 buf_add_printf(data, "%s %" PRIi64 "\n", entry->name,
112 }
113 break;
114 }
115 case METRICS_TYPE_HISTOGRAM:
116 format_histogram(entry, data);
117 break;
118 default:
119 tor_assert_unreached();
120 }
121}
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:56
const char * metrics_type_to_str(const metrics_type_t type)
uint64_t metrics_store_hist_entry_get_count(const metrics_store_entry_t *entry)
int64_t metrics_store_hist_entry_get_sum(const metrics_store_entry_t *entry)
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:88
static const char * format_labels(smartlist_t *labels)
Definition: prometheus.c:27
static void format_histogram(const metrics_store_entry_t *entry, buf_t *data)
Definition: prometheus.c:51
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:103