[tor-commits] [tor/master] metrics: Print once the Promtheus comments

dgoulet at torproject.org dgoulet at torproject.org
Wed May 12 16:03:05 UTC 2021


commit cc2947c0070faac0805478381f13956bb2775621
Author: David Goulet <dgoulet at torproject.org>
Date:   Thu May 6 10:54:50 2021 -0400

    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 at torproject.org>
---
 src/lib/metrics/metrics_store.c | 10 ++++++++--
 src/lib/metrics/prometheus.c    | 11 +++++++----
 src/lib/metrics/prometheus.h    |  2 +-
 3 files changed, 16 insertions(+), 7 deletions(-)

diff --git a/src/lib/metrics/metrics_store.c b/src/lib/metrics/metrics_store.c
index 4cab5245f3..1a520f4921 100644
--- a/src/lib/metrics/metrics_store.c
+++ b/src/lib/metrics/metrics_store.c
@@ -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;
 }
diff --git a/src/lib/metrics/prometheus.c b/src/lib/metrics/prometheus.c
index 65241ed6c1..aac23ac92e 100644
--- a/src/lib/metrics/prometheus.c
+++ b/src/lib/metrics/prometheus.c
@@ -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));
diff --git a/src/lib/metrics/prometheus.h b/src/lib/metrics/prometheus.h
index 19770e7911..faa7681daa 100644
--- a/src/lib/metrics/prometheus.h
+++ b/src/lib/metrics/prometheus.h
@@ -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) */





More information about the tor-commits mailing list