Skip to content
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

Add error log when getting a http error code #1581

Merged
merged 1 commit into from
Aug 25, 2022
Merged
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
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