Unverified Commit cde2db78 authored by Dominik Charousset's avatar Dominik Charousset Committed by GitHub

Merge pull request #1228

Add metric_registry::merge function
parents 3b0d4639 1c2a49a2
......@@ -471,6 +471,12 @@ public:
visit_family(f, ptr.get());
}
// -- modifiers --------------------------------------------------------------
/// Takes ownership of all metric families in `other`.
/// @pre `other` *must not* contain any duplicated metric family
void merge(metric_registry& other);
private:
/// @pre `families_mx_` is locked.
metric_family* fetch(const string_view& prefix, const string_view& name);
......
......@@ -6,6 +6,7 @@
#include "caf/actor_system_config.hpp"
#include "caf/config.hpp"
#include "caf/raise_error.hpp"
#include "caf/telemetry/dbl_gauge.hpp"
#include "caf/telemetry/int_gauge.hpp"
#include "caf/telemetry/metric_family_impl.hpp"
......@@ -35,6 +36,22 @@ metric_registry::~metric_registry() {
// nop
}
void metric_registry::merge(metric_registry& other) {
if (this == &other)
return;
std::unique_lock<std::mutex> guard1{families_mx_, std::defer_lock};
std::unique_lock<std::mutex> guard2{other.families_mx_, std::defer_lock};
std::lock(guard1, guard2);
families_.reserve(families_.size() + other.families_.size());
for (auto& fptr : other.families_)
if (fetch(fptr->prefix(), fptr->name()) != nullptr)
CAF_RAISE_ERROR("failed to merge metrics: duplicated family found");
families_.insert(families_.end(),
std::make_move_iterator(other.families_.begin()),
std::make_move_iterator(other.families_.end()));
other.families_.clear();
}
metric_family* metric_registry::fetch(const string_view& prefix,
const string_view& name) {
auto eq = [&](const auto& ptr) {
......
......@@ -6,7 +6,7 @@
#include "caf/telemetry/metric_registry.hpp"
#include "caf/test/dsl.hpp"
#include "core-test.hpp"
#include "caf/string_view.hpp"
#include "caf/telemetry/counter.hpp"
......@@ -193,6 +193,25 @@ CAF_TEST(counter_instance is a shortcut for using the family manually) {
CAF_CHECK_EQUAL(count, count2);
}
SCENARIO("metric registries can merge families from other registries") {
GIVEN("a registry with some metrics") {
metric_registry tmp;
auto foo_bar = tmp.counter_singleton("foo", "bar", "test metric");
auto bar_foo = tmp.counter_singleton("bar", "foo", "test metric");
WHEN("merging the registry into another one") {
registry.merge(tmp);
THEN("all metrics move into the new location") {
CHECK_EQ(foo_bar,
registry.counter_singleton("foo", "bar", "test metric"));
CHECK_EQ(bar_foo,
registry.counter_singleton("bar", "foo", "test metric"));
tmp.collect(collector);
CHECK(collector.result.empty());
}
}
}
}
CAF_TEST_FIXTURE_SCOPE_END()
#define CHECK_CONTAINS(str) \
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment