mirror of
https://github.com/jupp0r/prometheus-cpp.git
synced 2026-07-21 14:33:30 +00:00
Merge pull request #432 from jupp0r/remove-collectable
Remove collectable
This commit is contained in:
+1
-1
@@ -4,7 +4,7 @@ if(POLICY CMP0091)
|
||||
cmake_policy(SET CMP0091 NEW) # recognize CMAKE_MSVC_RUNTIME_LIBRARY
|
||||
endif()
|
||||
|
||||
project(prometheus-cpp VERSION 0.11.0)
|
||||
project(prometheus-cpp VERSION 0.12.0)
|
||||
|
||||
include(GenerateExportHeader)
|
||||
include(GNUInstallDirs)
|
||||
|
||||
@@ -33,6 +33,9 @@ class PROMETHEUS_CPP_PULL_EXPORT Exposer {
|
||||
const std::string& realm = "Prometheus-cpp Exporter",
|
||||
const std::string& uri = std::string("/metrics"));
|
||||
|
||||
void RemoveCollectable(const std::weak_ptr<Collectable>& collectable,
|
||||
const std::string& uri = std::string("/metrics"));
|
||||
|
||||
std::vector<int> GetListeningPorts() const;
|
||||
|
||||
private:
|
||||
|
||||
@@ -41,6 +41,11 @@ void Endpoint::RegisterAuth(
|
||||
auth_handler_ = std::move(new_handler);
|
||||
}
|
||||
|
||||
void Endpoint::RemoveCollectable(
|
||||
const std::weak_ptr<Collectable>& collectable) {
|
||||
metrics_handler_->RemoveCollectable(collectable);
|
||||
}
|
||||
|
||||
const std::string& Endpoint::GetURI() const { return uri_; }
|
||||
|
||||
} // namespace detail
|
||||
|
||||
@@ -24,6 +24,7 @@ class Endpoint {
|
||||
void RegisterAuth(
|
||||
std::function<bool(const std::string&, const std::string&)> authCB,
|
||||
const std::string& realm);
|
||||
void RemoveCollectable(const std::weak_ptr<Collectable>& collectable);
|
||||
|
||||
const std::string& GetURI() const;
|
||||
|
||||
|
||||
@@ -35,6 +35,12 @@ void Exposer::RegisterAuth(
|
||||
endpoint.RegisterAuth(std::move(authCB), realm);
|
||||
}
|
||||
|
||||
void Exposer::RemoveCollectable(const std::weak_ptr<Collectable>& collectable,
|
||||
const std::string& uri) {
|
||||
auto& endpoint = GetEndpointForUri(uri);
|
||||
endpoint.RemoveCollectable(collectable);
|
||||
}
|
||||
|
||||
std::vector<int> Exposer::GetListeningPorts() const {
|
||||
return server_->getListeningPorts();
|
||||
}
|
||||
|
||||
@@ -121,6 +121,20 @@ void MetricsHandler::RegisterCollectable(
|
||||
collectables_.push_back(collectable);
|
||||
}
|
||||
|
||||
void MetricsHandler::RemoveCollectable(
|
||||
const std::weak_ptr<Collectable>& collectable) {
|
||||
std::lock_guard<std::mutex> lock{collectables_mutex_};
|
||||
|
||||
auto locked = collectable.lock();
|
||||
auto same_pointer = [&locked](const std::weak_ptr<Collectable>& candidate) {
|
||||
return locked == candidate.lock();
|
||||
};
|
||||
|
||||
collectables_.erase(std::remove_if(std::begin(collectables_),
|
||||
std::end(collectables_), same_pointer),
|
||||
std::end(collectables_));
|
||||
}
|
||||
|
||||
bool MetricsHandler::handleGet(CivetServer*, struct mg_connection* conn) {
|
||||
auto start_time_of_request = std::chrono::steady_clock::now();
|
||||
|
||||
|
||||
@@ -16,6 +16,7 @@ class MetricsHandler : public CivetHandler {
|
||||
explicit MetricsHandler(Registry& registry);
|
||||
|
||||
void RegisterCollectable(const std::weak_ptr<Collectable>& collectable);
|
||||
void RemoveCollectable(const std::weak_ptr<Collectable>& collectable);
|
||||
|
||||
bool handleGet(CivetServer* server, struct mg_connection* conn) override;
|
||||
|
||||
|
||||
@@ -122,5 +122,17 @@ TEST_F(IntegrationTest, exposesCountersOnDifferentUrls) {
|
||||
EXPECT_THAT(second_metrics.body, Not(HasSubstr(first_counter_name)));
|
||||
}
|
||||
|
||||
TEST_F(IntegrationTest, unexposeRegistry) {
|
||||
const std::string counter_name = "some_counter_total";
|
||||
const auto registry =
|
||||
RegisterSomeCounter(counter_name, default_metrics_path_);
|
||||
|
||||
exposer_->RemoveCollectable(registry, default_metrics_path_);
|
||||
|
||||
const auto metrics = FetchMetrics(default_metrics_path_);
|
||||
ASSERT_EQ(metrics.code, 200);
|
||||
EXPECT_THAT(metrics.body, Not(HasSubstr(counter_name)));
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace prometheus
|
||||
|
||||
Reference in New Issue
Block a user