]> git.sesse.net Git - nageru/blobdiff - metrics.h
Pre-serialize only the labels for metrics; the ordering constraints did not really...
[nageru] / metrics.h
index 40aa8f7bf387159c6b31a40688e9e7b06b1fa37f..0371668bd2a1e25c1adaed613071ac8e3c6c0551 100644 (file)
--- a/metrics.h
+++ b/metrics.h
@@ -52,6 +52,7 @@ public:
 
 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,18 +61,20 @@ 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;
@@ -83,7 +86,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;