Skip to content

Commit

Permalink
metrics: Wrap metric_info/id in foreign_ptr for metadata usage
Browse files Browse the repository at this point in the history
  • Loading branch information
StephanDollberg committed Dec 11, 2024
1 parent 4adc6f5 commit eef0ddd
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 58 deletions.
86 changes: 57 additions & 29 deletions include/seastar/core/metrics_api.hh
Original file line number Diff line number Diff line change
Expand Up @@ -104,18 +104,20 @@ namespace impl {
* In the seastar environment it is typical to have a metric per shard.
*
*/
std::string safe_name(const sstring& name);

class metric_id {
template <typename label_type>
class metric_id_t {
public:
metric_id() = default;
metric_id(group_name_type group, metric_name_type name,
internalized_labels_ref labels)
metric_id_t() = default;
metric_id_t(group_name_type group, metric_name_type name,
label_type labels)
: _group(std::move(group)), _name(
std::move(name)), _labels(labels) {
std::move(name)), _labels(std::move(labels)) {
}
metric_id(metric_id &&) noexcept = default;
metric_id_t(metric_id_t &&) noexcept = default;

metric_id & operator=(metric_id &&) noexcept = default;
metric_id_t & operator=(metric_id_t &&) noexcept = default;

const group_name_type & group_name() const {
return _group;
Expand All @@ -132,31 +134,48 @@ public:
const labels_type& labels() const {
return *_labels;
}
internalized_labels_ref internalized_labels() const {
label_type internalized_labels() const {
return _labels;
}
void update_labels(internalized_labels_ref labels) {
void update_labels(label_type labels) {
_labels = labels;
}
sstring full_name() const;
sstring full_name() const {
return safe_name(_group + "_" + _name);
}

metric_id copy() const {
metric_id_t copy() const {
return *this;
}

bool operator<(const metric_id&) const;
bool operator==(const metric_id&) const;
bool operator<(const metric_id_t& id2) const {
return as_tuple() < id2.as_tuple();
}


bool operator==(const metric_id_t & id2) const {
return as_tuple() == id2.as_tuple();
}
private:
metric_id(const metric_id &) = default;
metric_id & operator=(const metric_id &) = default;

metric_id_t(const metric_id_t &) = default;
metric_id_t & operator=(const metric_id_t &) = default;

auto as_tuple() const {
return std::tie(group_name(), instance_id(), name(), labels());
}
group_name_type _group;
metric_name_type _name;
internalized_labels_ref _labels;
label_type _labels;
};

using metric_id = metric_id_t<internalized_labels_ref>;
using foreign_metric_id = metric_id_t<foreign_ptr<internalized_labels_ref>>;

inline foreign_metric_id make_foreign_metric_id(metric_id& id) {
return {id.group_name(), id.name(), make_foreign(id.internalized_labels())};
}

}
}
}
Expand Down Expand Up @@ -200,27 +219,36 @@ struct metric_family_info {
/*!
* \brief holds metric metadata
*/
struct metric_info {
metric_id id;
internalized_labels_ref original_labels;
template<typename metric_id_type, typename label_type>
struct metric_info_t {
metric_id_type id;
label_type original_labels;
bool enabled;
skip_when_empty should_skip_when_empty;

metric_info() noexcept = default;
metric_info(metric_id id, internalized_labels_ref labels, bool enabled, skip_when_empty skip) :
id(std::move(id)), original_labels(std::move(labels)), enabled(enabled), should_skip_when_empty(skip) {
};
metric_info_t() noexcept = default;
metric_info_t(metric_id_type id, label_type labels, bool enabled,
skip_when_empty skip)
: id(std::move(id)), original_labels(std::move(labels)),
enabled(enabled), should_skip_when_empty(skip) {};

metric_info(metric_info &&) noexcept = default;
metric_info& operator=(metric_info &&) noexcept = default;
metric_info(const metric_info&) = delete;
metric_info& operator=(const metric_info&) = delete;
metric_info_t(metric_info_t &&) noexcept = default;
metric_info_t& operator=(metric_info_t &&) noexcept = default;
metric_info_t(const metric_info_t&) = delete;
metric_info_t& operator=(const metric_info_t&) = delete;

metric_info copy() const {
metric_info_t copy() const {
return {id.copy(), original_labels, enabled, should_skip_when_empty};
}
};

using metric_info = metric_info_t<metric_id, internalized_labels_ref>;
using foreign_metric_info = metric_info_t<foreign_metric_id, foreign_ptr<internalized_labels_ref>>;

inline foreign_metric_info make_foreign_metric_info(metric_info& info) {
return {make_foreign_metric_id(info.id), make_foreign(info.original_labels), info.enabled, info.should_skip_when_empty};
}

class internalized_holder {
internalized_labels_ref _labels;
public:
Expand Down Expand Up @@ -382,7 +410,7 @@ public:

using value_map = std::map<sstring, metric_family>;

using metric_metadata_fifo = std::deque<metric_info>;
using metric_metadata_fifo = std::deque<foreign_metric_info>;

/*!
* \brief holds a metric family metadata
Expand Down
18 changes: 2 additions & 16 deletions src/core/metrics.cc
Original file line number Diff line number Diff line change
Expand Up @@ -347,26 +347,12 @@ metric_groups_impl& metric_groups_impl::add_group(group_name_type name, const st
return *this;
}

bool metric_id::operator<(
const metric_id& id2) const {
return as_tuple() < id2.as_tuple();
}

static std::string safe_name(const sstring& name) {
std::string safe_name(const sstring& name) {
auto rep = boost::replace_all_copy(boost::replace_all_copy(name, "-", "_"), " ", "_");
boost::remove_erase_if(rep, boost::is_any_of("+()"));
return rep;
}

sstring metric_id::full_name() const {
return safe_name(_group + "_" + _name);
}

bool metric_id::operator==(
const metric_id & id2) const {
return as_tuple() == id2.as_tuple();
}

// Unfortunately, metrics_impl can not be shared because it
// need to be available before the first users (reactor) will call it

Expand Down Expand Up @@ -444,7 +430,7 @@ void impl::update_metrics_if_needed() {
_current_metrics[i].clear();
for (auto&& m : mf.second) {
if (m.second && m.second->is_enabled()) {
metrics.emplace_back(m.second->info().copy());
metrics.emplace_back(make_foreign_metric_info(m.second->info()));
_current_metrics[i].emplace_back(m.second->get_function());
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/core/prometheus.cc
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ class metric_family {
return *_family_info;
}

void foreach_metric(std::function<void(const mi::metric_value&, const mi::metric_info&)>&& f);
void foreach_metric(std::function<void(const mi::metric_value&, const mi::foreign_metric_info&)>&& f);

bool end() const {
return !_name || !_family_info;
Expand Down Expand Up @@ -559,7 +559,7 @@ class metric_family_iterator {
return _positions.empty() || _info.end();
}

void foreach_metric(std::function<void(const mi::metric_value&, const mi::metric_info&)>&& f) {
void foreach_metric(std::function<void(const mi::metric_value&, const mi::foreign_metric_info&)>&& f) {
// iterating over the shard vector and the position vector
for (auto&& i : boost::combine(_positions, _families)) {
auto& pos_in_metric_per_shard = boost::get<0>(i);
Expand All @@ -585,7 +585,7 @@ class metric_family_iterator {

};

void metric_family::foreach_metric(std::function<void(const mi::metric_value&, const mi::metric_info&)>&& f) {
void metric_family::foreach_metric(std::function<void(const mi::metric_value&, const mi::foreign_metric_info&)>&& f) {
_iterator_state.foreach_metric(std::move(f));
}

Expand Down Expand Up @@ -768,7 +768,7 @@ future<> write_text_representation(output_stream<char>& out, const config& ctx,
found = false;
metric_aggregate_by_labels aggregated_values(metric_family.metadata().aggregate_labels);
bool should_aggregate = enable_aggregation && !metric_family.metadata().aggregate_labels.empty();
metric_family.foreach_metric([&s, &out, &ctx, &found, &name, &metric_family, &aggregated_values, should_aggregate, show_help, &filter](const mi::metric_value& value, const mi::metric_info& value_info) mutable {
metric_family.foreach_metric([&s, &out, &ctx, &found, &name, &metric_family, &aggregated_values, should_aggregate, show_help, &filter](const mi::metric_value& value, const mi::foreign_metric_info& value_info) mutable {
s.clear();
s.str("");
if ((value_info.should_skip_when_empty && value.is_empty()) || !filter(value_info.id.labels())) {
Expand Down
8 changes: 5 additions & 3 deletions src/core/scollectd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,8 @@ struct cpwriter {
return *this;
}

sstring get_type_instance(const seastar::metrics::impl::metric_id & id) {
template<typename label_type>
sstring get_type_instance(const seastar::metrics::impl::metric_id_t<label_type> & id) {
if (id.labels().empty()) {
return id.name();
}
Expand Down Expand Up @@ -288,7 +289,8 @@ struct cpwriter {
}
return *this;
}
cpwriter & put(const sstring & host, const seastar::metrics::impl::metric_id & id, const type_id& type) {
template<typename label_type>
cpwriter & put(const sstring & host, const seastar::metrics::impl::metric_id_t<label_type>& id, const type_id& type) {
const auto ts = std::chrono::system_clock::now().time_since_epoch();
const auto lrts =
std::chrono::duration_cast<std::chrono::seconds>(ts).count();
Expand Down Expand Up @@ -324,7 +326,7 @@ struct cpwriter {
cpwriter & put(const sstring & host,
const duration & period,
const type_id& type,
const seastar::metrics::impl::metric_id & id, const seastar::metrics::impl::metric_value & v) {
const seastar::metrics::impl::foreign_metric_id & id, const seastar::metrics::impl::metric_value & v) {
const auto ps = std::chrono::duration_cast<collectd_hres_duration>(
period).count();
put(host, id, type);
Expand Down
14 changes: 8 additions & 6 deletions tests/unit/metrics_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ int count_by_label(const std::string& label) {
return count;
}

int count_by_fun(std::function<bool(const seastar::metrics::impl::metric_info&)> f) {
int count_by_fun(std::function<bool(const seastar::metrics::impl::foreign_metric_info&)> f) {
seastar::foreign_ptr<seastar::metrics::impl::values_reference> values = seastar::metrics::impl::get_values();
int count = 0;
for (auto&& md : (*values->metadata)) {
Expand Down Expand Up @@ -285,7 +285,7 @@ SEASTAR_THREAD_TEST_CASE(test_relabel_enable_disable_skip_when_empty) {
sm::metric_relabeling_result success = sm::set_relabel_configs(rl).get();
BOOST_CHECK_EQUAL(success.metrics_relabeled_due_to_collision, 0);
BOOST_CHECK_EQUAL(count_by_label(""), 3);
BOOST_CHECK_EQUAL(count_by_fun([](const seastar::metrics::impl::metric_info& mi) {
BOOST_CHECK_EQUAL(count_by_fun([](const seastar::metrics::impl::foreign_metric_info& mi) {
return mi.should_skip_when_empty == sm::skip_when_empty::yes;
}), 0);

Expand All @@ -304,7 +304,7 @@ SEASTAR_THREAD_TEST_CASE(test_relabel_enable_disable_skip_when_empty) {
success = sm::set_relabel_configs(rl2).get();
BOOST_CHECK_EQUAL(success.metrics_relabeled_due_to_collision, 0);
BOOST_CHECK_EQUAL(count_by_label(""), 3);
BOOST_CHECK_EQUAL(count_by_fun([](const seastar::metrics::impl::metric_info& mi) {
BOOST_CHECK_EQUAL(count_by_fun([](const seastar::metrics::impl::foreign_metric_info& mi) {
return mi.should_skip_when_empty == sm::skip_when_empty::yes;
}), 3);
// clear the configuration
Expand All @@ -326,9 +326,11 @@ SEASTAR_THREAD_TEST_CASE(test_relabel_enable_disable_skip_when_empty) {

success = sm::set_relabel_configs(rl3).get();
BOOST_CHECK_EQUAL(success.metrics_relabeled_due_to_collision, 0);
BOOST_CHECK_EQUAL(count_by_fun([](const seastar::metrics::impl::metric_info& mi) {
return mi.should_skip_when_empty == sm::skip_when_empty::yes;
}), 0);
BOOST_CHECK_EQUAL(
count_by_fun([](const seastar::metrics::impl::foreign_metric_info &mi) {
return mi.should_skip_when_empty == sm::skip_when_empty::yes;
}),
0);
sm::set_relabel_configs({}).get();
}

Expand Down

0 comments on commit eef0ddd

Please sign in to comment.