Skip to content

Commit d8b3aa9

Browse files
committed
feat(core): Allow to remove a Family from their Registry
Fixes: #498
1 parent 91dd416 commit d8b3aa9

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

core/include/prometheus/registry.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,19 @@ class PROMETHEUS_CPP_CORE_EXPORT Registry : public Collectable {
8080
/// \return Zero or more metrics and their samples.
8181
std::vector<MetricFamily> Collect() const override;
8282

83+
/// \brief Removes a metrics family from the registry.
84+
///
85+
/// Please note that this operation invalidates the previously
86+
/// returned reference to the Family and all of their added
87+
/// metric objects.
88+
///
89+
/// \tparam T One of the metric types Counter, Gauge, Histogram or Summary.
90+
/// \param family The family to remove
91+
///
92+
/// \return True if the family was found and removed.
93+
template <typename T>
94+
bool Remove(const Family<T>& family);
95+
8396
private:
8497
template <typename T>
8598
friend class detail::Builder;

core/src/registry.cc

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,4 +151,34 @@ template Family<Histogram>& Registry::Add(const std::string& name,
151151
const std::string& help,
152152
const Labels& labels);
153153

154+
template <typename T>
155+
bool Registry::Remove(const Family<T>& family) {
156+
std::lock_guard<std::mutex> lock{mutex_};
157+
158+
auto& families = GetFamilies<T>();
159+
auto same_family = [&family](const std::unique_ptr<Family<T>>& in) {
160+
return &family == in.get();
161+
};
162+
163+
auto it = std::find_if(families.begin(), families.end(), same_family);
164+
if (it == families.end()) {
165+
return false;
166+
}
167+
168+
families.erase(it);
169+
return true;
170+
}
171+
172+
template bool PROMETHEUS_CPP_CORE_EXPORT
173+
Registry::Remove(const Family<Counter>& family);
174+
175+
template bool PROMETHEUS_CPP_CORE_EXPORT
176+
Registry::Remove(const Family<Gauge>& family);
177+
178+
template bool PROMETHEUS_CPP_CORE_EXPORT
179+
Registry::Remove(const Family<Summary>& family);
180+
181+
template bool PROMETHEUS_CPP_CORE_EXPORT
182+
Registry::Remove(const Family<Histogram>& family);
183+
154184
} // namespace prometheus

core/tests/registry_test.cc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,20 @@ TEST(RegistryTest, build_histogram_family) {
4141
ASSERT_EQ(collected.size(), 1U);
4242
}
4343

44+
TEST(RegistryTest, unable_to_remove_family) {
45+
Family<Counter> family{"name", "help", {}};
46+
Registry registry{};
47+
EXPECT_FALSE(registry.Remove(family));
48+
}
49+
50+
TEST(RegistryTest, remove_and_readd_family) {
51+
Registry registry{Registry::InsertBehavior::Throw};
52+
53+
auto& counter = BuildCounter().Name("name").Register(registry);
54+
EXPECT_TRUE(registry.Remove(counter));
55+
EXPECT_NO_THROW(BuildCounter().Name("name").Register(registry));
56+
}
57+
4458
TEST(RegistryTest, reject_different_type_than_counter) {
4559
const auto same_name = std::string{"same_name"};
4660
Registry registry{};

0 commit comments

Comments
 (0)