|
4 | 4 | #include "opentelemetry/exporters/elasticsearch/es_log_record_exporter.h" |
5 | 5 | #include "opentelemetry/common/timestamp.h" |
6 | 6 | #include "opentelemetry/exporters/elasticsearch/es_log_recordable.h" |
| 7 | +#include "opentelemetry/ext/http/client/http_client.h" |
7 | 8 | #include "opentelemetry/logs/severity.h" |
| 9 | +#include "opentelemetry/nostd/function_ref.h" |
8 | 10 | #include "opentelemetry/nostd/span.h" |
9 | 11 | #include "opentelemetry/nostd/string_view.h" |
10 | 12 | #include "opentelemetry/nostd/utility.h" |
|
23 | 25 | #include <utility> |
24 | 26 | #include "nlohmann/json.hpp" |
25 | 27 |
|
| 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 | + |
26 | 125 | namespace sdklogs = opentelemetry::sdk::logs; |
27 | 126 | namespace logs_api = opentelemetry::logs; |
28 | 127 | namespace nostd = opentelemetry::nostd; |
29 | 128 | namespace logs_exporter = opentelemetry::exporter::logs; |
30 | 129 |
|
| 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 | + |
31 | 152 | TEST(ElasticsearchLogsExporterTests, CustomClientConstructionSucceeds) |
32 | 153 | { |
33 | 154 | logs_exporter::ElasticsearchExporterOptions opts; |
|
0 commit comments