X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=metrics.h;h=a8156587a5f2484da831eda01af26cc0c4742454;hb=1df7849fad1d0647a02951abaa60f9e4f40ce360;hp=2a19e259cc32bf4df11c1eb779d6339b16382fd9;hpb=4194f21b1a5e4ccfe79d40024075ab9795814029;p=nageru diff --git a/metrics.h b/metrics.h index 2a19e25..a815658 100644 --- a/metrics.h +++ b/metrics.h @@ -7,16 +7,20 @@ // which makes it quite unwieldy. Thus, we'll package our own for the time being. #include +#include +#include #include #include -#include #include +class Histogram; + 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) @@ -29,11 +33,14 @@ public: 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); - - // Only integer histogram, ie. keys are 0..(N-1). - void add_histogram(const std::string &name, const std::vector> &labels, std::atomic *location, size_t num_elements); + void add(const std::string &name, const std::vector> &labels, Histogram *location); std::string serialize() const; @@ -41,6 +48,7 @@ private: enum DataType { DATA_TYPE_INT64, DATA_TYPE_DOUBLE, + DATA_TYPE_HISTOGRAM, }; struct Metric { @@ -50,23 +58,38 @@ private: union { std::atomic *location_int64; std::atomic *location_double; + Histogram *location_histogram; }; }; - // TODO: This needs to be more general. - struct Histogram { - std::string name; - std::vector> labels; - std::atomic *location_int64; // First bucket. - size_t num_elements; - }; - mutable std::mutex mu; std::map types; std::vector metrics; std::vector histograms; }; +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; #endif // !defined(_METRICS_H)