diff --git a/pull/include/prometheus/exposer.h b/pull/include/prometheus/exposer.h index b470fbd0..83e0e7f0 100644 --- a/pull/include/prometheus/exposer.h +++ b/pull/include/prometheus/exposer.h @@ -3,6 +3,7 @@ #include #include #include +#include #include #include @@ -50,6 +51,7 @@ class PROMETHEUS_CPP_PULL_EXPORT Exposer { std::unique_ptr server_; std::vector> endpoints_; + std::mutex mutex_; }; } // namespace prometheus diff --git a/pull/src/exposer.cc b/pull/src/exposer.cc index 546c9bc1..5ee3adea 100644 --- a/pull/src/exposer.cc +++ b/pull/src/exposer.cc @@ -27,6 +27,7 @@ Exposer::~Exposer() = default; void Exposer::RegisterCollectable(const std::weak_ptr& collectable, const std::string& uri) { + std::lock_guard lock{mutex_}; auto& endpoint = GetEndpointForUri(uri); endpoint.RegisterCollectable(collectable); } @@ -34,12 +35,14 @@ void Exposer::RegisterCollectable(const std::weak_ptr& collectable, void Exposer::RegisterAuth( std::function authCB, const std::string& realm, const std::string& uri) { + std::lock_guard lock{mutex_}; auto& endpoint = GetEndpointForUri(uri); endpoint.RegisterAuth(std::move(authCB), realm); } void Exposer::RemoveCollectable(const std::weak_ptr& collectable, const std::string& uri) { + std::lock_guard lock{mutex_}; auto& endpoint = GetEndpointForUri(uri); endpoint.RemoveCollectable(collectable); }