]> git.ipfire.org Git - thirdparty/tor.git/commitdiff
metrics: Print once the Promtheus comments
authorDavid Goulet <dgoulet@torproject.org>
Thu, 6 May 2021 14:54:50 +0000 (10:54 -0400)
committerDavid Goulet <dgoulet@torproject.org>
Wed, 12 May 2021 15:58:25 +0000 (11:58 -0400)
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>
src/lib/metrics/metrics_store.c
src/lib/metrics/prometheus.c
src/lib/metrics/prometheus.h

index 4cab5245f31474728eeba233f9d1c8c36a4d1668..1a520f49212ba7d8fe775e6d25d806931d8e90c4 100644 (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;
 }
index 65241ed6c11646923c70063ea9c93902c2de7cea..aac23ac92e4ad2a6e29f36a9052c07ee2d43c8cb 100644 (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));
index 19770e7911c05a09103af9e537f8d74f6e4a15c0..faa7681daad7f60f285c076b13004ee09c208e87 100644 (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) */