diff --git a/zipkin/src/zipkin_http_transporter.cc b/zipkin/src/zipkin_http_transporter.cc index 0424321..74b1522 100644 --- a/zipkin/src/zipkin_http_transporter.cc +++ b/zipkin/src/zipkin_http_transporter.cc @@ -13,43 +13,37 @@ static std::string getUrl(const char *collector_host, uint32_t collector_port) { ZipkinCoreConstants::get().DEFAULT_COLLECTOR_ENDPOINT; } +template 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(headers_)); - if (rcode != CURLE_OK) { - throw CurlError{rcode}; - } + handle_.setopt(CURLOPT_HTTPHEADER, static_cast(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'; } diff --git a/zipkin/src/zipkin_http_transporter.h b/zipkin/src/zipkin_http_transporter.h index e70de64..46ca204 100644 --- a/zipkin/src/zipkin_http_transporter.h +++ b/zipkin/src/zipkin_http_transporter.h @@ -46,6 +46,8 @@ class CurlHandle { CURL *operator->() { return handle_; } + template void setopt(CURLoption option, Types... args); + private: CURL *handle_; };