]> git.sesse.net Git - nageru/blobdiff - metrics.cpp
Tighten up the Movit #include paths a bit; fixes compilation error in some cases.
[nageru] / metrics.cpp
index db3c4b8c4da97c571f264c0b7f1eb60810d3208b..f44ced2c15a66715977b352736122442c9ba4541 100644 (file)
@@ -19,9 +19,14 @@ double get_timestamp_for_metrics()
 }
 
 string Metrics::serialize_name(const string &name, const vector<pair<string, string>> &labels)
+{
+       return "nageru_" + name + serialize_labels(labels);
+}
+
+string Metrics::serialize_labels(const vector<pair<string, string>> &labels)
 {
        if (labels.empty()) {
-               return "nageru_" + name;
+               return "";
        }
 
        string label_str;
@@ -31,7 +36,7 @@ string Metrics::serialize_name(const string &name, const vector<pair<string, str
                }
                label_str += label.first + "=\"" + label.second + "\"";
        }
-       return "nageru_" + name + "{" + label_str + "}";
+       return "{" + label_str + "}";
 }
 
 void Metrics::add(const string &name, const vector<pair<string, string>> &labels, atomic<int64_t> *location, Metrics::Type type)
@@ -70,6 +75,22 @@ void Metrics::add(const string &name, const vector<pair<string, string>> &labels
        types[name] = TYPE_HISTOGRAM;
 }
 
+void Metrics::remove(const string &name, const vector<pair<string, string>> &labels)
+{
+       lock_guard<mutex> lock(mu);
+
+       auto it = metrics.find(MetricKey(name, labels));
+       assert(it != metrics.end());
+
+       // If this is the last metric with this name, remove the type as well.
+       if (!((it != metrics.begin() && prev(it)->first.name == name) ||
+             (it != metrics.end() && next(it)->first.name == name))) {
+               types.erase(name);
+       }
+
+       metrics.erase(it);
+}
+
 string Metrics::serialize() const
 {
        stringstream ss;
@@ -79,7 +100,7 @@ string Metrics::serialize() const
        lock_guard<mutex> 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() &&