diff --git a/push/include/prometheus/gateway.h b/push/include/prometheus/gateway.h index 665a858b..c139320d 100644 --- a/push/include/prometheus/gateway.h +++ b/push/include/prometheus/gateway.h @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -57,6 +58,7 @@ class PROMETHEUS_CPP_PUSH_EXPORT Gateway { std::string jobUri_; std::string labels_; std::unique_ptr curlWrapper_; + std::mutex mutex_; using CollectableEntry = std::pair, std::string>; std::vector collectables_; diff --git a/push/src/gateway.cc b/push/src/gateway.cc index f1b8ef73..e7acf13a 100644 --- a/push/src/gateway.cc +++ b/push/src/gateway.cc @@ -55,6 +55,7 @@ void Gateway::RegisterCollectable(const std::weak_ptr& collectable, } } + std::lock_guard lock{mutex_}; CleanupStalePointers(collectables_); collectables_.push_back(std::make_pair(collectable, ss.str())); } @@ -73,6 +74,7 @@ int Gateway::PushAdd() { return push(detail::HttpMethod::Put); } int Gateway::push(detail::HttpMethod method) { const auto serializer = TextSerializer{}; + std::lock_guard lock{mutex_}; for (auto& wcollectable : collectables_) { auto collectable = wcollectable.first.lock(); if (!collectable) { @@ -104,6 +106,7 @@ std::future Gateway::async_push(detail::HttpMethod method) { const auto serializer = TextSerializer{}; std::vector> futures; + std::lock_guard lock{mutex_}; for (auto& wcollectable : collectables_) { auto collectable = wcollectable.first.lock(); if (!collectable) {