Skip to content

Commit

Permalink
feat(push): allow timeout for HTTP requests
Browse files Browse the repository at this point in the history
Allows specification of a timeout for HTTP requests which is passed to
curl. This allows the operation to eventually fail if the remote
endpoint does not respond.

Default value for the added parameter maintains existing behaviour.
  • Loading branch information
OwenKnight-FLINTpro authored and gjasny committed Dec 19, 2023
1 parent 540a5a5 commit da220f5
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 9 deletions.
5 changes: 4 additions & 1 deletion push/include/prometheus/gateway.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <chrono>
#include <future>
#include <memory>
#include <mutex>
Expand All @@ -22,7 +23,8 @@ class PROMETHEUS_CPP_PUSH_EXPORT Gateway {
public:
Gateway(const std::string& host, const std::string& port,
const std::string& jobname, const Labels& labels = {},
const std::string& username = {}, const std::string& password = {});
const std::string& username = {}, const std::string& password = {},
std::chrono::seconds timeout = {});

Gateway(const Gateway&) = delete;
Gateway(Gateway&&) = delete;
Expand Down Expand Up @@ -68,6 +70,7 @@ 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
4 changes: 3 additions & 1 deletion push/src/detail/curl_wrapper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ CurlWrapper::~CurlWrapper() {
}

int CurlWrapper::performHttpRequest(HttpMethod method, const std::string& uri,
const std::string& body) {
const std::string& body, long timeout) {
std::lock_guard<std::mutex> l(mutex_);

curl_easy_reset(curl_);
Expand Down Expand Up @@ -76,6 +76,8 @@ 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
2 changes: 1 addition & 1 deletion push/src/detail/curl_wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class CurlWrapper {
~CurlWrapper();

int performHttpRequest(HttpMethod method, const std::string& uri,
const std::string& body);
const std::string& body, long timeout = 0L);
bool addHttpHeader(const std::string& header);

private:
Expand Down
16 changes: 10 additions & 6 deletions push/src/gateway.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ namespace prometheus {

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) {
const std::string& username, const std::string& password,
std::chrono::seconds timeout)
: timeout_(timeout) {
curlWrapper_ = detail::make_unique<detail::CurlWrapper>(username, password);

std::stringstream jobUriStream;
Expand Down Expand Up @@ -84,7 +86,8 @@ 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);
auto status_code =
curlWrapper_->performHttpRequest(method, uri, body, timeout_.count());

if (status_code < 100 || status_code >= 400) {
return status_code;
Expand Down Expand Up @@ -118,7 +121,8 @@ 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);
return curlWrapper_->performHttpRequest(method, uri, *body,
timeout_.count());
}));
}

Expand All @@ -141,16 +145,16 @@ std::future<int> Gateway::async_push(detail::HttpMethod method) {

int Gateway::Delete() {
return curlWrapper_->performHttpRequest(detail::HttpMethod::Delete, jobUri_,
{});
{}, timeout_.count());
}

std::future<int> Gateway::AsyncDelete() {
return std::async(std::launch::async, [&] { return Delete(); });
}

int Gateway::DeleteForInstance() {
return curlWrapper_->performHttpRequest(detail::HttpMethod::Delete,
jobUri_ + labels_, {});
return curlWrapper_->performHttpRequest(
detail::HttpMethod::Delete, jobUri_ + labels_, {}, timeout_.count());
}

std::future<int> Gateway::AsyncDeleteForInstance() {
Expand Down

0 comments on commit da220f5

Please sign in to comment.