]> git.sesse.net Git - nageru/blob - metrics.h
Add the first beginnings of Prometheus 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 <mutex>
11 #include <string>
12 #include <unordered_map>
13
14 class Metrics {
15 public:
16         void register_int_metric(const std::string &name, std::atomic<int64_t> *location);
17         void register_double_metric(const std::string &name, std::atomic<double> *location);
18         std::string serialize() const;
19
20 private:
21         mutable std::mutex mu;
22         std::unordered_map<std::string, std::atomic<int64_t> *> int_metrics;
23         std::unordered_map<std::string, std::atomic<double> *> double_metrics;
24 };
25
26 extern Metrics global_metrics;
27
28 #endif  // !defined(_METRICS_H)