Skip to content

Commit cecfc19

Browse files
authored
Add error log when getting a http error code (#1581)
1 parent 4535347 commit cecfc19

File tree

2 files changed

+62
-12
lines changed

2 files changed

+62
-12
lines changed

exporters/elasticsearch/src/es_log_exporter.cc

+31-2
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,52 @@ class ResponseHandler : public http_client::EventHandler
3131
*/
3232
ResponseHandler(bool console_debug = false) : console_debug_{console_debug} {}
3333

34+
std::string BuildResponseLogMessage(http_client::Response &response,
35+
const std::string &body) noexcept
36+
{
37+
std::stringstream ss;
38+
ss << "Status:" << response.GetStatusCode() << "Header:";
39+
response.ForEachHeader([&ss](opentelemetry::nostd::string_view header_name,
40+
opentelemetry::nostd::string_view header_value) {
41+
ss << "\t" << header_name.data() << " : " << header_value.data() << ",";
42+
return true;
43+
});
44+
ss << "Body:" << body;
45+
46+
return ss.str();
47+
}
48+
3449
/**
3550
* Automatically called when the response is received, store the body into a string and notify any
3651
* threads blocked on this result
3752
*/
3853
void OnResponse(http_client::Response &response) noexcept override
3954
{
55+
std::string log_message;
56+
4057
// Lock the private members so they can't be read while being modified
4158
{
4259
std::unique_lock<std::mutex> lk(mutex_);
4360

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

64+
if (response.GetStatusCode() != 200 && response.GetStatusCode() != 202)
65+
{
66+
log_message = BuildResponseLogMessage(response, body_);
67+
68+
OTEL_INTERNAL_LOG_ERROR("ES Log Exporter] Export failed, " << log_message);
69+
}
70+
4771
if (console_debug_)
4872
{
49-
OTEL_INTERNAL_LOG_DEBUG(
50-
"[ES Log Exporter] Got response from Elasticsearch, response body: " << body_);
73+
if (log_message.empty())
74+
{
75+
log_message = BuildResponseLogMessage(response, body_);
76+
}
77+
78+
OTEL_INTERNAL_LOG_DEBUG("[ES Log Exporter] Got response from Elasticsearch, "
79+
<< log_message);
5180
}
5281

5382
// Set the response_received_ flag to true and notify any threads waiting on this result

exporters/otlp/src/otlp_http_client.cc

+31-10
Original file line numberDiff line numberDiff line change
@@ -79,38 +79,59 @@ class ResponseHandler : public http_client::EventHandler
7979
stopping_.store(false);
8080
}
8181

82+
std::string BuildResponseLogMessage(http_client::Response &response,
83+
const std::string &body) noexcept
84+
{
85+
std::stringstream ss;
86+
ss << "Status:" << response.GetStatusCode() << "Header:";
87+
response.ForEachHeader([&ss](opentelemetry::nostd::string_view header_name,
88+
opentelemetry::nostd::string_view header_value) {
89+
ss << "\t" << header_name.data() << " : " << header_value.data() << ",";
90+
return true;
91+
});
92+
ss << "Body:" << body;
93+
94+
return ss.str();
95+
}
96+
8297
/**
8398
* Automatically called when the response is received, store the body into a string and notify any
8499
* threads blocked on this result
85100
*/
86101
void OnResponse(http_client::Response &response) noexcept override
87102
{
103+
sdk::common::ExportResult result = sdk::common::ExportResult::kSuccess;
104+
std::string log_message;
88105
// Lock the private members so they can't be read while being modified
89106
{
90107
std::unique_lock<std::mutex> lk(mutex_);
91108

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

112+
if (response.GetStatusCode() != 200 && response.GetStatusCode() != 202)
113+
{
114+
log_message = BuildResponseLogMessage(response, body_);
115+
116+
OTEL_INTERNAL_LOG_ERROR("OTLP HTTP Client] Export failed, " << log_message);
117+
result = sdk::common::ExportResult::kFailure;
118+
}
119+
95120
if (console_debug_)
96121
{
97-
std::stringstream ss;
98-
ss << "[OTLP HTTP Client] Status:" << response.GetStatusCode() << "Header:";
99-
response.ForEachHeader([&ss](opentelemetry::nostd::string_view header_name,
100-
opentelemetry::nostd::string_view header_value) {
101-
ss << "\t" << header_name.data() << " : " << header_value.data() << ",";
102-
return true;
103-
});
104-
ss << "Body:" << body_;
105-
OTEL_INTERNAL_LOG_DEBUG(ss.str());
122+
if (log_message.empty())
123+
{
124+
log_message = BuildResponseLogMessage(response, body_);
125+
}
126+
OTEL_INTERNAL_LOG_DEBUG("[OTLP HTTP Client] Export success, " << log_message);
106127
}
107128
}
108129

109130
{
110131
bool expected = false;
111132
if (stopping_.compare_exchange_strong(expected, true, std::memory_order_release))
112133
{
113-
Unbind(sdk::common::ExportResult::kSuccess);
134+
Unbind(result);
114135
}
115136
}
116137
}

0 commit comments

Comments
 (0)