metrics: Print once the Promtheus comments

An entry can have multiple labels but only print once the comments at
the first one. This follows the Promtheus best practices.

Signed-off-by: David Goulet <dgoulet@torproject.org>
This commit is contained in:
David Goulet 2021-05-06 10:54:50 -04:00
parent 0a915d9171
commit cc2947c007
3 changed files with 16 additions and 7 deletions

View File

@ -34,7 +34,8 @@ struct metrics_store_t {
};
/** Function pointer to the format function of a specific driver. */
typedef void (fmt_driver_fn_t)(const metrics_store_entry_t *, buf_t *);
typedef void (fmt_driver_fn_t)(const metrics_store_entry_t *, buf_t *,
bool no_comment);
/** Helper: Free a single entry in a metrics_store_t taking a void pointer
* parameter. */
@ -47,6 +48,8 @@ metrics_store_free_void(void *p)
smartlist_free(list);
}
#include <stdio.h>
/** Put the given store output in the buffer data and use the format function
* given in fmt to get it for each entry. */
static void
@ -57,8 +60,11 @@ get_output(const metrics_store_t *store, buf_t *data, fmt_driver_fn_t fmt)
tor_assert(fmt);
STRMAP_FOREACH(store->entries, key, const smartlist_t *, entries) {
/* Indicate that we've formatted the coment already for the entries. */
bool comment_formatted = false;
SMARTLIST_FOREACH_BEGIN(entries, const metrics_store_entry_t *, entry) {
fmt(entry, data);
fmt(entry, data, comment_formatted);
comment_formatted = true;
} SMARTLIST_FOREACH_END(entry);
} STRMAP_FOREACH_END;
}

View File

@ -42,14 +42,17 @@ format_labels(smartlist_t *labels)
/** Format the given entry in to the buffer data. */
void
prometheus_format_store_entry(const metrics_store_entry_t *entry, buf_t *data)
prometheus_format_store_entry(const metrics_store_entry_t *entry, buf_t *data,
bool no_comment)
{
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));
if (!no_comment) {
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));

View File

@ -13,6 +13,6 @@
#include "lib/metrics/metrics_store_entry.h"
void prometheus_format_store_entry(const metrics_store_entry_t *entry,
buf_t *data);
buf_t *data, bool no_comment);
#endif /* !defined(TOR_LIB_METRICS_PROMETHEUS_H) */