Skip to content

Commit

Permalink
Add error log when getting a http error code (#1581)
Browse files Browse the repository at this point in the history
  • Loading branch information
owent authored Aug 25, 2022
1 parent 4535347 commit cecfc19
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

1 comment on commit cecfc19

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark 'OpenTelemetry-cpp sdk Benchmark'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 2.

Benchmark suite Current: cecfc19 Previous: 4535347 Ratio
BM_BaselineBuffer/4 13008730.411529541 ns/iter 5550124.645233154 ns/iter 2.34

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.