Skip to content

Commit

Permalink
pull: Add RemoveCollectable
Browse files Browse the repository at this point in the history
Closes: #415
  • Loading branch information
Mark Klein (mklein201) authored and gjasny committed Dec 17, 2020
1 parent 4061c10 commit bd887da
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pull/include/prometheus/exposer.h
Expand Up @@ -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:
Expand Down
5 changes: 5 additions & 0 deletions pull/src/endpoint.cc
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions pull/src/endpoint.h
Expand Up @@ -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;

Expand Down
6 changes: 6 additions & 0 deletions pull/src/exposer.cc
Expand Up @@ -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();
}
Expand Down
14 changes: 14 additions & 0 deletions pull/src/handler.cc
Expand Up @@ -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();

Expand Down
1 change: 1 addition & 0 deletions pull/src/handler.h
Expand Up @@ -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;

Expand Down
12 changes: 12 additions & 0 deletions pull/tests/integration/integration_test.cc
Expand Up @@ -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

0 comments on commit bd887da

Please sign in to comment.