Skip to content

Commit

Permalink
Helper method CurlHandle::setopt
Browse files Browse the repository at this point in the history
Introduced helper method CurlHandle::setopt to automate error checking
of curl_easy_setopt call.
  • Loading branch information
turchanov committed Dec 23, 2019
1 parent b781c2c commit 8b341dc
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 22 deletions.
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

0 comments on commit 8b341dc

Please sign in to comment.