]> git.sesse.net Git - nageru/blobdiff - metrics.cpp
Add the GPU memory metrics to the Grafana dashboard.
[nageru] / metrics.cpp
index fd5e54c1c7f48b09d38f64ddd91d84bee4a3b6c0..86c3d591872a4a38d53d34a55216b66e4d6d1405 100644 (file)
@@ -18,12 +18,15 @@ double get_timestamp_for_metrics()
        return duration<double>(system_clock::now().time_since_epoch()).count();
 }
 
-namespace {
+string Metrics::serialize_name(const string &name, const vector<pair<string, string>> &labels)
+{
+       return "nageru_" + name + serialize_labels(labels);
+}
 
-string serialize_name(const string &name, const vector<pair<string, string>> &labels)
+string Metrics::serialize_labels(const vector<pair<string, string>> &labels)
 {
        if (labels.empty()) {
-               return "nageru_" + name;
+               return "";
        }
 
        string label_str;
@@ -33,21 +36,17 @@ string serialize_name(const string &name, const vector<pair<string, string>> &la
                }
                label_str += label.first + "=\"" + label.second + "\"";
        }
-       return "nageru_" + name + "{" + label_str + "}";
+       return "{" + label_str + "}";
 }
 
-}  // namespace
-
 void Metrics::add(const string &name, const vector<pair<string, string>> &labels, atomic<int64_t> *location, Metrics::Type type)
 {
        Metric metric;
        metric.data_type = DATA_TYPE_INT64;
-       metric.name = name;
-       metric.labels = labels;
        metric.location_int64 = location;
 
        lock_guard<mutex> lock(mu);
-       metrics.push_back(metric);
+       metrics.emplace(MetricKey(name, labels), metric);
        assert(types.count(name) == 0 || types[name] == type);
        types[name] = type;
 }
@@ -56,30 +55,56 @@ void Metrics::add(const string &name, const vector<pair<string, string>> &labels
 {
        Metric metric;
        metric.data_type = DATA_TYPE_DOUBLE;
-       metric.name = name;
-       metric.labels = labels;
        metric.location_double = location;
 
        lock_guard<mutex> lock(mu);
-       metrics.push_back(metric);
+       metrics.emplace(MetricKey(name, labels), metric);
        assert(types.count(name) == 0 || types[name] == type);
        types[name] = type;
 }
 
-void Metrics::add(const string &name, const vector<pair<string, string>> &labels, Histogram *location)
+void Metrics::add(const string &name, const vector<pair<string, string>> &labels, Histogram *location, Laziness laziness)
 {
        Metric metric;
        metric.data_type = DATA_TYPE_HISTOGRAM;
-       metric.name = name;
-       metric.labels = labels;
+       metric.laziness = laziness;
        metric.location_histogram = location;
 
        lock_guard<mutex> lock(mu);
-       metrics.push_back(metric);
+       metrics.emplace(MetricKey(name, labels), metric);
        assert(types.count(name) == 0 || types[name] == TYPE_HISTOGRAM);
        types[name] = TYPE_HISTOGRAM;
 }
 
+void Metrics::add(const string &name, const vector<pair<string, string>> &labels, Summary *location, Laziness laziness)
+{
+       Metric metric;
+       metric.data_type = DATA_TYPE_SUMMARY;
+       metric.laziness = laziness;
+       metric.location_summary = location;
+
+       lock_guard<mutex> lock(mu);
+       metrics.emplace(MetricKey(name, labels), metric);
+       assert(types.count(name) == 0 || types[name] == TYPE_SUMMARY);
+       types[name] = TYPE_SUMMARY;
+}
+
+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;
@@ -87,22 +112,39 @@ string Metrics::serialize() const
        ss.precision(20);
 
        lock_guard<mutex> lock(mu);
-       for (const auto &name_and_type : types) {
-               if (name_and_type.second == TYPE_GAUGE) {
-                       ss << "# TYPE nageru_" << name_and_type.first << " gauge\n";
-               } else if (name_and_type.second == TYPE_HISTOGRAM) {
-                       ss << "# TYPE nageru_" << name_and_type.first << " histogram\n";
+       auto type_it = types.cbegin();
+       for (const auto &key_and_metric : metrics) {
+               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() &&
+                   key_and_metric.first.name == type_it->first) {
+                       // It's the first time we print out any metric with this name,
+                       // so add the type header.
+                       if (type_it->second == TYPE_GAUGE) {
+                               ss << "# TYPE nageru_" << type_it->first << " gauge\n";
+                       } else if (type_it->second == TYPE_HISTOGRAM) {
+                               ss << "# TYPE nageru_" << type_it->first << " histogram\n";
+                       } else if (type_it->second == TYPE_SUMMARY) {
+                               ss << "# TYPE nageru_" << type_it->first << " summary\n";
+                       }
+                       ++type_it;
                }
-       }
-       for (const Metric &metric : metrics) {
-               string name = serialize_name(metric.name, metric.labels);
 
                if (metric.data_type == DATA_TYPE_INT64) {
                        ss << name << " " << metric.location_int64->load() << "\n";
                } else if (metric.data_type == DATA_TYPE_DOUBLE) {
-                       ss << name << " " << metric.location_double->load() << "\n";
+                       double val = metric.location_double->load();
+                       if (isnan(val)) {
+                               // Prometheus can't handle “-nan”.
+                               ss << name << " NaN\n";
+                       } else {
+                               ss << name << " " << val << "\n";
+                       }
+               } else if (metric.data_type == DATA_TYPE_HISTOGRAM) {
+                       ss << metric.location_histogram->serialize(metric.laziness, key_and_metric.first.name, key_and_metric.first.labels);
                } else {
-                       ss << metric.location_histogram->serialize(metric.name, metric.labels);
+                       ss << metric.location_summary->serialize(metric.laziness, key_and_metric.first.name, key_and_metric.first.labels);
                }
        }
 
@@ -151,8 +193,22 @@ void Histogram::count_event(double val)
        sum = sum + val;
 }
 
-string Histogram::serialize(const string &name, const vector<pair<string, string>> &labels) const
+string Histogram::serialize(Metrics::Laziness laziness, const string &name, const vector<pair<string, string>> &labels) const
 {
+       // Check if the histogram is empty and should not be serialized.
+       if (laziness == Metrics::PRINT_WHEN_NONEMPTY && count_after_last_bucket.load() == 0) {
+               bool empty = true;
+               for (size_t bucket_idx = 0; bucket_idx < num_buckets; ++bucket_idx) {
+                       if (buckets[bucket_idx].count.load() != 0) {
+                               empty = false;
+                               break;
+                       }
+               }
+               if (empty) {
+                       return "";
+               }
+       }
+
        stringstream ss;
        ss.imbue(locale("C"));
        ss.precision(20);
@@ -167,13 +223,110 @@ string Histogram::serialize(const string &name, const vector<pair<string, string
                bucket_labels.emplace_back("le", le_ss.str());
 
                count += buckets[bucket_idx].count.load();
-               ss << serialize_name(name + "_bucket", bucket_labels) << " " << count << "\n";
+               ss << Metrics::serialize_name(name + "_bucket", bucket_labels) << " " << count << "\n";
        }
 
        count += count_after_last_bucket.load();
 
-       ss << serialize_name(name + "_sum", labels) << " " << sum.load() << "\n";
-       ss << serialize_name(name + "_count", labels) << " " << count << "\n";
+       ss << Metrics::serialize_name(name + "_sum", labels) << " " << sum.load() << "\n";
+       ss << Metrics::serialize_name(name + "_count", labels) << " " << count << "\n";
+
+       return ss.str();
+}
+
+void Summary::init(const vector<double> &quantiles, double window_seconds)
+{
+       this->quantiles = quantiles;
+       window = duration<double>(window_seconds);
+}
+
+void Summary::count_event(double val)
+{
+       steady_clock::time_point now = steady_clock::now();
+       steady_clock::time_point cutoff = now - duration_cast<steady_clock::duration>(window);
+
+       lock_guard<mutex> lock(mu);
+       values.emplace_back(now, val);
+       while (!values.empty() && values.front().first < cutoff) {
+               values.pop_front();
+       }
+
+       // Non-atomic add, but that's fine, since there are no concurrent writers.
+       sum = sum + val;
+       ++count;
+}
+
+string Summary::serialize(Metrics::Laziness laziness, const string &name, const vector<pair<string, string>> &labels)
+{
+       steady_clock::time_point now = steady_clock::now();
+       steady_clock::time_point cutoff = now - duration_cast<steady_clock::duration>(window);
+
+       vector<double> values_copy;
+       {
+               lock_guard<mutex> lock(mu);
+               while (!values.empty() && values.front().first < cutoff) {
+                       values.pop_front();
+               }
+               values_copy.reserve(values.size());
+               for (const auto &time_and_value : values) {
+                       values_copy.push_back(time_and_value.second);
+               }
+       }
+
+       vector<pair<double, double>> answers;
+       if (values_copy.size() == 0) {
+               if (laziness == Metrics::PRINT_WHEN_NONEMPTY) {
+                       return "";
+               }
+               for (double quantile : quantiles) {
+                       answers.emplace_back(quantile, 0.0 / 0.0);
+               }
+       } else if (values_copy.size() == 1) {
+               for (double quantile : quantiles) {
+                       answers.emplace_back(quantile, values_copy[0]);
+               }
+       } else {
+               // We could probably do repeated nth_element, but the constant factor
+               // gets a bit high, so just sorting probably is about as fast.
+               sort(values_copy.begin(), values_copy.end());
+               for (double quantile : quantiles) {
+                       double idx = quantile * (values_copy.size() - 1);
+                       size_t idx_floor = size_t(floor(idx));
+                       const double v0 = values_copy[idx_floor];
+
+                       if (idx_floor == values_copy.size() - 1) {
+                               answers.emplace_back(quantile, values_copy[idx_floor]);
+                       } else {
+                               // Linear interpolation.
+                               double t = idx - idx_floor;
+                               const double v1 = values_copy[idx_floor + 1];
+                               answers.emplace_back(quantile, v0 + t * (v1 - v0));
+                       }
+               }
+       }
+
+       stringstream ss;
+       ss.imbue(locale("C"));
+       ss.precision(20);
+
+       for (const auto &quantile_and_value : answers) {
+               stringstream quantile_ss;
+               quantile_ss.imbue(locale("C"));
+               quantile_ss.precision(3);
+               quantile_ss << quantile_and_value.first;
+               vector<pair<string, string>> quantile_labels = labels;
+               quantile_labels.emplace_back("quantile", quantile_ss.str());
+
+               double val = quantile_and_value.second;;
+               if (isnan(val)) {
+                       // Prometheus can't handle “-nan”.
+                       ss << Metrics::serialize_name(name, quantile_labels) << " NaN\n";
+               } else {
+                       ss << Metrics::serialize_name(name, quantile_labels) << " " << val << "\n";
+               }
+       }
 
+       ss << Metrics::serialize_name(name + "_sum", labels) << " " << sum.load() << "\n";
+       ss << Metrics::serialize_name(name + "_count", labels) << " " << count.load() << "\n";
        return ss.str();
 }