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.
23 TYPE_HISTOGRAM, // Internal use only.
26 void add(const std::string &name, std::atomic<int64_t> *location, Type type = TYPE_COUNTER)
28 add(name, {}, location, type);
31 void add(const std::string &name, std::atomic<double> *location, Type type = TYPE_COUNTER)
33 add(name, {}, location, type);
36 void add(const std::string &name, Histogram *location)
38 add(name, {}, location);
41 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);
42 void add(const std::string &name, const std::vector<std::pair<std::string, std::string>> &labels, std::atomic<double> *location, Type type = TYPE_COUNTER);
43 void add(const std::string &name, const std::vector<std::pair<std::string, std::string>> &labels, Histogram *location);
45 std::string serialize() const;
57 std::vector<std::pair<std::string, std::string>> labels;
59 std::atomic<int64_t> *location_int64;
60 std::atomic<double> *location_double;
61 Histogram *location_histogram;
65 mutable std::mutex mu;
66 std::map<std::string, Type> types;
67 std::vector<Metric> metrics;
68 std::vector<Histogram> histograms;
73 void init(const std::vector<double> &bucket_vals);
74 void init_uniform(size_t num_buckets); // Sets up buckets 0..(N-1).
75 void init_geometric(double min, double max, size_t num_buckets);
76 void count_event(double val);
77 std::string serialize(const std::string &name, const std::vector<std::pair<std::string, std::string>> &labels) const;
80 // Bucket <i> counts number of events where val[i - 1] < x <= val[i].
81 // The end histogram ends up being made into a cumulative one,
82 // but that's not how we store it here.
85 std::atomic<int64_t> count{0};
87 std::unique_ptr<Bucket[]> buckets;
89 std::atomic<double> sum{0.0};
90 std::atomic<int64_t> count_after_last_bucket{0};
93 extern Metrics global_metrics;
95 #endif // !defined(_METRICS_H)