X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=metrics.cpp;h=5079acb2064e5515a1a549af00c35924edcc57dd;hb=96cb6414f85e0ef4d660b7bd56267303e80fcd05;hp=f44ced2c15a66715977b352736122442c9ba4541;hpb=ba675cdb4f3156d9906c72746b535f54a1aff800;p=nageru diff --git a/metrics.cpp b/metrics.cpp index f44ced2..5079acb 100644 --- a/metrics.cpp +++ b/metrics.cpp @@ -63,10 +63,11 @@ void Metrics::add(const string &name, const vector> &labels types[name] = type; } -void Metrics::add(const string &name, const vector> &labels, Histogram *location) +void Metrics::add(const string &name, const vector> &labels, Histogram *location, Laziness laziness) { Metric metric; metric.data_type = DATA_TYPE_HISTOGRAM; + metric.laziness = laziness; metric.location_histogram = location; lock_guard lock(mu); @@ -118,9 +119,15 @@ string Metrics::serialize() const 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 { - ss << metric.location_histogram->serialize(key_and_metric.first.name, key_and_metric.first.labels); + ss << metric.location_histogram->serialize(metric.laziness, key_and_metric.first.name, key_and_metric.first.labels); } } @@ -169,8 +176,22 @@ void Histogram::count_event(double val) sum = sum + val; } -string Histogram::serialize(const string &name, const vector> &labels) const +string Histogram::serialize(Metrics::Laziness laziness, const string &name, const vector> &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);