Skip to content

Commit 8165c2d

Browse files
authored
[EXPORTER] Fix Elasticsearch log exporter aborting on invalid UTF-8 (#4501)
1 parent a0d2d75 commit 8165c2d

3 files changed

Lines changed: 135 additions & 1 deletion

File tree

‎CHANGELOG.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ Increment the:
1515

1616
## [Unreleased]
1717

18+
* [EXPORTER] Fix the Elasticsearch log exporter aborting the process when a log
19+
record's body or attributes contain bytes that are not valid UTF-8. The
20+
exporter now substitutes the replacement character for the invalid bytes
21+
and continues, instead of `nlohmann::json::dump()` throwing out of a
22+
`noexcept` function.
23+
[#4439](https://github.com/open-telemetry/opentelemetry-cpp/issues/4439)
24+
1825
* [CONFIGURATION] Apply general `attribute_limits` per individual limit field.
1926
If a model-specific limit is set it is used, otherwise the matching general
2027
limit, otherwise the model-specific default. Limit fields on

‎exporters/elasticsearch/src/es_log_record_exporter.cc‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,13 @@ sdk::common::ExportResult ElasticsearchLogRecordExporter::Export(
429429
// Add the context of the Recordable
430430
auto json_record = std::unique_ptr<ElasticSearchRecordable>(
431431
static_cast<ElasticSearchRecordable *>(record.release()));
432-
body += json_record->GetJSON().dump() + "\n";
432+
// A log record's body or attributes may carry bytes that are not valid UTF-8 (e.g. a
433+
// truncated multibyte sequence, or a payload read in another encoding). dump() throws
434+
// on those by default, and Export() is noexcept, so the exception would otherwise
435+
// terminate the process. error_handler_t::replace substitutes U+FFFD for the invalid
436+
// bytes instead, so the record is still exported with everything else intact.
437+
body += json_record->GetJSON().dump(-1, ' ', false, nlohmann::json::error_handler_t::replace) +
438+
"\n";
433439
}
434440
std::vector<uint8_t> body_vec(body.begin(), body.end());
435441
request->SetBody(body_vec);

‎exporters/elasticsearch/test/es_log_record_exporter_test.cc‎

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
#include "opentelemetry/exporters/elasticsearch/es_log_record_exporter.h"
55
#include "opentelemetry/common/timestamp.h"
66
#include "opentelemetry/exporters/elasticsearch/es_log_recordable.h"
7+
#include "opentelemetry/ext/http/client/http_client.h"
78
#include "opentelemetry/logs/severity.h"
9+
#include "opentelemetry/nostd/function_ref.h"
810
#include "opentelemetry/nostd/span.h"
911
#include "opentelemetry/nostd/string_view.h"
1012
#include "opentelemetry/nostd/utility.h"
@@ -23,11 +25,130 @@
2325
#include <utility>
2426
#include "nlohmann/json.hpp"
2527

28+
namespace
29+
{
30+
namespace http_client = opentelemetry::ext::http::client;
31+
32+
// A response shaped like a successful Elasticsearch bulk reply: the exporter looks for
33+
// `"failed" : 0` in the body (see ElasticsearchLogRecordExporter::Export) in addition to the
34+
// status code before reporting success.
35+
class FakeResponse final : public http_client::Response
36+
{
37+
public:
38+
FakeResponse()
39+
{
40+
static const std::string kSuccessBody = R"({"errors": false, "failed" : 0})";
41+
body_.assign(kSuccessBody.begin(), kSuccessBody.end());
42+
}
43+
44+
const http_client::Body &GetBody() const noexcept override { return body_; }
45+
46+
bool ForEachHeader(opentelemetry::nostd::function_ref<bool(opentelemetry::nostd::string_view,
47+
opentelemetry::nostd::string_view)>)
48+
const noexcept override
49+
{
50+
return true;
51+
}
52+
53+
bool ForEachHeader(const opentelemetry::nostd::string_view &,
54+
opentelemetry::nostd::function_ref<bool(opentelemetry::nostd::string_view,
55+
opentelemetry::nostd::string_view)>)
56+
const noexcept override
57+
{
58+
return true;
59+
}
60+
61+
http_client::StatusCode GetStatusCode() const noexcept override { return 200; }
62+
63+
private:
64+
http_client::Body body_;
65+
};
66+
67+
// A request that accepts and discards everything set on it: the synchronous export path only
68+
// needs a Request to exist, not to inspect what was written to it.
69+
class FakeRequest final : public http_client::Request
70+
{
71+
public:
72+
void SetMethod(http_client::Method) noexcept override {}
73+
void SetUri(opentelemetry::nostd::string_view) noexcept override {}
74+
void SetSslOptions(const http_client::HttpSslOptions &) noexcept override {}
75+
void SetBody(http_client::Body &) noexcept override {}
76+
void AddHeader(opentelemetry::nostd::string_view,
77+
opentelemetry::nostd::string_view) noexcept override
78+
{}
79+
void ReplaceHeader(opentelemetry::nostd::string_view,
80+
opentelemetry::nostd::string_view) noexcept override
81+
{}
82+
void SetTimeoutMs(std::chrono::milliseconds) noexcept override {}
83+
void SetCompression(const http_client::Compression &) noexcept override {}
84+
void EnableLogging(bool) noexcept override {}
85+
void SetRetryPolicy(const http_client::RetryPolicy &) noexcept override {}
86+
};
87+
88+
// A session whose SendRequest() answers synchronously with a successful FakeResponse, so the
89+
// exporter's own wait for a response returns immediately without needing a real connection.
90+
class FakeSession final : public http_client::Session
91+
{
92+
public:
93+
std::shared_ptr<http_client::Request> CreateRequest() noexcept override
94+
{
95+
return std::make_shared<FakeRequest>();
96+
}
97+
98+
void SendRequest(std::shared_ptr<http_client::EventHandler> handler) noexcept override
99+
{
100+
FakeResponse response;
101+
handler->OnResponse(response);
102+
}
103+
104+
bool IsSessionActive() noexcept override { return true; }
105+
bool CancelSession() noexcept override { return true; }
106+
bool FinishSession() noexcept override { return true; }
107+
};
108+
109+
class FakeHttpClient final : public http_client::HttpClient
110+
{
111+
public:
112+
std::shared_ptr<http_client::Session> CreateSession(
113+
opentelemetry::nostd::string_view) noexcept override
114+
{
115+
return std::make_shared<FakeSession>();
116+
}
117+
118+
bool CancelAllSessions() noexcept override { return true; }
119+
bool FinishAllSessions() noexcept override { return true; }
120+
void SetMaxSessionsPerConnection(std::size_t) noexcept override {}
121+
};
122+
123+
} // namespace
124+
26125
namespace sdklogs = opentelemetry::sdk::logs;
27126
namespace logs_api = opentelemetry::logs;
28127
namespace nostd = opentelemetry::nostd;
29128
namespace logs_exporter = opentelemetry::exporter::logs;
30129

130+
// Regression test: a log record whose body carries bytes that are not valid UTF-8 used to
131+
// abort the process. ElasticSearchRecordable::WriteValue stores the value as given, and
132+
// Export() previously called nlohmann::json::dump() with its default strict error handler,
133+
// which throws on invalid UTF-8; since Export() is noexcept, that throw became
134+
// std::terminate(). The exporter now tolerates it instead of crashing.
135+
TEST(ElasticsearchLogsExporterTests, ExportingARecordWithInvalidUtf8DoesNotAbort)
136+
{
137+
logs_exporter::ElasticsearchExporterOptions options;
138+
auto http_client = std::make_shared<FakeHttpClient>();
139+
auto exporter = std::unique_ptr<sdklogs::LogRecordExporter>(
140+
new logs_exporter::ElasticsearchLogRecordExporter(options, http_client));
141+
142+
auto record = exporter->MakeRecordable();
143+
std::string body = "payload ";
144+
body += "\xC3\x28"; // a two byte sequence that is not valid UTF-8
145+
record->SetBody(body);
146+
147+
auto result = exporter->Export(nostd::span<std::unique_ptr<sdklogs::Recordable>>(&record, 1));
148+
149+
EXPECT_EQ(result, opentelemetry::sdk::common::ExportResult::kSuccess);
150+
}
151+
31152
TEST(ElasticsearchLogsExporterTests, CustomClientConstructionSucceeds)
32153
{
33154
logs_exporter::ElasticsearchExporterOptions opts;

0 commit comments

Comments
 (0)