Skip to content

Commit 12c5783

Browse files
authored
Merge pull request #432 from jupp0r/remove-collectable
Remove collectable
2 parents 4061c10 + b128da6 commit 12c5783

File tree

8 files changed

+43
-1
lines changed

8 files changed

+43
-1
lines changed

CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ if(POLICY CMP0091)
44
cmake_policy(SET CMP0091 NEW) # recognize CMAKE_MSVC_RUNTIME_LIBRARY
55
endif()
66

7-
project(prometheus-cpp VERSION 0.11.0)
7+
project(prometheus-cpp VERSION 0.12.0)
88

99
include(GenerateExportHeader)
1010
include(GNUInstallDirs)

pull/include/prometheus/exposer.h

+3
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ class PROMETHEUS_CPP_PULL_EXPORT Exposer {
3333
const std::string& realm = "Prometheus-cpp Exporter",
3434
const std::string& uri = std::string("/metrics"));
3535

36+
void RemoveCollectable(const std::weak_ptr<Collectable>& collectable,
37+
const std::string& uri = std::string("/metrics"));
38+
3639
std::vector<int> GetListeningPorts() const;
3740

3841
private:

pull/src/endpoint.cc

+5
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ void Endpoint::RegisterAuth(
4141
auth_handler_ = std::move(new_handler);
4242
}
4343

44+
void Endpoint::RemoveCollectable(
45+
const std::weak_ptr<Collectable>& collectable) {
46+
metrics_handler_->RemoveCollectable(collectable);
47+
}
48+
4449
const std::string& Endpoint::GetURI() const { return uri_; }
4550

4651
} // namespace detail

pull/src/endpoint.h

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class Endpoint {
2424
void RegisterAuth(
2525
std::function<bool(const std::string&, const std::string&)> authCB,
2626
const std::string& realm);
27+
void RemoveCollectable(const std::weak_ptr<Collectable>& collectable);
2728

2829
const std::string& GetURI() const;
2930

pull/src/exposer.cc

+6
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ void Exposer::RegisterAuth(
3535
endpoint.RegisterAuth(std::move(authCB), realm);
3636
}
3737

38+
void Exposer::RemoveCollectable(const std::weak_ptr<Collectable>& collectable,
39+
const std::string& uri) {
40+
auto& endpoint = GetEndpointForUri(uri);
41+
endpoint.RemoveCollectable(collectable);
42+
}
43+
3844
std::vector<int> Exposer::GetListeningPorts() const {
3945
return server_->getListeningPorts();
4046
}

pull/src/handler.cc

+14
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,20 @@ void MetricsHandler::RegisterCollectable(
121121
collectables_.push_back(collectable);
122122
}
123123

124+
void MetricsHandler::RemoveCollectable(
125+
const std::weak_ptr<Collectable>& collectable) {
126+
std::lock_guard<std::mutex> lock{collectables_mutex_};
127+
128+
auto locked = collectable.lock();
129+
auto same_pointer = [&locked](const std::weak_ptr<Collectable>& candidate) {
130+
return locked == candidate.lock();
131+
};
132+
133+
collectables_.erase(std::remove_if(std::begin(collectables_),
134+
std::end(collectables_), same_pointer),
135+
std::end(collectables_));
136+
}
137+
124138
bool MetricsHandler::handleGet(CivetServer*, struct mg_connection* conn) {
125139
auto start_time_of_request = std::chrono::steady_clock::now();
126140

pull/src/handler.h

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class MetricsHandler : public CivetHandler {
1616
explicit MetricsHandler(Registry& registry);
1717

1818
void RegisterCollectable(const std::weak_ptr<Collectable>& collectable);
19+
void RemoveCollectable(const std::weak_ptr<Collectable>& collectable);
1920

2021
bool handleGet(CivetServer* server, struct mg_connection* conn) override;
2122

pull/tests/integration/integration_test.cc

+12
Original file line numberDiff line numberDiff line change
@@ -122,5 +122,17 @@ TEST_F(IntegrationTest, exposesCountersOnDifferentUrls) {
122122
EXPECT_THAT(second_metrics.body, Not(HasSubstr(first_counter_name)));
123123
}
124124

125+
TEST_F(IntegrationTest, unexposeRegistry) {
126+
const std::string counter_name = "some_counter_total";
127+
const auto registry =
128+
RegisterSomeCounter(counter_name, default_metrics_path_);
129+
130+
exposer_->RemoveCollectable(registry, default_metrics_path_);
131+
132+
const auto metrics = FetchMetrics(default_metrics_path_);
133+
ASSERT_EQ(metrics.code, 200);
134+
EXPECT_THAT(metrics.body, Not(HasSubstr(counter_name)));
135+
}
136+
125137
} // namespace
126138
} // namespace prometheus

0 commit comments

Comments
 (0)