Skip to content

Commit

Permalink
feat(push): Add push support http header customization
Browse files Browse the repository at this point in the history
Closes: #598
  • Loading branch information
jinrui authored and gjasny committed Jul 9, 2022
1 parent 94c5892 commit 689d4ae
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 5 deletions.
6 changes: 6 additions & 0 deletions push/include/prometheus/gateway.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ class PROMETHEUS_CPP_PUSH_EXPORT Gateway {
// Delete metrics from the given pushgateway (for configured instance labels).
std::future<int> AsyncDeleteForInstance();

/// \brief Add a custom HTTP header.
///
/// \param header custom header in the form "key:value".
/// \return true on success, otherwise false
bool AddHttpHeader(const std::string& header);

private:
std::string jobUri_;
std::string labels_;
Expand Down
23 changes: 18 additions & 5 deletions push/src/curl_wrapper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,18 @@ CurlWrapper::CurlWrapper(const std::string& username,
throw std::runtime_error("Cannot initialize easy curl!");
}

optHttpHeader_ = curl_slist_append(nullptr, CONTENT_TYPE);
if (!optHttpHeader_) {
throw std::runtime_error("Cannot append the header of the content type");
}

if (!username.empty()) {
auth_ = username + ":" + password;
}
}

CurlWrapper::~CurlWrapper() {
curl_slist_free_all(optHttpHeader_);
curl_easy_cleanup(curl_);
curl_global_cleanup();
}
Expand All @@ -39,9 +45,7 @@ int CurlWrapper::performHttpRequest(HttpMethod method, const std::string& uri,
curl_easy_reset(curl_);
curl_easy_setopt(curl_, CURLOPT_URL, uri.c_str());

curl_slist* header_chunk = nullptr;
header_chunk = curl_slist_append(header_chunk, CONTENT_TYPE);
curl_easy_setopt(curl_, CURLOPT_HTTPHEADER, header_chunk);
curl_easy_setopt(curl_, CURLOPT_HTTPHEADER, optHttpHeader_);

if (!body.empty()) {
curl_easy_setopt(curl_, CURLOPT_POSTFIELDSIZE, body.size());
Expand Down Expand Up @@ -77,14 +81,23 @@ int CurlWrapper::performHttpRequest(HttpMethod method, const std::string& uri,
long response_code;
curl_easy_getinfo(curl_, CURLINFO_RESPONSE_CODE, &response_code);

curl_slist_free_all(header_chunk);

if (curl_error != CURLE_OK) {
return -curl_error;
}

return response_code;
}

bool CurlWrapper::addHttpHeader(const std::string& header) {
std::lock_guard<std::mutex> lock{mutex_};
auto updated_header = curl_slist_append(optHttpHeader_, header.c_str());
if (!updated_header) {
return false;
}

optHttpHeader_ = updated_header;
return true;
}

} // namespace detail
} // namespace prometheus
2 changes: 2 additions & 0 deletions push/src/curl_wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ class CurlWrapper {

int performHttpRequest(HttpMethod method, const std::string& uri,
const std::string& body);
bool addHttpHeader(const std::string& header);

private:
CURL* curl_;
std::string auth_;
std::mutex mutex_;
curl_slist* optHttpHeader_;
};

} // namespace detail
Expand Down
4 changes: 4 additions & 0 deletions push/src/gateway.cc
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,8 @@ void Gateway::CleanupStalePointers(
std::end(collectables));
}

bool Gateway::AddHttpHeader(const std::string& header) {
return curlWrapper_->addHttpHeader(header);
}

} // namespace prometheus

0 comments on commit 689d4ae

Please sign in to comment.