aboutsummaryrefslogtreecommitdiff
path: root/src/feature/metrics/metrics.c
blob: 5f6fe776b72b05537ec7c79850a510351c7db0ba (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
60
61
62
63
64
65
66
/* Copyright (c) 2007-2020, The Tor Project, Inc. */
/* See LICENSE for licensing information */

/**
 * @file metrics.c
 * @brief Metrics subsystem.
 **/

#include "orconfig.h"

#include "lib/container/smartlist.h"
#include "lib/log/util_bug.h"
#include "lib/malloc/malloc.h"
#include "lib/metrics/metrics_store.h"
#include "lib/string/printf.h"

#include "feature/metrics/metrics.h"

#include "app/main/subsysmgr.h"

/** Return newly allocated string containing the output of all subsystems
 * having metrics.
 *
 * This is used to output the content on the MetricsPort. */
char *
metrics_get_output(const metrics_format_t fmt)
{
  char *data;
  smartlist_t *chunks = smartlist_new();

  /* Go over all subsystems that exposes a metrics store. */
  for (unsigned i = 0; i < n_tor_subsystems; ++i) {
    const smartlist_t *stores;
    const subsys_fns_t *sys = tor_subsystems[i];

    /* Skip unsupported subsystems. */
    if (!sys->supported) {
      continue;
    }

    if (sys->get_metrics && (stores = sys->get_metrics())) {
      SMARTLIST_FOREACH_BEGIN(stores, const metrics_store_t *, store) {
        smartlist_add(chunks, metrics_store_get_output(fmt, store));
      } SMARTLIST_FOREACH_END(store);
    }
  }

  data = smartlist_join_strings(chunks, "\n", 0, NULL);

  SMARTLIST_FOREACH(chunks, char *, c, tor_free(c));
  smartlist_free(chunks);

  return data;
}

/** Initialize the subsystem. */
void
metrics_init(void)
{
}

/** Cleanup and free any global memory of this subsystem. */
void
metrics_cleanup(void)
{
}