X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=metrics.h;h=2a19e259cc32bf4df11c1eb779d6339b16382fd9;hb=86950850f835fc611818d361d10a3ddbf6bb4f79;hp=6c72ad53ed71c6bc66b8923a643697d0dea16701;hpb=42cd4144d95cb26e61f7c6ccc4bf4813845dc291;p=nageru diff --git a/metrics.h b/metrics.h index 6c72ad5..2a19e25 100644 --- a/metrics.h +++ b/metrics.h @@ -9,18 +9,62 @@ #include #include #include -#include +#include +#include class Metrics { public: - void register_int_metric(const std::string &name, std::atomic *location); - void register_double_metric(const std::string &name, std::atomic *location); + enum Type { + TYPE_COUNTER, + TYPE_GAUGE, + }; + + 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, 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); + std::string serialize() const; private: + enum DataType { + DATA_TYPE_INT64, + DATA_TYPE_DOUBLE, + }; + + struct Metric { + DataType data_type; + std::string name; + std::vector> labels; + union { + std::atomic *location_int64; + std::atomic *location_double; + }; + }; + + // 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::unordered_map *> int_metrics; - std::unordered_map *> double_metrics; + std::map types; + std::vector metrics; + std::vector histograms; }; extern Metrics global_metrics;