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.
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();
29 TYPE_HISTOGRAM, // Internal use only.
36 void add(const std::string &name, std::atomic<int64_t> *location, Type type = TYPE_COUNTER)
38 add(name, {}, location, type);
41 void add(const std::string &name, std::atomic<double> *location, Type type = TYPE_COUNTER)
43 add(name, {}, location, type);
46 void add(const std::string &name, Histogram *location)
48 add(name, {}, location);
51 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);
52 void add(const std::string &name, const std::vector<std::pair<std::string, std::string>> &labels, std::atomic<double> *location, Type type = TYPE_COUNTER);
53 void add(const std::string &name, const std::vector<std::pair<std::string, std::string>> &labels, Histogram *location, Laziness laziness = PRINT_ALWAYS);
55 void remove(const std::string &name)
60 void remove(const std::string &name, const std::vector<std::pair<std::string, std::string>> &labels);
62 std::string serialize() const;
65 static std::string serialize_name(const std::string &name, const std::vector<std::pair<std::string, std::string>> &labels);
66 static std::string serialize_labels(const std::vector<std::pair<std::string, std::string>> &labels);
74 MetricKey(const std::string &name, const std::vector<std::pair<std::string, std::string>> labels)
75 : name(name), labels(labels), serialized_labels(serialize_labels(labels))
79 bool operator< (const MetricKey &other) const
81 if (name != other.name)
82 return name < other.name;
83 return serialized_labels < other.serialized_labels;
86 const std::string name;
87 const std::vector<std::pair<std::string, std::string>> labels;
88 const std::string serialized_labels;
92 Laziness laziness; // Only for TYPE_HISTOGRAM.
94 std::atomic<int64_t> *location_int64;
95 std::atomic<double> *location_double;
96 Histogram *location_histogram;
100 mutable std::mutex mu;
101 std::map<std::string, Type> types; // Ordered the same as metrics.
102 std::map<MetricKey, Metric> metrics;
103 std::vector<Histogram> histograms;
105 friend class Histogram;
110 void init(const std::vector<double> &bucket_vals);
111 void init_uniform(size_t num_buckets); // Sets up buckets 0..(N-1).
112 void init_geometric(double min, double max, size_t num_buckets);
113 void count_event(double val);
114 std::string serialize(Metrics::Laziness laziness, const std::string &name, const std::vector<std::pair<std::string, std::string>> &labels) const;
117 // Bucket <i> counts number of events where val[i - 1] < x <= val[i].
118 // The end histogram ends up being made into a cumulative one,
119 // but that's not how we store it here.
122 std::atomic<int64_t> count{0};
124 std::unique_ptr<Bucket[]> buckets;
126 std::atomic<double> sum{0.0};
127 std::atomic<int64_t> count_after_last_bucket{0};
130 extern Metrics global_metrics;
132 #endif // !defined(_METRICS_H)