LCOV - code coverage report
Current view: top level - lib/metrics - prometheus.c (source / functions) Hit Total Coverage
Test: lcov.info Lines: 21 21 100.0 %
Date: 2021-11-24 03:28:48 Functions: 2 2 100.0 %

          Line data    Source code
       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             : 
      13             : #include "lib/container/smartlist.h"
      14             : #include "lib/log/util_bug.h"
      15             : #include "lib/malloc/malloc.h"
      16             : #include "lib/string/printf.h"
      17             : 
      18             : #include "lib/metrics/prometheus.h"
      19             : 
      20             : /** Return a static buffer containing all the labels properly formatted
      21             :  * for the output as a string.
      22             :  *
      23             :  * Subsequent calls to this invalidates the previous result. */
      24             : static const char *
      25          55 : format_labels(smartlist_t *labels)
      26             : {
      27          55 :   static char buf[1024];
      28          55 :   char *line = NULL;
      29             : 
      30          55 :   if (smartlist_len(labels) == 0) {
      31           2 :     buf[0] = '\0';
      32           2 :     goto end;
      33             :   }
      34             : 
      35          53 :   line = smartlist_join_strings(labels, ",", 0, NULL);
      36          53 :   tor_snprintf(buf, sizeof(buf), "{%s}", line);
      37             : 
      38          55 :  end:
      39          55 :   tor_free(line);
      40          55 :   return buf;
      41             : }
      42             : 
      43             : /** Format the given entry in to the buffer data. */
      44             : void
      45          55 : prometheus_format_store_entry(const metrics_store_entry_t *entry, buf_t *data,
      46             :                               bool no_comment)
      47             : {
      48          55 :   tor_assert(entry);
      49          55 :   tor_assert(data);
      50             : 
      51          55 :   if (!no_comment) {
      52           8 :     buf_add_printf(data, "# HELP %s %s\n", entry->name, entry->help);
      53           8 :     buf_add_printf(data, "# TYPE %s %s\n", entry->name,
      54           8 :                    metrics_type_to_str(entry->type));
      55             :   }
      56         110 :   buf_add_printf(data, "%s%s %" PRIi64 "\n", entry->name,
      57          55 :                  format_labels(entry->labels),
      58             :                  metrics_store_entry_get_value(entry));
      59          55 : }

Generated by: LCOV version 1.14