aboutsummaryrefslogtreecommitdiff
path: root/src/lib/metrics/prometheus.c
blob: aac23ac92e4ad2a6e29f36a9052c07ee2d43c8cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/* Copyright (c) 2020-2021, 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,
                              bool no_comment)
{
  tor_assert(entry);
  tor_assert(data);

  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));
}