]> git.sesse.net Git - nageru/blob - metrics.h
0371668bd2a1e25c1adaed613071ac8e3c6c0551
[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         std::string serialize() const;
52
53 private:
54         static std::string serialize_name(const std::string &name, const std::vector<std::pair<std::string, std::string>> &labels);
55         static std::string serialize_labels(const std::vector<std::pair<std::string, std::string>> &labels);
56
57         enum DataType {
58                 DATA_TYPE_INT64,
59                 DATA_TYPE_DOUBLE,
60                 DATA_TYPE_HISTOGRAM,
61         };
62         struct MetricKey {
63                 MetricKey(const std::string &name, const std::vector<std::pair<std::string, std::string>> labels)
64                         : name(name), labels(labels), serialized_labels(serialize_labels(labels))
65                 {
66                 }
67
68                 bool operator< (const MetricKey &other) const
69                 {
70                         if (name != other.name)
71                                 return name < other.name;
72                         return serialized_labels < other.serialized_labels;
73                 }
74
75                 const std::string name;
76                 const std::vector<std::pair<std::string, std::string>> labels;
77                 const std::string serialized_labels;
78         };
79         struct Metric {
80                 DataType data_type;
81                 union {
82                         std::atomic<int64_t> *location_int64;
83                         std::atomic<double> *location_double;
84                         Histogram *location_histogram;
85                 };
86         };
87
88         mutable std::mutex mu;
89         std::map<std::string, Type> types;  // Ordered the same as metrics.
90         std::map<MetricKey, Metric> metrics;
91         std::vector<Histogram> histograms;
92
93         friend class Histogram;
94 };
95
96 class Histogram {
97 public:
98         void init(const std::vector<double> &bucket_vals);
99         void init_uniform(size_t num_buckets);  // Sets up buckets 0..(N-1).
100         void init_geometric(double min, double max, size_t num_buckets);
101         void count_event(double val);
102         std::string serialize(const std::string &name, const std::vector<std::pair<std::string, std::string>> &labels) const;
103
104 private:
105         // Bucket <i> counts number of events where val[i - 1] < x <= val[i].
106         // The end histogram ends up being made into a cumulative one,
107         // but that's not how we store it here.
108         struct Bucket {
109                 double val;
110                 std::atomic<int64_t> count{0};
111         };
112         std::unique_ptr<Bucket[]> buckets;
113         size_t num_buckets;
114         std::atomic<double> sum{0.0};
115         std::atomic<int64_t> count_after_last_bucket{0};
116 };
117
118 extern Metrics global_metrics;
119
120 #endif  // !defined(_METRICS_H)