Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(push): Add push support http header customization #598

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions push/include/prometheus/gateway.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ class PROMETHEUS_CPP_PUSH_EXPORT Gateway {
// Delete metrics from the given pushgateway (for configured instance labels).
std::future<int> AsyncDeleteForInstance();

/**
* Add a custom http header, it must use after create gateway before push
* `header` is the custom header, its content format is “key:value”, such
* as "user:gateway"
*/
int AddHttpHeader(const std::string& header);

private:
std::string jobUri_;
std::string labels_;
Expand Down
41 changes: 40 additions & 1 deletion 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_ = nullptr;
gjasny marked this conversation as resolved.
Show resolved Hide resolved

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

CurlWrapper::~CurlWrapper() {
if (optHttpHeader_ != nullptr)
jerry-tom marked this conversation as resolved.
Show resolved Hide resolved
{
curl_slist_free_all(optHttpHeader_);
}
curl_easy_cleanup(curl_);
curl_global_cleanup();
}
Expand All @@ -40,7 +46,26 @@ int CurlWrapper::performHttpRequest(HttpMethod method, const std::string& uri,
curl_easy_setopt(curl_, CURLOPT_URL, uri.c_str());

curl_slist* header_chunk = nullptr;
header_chunk = curl_slist_append(header_chunk, CONTENT_TYPE);
header_chunk = curl_slist_append(header_chunk, CONTENT_TYPE);
if (nullptr == header_chunk)
{
return -1;
}

curl_slist* optHeader_tmp = optHttpHeader_;
jerry-tom marked this conversation as resolved.
Show resolved Hide resolved
while (optHeader_tmp)
{
curl_slist* header_tmp = curl_slist_append(header_chunk, optHeader_tmp->data);
if (nullptr == header_tmp)
{
curl_slist_free_all(header_chunk);
return -1;
}

header_chunk = header_tmp;
optHeader_tmp = optHeader_tmp->next;
}

jerry-tom marked this conversation as resolved.
Show resolved Hide resolved
curl_easy_setopt(curl_, CURLOPT_HTTPHEADER, header_chunk);

if (!body.empty()) {
Expand Down Expand Up @@ -86,5 +111,19 @@ int CurlWrapper::performHttpRequest(HttpMethod method, const std::string& uri,
return response_code;
}

int CurlWrapper::addOptHttpHeader(const std::string& header)
{
std::lock_guard<std::mutex> lock{mutex_};
curl_slist* header_tmp = nullptr;
jerry-tom marked this conversation as resolved.
Show resolved Hide resolved
header_tmp = curl_slist_append(optHttpHeader_, header.c_str());
if (nullptr == header_tmp)
{
return -1;
}

optHttpHeader_ = header_tmp;
return 0;
}

} // 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);
int addOptHttpHeader(const std::string& header);
jerry-tom marked this conversation as resolved.
Show resolved Hide resolved

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

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

int Gateway::AddHttpHeader(const std::string& header)
{
std::lock_guard<std::mutex> lock{mutex_};
jerry-tom marked this conversation as resolved.
Show resolved Hide resolved
return curlWrapper_->addOptHttpHeader(header);
}

} // namespace prometheus