diff --git a/include/seastar/core/metrics_api.hh b/include/seastar/core/metrics_api.hh index 42d7c25b5bb..69a9f97d7e6 100644 --- a/include/seastar/core/metrics_api.hh +++ b/include/seastar/core/metrics_api.hh @@ -69,6 +69,8 @@ namespace metrics { SEASTAR_MODULE_EXPORT struct relabel_config; +SEASTAR_MODULE_EXPORT +struct metrics_family_config; /*! * \brief result of metric relabeling * @@ -330,10 +332,17 @@ using metric_metadata_fifo = std::deque; * * The meta data of a metric family compose of the * metadata of the family, and a vector of the metadata for - * each of the metric. + * each of the metrics. + * + * The struct is used for two purposes. First, it allows iterating over all metric_families + * and all metrics related to them. Second, it only contains enabled metrics, + * making disabled metrics more efficient. + * The struct is recreated when impl._value_map changes + * Using a pointer to the family_info metadata is an efficient way to get + * from a metric_family to its metadata based on its name. */ struct metric_family_metadata { - metric_family_info& mf; + metric_family_info& mf; //This points to impl._value_map metric_metadata_fifo metrics; }; @@ -358,6 +367,7 @@ class impl { std::set _labels; std::vector> _current_metrics; std::vector _relabel_configs; + std::vector _metrics_family_configs; public: value_map& get_value_map() { return _value_map; @@ -398,6 +408,13 @@ public: const std::vector& get_relabel_configs() const noexcept { return _relabel_configs; } + const std::vector& get_metrics_family_configs() const noexcept { + return _metrics_family_configs; + } + + void set_metrics_family_configs(const std::vector& metrics_config); + + void update_aggregate(metric_family_info& mf) const noexcept; }; const value_map& get_value_map(); @@ -486,5 +503,29 @@ future set_relabel_configs(const std::vector& get_relabel_configs(); +/* + * \brief change the metrics family config + * + * Family config is a configuration that relates to all metrics with the same name but with different labels. + * set_metrics_family_configs allows changing that configuration during run time. + * Specifically, change the label aggregation based on a metric name. + * + * The following is an example for setting the aggregate labels for the metric test_gauge_1 + * and all metrics matching the regex test_gauge1.*: + * + * std::vector fc(2); + * fc[0].name = "test_gauge_1"; + * fc[0].aggregate_labels = { "lb" }; + * fc[1].regex_name = "test_gauge1.*"; + * fc[1].aggregate_labels = { "ll", "aa" }; + * sm::set_metrics_family_configs(fc); + */ +void set_metrics_family_configs(const std::vector& metrics_config); + +/* + * \brief return the current metrics_family_config + * This function returns a vector of the current metrics family config + */ +const std::vector& get_metrics_family_configs(); } } diff --git a/src/core/metrics.cc b/src/core/metrics.cc index af2e8937e0f..c2a314f7a2a 100644 --- a/src/core/metrics.cc +++ b/src/core/metrics.cc @@ -130,7 +130,20 @@ future set_relabel_configs(const std::vector& get_relabel_configs() { return impl::get_local_impl()->get_relabel_configs(); } +void impl::impl::update_aggregate(metric_family_info& mf) const noexcept { + for (const auto& fc : _metrics_family_configs) { + if (fc.name == mf.name || fc.regex_name.match(mf.name)) { + mf.aggregate_labels = fc.aggregate_labels; + } + } +} +void set_metrics_family_configs(const std::vector& family_config) { + impl::get_local_impl()->set_metrics_family_configs(family_config); +} +const std::vector& get_metrics_family_configs() { + return impl::get_local_impl()->get_metrics_family_configs(); +} static bool apply_relabeling(const relabel_config& rc, impl::metric_info& info) { std::stringstream s; @@ -454,6 +467,7 @@ void impl::add_registration(const metric_id& id, const metric_type& type, metric _value_map[name].info().inherit_type = type.type_name; _value_map[name].info().name = id.full_name(); _value_map[name].info().aggregate_labels = aggregate_labels; + impl::update_aggregate(_value_map[name].info()); _value_map[name][rm->info().id.labels()] = rm; } dirty(); @@ -503,6 +517,26 @@ future impl::set_relabel_configs(const std::vector(conflicts); } + +void impl::set_metrics_family_configs(const std::vector& family_config) { + _metrics_family_configs = family_config; + bool has_regex = false; + for (const auto& fc : family_config) { + has_regex |= !fc.regex_name.empty(); + if (fc.name != "" && _value_map.find(fc.name) != _value_map.end()) { + _value_map[fc.name].info().aggregate_labels = fc.aggregate_labels; + } + } + if (has_regex) { + for (auto& [name, family] : _value_map) { + for (const auto& fc : family_config) { + if (fc.regex_name.match(name)) { + family.info().aggregate_labels = fc.aggregate_labels; + } + } + } + } +} } const bool metric_disabled = false;