From 5223bbfc9947bd627df49b482aacbd945bcbe621 Mon Sep 17 00:00:00 2001 From: Gregor Jasny Date: Sun, 2 Jun 2024 13:55:27 +0200 Subject: [PATCH] feat(push): simplify gateway constructors --- cmake/prometheus-cpp-push.pc.in | 4 +-- push/CMakeLists.txt | 2 +- push/include/prometheus/gateway.h | 8 +++-- push/src/detail/curl_wrapper.cc | 17 ++-------- push/src/detail/curl_wrapper.h | 7 ++--- push/src/gateway.cc | 52 ++++++++++++++++++++++--------- 6 files changed, 50 insertions(+), 40 deletions(-) diff --git a/cmake/prometheus-cpp-push.pc.in b/cmake/prometheus-cpp-push.pc.in index 9529abab..a362310c 100644 --- a/cmake/prometheus-cpp-push.pc.in +++ b/cmake/prometheus-cpp-push.pc.in @@ -7,8 +7,8 @@ Name: @PROJECT_NAME@-push Description: @PROJECT_DESCRIPTION@ URL: @PROJECT_HOMEPAGE_URL@ Version: @PROJECT_VERSION@ -Requires: @PROJECT_NAME@-core -Requires.private: @PKGCONFIG_REQUIRES@ +Requires: @PROJECT_NAME@-core @PKGCONFIG_REQUIRES@ +Requires.private: Cflags: -I${includedir} Libs: -L${libdir} -l@PROJECT_NAME@-push Libs.private: @CMAKE_THREAD_LIBS_INIT@ @PKGCONFIG_LIBS@ diff --git a/push/CMakeLists.txt b/push/CMakeLists.txt index bf21e818..f6110b1d 100644 --- a/push/CMakeLists.txt +++ b/push/CMakeLists.txt @@ -20,10 +20,10 @@ target_compile_features(push target_link_libraries(push PUBLIC ${PROJECT_NAME}::core + CURL::libcurl PRIVATE ${PROJECT_NAME}::util Threads::Threads - CURL::libcurl $<$,$>>:rt> ) diff --git a/push/include/prometheus/gateway.h b/push/include/prometheus/gateway.h index f5c74708..35d0d89b 100644 --- a/push/include/prometheus/gateway.h +++ b/push/include/prometheus/gateway.h @@ -28,8 +28,11 @@ class PROMETHEUS_CPP_PUSH_EXPORT Gateway { Gateway(const std::string& host, const std::string& port, const std::string& jobname, const Labels& labels = {}, const std::string& username = {}, const std::string& password = {}, - std::chrono::seconds timeout = {}, - std::function presetupCurl = nullptr); + std::chrono::seconds timeout = {}); + + Gateway(const std::string& host, const std::string& port, + std::function presetupCurl, const std::string& jobname, + const Labels& labels = {}); Gateway(const Gateway&) = delete; Gateway(Gateway&&) = delete; @@ -75,7 +78,6 @@ class PROMETHEUS_CPP_PUSH_EXPORT Gateway { std::string jobUri_; std::string labels_; std::unique_ptr curlWrapper_; - std::chrono::seconds timeout_; std::mutex mutex_; using CollectableEntry = std::pair, std::string>; diff --git a/push/src/detail/curl_wrapper.cc b/push/src/detail/curl_wrapper.cc index a756d2d5..997c3745 100644 --- a/push/src/detail/curl_wrapper.cc +++ b/push/src/detail/curl_wrapper.cc @@ -8,9 +8,7 @@ namespace detail { static const char CONTENT_TYPE[] = "Content-Type: text/plain; version=0.0.4; charset=utf-8"; -CurlWrapper::CurlWrapper(const std::string& username, - const std::string& password, - std::function presetupCurl) +CurlWrapper::CurlWrapper(std::function presetupCurl) : presetupCurl_(presetupCurl) { /* In windows, this will init the winsock stuff */ auto error = curl_global_init(CURL_GLOBAL_ALL); @@ -32,10 +30,6 @@ CurlWrapper::CurlWrapper(const std::string& username, if (!optHttpHeader_) { throw std::runtime_error("Cannot append the header of the content type"); } - - if (!username.empty()) { - auth_ = username + ":" + password; - } } CurlWrapper::~CurlWrapper() { @@ -45,7 +39,7 @@ CurlWrapper::~CurlWrapper() { } int CurlWrapper::performHttpRequest(HttpMethod method, const std::string& uri, - const std::string& body, long timeout) { + const std::string& body) { std::lock_guard l(mutex_); curl_easy_reset(curl_); @@ -64,11 +58,6 @@ int CurlWrapper::performHttpRequest(HttpMethod method, const std::string& uri, curl_easy_setopt(curl_, CURLOPT_POSTFIELDSIZE, 0L); } - if (!auth_.empty()) { - curl_easy_setopt(curl_, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); - curl_easy_setopt(curl_, CURLOPT_USERPWD, auth_.c_str()); - } - switch (method) { case HttpMethod::Post: curl_easy_setopt(curl_, CURLOPT_POST, 1L); @@ -86,8 +75,6 @@ int CurlWrapper::performHttpRequest(HttpMethod method, const std::string& uri, break; } - curl_easy_setopt(curl_, CURLOPT_TIMEOUT, timeout); - auto curl_error = curl_easy_perform(curl_); long response_code; diff --git a/push/src/detail/curl_wrapper.h b/push/src/detail/curl_wrapper.h index 104463cb..69c6d6a1 100644 --- a/push/src/detail/curl_wrapper.h +++ b/push/src/detail/curl_wrapper.h @@ -11,9 +11,7 @@ namespace detail { class CurlWrapper { public: - CurlWrapper(const std::string& username, - const std::string& password, - std::function presetupCurl = nullptr); + CurlWrapper(std::function presetupCurl); CurlWrapper(const CurlWrapper&) = delete; CurlWrapper(CurlWrapper&&) = delete; @@ -23,12 +21,11 @@ class CurlWrapper { ~CurlWrapper(); int performHttpRequest(HttpMethod method, const std::string& uri, - const std::string& body, long timeout = 0L); + const std::string& body = {}); bool addHttpHeader(const std::string& header); private: CURL* curl_; - std::string auth_; std::mutex mutex_; curl_slist* optHttpHeader_; std::function presetupCurl_; diff --git a/push/src/gateway.cc b/push/src/gateway.cc index 85bc2707..1e8b55af 100644 --- a/push/src/gateway.cc +++ b/push/src/gateway.cc @@ -19,15 +19,42 @@ namespace prometheus { +namespace { +class SetupAdapter { + public: + SetupAdapter(const std::string& username, const std::string& password, + std::chrono::seconds timeout) + : timeout_(timeout) { + if (!username.empty()) { + auth_ = username + ":" + password; + } + } + + void operator()(CURL* curl) { + if (!auth_.empty()) { + curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); + curl_easy_setopt(curl, CURLOPT_USERPWD, auth_.c_str()); + } + curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout_); + } + + private: + std::string auth_; + std::chrono::seconds timeout_; +}; +} // namespace + Gateway::Gateway(const std::string& host, const std::string& port, const std::string& jobname, const Labels& labels, const std::string& username, const std::string& password, - std::chrono::seconds timeout, - std::function presetupCurl) - : timeout_(timeout) { - curlWrapper_ = detail::make_unique(username, - password, - presetupCurl); + std::chrono::seconds timeout) + : Gateway(host, port, SetupAdapter{username, password, timeout}, jobname, + labels) {} + +Gateway::Gateway(const std::string& host, const std::string& port, + std::function presetupCurl, + const std::string& jobname, const Labels& labels) { + curlWrapper_ = detail::make_unique(presetupCurl); std::stringstream jobUriStream; jobUriStream << host << ':' << port << "/metrics"; @@ -89,8 +116,7 @@ int Gateway::push(detail::HttpMethod method) { auto metrics = collectable->Collect(); auto body = serializer.Serialize(metrics); auto uri = getUri(wcollectable); - auto status_code = - curlWrapper_->performHttpRequest(method, uri, body, timeout_.count()); + auto status_code = curlWrapper_->performHttpRequest(method, uri, body); if (status_code < 100 || status_code >= 400) { return status_code; @@ -124,8 +150,7 @@ std::future Gateway::async_push(detail::HttpMethod method) { auto uri = getUri(wcollectable); futures.push_back(std::async(std::launch::async, [method, uri, body, this] { - return curlWrapper_->performHttpRequest(method, uri, *body, - timeout_.count()); + return curlWrapper_->performHttpRequest(method, uri, *body); })); } @@ -147,8 +172,7 @@ std::future Gateway::async_push(detail::HttpMethod method) { } int Gateway::Delete() { - return curlWrapper_->performHttpRequest(detail::HttpMethod::Delete, jobUri_, - {}, timeout_.count()); + return curlWrapper_->performHttpRequest(detail::HttpMethod::Delete, jobUri_); } std::future Gateway::AsyncDelete() { @@ -156,8 +180,8 @@ std::future Gateway::AsyncDelete() { } int Gateway::DeleteForInstance() { - return curlWrapper_->performHttpRequest( - detail::HttpMethod::Delete, jobUri_ + labels_, {}, timeout_.count()); + return curlWrapper_->performHttpRequest(detail::HttpMethod::Delete, + jobUri_ + labels_); } std::future Gateway::AsyncDeleteForInstance() {