Skip to content

Commit

Permalink
feat(push): simplify gateway constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
gjasny committed Jun 2, 2024
1 parent bd9cd33 commit 5223bbf
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 40 deletions.
4 changes: 2 additions & 2 deletions cmake/prometheus-cpp-push.pc.in
Original file line number Diff line number Diff line change
Expand Up @@ -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@
2 changes: 1 addition & 1 deletion push/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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>
)

Expand Down
8 changes: 5 additions & 3 deletions push/include/prometheus/gateway.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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>;
Expand Down
17 changes: 2 additions & 15 deletions push/src/detail/curl_wrapper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand 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() {
Expand All @@ -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_);
Expand All @@ -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);
Expand All @@ -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;
Expand Down
7 changes: 2 additions & 5 deletions push/src/detail/curl_wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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_;
Expand Down
52 changes: 38 additions & 14 deletions push/src/gateway.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}));
}

Expand All @@ -147,17 +172,16 @@ 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() {
return std::async(std::launch::async, [&] { return Delete(); });
}

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() {
Expand Down

0 comments on commit 5223bbf

Please sign in to comment.