]> git.sesse.net Git - nageru/blobdiff - metrics.h
Update the queue length metric after trimming, not before.
[nageru] / metrics.h
index 40aa8f7bf387159c6b31a40688e9e7b06b1fa37f..d515720ade805b8c4d43eeea23fe23955e7aa819 100644 (file)
--- a/metrics.h
+++ b/metrics.h
@@ -28,6 +28,10 @@ public:
                TYPE_GAUGE,
                TYPE_HISTOGRAM,  // Internal use only.
        };
+       enum Laziness {
+               PRINT_ALWAYS,
+               PRINT_WHEN_NONEMPTY,
+       };
 
        void add(const std::string &name, std::atomic<int64_t> *location, Type type = TYPE_COUNTER)
        {
@@ -46,12 +50,20 @@ public:
 
        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);
        void add(const std::string &name, const std::vector<std::pair<std::string, std::string>> &labels, std::atomic<double> *location, Type type = TYPE_COUNTER);
-       void add(const std::string &name, const std::vector<std::pair<std::string, std::string>> &labels, Histogram *location);
+       void add(const std::string &name, const std::vector<std::pair<std::string, std::string>> &labels, Histogram *location, Laziness laziness = PRINT_ALWAYS);
+
+       void remove(const std::string &name)
+       {
+               remove(name, {});
+       }
+
+       void remove(const std::string &name, const std::vector<std::pair<std::string, std::string>> &labels);
 
        std::string serialize() const;
 
 private:
        static std::string serialize_name(const std::string &name, const std::vector<std::pair<std::string, std::string>> &labels);
+       static std::string serialize_labels(const std::vector<std::pair<std::string, std::string>> &labels);
 
        enum DataType {
                DATA_TYPE_INT64,
@@ -60,21 +72,24 @@ private:
        };
        struct MetricKey {
                MetricKey(const std::string &name, const std::vector<std::pair<std::string, std::string>> labels)
-                       : name(name), labels(labels), serialized(serialize_name(name, labels))
+                       : name(name), labels(labels), serialized_labels(serialize_labels(labels))
                {
                }
 
                bool operator< (const MetricKey &other) const
                {
-                       return serialized < other.serialized;
+                       if (name != other.name)
+                               return name < other.name;
+                       return serialized_labels < other.serialized_labels;
                }
 
                const std::string name;
                const std::vector<std::pair<std::string, std::string>> labels;
-               const std::string serialized;
+               const std::string serialized_labels;
        };
        struct Metric {
                DataType data_type;
+               Laziness laziness;  // Only for TYPE_HISTOGRAM.
                union {
                        std::atomic<int64_t> *location_int64;
                        std::atomic<double> *location_double;
@@ -83,7 +98,7 @@ private:
        };
 
        mutable std::mutex mu;
-       std::map<std::string, Type> types;  // Ordered the same as metrics, assuming no { signs in names (which are illegal).
+       std::map<std::string, Type> types;  // Ordered the same as metrics.
        std::map<MetricKey, Metric> metrics;
        std::vector<Histogram> histograms;
 
@@ -96,7 +111,7 @@ public:
        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<std::pair<std::string, std::string>> &labels) const;
+       std::string serialize(Metrics::Laziness laziness, const std::string &name, const std::vector<std::pair<std::string, std::string>> &labels) const;
 
 private:
        // Bucket <i> counts number of events where val[i - 1] < x <= val[i].