diff options
author | David Goulet <dgoulet@torproject.org> | 2020-10-19 15:15:47 -0400 |
---|---|---|
committer | David Goulet <dgoulet@torproject.org> | 2020-10-27 10:43:42 -0400 |
commit | ec731290a5a790093961f0fdb06cf69000194adf (patch) | |
tree | da6a2351d3bb17071c64f493edfa292e4257c7ec /src/lib/metrics/prometheus.c | |
parent | bd582583f610d568bb61d1a108d1ff4f38ef08b5 (diff) | |
download | tor-ec731290a5a790093961f0fdb06cf69000194adf.tar.gz tor-ec731290a5a790093961f0fdb06cf69000194adf.zip |
lib: New metrics library
Used to provide an interface to create metrics store and update the entries.
Related to #40063
Signed-off-by: David Goulet <dgoulet@torproject.org>
Diffstat (limited to 'src/lib/metrics/prometheus.c')
-rw-r--r-- | src/lib/metrics/prometheus.c | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/src/lib/metrics/prometheus.c b/src/lib/metrics/prometheus.c new file mode 100644 index 0000000000..c2b54e436f --- /dev/null +++ b/src/lib/metrics/prometheus.c @@ -0,0 +1,56 @@ +/* Copyright (c) 2020, The Tor Project, Inc. */ +/* See LICENSE for licensing information */ + +/** + * @file prometheus.c + * @brief Metrics format driver for Prometheus data model. + **/ + +#define METRICS_STORE_ENTRY_PRIVATE + +#include "orconfig.h" + +#include "lib/container/smartlist.h" +#include "lib/log/util_bug.h" +#include "lib/malloc/malloc.h" +#include "lib/string/printf.h" + +#include "lib/metrics/prometheus.h" + +/** Return a static buffer containing all the labels properly formatted + * for the output as a string. + * + * Subsequent calls to this invalidates the previous result. */ +static const char * +format_labels(smartlist_t *labels) +{ + static char buf[1024]; + char *line = NULL; + + if (smartlist_len(labels) == 0) { + buf[0] = '\0'; + goto end; + } + + line = smartlist_join_strings(labels, ",", 0, NULL); + tor_snprintf(buf, sizeof(buf), "{%s}", line); + + end: + tor_free(line); + return buf; +} + +/** Format the given entry in to the buffer data. */ +void +prometheus_format_store_entry(const metrics_store_entry_t *entry, buf_t *data) +{ + tor_assert(entry); + tor_assert(data); + + buf_add_printf(data, "# HELP %s %s\n", entry->name, entry->help); + buf_add_printf(data, "# TYPE %s %s\n", entry->name, + metrics_type_to_str(entry->type)); + buf_add_printf(data, "%s%s %" PRIi64 "\n", entry->name, + format_labels(entry->labels), + metrics_store_entry_get_value(entry)); +} |