]> git.sesse.net Git - nageru/blob - metrics.h
Mark the appropriate metrics as gauges.
[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 <mutex>
11 #include <string>
12 #include <unordered_map>
13
14 class Metrics {
15 public:
16         enum Type {
17                 TYPE_COUNTER,
18                 TYPE_GAUGE,
19         };
20
21         void register_int_metric(const std::string &name, std::atomic<int64_t> *location, Type type = TYPE_COUNTER);
22         void register_double_metric(const std::string &name, std::atomic<double> *location, Type type = TYPE_COUNTER);
23         std::string serialize() const;
24
25 private:
26         template<class T>
27         struct Metric {
28                 Type type;
29                 std::atomic<T> *location;
30         };
31
32         mutable std::mutex mu;
33         std::unordered_map<std::string, Metric<int64_t>> int_metrics;
34         std::unordered_map<std::string, Metric<double>> double_metrics;
35 };
36
37 extern Metrics global_metrics;
38
39 #endif  // !defined(_METRICS_H)