]> git.sesse.net Git - nageru/blob - metrics.h
Add support for bus audio level metrics, which includes support for removing metrics.
[nageru] / metrics.h
1 #ifndef _METRICS_H
2 #define _METRICS_H 1
3
4 // A simple global class to keep track of metrics export in Prometheus format.
5 // It would be better to use a more full-featured Prometheus client library for this,
6 // but it would introduce a dependency that is not commonly packaged in distributions,
7 // which makes it quite unwieldy. Thus, we'll package our own for the time being.
8
9 #include <atomic>
10 #include <map>
11 #include <memory>
12 #include <mutex>
13 #include <string>
14 #include <vector>
15
16 class Histogram;
17
18 // Prometheus recommends the use of timestamps instead of “time since event”,
19 // so you can use this to get the number of seconds since the epoch.
20 // Note that this will be wrong if your clock changes, so for non-metric use,
21 // you should use std::chrono::steady_clock instead.
22 double get_timestamp_for_metrics();
23
24 class Metrics {
25 public:
26         enum Type {
27                 TYPE_COUNTER,
28                 TYPE_GAUGE,
29                 TYPE_HISTOGRAM,  // Internal use only.
30         };
31
32         void add(const std::string &name, std::atomic<int64_t> *location, Type type = TYPE_COUNTER)
33         {
34                 add(name, {}, location, type);
35         }
36
37         void add(const std::string &name, std::atomic<double> *location, Type type = TYPE_COUNTER)
38         {
39                 add(name, {}, location, type);
40         }
41
42         void add(const std::string &name, Histogram *location)
43         {
44                 add(name, {}, location);
45         }
46
47         void add(const std::string &name, const std::vector<std::pair<std::string, std::string>> &labels, std::atomic<int64_t> *location, Type type = TYPE_COUNTER);
48         void add(const std::string &name, const std::vector<std::pair<std::string, std::string>> &labels, std::atomic<double> *location, Type type = TYPE_COUNTER);
49         void add(const std::string &name, const std::vector<std::pair<std::string, std::string>> &labels, Histogram *location);
50
51         void remove(const std::string &name)
52         {
53                 remove(name, {});
54         }
55
56         void remove(const std::string &name, const std::vector<std::pair<std::string, std::string>> &labels);
57
58         std::string serialize() const;
59
60 private:
61         static std::string serialize_name(const std::string &name, const std::vector<std::pair<std::string, std::string>> &labels);
62         static std::string serialize_labels(const std::vector<std::pair<std::string, std::string>> &labels);
63
64         enum DataType {
65                 DATA_TYPE_INT64,
66                 DATA_TYPE_DOUBLE,
67                 DATA_TYPE_HISTOGRAM,
68         };
69         struct MetricKey {
70                 MetricKey(const std::string &name, const std::vector<std::pair<std::string, std::string>> labels)
71                         : name(name), labels(labels), serialized_labels(serialize_labels(labels))
72                 {
73                 }
74
75                 bool operator< (const MetricKey &other) const
76                 {
77                         if (name != other.name)
78                                 return name < other.name;
79                         return serialized_labels < other.serialized_labels;
80                 }
81
82                 const std::string name;
83                 const std::vector<std::pair<std::string, std::string>> labels;
84                 const std::string serialized_labels;
85         };
86         struct Metric {
87                 DataType data_type;
88                 union {
89                         std::atomic<int64_t> *location_int64;
90                         std::atomic<double> *location_double;
91                         Histogram *location_histogram;
92                 };
93         };
94
95         mutable std::mutex mu;
96         std::map<std::string, Type> types;  // Ordered the same as metrics.
97         std::map<MetricKey, Metric> metrics;
98         std::vector<Histogram> histograms;
99
100         friend class Histogram;
101 };
102
103 class Histogram {
104 public:
105         void init(const std::vector<double> &bucket_vals);
106         void init_uniform(size_t num_buckets);  // Sets up buckets 0..(N-1).
107         void init_geometric(double min, double max, size_t num_buckets);
108         void count_event(double val);
109         std::string serialize(const std::string &name, const std::vector<std::pair<std::string, std::string>> &labels) const;
110
111 private:
112         // Bucket <i> counts number of events where val[i - 1] < x <= val[i].
113         // The end histogram ends up being made into a cumulative one,
114         // but that's not how we store it here.
115         struct Bucket {
116                 double val;
117                 std::atomic<int64_t> count{0};
118         };
119         std::unique_ptr<Bucket[]> buckets;
120         size_t num_buckets;
121         std::atomic<double> sum{0.0};
122         std::atomic<int64_t> count_after_last_bucket{0};
123 };
124
125 extern Metrics global_metrics;
126
127 #endif  // !defined(_METRICS_H)