X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=metrics.h;h=0371668bd2a1e25c1adaed613071ac8e3c6c0551;hb=1836dccf699779d9092a75755cec96cea1734a2a;hp=3991d2084b48b9d590e6661c12e2e366699369ca;hpb=58bcd6473f3153a54672a6af98d56e02d558ceb6;p=nageru diff --git a/metrics.h b/metrics.h index 3991d20..0371668 100644 --- a/metrics.h +++ b/metrics.h @@ -7,31 +7,112 @@ // which makes it quite unwieldy. Thus, we'll package our own for the time being. #include +#include +#include #include #include -#include +#include + +class Histogram; + +// Prometheus recommends the use of timestamps instead of “time since event”, +// so you can use this to get the number of seconds since the epoch. +// Note that this will be wrong if your clock changes, so for non-metric use, +// you should use std::chrono::steady_clock instead. +double get_timestamp_for_metrics(); class Metrics { public: enum Type { TYPE_COUNTER, TYPE_GAUGE, + TYPE_HISTOGRAM, // Internal use only. }; - void add(const std::string &name, std::atomic *location, Type type = TYPE_COUNTER); - void add(const std::string &name, std::atomic *location, Type type = TYPE_COUNTER); + void add(const std::string &name, std::atomic *location, Type type = TYPE_COUNTER) + { + add(name, {}, location, type); + } + + void add(const std::string &name, std::atomic *location, Type type = TYPE_COUNTER) + { + add(name, {}, location, type); + } + + void add(const std::string &name, Histogram *location) + { + add(name, {}, location); + } + + void add(const std::string &name, const std::vector> &labels, std::atomic *location, Type type = TYPE_COUNTER); + void add(const std::string &name, const std::vector> &labels, std::atomic *location, Type type = TYPE_COUNTER); + void add(const std::string &name, const std::vector> &labels, Histogram *location); + std::string serialize() const; private: - template + static std::string serialize_name(const std::string &name, const std::vector> &labels); + static std::string serialize_labels(const std::vector> &labels); + + enum DataType { + DATA_TYPE_INT64, + DATA_TYPE_DOUBLE, + DATA_TYPE_HISTOGRAM, + }; + struct MetricKey { + MetricKey(const std::string &name, const std::vector> labels) + : name(name), labels(labels), serialized_labels(serialize_labels(labels)) + { + } + + bool operator< (const MetricKey &other) const + { + if (name != other.name) + return name < other.name; + return serialized_labels < other.serialized_labels; + } + + const std::string name; + const std::vector> labels; + const std::string serialized_labels; + }; struct Metric { - Type type; - std::atomic *location; + DataType data_type; + union { + std::atomic *location_int64; + std::atomic *location_double; + Histogram *location_histogram; + }; }; mutable std::mutex mu; - std::unordered_map> int_metrics; - std::unordered_map> double_metrics; + std::map types; // Ordered the same as metrics. + std::map metrics; + std::vector histograms; + + friend class Histogram; +}; + +class Histogram { +public: + void init(const std::vector &bucket_vals); + void init_uniform(size_t num_buckets); // Sets up buckets 0..(N-1). + void init_geometric(double min, double max, size_t num_buckets); + void count_event(double val); + std::string serialize(const std::string &name, const std::vector> &labels) const; + +private: + // Bucket counts number of events where val[i - 1] < x <= val[i]. + // The end histogram ends up being made into a cumulative one, + // but that's not how we store it here. + struct Bucket { + double val; + std::atomic count{0}; + }; + std::unique_ptr buckets; + size_t num_buckets; + std::atomic sum{0.0}; + std::atomic count_after_last_bucket{0}; }; extern Metrics global_metrics;