Skip to content

Commit

Permalink
Add error log when getting a http error code (open-telemetry#1581)
Browse files Browse the repository at this point in the history
  • Loading branch information
owent authored and yxue committed Dec 5, 2022
1 parent 990e37b commit a017620
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 12 deletions.
33 changes: 31 additions & 2 deletions exporters/elasticsearch/src/es_log_exporter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,52 @@ class ResponseHandler : public http_client::EventHandler
*/
ResponseHandler(bool console_debug = false) : console_debug_{console_debug} {}

std::string BuildResponseLogMessage(http_client::Response &response,
const std::string &body) noexcept
{
std::stringstream ss;
ss << "Status:" << response.GetStatusCode() << "Header:";
response.ForEachHeader([&ss](opentelemetry::nostd::string_view header_name,
opentelemetry::nostd::string_view header_value) {
ss << "\t" << header_name.data() << " : " << header_value.data() << ",";
return true;
});
ss << "Body:" << body;

return ss.str();
}

/**
* Automatically called when the response is received, store the body into a string and notify any
* threads blocked on this result
*/
void OnResponse(http_client::Response &response) noexcept override
{
std::string log_message;

// Lock the private members so they can't be read while being modified
{
std::unique_lock<std::mutex> lk(mutex_);

// Store the body of the request
body_ = std::string(response.GetBody().begin(), response.GetBody().end());

if (response.GetStatusCode() != 200 && response.GetStatusCode() != 202)
{
log_message = BuildResponseLogMessage(response, body_);

OTEL_INTERNAL_LOG_ERROR("ES Log Exporter] Export failed, " << log_message);
}

if (console_debug_)
{
OTEL_INTERNAL_LOG_DEBUG(
"[ES Log Exporter] Got response from Elasticsearch, response body: " << body_);
if (log_message.empty())
{
log_message = BuildResponseLogMessage(response, body_);
}

OTEL_INTERNAL_LOG_DEBUG("[ES Log Exporter] Got response from Elasticsearch, "
<< log_message);
}

// Set the response_received_ flag to true and notify any threads waiting on this result
Expand Down
41 changes: 31 additions & 10 deletions exporters/otlp/src/otlp_http_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -79,38 +79,59 @@ class ResponseHandler : public http_client::EventHandler
stopping_.store(false);
}

std::string BuildResponseLogMessage(http_client::Response &response,
const std::string &body) noexcept
{
std::stringstream ss;
ss << "Status:" << response.GetStatusCode() << "Header:";
response.ForEachHeader([&ss](opentelemetry::nostd::string_view header_name,
opentelemetry::nostd::string_view header_value) {
ss << "\t" << header_name.data() << " : " << header_value.data() << ",";
return true;
});
ss << "Body:" << body;

return ss.str();
}

/**
* Automatically called when the response is received, store the body into a string and notify any
* threads blocked on this result
*/
void OnResponse(http_client::Response &response) noexcept override
{
sdk::common::ExportResult result = sdk::common::ExportResult::kSuccess;
std::string log_message;
// Lock the private members so they can't be read while being modified
{
std::unique_lock<std::mutex> lk(mutex_);

// Store the body of the request
body_ = std::string(response.GetBody().begin(), response.GetBody().end());

if (response.GetStatusCode() != 200 && response.GetStatusCode() != 202)
{
log_message = BuildResponseLogMessage(response, body_);

OTEL_INTERNAL_LOG_ERROR("OTLP HTTP Client] Export failed, " << log_message);
result = sdk::common::ExportResult::kFailure;
}

if (console_debug_)
{
std::stringstream ss;
ss << "[OTLP HTTP Client] Status:" << response.GetStatusCode() << "Header:";
response.ForEachHeader([&ss](opentelemetry::nostd::string_view header_name,
opentelemetry::nostd::string_view header_value) {
ss << "\t" << header_name.data() << " : " << header_value.data() << ",";
return true;
});
ss << "Body:" << body_;
OTEL_INTERNAL_LOG_DEBUG(ss.str());
if (log_message.empty())
{
log_message = BuildResponseLogMessage(response, body_);
}
OTEL_INTERNAL_LOG_DEBUG("[OTLP HTTP Client] Export success, " << log_message);
}
}

{
bool expected = false;
if (stopping_.compare_exchange_strong(expected, true, std::memory_order_release))
{
Unbind(sdk::common::ExportResult::kSuccess);
Unbind(result);
}
}
}
Expand Down

0 comments on commit a017620

Please sign in to comment.