Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

metrics: Add update_aggregate_labels() #1966

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions include/seastar/core/metrics.hh
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,13 @@ impl::metric_definition_impl make_total_operations(metric_name_type name,
return make_counter(name, std::forward<T>(val), d, labels).set_type("total_operations");
}

/*!
* \brief Update the aggregation labels of a metric family
*/
void update_aggregate_labels(const group_name_type& group_name,
const metric_name_type& metric_name,
const std::vector<label>& aggregate_labels);

/*! @} */
}
}
1 change: 1 addition & 0 deletions include/seastar/core/metrics_api.hh
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,7 @@ public:
}

void add_registration(const metric_id& id, const metric_type& type, metric_function f, const description& d, bool enabled, skip_when_empty skip, const std::vector<std::string>& aggregate_labels);
void update_aggregate_labels(const metric_id& id, const std::vector<label>& aggregate_labels);
void remove_registration(const metric_id& id);
future<> stop() {
return make_ready_future<>();
Expand Down
1 change: 1 addition & 0 deletions include/seastar/core/metrics_registration.hh
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class metric_groups_impl;
SEASTAR_MODULE_EXPORT_BEGIN

using group_name_type = sstring; /*!< A group of logically related metrics */
using metric_name_type = sstring; /*!< A single metric name */
class metric_groups;

class metric_definition {
Expand Down
19 changes: 19 additions & 0 deletions src/core/metrics.cc
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,19 @@ void impl::add_registration(const metric_id& id, const metric_type& type, metric
dirty();
}

void impl::update_aggregate_labels(const metric_id& id,
const std::vector<label>& aggregate_labels) {
auto iter = _value_map.find(id.full_name());
if (iter != _value_map.end()) {
iter->second.info().aggregate_labels.clear();
std::transform(aggregate_labels.begin(), aggregate_labels.end(),
std::back_inserter(iter->second.info().aggregate_labels),
[] (const label& l) { return l.name(); });

dirty();
}
}

future<metric_relabeling_result> impl::set_relabel_configs(const std::vector<relabel_config>& relabel_configs) {
_relabel_configs = relabel_configs;
metric_relabeling_result conflicts{0};
Expand Down Expand Up @@ -556,5 +569,11 @@ histogram histogram::operator+(histogram&& c) const {
return std::move(c);
}

void update_aggregate_labels(const group_name_type& group_name,
const metric_name_type& metric_name,
const std::vector<label>& aggregate_labels) {
impl::metric_id id(group_name, metric_name, {});
impl::get_local_impl()->update_aggregate_labels(id, aggregate_labels);
}
}
}
44 changes: 44 additions & 0 deletions tests/unit/metrics_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,50 @@ SEASTAR_THREAD_TEST_CASE(test_relabel_enable_disable_skip_when_empty) {
sm::set_relabel_configs({}).get();
}

SEASTAR_THREAD_TEST_CASE(test_relabel_update_aggregate_labels) {
using namespace seastar::metrics;
namespace sm = seastar::metrics;
sm::metric_groups app_metrics;
std::string metrics_family_name = "update_aggregate_labels_test";
std::string metric_name = "metric";
sm::label label("label");

for (int i = 0; i < 3; ++i) {
app_metrics.add_group(metrics_family_name, {
sm::make_counter(metric_name, sm::description(""),
{ sm::label_instance(label.name(), std::to_string(i))},
[] { return 0; }),
});
}

auto check_aggregate_labels = [&] (
std::vector<std::string> expected_labels, size_t expected_metrics_count) {
seastar::foreign_ptr<seastar::metrics::impl::values_reference> values = seastar::metrics::impl::get_values();
auto full_name = metrics_family_name + "_" + metric_name;
auto metadata = std::find_if(values->metadata->begin(), values->metadata->end(),
[&] (const auto& md) { return md.mf.name == full_name; });
BOOST_REQUIRE(metadata != values->metadata->end());
BOOST_REQUIRE_EQUAL(metadata->mf.aggregate_labels, expected_labels);
BOOST_REQUIRE_EQUAL(metadata->metrics.size(), expected_metrics_count);
};

check_aggregate_labels({}, 3);

// now add one with different aggregate labels which will be ignored
app_metrics.add_group(metrics_family_name, {
sm::make_counter(metric_name, sm::description(""),
{ sm::label_instance(label.name(), 3)},
[] { return 0; }).aggregate({label}),
});

check_aggregate_labels({}, 4);

// properly update the aggregate labels
update_aggregate_labels(metrics_family_name, metric_name, {label});

check_aggregate_labels({label.name()}, 4);
}

SEASTAR_THREAD_TEST_CASE(test_estimated_histogram) {
using namespace seastar::metrics;
using namespace std::chrono_literals;
Expand Down