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.
22 // Prometheus recommends the use of timestamps instead of “time since event”,
23 // so you can use this to get the number of seconds since the epoch.
24 // Note that this will be wrong if your clock changes, so for non-metric use,
25 // you should use std::chrono::steady_clock instead.
26 double get_timestamp_for_metrics();
33 TYPE_HISTOGRAM, // Internal use only.
34 TYPE_SUMMARY, // Internal use only.
41 void set_prefix(const std::string &prefix) // Not thread-safe; must be set before HTTPD starts up.
43 this->prefix = prefix;
46 void add(const std::string &name, std::atomic<int64_t> *location, Type type = TYPE_COUNTER)
48 add(name, {}, location, type);
51 void add(const std::string &name, std::atomic<double> *location, Type type = TYPE_COUNTER)
53 add(name, {}, location, type);
56 void add(const std::string &name, Histogram *location)
58 add(name, {}, location);
61 void add(const std::string &name, Summary *location)
63 add(name, {}, location);
66 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);
67 void add(const std::string &name, const std::vector<std::pair<std::string, std::string>> &labels, std::atomic<double> *location, Type type = TYPE_COUNTER);
68 void add(const std::string &name, const std::vector<std::pair<std::string, std::string>> &labels, Histogram *location, Laziness laziness = PRINT_ALWAYS);
69 void add(const std::string &name, const std::vector<std::pair<std::string, std::string>> &labels, Summary *location, Laziness laziness = PRINT_ALWAYS);
71 void remove(const std::string &name)
76 void remove(const std::string &name, const std::vector<std::pair<std::string, std::string>> &labels);
78 std::string serialize() const;
81 static std::string serialize_name(const std::string &name, const std::vector<std::pair<std::string, std::string>> &labels);
82 static std::string serialize_labels(const std::vector<std::pair<std::string, std::string>> &labels);
91 MetricKey(const std::string &name, const std::vector<std::pair<std::string, std::string>> labels)
92 : name(name), labels(labels), serialized_labels(serialize_labels(labels))
96 bool operator< (const MetricKey &other) const
98 if (name != other.name)
99 return name < other.name;
100 return serialized_labels < other.serialized_labels;
103 const std::string name;
104 const std::vector<std::pair<std::string, std::string>> labels;
105 const std::string serialized_labels;
109 Laziness laziness; // Only for TYPE_HISTOGRAM.
111 std::atomic<int64_t> *location_int64;
112 std::atomic<double> *location_double;
113 Histogram *location_histogram;
114 Summary *location_summary;
118 mutable std::mutex mu;
119 std::map<std::string, Type> types; // Ordered the same as metrics.
120 std::map<MetricKey, Metric> metrics;
121 static std::string prefix;
123 friend class Histogram;
124 friend class Summary;
129 void init(const std::vector<double> &bucket_vals);
130 void init_uniform(size_t num_buckets); // Sets up buckets 0..(N-1).
131 void init_geometric(double min, double max, size_t num_buckets);
132 void count_event(double val);
133 std::string serialize(Metrics::Laziness laziness, const std::string &name, const std::vector<std::pair<std::string, std::string>> &labels) const;
136 // Bucket <i> counts number of events where val[i - 1] < x <= val[i].
137 // The end histogram ends up being made into a cumulative one,
138 // but that's not how we store it here.
141 std::atomic<int64_t> count{0};
143 std::unique_ptr<Bucket[]> buckets;
145 std::atomic<double> sum{0.0};
146 std::atomic<int64_t> count_after_last_bucket{0};
149 // This is a pretty dumb streaming quantile class, but it's exact, and we don't have
150 // too many values (typically one per frame, and one-minute interval), so we don't
151 // need anything fancy.
154 void init(const std::vector<double> &quantiles, double window_seconds);
155 void count_event(double val);
156 std::string serialize(Metrics::Laziness laziness, const std::string &name, const std::vector<std::pair<std::string, std::string>> &labels);
159 std::vector<double> quantiles;
160 std::chrono::duration<double> window;
162 mutable std::mutex mu;
163 std::deque<std::pair<std::chrono::steady_clock::time_point, double>> values;
164 std::atomic<double> sum{0.0};
165 std::atomic<int64_t> count{0};
168 extern Metrics global_metrics;
170 #endif // !defined(_METRICS_H)