From 1836dccf699779d9092a75755cec96cea1734a2a Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Tue, 13 Jun 2017 23:30:37 +0200 Subject: [PATCH] Pre-serialize only the labels for metrics; the ordering constraints did not really feel too safe. --- metrics.cpp | 11 ++++++++--- metrics.h | 11 +++++++---- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/metrics.cpp b/metrics.cpp index db3c4b8..3fadb72 100644 --- a/metrics.cpp +++ b/metrics.cpp @@ -19,9 +19,14 @@ double get_timestamp_for_metrics() } string Metrics::serialize_name(const string &name, const vector> &labels) +{ + return "nageru_" + name + serialize_labels(labels); +} + +string Metrics::serialize_labels(const vector> &labels) { if (labels.empty()) { - return "nageru_" + name; + return ""; } string label_str; @@ -31,7 +36,7 @@ string Metrics::serialize_name(const string &name, const vector> &labels, atomic *location, Metrics::Type type) @@ -79,7 +84,7 @@ string Metrics::serialize() const lock_guard lock(mu); auto type_it = types.cbegin(); for (const auto &key_and_metric : metrics) { - const string &name = key_and_metric.first.serialized; + string name = "nageru_" + key_and_metric.first.name + key_and_metric.first.serialized_labels; const Metric &metric = key_and_metric.second; if (type_it != types.cend() && diff --git a/metrics.h b/metrics.h index 40aa8f7..0371668 100644 --- a/metrics.h +++ b/metrics.h @@ -52,6 +52,7 @@ public: private: 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, @@ -60,18 +61,20 @@ private: }; struct MetricKey { MetricKey(const std::string &name, const std::vector> 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> 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 types; // Ordered the same as metrics, assuming no { signs in names (which are illegal). + std::map types; // Ordered the same as metrics. std::map metrics; std::vector histograms; -- 2.39.2