feat(push): simplify gateway constructors

This commit is contained in:
Gregor Jasny
2024-06-02 13:55:27 +02:00
parent 895bee3c82
commit 6492e820cd
6 changed files with 50 additions and 40 deletions
+2 -2
View File
@@ -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@
+1 -1
View File
@@ -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
$<$<AND:$<BOOL:UNIX>,$<NOT:$<BOOL:APPLE>>>:rt>
)
+5 -3
View File
@@ -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<void(CURL*)> presetupCurl = nullptr);
std::chrono::seconds timeout = {});
Gateway(const std::string& host, const std::string& port,
std::function<void(CURL*)> 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<detail::CurlWrapper> curlWrapper_;
std::chrono::seconds timeout_;
std::mutex mutex_;
using CollectableEntry = std::pair<std::weak_ptr<Collectable>, std::string>;
+2 -15
View File
@@ -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<void(CURL*)> presetupCurl)
CurlWrapper::CurlWrapper(std::function<void(CURL*)> 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<std::mutex> 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;
+2 -5
View File
@@ -11,9 +11,7 @@ namespace detail {
class CurlWrapper {
public:
CurlWrapper(const std::string& username,
const std::string& password,
std::function<void(CURL*)> presetupCurl = nullptr);
CurlWrapper(std::function<void(CURL*)> 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<void(CURL*)> presetupCurl_;
+38 -14
View File
@@ -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<void(CURL*)> presetupCurl)
: timeout_(timeout) {
curlWrapper_ = detail::make_unique<detail::CurlWrapper>(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<void(CURL*)> presetupCurl,
const std::string& jobname, const Labels& labels) {
curlWrapper_ = detail::make_unique<detail::CurlWrapper>(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<int> 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<int> 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<int> Gateway::AsyncDelete() {
@@ -156,8 +180,8 @@ std::future<int> 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<int> Gateway::AsyncDeleteForInstance() {