Skip to content

Helper method CurlHandle::setopt #41

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
38 changes: 16 additions & 22 deletions zipkin/src/zipkin_http_transporter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,43 +13,37 @@ static std::string getUrl(const char *collector_host, uint32_t collector_port) {
ZipkinCoreConstants::get().DEFAULT_COLLECTOR_ENDPOINT;
}

template <class... Types> void CurlHandle::setopt(CURLoption option, Types... args) {
auto rcode = curl_easy_setopt(handle_, option, args...);
if (rcode != CURLE_OK) {
throw CurlError{rcode};
}
}

ZipkinHttpTransporter::ZipkinHttpTransporter(const char *collector_host,
uint32_t collector_port,
std::chrono::milliseconds collector_timeout) {
auto rcode = curl_easy_setopt(handle_, CURLOPT_URL,
getUrl(collector_host, collector_port).c_str());
if (rcode != CURLE_OK) {
throw CurlError{rcode};
}
handle_.setopt(CURLOPT_URL, getUrl(collector_host, collector_port).c_str());

headers_.append("Content-Type: application/json");
rcode = curl_easy_setopt(handle_, CURLOPT_HTTPHEADER,
static_cast<curl_slist *>(headers_));
if (rcode != CURLE_OK) {
throw CurlError{rcode};
}
handle_.setopt(CURLOPT_HTTPHEADER, static_cast<curl_slist *>(headers_));

rcode = curl_easy_setopt(handle_, CURLOPT_ERRORBUFFER, error_buffer_);
if (rcode != CURLE_OK) {
throw CurlError{rcode};
}
handle_.setopt(CURLOPT_ERRORBUFFER, error_buffer_);

rcode = curl_easy_setopt(handle_, CURLOPT_TIMEOUT_MS, collector_timeout.count());
if (rcode != CURLE_OK) {
throw CurlError{rcode};
}
handle_.setopt(CURLOPT_TIMEOUT_MS, collector_timeout.count());
}

ZipkinHttpTransporter::~ZipkinHttpTransporter() {}

void ZipkinHttpTransporter::transportSpans(SpanBuffer &spans) try {
auto data = spans.toStringifiedJsonArray();
auto rcode = curl_easy_setopt(handle_, CURLOPT_POSTFIELDS, data.c_str());
if (rcode != CURLE_OK) {
std::cerr << curl_easy_strerror(rcode) << '\n';
try {
handle_.setopt(CURLOPT_POSTFIELDS, data.c_str());
} catch(const CurlError &e) {
std::cerr << e.what() << '\n';
return;
}
rcode = curl_easy_perform(handle_);
auto rcode = curl_easy_perform(handle_);
if (rcode != CURLE_OK) {
std::cerr << error_buffer_ << '\n';
}
Expand Down
2 changes: 2 additions & 0 deletions zipkin/src/zipkin_http_transporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ class CurlHandle {

CURL *operator->() { return handle_; }

template <class... Types> void setopt(CURLoption option, Types... args);

private:
CURL *handle_;
};
Expand Down