From ccdd278d888a7a954ccd2ffe1d2664b44130963c Mon Sep 17 00:00:00 2001 From: Amnon Heiman Date: Thu, 23 Jun 2022 14:27:11 +0300 Subject: [PATCH 1/4] metrics.hh: Support SUMMARY type This patch add support for the summary type on the metrics layer. A summary is a different kind of histogram, it's buckets are percentile so the reporting layer (i.e. Prometheus for example) would know to report it correctly. Signed-off-by: Amnon Heiman --- include/seastar/core/metrics.hh | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/include/seastar/core/metrics.hh b/include/seastar/core/metrics.hh index 450db26944f..8d8dca3a8f0 100644 --- a/include/seastar/core/metrics.hh +++ b/include/seastar/core/metrics.hh @@ -256,6 +256,7 @@ enum class data_type : uint8_t { REAL_COUNTER, GAUGE, HISTOGRAM, + SUMMARY, }; template @@ -595,6 +596,18 @@ impl::metric_definition_impl make_histogram(metric_name_type name, return {name, {impl::data_type::HISTOGRAM, "histogram"}, make_function(std::forward(val), impl::data_type::HISTOGRAM), d, {}}; } +/*! + * \brief create a summary metric. + * + * Summaries are a different kind of histograms. It reports in quantiles. + * For example, the p99 and p95 latencies. + */ +template +impl::metric_definition_impl make_summary(metric_name_type name, + description d, T&& val) { + return {name, {impl::data_type::SUMMARY, "summary"}, make_function(std::forward(val), impl::data_type::SUMMARY), d, {}}; +} + /*! * \brief create a total_bytes metric. From 893e2de1af512dee5f21ce6ba5cc082c962ea56e Mon Sep 17 00:00:00 2001 From: Amnon Heiman Date: Fri, 27 May 2022 16:45:10 +0300 Subject: [PATCH 2/4] metrics.cc: missing count and sum when aggregating histograms This patch adds a missing part to how histograms are being aggregated, it needs to aggregate the sum and count as well. Signed-off-by: Amnon Heiman --- src/core/metrics.cc | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/core/metrics.cc b/src/core/metrics.cc index 98a14874a02..ea39b3e72d0 100644 --- a/src/core/metrics.cc +++ b/src/core/metrics.cc @@ -365,6 +365,8 @@ histogram& histogram::operator+=(const histogram& c) { buckets[i].count += c.buckets[i].count; } } + sample_count += c.sample_count; + sample_sum += c.sample_sum; return *this; } From 9c84d52b49a3f637b4833b342abc07183765971b Mon Sep 17 00:00:00 2001 From: Amnon Heiman Date: Thu, 23 Jun 2022 14:28:16 +0300 Subject: [PATCH 3/4] metrics: support aggregate by labels and skip_when_empty Aggregate labels are a mechanism for reporting aggregated results. Most commonly it allows to report one histogram per node instead of per shard. This patch adds an option to mark a metric with a vector of labels. That vector will be part of the metric meta-data so the reporting layer would be able to aggregate over it. Skip when empty, means that metrics that are not in used, will not be reported. A common scenario is that user register a metrics, but that metrics is never used. The most common case is histogram and summary but it it can also happen with counters. This patch adds an option to mark a metric with skip_when_empty. When done so, if a metric was never used (true for histogram, counters and summary) it will not be reported. Signed-off-by: Amnon Heiman --- include/seastar/core/metrics.hh | 18 ++++++++++++++++-- include/seastar/core/metrics_api.hh | 10 +++++++--- src/core/metrics.cc | 26 +++++++++++++++++++++----- 3 files changed, 44 insertions(+), 10 deletions(-) diff --git a/include/seastar/core/metrics.hh b/include/seastar/core/metrics.hh index 8d8dca3a8f0..570cc3651ec 100644 --- a/include/seastar/core/metrics.hh +++ b/include/seastar/core/metrics.hh @@ -341,7 +341,16 @@ public: const histogram& get_histogram() const { return std::get(u); } - + /*! + * \brief return true if this is metrics was never used + * + * Histograms, Summaries and counters are ever growing by nature, so + * it possible to check if they have been used or not. + */ + bool is_empty() const { + return ((_type == data_type::HISTOGRAM || _type == data_type::SUMMARY) && get_histogram().sample_count == 0) || + ((_type == data_type::COUNTER || _type == data_type::REAL_COUNTER) && d() == 0); + } private: static void ulong_conversion_error(double d); }; @@ -359,16 +368,21 @@ struct metric_definition_impl { metric_function f; description d; bool enabled = true; + bool _skip_when_empty = false; + std::vector aggregate_labels; std::map labels; metric_definition_impl& operator ()(bool enabled); metric_definition_impl& operator ()(const label_instance& label); + metric_definition_impl& aggregate(const std::vector