From 88840777db620c0bc2d8c1a2a87997123dadf251 Mon Sep 17 00:00:00 2001 From: Oblivion Date: Fri, 28 Jan 2022 19:01:16 +0000 Subject: [PATCH 01/35] zipkin --- exporters/zipkin/test/zipkin_exporter_test.cc | 144 ++---------------- 1 file changed, 10 insertions(+), 134 deletions(-) diff --git a/exporters/zipkin/test/zipkin_exporter_test.cc b/exporters/zipkin/test/zipkin_exporter_test.cc index 67d5c1c8ce..d7e0a1007d 100644 --- a/exporters/zipkin/test/zipkin_exporter_test.cc +++ b/exporters/zipkin/test/zipkin_exporter_test.cc @@ -14,8 +14,6 @@ # include # include "gmock/gmock.h" -# include "nlohmann/json.hpp" - # if defined(_MSC_VER) # include "opentelemetry/sdk/common/env_variables.h" using opentelemetry::sdk::common::setenv; @@ -39,124 +37,9 @@ static nostd::span MakeSpan(T (&array)[N]) return nostd::span(array); } -class ZipkinExporterTestPeer : public ::testing::Test, HTTP_SERVER_NS::HttpRequestCallback +class ZipkinExporterTestPeer : public ::testing::Test { -protected: - HTTP_SERVER_NS::HttpServer server_; - std::string server_address_; - std::atomic is_setup_; - std::atomic is_running_; - std::mutex mtx_requests; - std::condition_variable cv_got_events; - std::vector received_requests_json_; - std::map received_requests_headers_; - public: - ZipkinExporterTestPeer() : is_setup_(false), is_running_(false){}; - - virtual void SetUp() override - { - if (is_setup_.exchange(true)) - { - return; - } - int port = server_.addListeningPort(14371); - std::ostringstream os; - os << "localhost:" << port; - server_address_ = "http://" + os.str() + "/v1/traces"; - server_.setServerName(os.str()); - server_.setKeepalive(false); - server_.addHandler("/v1/traces", *this); - server_.start(); - is_running_ = true; - } - - virtual void TearDown() override - { - if (!is_setup_.exchange(false)) - return; - server_.stop(); - is_running_ = false; - } - - virtual int onHttpRequest(HTTP_SERVER_NS::HttpRequest const &request, - HTTP_SERVER_NS::HttpResponse &response) override - { - const std::string *request_content_type = nullptr; - { - auto it = request.headers.find("Content-Type"); - if (it != request.headers.end()) - { - request_content_type = &it->second; - } - } - received_requests_headers_ = request.headers; - - int response_status = 0; - std::string kHttpJsonContentType{"application/json"}; - if (request.uri == "/v1/traces") - { - response.headers["Content-Type"] = kHttpJsonContentType; - std::unique_lock lk(mtx_requests); - if (nullptr != request_content_type && *request_content_type == kHttpJsonContentType) - { - auto json = nlohmann::json::parse(request.content, nullptr, false); - response.headers["Content-Type"] = kHttpJsonContentType; - if (json.is_discarded()) - { - response.body = "{\"code\": 400, \"message\": \"Parse json failed\"}"; - response_status = 400; - } - else - { - received_requests_json_.push_back(json); - response.body = "{\"code\": 0, \"message\": \"success\"}"; - } - } - else - { - response.body = "{\"code\": 400, \"message\": \"Unsupported content type\"}"; - response_status = 400; - } - - response_status = 200; - } - else - { - std::unique_lock lk(mtx_requests); - response.headers["Content-Type"] = "text/plain"; - response.body = "404 Not Found"; - response_status = 200; - } - - cv_got_events.notify_one(); - - return response_status; - } - - bool waitForRequests(unsigned timeOutSec, size_t expected_count = 1) - { - std::unique_lock lk(mtx_requests); - if (cv_got_events.wait_for(lk, std::chrono::milliseconds(1000 * timeOutSec), - [&] { return getCurrentRequestCount() >= expected_count; })) - { - return true; - } - return false; - } - - size_t getCurrentRequestCount() const { return received_requests_json_.size(); } - -public: - std::unique_ptr GetExporter() - { - ZipkinExporterOptions opts; - opts.endpoint = server_address_; - opts.headers.insert( - std::make_pair("Custom-Header-Key", "Custom-Header-Value")); - return std::unique_ptr(new ZipkinExporter(opts)); - } - std::unique_ptr GetExporter( std::shared_ptr http_client) { @@ -188,8 +71,10 @@ class MockHttpClient : public opentelemetry::ext::http::client::HttpClientSync // Create spans, let processor call Export() TEST_F(ZipkinExporterTestPeer, ExportJsonIntegrationTest) { - size_t old_count = getCurrentRequestCount(); - auto exporter = GetExporter(); + auto mock_http_client = new MockHttpClient; + // Leave a comment line here or different version of clang-format has a different result here + auto exporter = GetExporter( + std::shared_ptr{mock_http_client}); resource::ResourceAttributes resource_attributes = {{"service.name", "unit_test_service"}, {"tenant.id", "test_user"}}; @@ -227,6 +112,11 @@ TEST_F(ZipkinExporterTestPeer, ExportJsonIntegrationTest) child_span_opts.parent = parent_span->GetContext(); auto child_span = tracer->StartSpan("Test child span", child_span_opts); + EXPECT_CALL(*mock_http_client, Post(_, _, _)) + .Times(Exactly(1)) + .WillOnce(Return(ByMove(std::move(ext::http::client::Result{ + std::unique_ptr{new ext::http::client::curl::Response()}, + ext::http::client::SessionState::Response})))); child_span->End(); parent_span->End(); @@ -235,20 +125,6 @@ TEST_F(ZipkinExporterTestPeer, ExportJsonIntegrationTest) .ToLowerBase16(MakeSpan(trace_id_hex)); report_trace_id.assign(trace_id_hex, sizeof(trace_id_hex)); } - - ASSERT_TRUE(waitForRequests(30, old_count + 1)); - auto check_json = received_requests_json_.back(); - auto trace_id_kv = check_json.at(0).find("traceId"); - auto received_trace_id = trace_id_kv.value().get(); - EXPECT_EQ(received_trace_id, report_trace_id); - { - auto custom_header = received_requests_headers_.find("Custom-Header-Key"); - ASSERT_TRUE(custom_header != received_requests_headers_.end()); - if (custom_header != received_requests_headers_.end()) - { - EXPECT_EQ("Custom-Header-Value", custom_header->second); - } - } } // Create spans, let processor call Export() From 843b2c27612a6fe648316786c9b91d45bebd07c7 Mon Sep 17 00:00:00 2001 From: Oblivion Date: Fri, 28 Jan 2022 20:36:25 +0000 Subject: [PATCH 02/35] otlp_http --- .../exporters/otlp/otlp_http_client.h | 4 +- .../exporters/otlp/otlp_http_exporter.h | 10 +- exporters/otlp/src/otlp_http_exporter.cc | 21 +- .../otlp/test/otlp_http_exporter_test.cc | 202 ++++-------------- 4 files changed, 60 insertions(+), 177 deletions(-) diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_client.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_client.h index 35939204b3..7bb185d281 100644 --- a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_client.h +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_client.h @@ -104,8 +104,8 @@ class OtlpHttpClient * @param message message to export, it should be ExportTraceServiceRequest, * ExportMetricsServiceRequest or ExportLogsServiceRequest */ - sdk::common::ExportResult Export(const google::protobuf::Message &message) noexcept; - + virtual sdk::common::ExportResult Export(const google::protobuf::Message &message) noexcept; + virtual ~OtlpHttpClient() {} /** * Shut down the HTTP client. * @param timeout an optional timeout, the default timeout of 0 means that no diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_exporter.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_exporter.h index 852745ac39..d5e80ecd7c 100644 --- a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_exporter.h +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_exporter.h @@ -98,7 +98,15 @@ class OtlpHttpExporter final : public opentelemetry::sdk::trace::SpanExporter const OtlpHttpExporterOptions options_; // Object that stores the HTTP sessions that have been created - OtlpHttpClient http_client_; + std::unique_ptr http_client_; + // For testing + friend class OtlpHttpExporterTestPeer; + /** + * Create an ZipkinExporter using the specified thrift sender. + * Only tests can call this constructor directly. + * @param http_client the http client to be used for exporting + */ + OtlpHttpExporter(std::unique_ptr http_client); }; } // namespace otlp } // namespace exporter diff --git a/exporters/otlp/src/otlp_http_exporter.cc b/exporters/otlp/src/otlp_http_exporter.cc index 8b017f25f1..ca705d2e30 100644 --- a/exporters/otlp/src/otlp_http_exporter.cc +++ b/exporters/otlp/src/otlp_http_exporter.cc @@ -23,15 +23,18 @@ OtlpHttpExporter::OtlpHttpExporter() : OtlpHttpExporter(OtlpHttpExporterOptions( OtlpHttpExporter::OtlpHttpExporter(const OtlpHttpExporterOptions &options) : options_(options), - http_client_(OtlpHttpClientOptions(options.url, - options.content_type, - options.json_bytes_mapping, - options.use_json_name, - options.console_debug, - options.timeout, - options.http_headers)) + http_client_(new OtlpHttpClient(OtlpHttpClientOptions(options.url, + options.content_type, + options.json_bytes_mapping, + options.use_json_name, + options.console_debug, + options.timeout, + options.http_headers))) {} +OtlpHttpExporter::OtlpHttpExporter(std::unique_ptr http_client) + : options_(OtlpHttpExporterOptions()), http_client_(std::move(http_client)) +{} // ----------------------------- Exporter methods ------------------------------ std::unique_ptr OtlpHttpExporter::MakeRecordable() noexcept @@ -45,12 +48,12 @@ opentelemetry::sdk::common::ExportResult OtlpHttpExporter::Export( { proto::collector::trace::v1::ExportTraceServiceRequest service_request; OtlpRecordableUtils::PopulateRequest(spans, &service_request); - return http_client_.Export(service_request); + return http_client_->Export(service_request); } bool OtlpHttpExporter::Shutdown(std::chrono::microseconds timeout) noexcept { - return http_client_.Shutdown(timeout); + return http_client_->Shutdown(timeout); } } // namespace otlp diff --git a/exporters/otlp/test/otlp_http_exporter_test.cc b/exporters/otlp/test/otlp_http_exporter_test.cc index 446b9aec29..515cdb5003 100755 --- a/exporters/otlp/test/otlp_http_exporter_test.cc +++ b/exporters/otlp/test/otlp_http_exporter_test.cc @@ -17,6 +17,7 @@ # include "opentelemetry/trace/provider.h" # include +# include "gmock/gmock.h" # include "nlohmann/json.hpp" @@ -42,144 +43,12 @@ static nostd::span MakeSpan(T (&array)[N]) return nostd::span(array); } -class OtlpHttpExporterTestPeer : public ::testing::Test, public HTTP_SERVER_NS::HttpRequestCallback +class OtlpHttpExporterTestPeer : public ::testing::Test { -protected: - HTTP_SERVER_NS::HttpServer server_; - std::string server_address_; - std::atomic is_setup_; - std::atomic is_running_; - std::mutex mtx_requests; - std::condition_variable cv_got_events; - std::vector received_requests_json_; - std::vector - received_requests_binary_; - std::map received_requests_headers_; - public: - OtlpHttpExporterTestPeer() : is_setup_(false), is_running_(false){}; - - virtual void SetUp() override - { - if (is_setup_.exchange(true)) - { - return; - } - int port = server_.addListeningPort(14371); - std::ostringstream os; - os << "localhost:" << port; - server_address_ = "http://" + os.str() + "/v1/traces"; - server_.setServerName(os.str()); - server_.setKeepalive(false); - server_.addHandler("/v1/traces", *this); - server_.start(); - is_running_ = true; - } - - virtual void TearDown() override - { - if (!is_setup_.exchange(false)) - return; - server_.stop(); - is_running_ = false; - } - - virtual int onHttpRequest(HTTP_SERVER_NS::HttpRequest const &request, - HTTP_SERVER_NS::HttpResponse &response) override + std::unique_ptr GetExporter(std::unique_ptr http_client) { - const std::string *request_content_type = nullptr; - { - auto it = request.headers.find("Content-Type"); - if (it != request.headers.end()) - { - request_content_type = &it->second; - } - } - received_requests_headers_ = request.headers; - - int response_status = 0; - - if (request.uri == "/v1/traces") - { - response.headers["Content-Type"] = "application/json"; - std::unique_lock lk(mtx_requests); - if (nullptr != request_content_type && *request_content_type == kHttpBinaryContentType) - { - opentelemetry::proto::collector::trace::v1::ExportTraceServiceRequest request_body; - if (request_body.ParseFromArray(&request.content[0], - static_cast(request.content.size()))) - { - received_requests_binary_.push_back(request_body); - response.body = "{\"code\": 0, \"message\": \"success\"}"; - } - else - { - response.body = "{\"code\": 400, \"message\": \"Parse binary failed\"}"; - response_status = 400; - } - } - else if (nullptr != request_content_type && *request_content_type == kHttpJsonContentType) - { - auto json = nlohmann::json::parse(request.content, nullptr, false); - response.headers["Content-Type"] = "application/json"; - if (json.is_discarded()) - { - response.body = "{\"code\": 400, \"message\": \"Parse json failed\"}"; - response_status = 400; - } - else - { - received_requests_json_.push_back(json); - response.body = "{\"code\": 0, \"message\": \"success\"}"; - } - } - else - { - response.body = "{\"code\": 400, \"message\": \"Unsupported content type\"}"; - response_status = 400; - } - - response_status = 200; - } - else - { - std::unique_lock lk(mtx_requests); - response.headers["Content-Type"] = "text/plain"; - response.body = "404 Not Found"; - response_status = 200; - } - - cv_got_events.notify_one(); - - return response_status; - } - - bool waitForRequests(unsigned timeOutSec, size_t expected_count = 1) - { - std::unique_lock lk(mtx_requests); - if (cv_got_events.wait_for(lk, std::chrono::milliseconds(1000 * timeOutSec), - [&] { return getCurrentRequestCount() >= expected_count; })) - { - return true; - } - return false; - } - - size_t getCurrentRequestCount() const - { - return received_requests_json_.size() + received_requests_binary_.size(); - } - -public: - std::unique_ptr GetExporter(HttpRequestContentType content_type) - { - OtlpHttpExporterOptions opts; - opts.url = server_address_; - opts.content_type = content_type; - opts.console_debug = true; - opts.http_headers.insert( - std::make_pair("Custom-Header-Key", "Custom-Header-Value")); - return std::unique_ptr(new OtlpHttpExporter(opts)); + return std::unique_ptr(new OtlpHttpExporter(std::move(http_client))); } // Get the options associated with the given exporter. @@ -189,11 +58,34 @@ class OtlpHttpExporterTestPeer : public ::testing::Test, public HTTP_SERVER_NS:: } }; +class MockOtlpHttpClient : public OtlpHttpClient +{ +public: + MockOtlpHttpClient(OtlpHttpClientOptions &&options) : OtlpHttpClient(std::move(options)) {} + MOCK_METHOD(sdk::common::ExportResult, + Export, + (const google::protobuf::Message &), + (noexcept, override)); +}; + +MockOtlpHttpClient *GetMockOtlpHttpClient(HttpRequestContentType content_type) +{ + OtlpHttpExporterOptions options; + options.content_type = content_type; + options.console_debug = true; + options.http_headers.insert( + std::make_pair("Custom-Header-Key", "Custom-Header-Value")); + OtlpHttpClientOptions otlpHttpClientOptions( + options.url, options.content_type, options.json_bytes_mapping, options.use_json_name, + options.console_debug, options.timeout, options.http_headers); + return new MockOtlpHttpClient(std::move(otlpHttpClientOptions)); +} + // Create spans, let processor call Export() TEST_F(OtlpHttpExporterTestPeer, ExportJsonIntegrationTest) { - size_t old_count = getCurrentRequestCount(); - auto exporter = GetExporter(HttpRequestContentType::kJson); + auto mockOtlpHttpClient = GetMockOtlpHttpClient(HttpRequestContentType::kJson); + auto exporter = GetExporter(std::unique_ptr{mockOtlpHttpClient}); resource::ResourceAttributes resource_attributes = {{"service.name", "unit_test_service"}, {"tenant.id", "test_user"}}; @@ -231,6 +123,9 @@ TEST_F(OtlpHttpExporterTestPeer, ExportJsonIntegrationTest) child_span_opts.parent = parent_span->GetContext(); auto child_span = tracer->StartSpan("Test child span", child_span_opts); + EXPECT_CALL(*mockOtlpHttpClient, Export(_)) + .Times(Exactly(1)) + .WillOnce(Return(sdk::common::ExportResult::kSuccess)); child_span->End(); parent_span->End(); @@ -239,30 +134,13 @@ TEST_F(OtlpHttpExporterTestPeer, ExportJsonIntegrationTest) .ToLowerBase16(MakeSpan(trace_id_hex)); report_trace_id.assign(trace_id_hex, sizeof(trace_id_hex)); } - - ASSERT_TRUE(waitForRequests(30, old_count + 1)); - auto check_json = received_requests_json_.back(); - auto resource_span = *check_json["resource_spans"].begin(); - auto instrumentation_library_span = *resource_span["instrumentation_library_spans"].begin(); - auto span = *instrumentation_library_span["spans"].begin(); - auto received_trace_id = span["trace_id"].get(); - EXPECT_EQ(received_trace_id, report_trace_id); - { - auto custom_header = received_requests_headers_.find("Custom-Header-Key"); - ASSERT_TRUE(custom_header != received_requests_headers_.end()); - if (custom_header != received_requests_headers_.end()) - { - EXPECT_EQ("Custom-Header-Value", custom_header->second); - } - } } // Create spans, let processor call Export() TEST_F(OtlpHttpExporterTestPeer, ExportBinaryIntegrationTest) { - size_t old_count = getCurrentRequestCount(); - - auto exporter = GetExporter(HttpRequestContentType::kBinary); + auto mockOtlpHttpClient = GetMockOtlpHttpClient(HttpRequestContentType::kBinary); + auto exporter = GetExporter(std::unique_ptr{mockOtlpHttpClient}); resource::ResourceAttributes resource_attributes = {{"service.name", "unit_test_service"}, {"tenant.id", "test_user"}}; @@ -301,6 +179,9 @@ TEST_F(OtlpHttpExporterTestPeer, ExportBinaryIntegrationTest) child_span_opts.parent = parent_span->GetContext(); auto child_span = tracer->StartSpan("Test child span", child_span_opts); + EXPECT_CALL(*mockOtlpHttpClient, Export(_)) + .Times(Exactly(1)) + .WillOnce(Return(sdk::common::ExportResult::kSuccess)); child_span->End(); parent_span->End(); @@ -309,15 +190,6 @@ TEST_F(OtlpHttpExporterTestPeer, ExportBinaryIntegrationTest) .CopyBytesTo(MakeSpan(trace_id_binary)); report_trace_id.assign(reinterpret_cast(trace_id_binary), sizeof(trace_id_binary)); } - - ASSERT_TRUE(waitForRequests(30, old_count + 1)); - - auto received_trace_id = received_requests_binary_.back() - .resource_spans(0) - .instrumentation_library_spans(0) - .spans(0) - .trace_id(); - EXPECT_EQ(received_trace_id, report_trace_id); } // Test exporter configuration options From fcd0816a95fa72b7c0adc9812cb028ec7be2ff44 Mon Sep 17 00:00:00 2001 From: Oblivion Date: Sat, 29 Jan 2022 18:30:35 +0000 Subject: [PATCH 03/35] virtual for test only --- .../opentelemetry/exporters/otlp/otlp_http_client.h | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_client.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_client.h index 7bb185d281..679f96e252 100644 --- a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_client.h +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_client.h @@ -88,6 +88,11 @@ struct OtlpHttpClientOptions {} }; +#ifdef TEST +# define VIRTUAL_TEST virtual +#else +# define VIRTUAL_TEST +#endif /** * The OTLP HTTP client exports span data in OpenTelemetry Protocol (OTLP) format. */ @@ -98,14 +103,16 @@ class OtlpHttpClient * Create an OtlpHttpClient using the given options. */ explicit OtlpHttpClient(OtlpHttpClientOptions &&options); +#ifdef TEST + VIRTUAL_TEST ~OtlpHttpClient() {} +#endif /** * Export * @param message message to export, it should be ExportTraceServiceRequest, * ExportMetricsServiceRequest or ExportLogsServiceRequest */ - virtual sdk::common::ExportResult Export(const google::protobuf::Message &message) noexcept; - virtual ~OtlpHttpClient() {} + VIRTUAL_TEST sdk::common::ExportResult Export(const google::protobuf::Message &message) noexcept; /** * Shut down the HTTP client. * @param timeout an optional timeout, the default timeout of 0 means that no From f914d646cd48d7574ee06f4dace1f180d43cba60 Mon Sep 17 00:00:00 2001 From: Oblivion Date: Sat, 29 Jan 2022 18:54:44 +0000 Subject: [PATCH 04/35] otlp_http_log --- .../exporters/otlp/otlp_http_exporter.h | 2 +- .../exporters/otlp/otlp_http_log_exporter.h | 10 +- exporters/otlp/src/otlp_http_log_exporter.cc | 21 +- .../otlp/test/otlp_http_log_exporter_test.cc | 229 +++--------------- 4 files changed, 59 insertions(+), 203 deletions(-) diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_exporter.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_exporter.h index d5e80ecd7c..9d85f1539c 100644 --- a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_exporter.h +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_exporter.h @@ -102,7 +102,7 @@ class OtlpHttpExporter final : public opentelemetry::sdk::trace::SpanExporter // For testing friend class OtlpHttpExporterTestPeer; /** - * Create an ZipkinExporter using the specified thrift sender. + * Create an OtlpHttpExporter using the specified http client. * Only tests can call this constructor directly. * @param http_client the http client to be used for exporting */ diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_log_exporter.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_log_exporter.h index 738a60d8f6..f2167539df 100644 --- a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_log_exporter.h +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_log_exporter.h @@ -97,7 +97,15 @@ class OtlpHttpLogExporter final : public opentelemetry::sdk::logs::LogExporter const OtlpHttpLogExporterOptions options_; // Object that stores the HTTP sessions that have been created - OtlpHttpClient http_client_; + std::unique_ptr http_client_; + // For testing + friend class OtlpHttpLogExporterTestPeer; + /** + * Create an OtlpHttpLogExporter using the specified http client. + * Only tests can call this constructor directly. + * @param http_client the http client to be used for exporting + */ + OtlpHttpLogExporter(std::unique_ptr http_client); }; } // namespace otlp } // namespace exporter diff --git a/exporters/otlp/src/otlp_http_log_exporter.cc b/exporters/otlp/src/otlp_http_log_exporter.cc index f4cc6a9f9c..3b64ee4eda 100644 --- a/exporters/otlp/src/otlp_http_log_exporter.cc +++ b/exporters/otlp/src/otlp_http_log_exporter.cc @@ -25,15 +25,18 @@ OtlpHttpLogExporter::OtlpHttpLogExporter() : OtlpHttpLogExporter(OtlpHttpLogExpo OtlpHttpLogExporter::OtlpHttpLogExporter(const OtlpHttpLogExporterOptions &options) : options_(options), - http_client_(OtlpHttpClientOptions(options.url, - options.content_type, - options.json_bytes_mapping, - options.use_json_name, - options.console_debug, - options.timeout, - options.http_headers)) + http_client_(new OtlpHttpClient(OtlpHttpClientOptions(options.url, + options.content_type, + options.json_bytes_mapping, + options.use_json_name, + options.console_debug, + options.timeout, + options.http_headers))) {} +OtlpHttpLogExporter::OtlpHttpLogExporter(std::unique_ptr http_client) + : options_(OtlpHttpLogExporterOptions()), http_client_(std::move(http_client)) +{} // ----------------------------- Exporter methods ------------------------------ std::unique_ptr OtlpHttpLogExporter::MakeRecordable() noexcept @@ -47,12 +50,12 @@ opentelemetry::sdk::common::ExportResult OtlpHttpLogExporter::Export( { proto::collector::logs::v1::ExportLogsServiceRequest service_request; OtlpRecordableUtils::PopulateRequest(logs, &service_request); - return http_client_.Export(service_request); + return http_client_->Export(service_request); } bool OtlpHttpLogExporter::Shutdown(std::chrono::microseconds timeout) noexcept { - return http_client_.Shutdown(timeout); + return http_client_->Shutdown(timeout); } } // namespace otlp diff --git a/exporters/otlp/test/otlp_http_log_exporter_test.cc b/exporters/otlp/test/otlp_http_log_exporter_test.cc index c3c9095ec2..fe929ac502 100755 --- a/exporters/otlp/test/otlp_http_log_exporter_test.cc +++ b/exporters/otlp/test/otlp_http_log_exporter_test.cc @@ -22,6 +22,7 @@ # include "opentelemetry/sdk/resource/resource.h" # include +# include "gmock/gmock.h" # include "nlohmann/json.hpp" @@ -45,145 +46,12 @@ static nostd::span MakeSpan(T (&array)[N]) return nostd::span(array); } -class OtlpHttpLogExporterTestPeer : public ::testing::Test, - public HTTP_SERVER_NS::HttpRequestCallback +class OtlpHttpLogExporterTestPeer : public ::testing::Test { -protected: - HTTP_SERVER_NS::HttpServer server_; - std::string server_address_; - std::atomic is_setup_; - std::atomic is_running_; - std::mutex mtx_requests; - std::condition_variable cv_got_events; - std::vector received_requests_json_; - std::vector - received_requests_binary_; - std::map received_requests_headers_; - -public: - OtlpHttpLogExporterTestPeer() : is_setup_(false), is_running_(false){}; - - virtual void SetUp() override - { - if (is_setup_.exchange(true)) - { - return; - } - int port = server_.addListeningPort(14372); - std::ostringstream os; - os << "localhost:" << port; - server_address_ = "http://" + os.str() + "/v1/logs"; - server_.setServerName(os.str()); - server_.setKeepalive(false); - server_.addHandler("/v1/logs", *this); - server_.start(); - is_running_ = true; - } - - virtual void TearDown() override - { - if (!is_setup_.exchange(false)) - return; - server_.stop(); - is_running_ = false; - } - - virtual int onHttpRequest(HTTP_SERVER_NS::HttpRequest const &request, - HTTP_SERVER_NS::HttpResponse &response) override - { - const std::string *request_content_type = nullptr; - { - auto it = request.headers.find("Content-Type"); - if (it != request.headers.end()) - { - request_content_type = &it->second; - } - } - received_requests_headers_ = request.headers; - - int response_status = 0; - - if (request.uri == "/v1/logs") - { - response.headers["Content-Type"] = "application/json"; - std::unique_lock lk(mtx_requests); - if (nullptr != request_content_type && *request_content_type == kHttpBinaryContentType) - { - opentelemetry::proto::collector::logs::v1::ExportLogsServiceRequest request_body; - if (request_body.ParseFromArray(&request.content[0], - static_cast(request.content.size()))) - { - received_requests_binary_.push_back(request_body); - response.body = "{\"code\": 0, \"message\": \"success\"}"; - } - else - { - response.body = "{\"code\": 400, \"message\": \"Parse binary failed\"}"; - response_status = 400; - } - } - else if (nullptr != request_content_type && *request_content_type == kHttpJsonContentType) - { - auto json = nlohmann::json::parse(request.content, nullptr, false); - response.headers["Content-Type"] = "application/json"; - if (json.is_discarded()) - { - response.body = "{\"code\": 400, \"message\": \"Parse json failed\"}"; - response_status = 400; - } - else - { - received_requests_json_.push_back(json); - response.body = "{\"code\": 0, \"message\": \"success\"}"; - } - } - else - { - response.body = "{\"code\": 400, \"message\": \"Unsupported content type\"}"; - response_status = 400; - } - - response_status = 200; - } - else - { - std::unique_lock lk(mtx_requests); - response.headers["Content-Type"] = "text/plain"; - response.body = "404 Not Found"; - response_status = 200; - } - - cv_got_events.notify_one(); - - return response_status; - } - - bool waitForRequests(unsigned timeOutSec, size_t expected_count = 1) - { - std::unique_lock lk(mtx_requests); - if (cv_got_events.wait_for(lk, std::chrono::milliseconds(1000 * timeOutSec), - [&] { return getCurrentRequestCount() >= expected_count; })) - { - return true; - } - return false; - } - - size_t getCurrentRequestCount() const - { - return received_requests_json_.size() + received_requests_binary_.size(); - } - public: - std::unique_ptr GetExporter(HttpRequestContentType content_type) + std::unique_ptr GetExporter(std::unique_ptr http_client) { - OtlpHttpLogExporterOptions opts; - opts.url = server_address_; - opts.content_type = content_type; - opts.console_debug = true; - opts.http_headers.insert( - std::make_pair("Custom-Header-Key", "Custom-Header-Value")); - return std::unique_ptr(new OtlpHttpLogExporter(opts)); + return std::unique_ptr(new OtlpHttpLogExporter(std::move(http_client))); } // Get the options associated with the given exporter. @@ -193,11 +61,34 @@ class OtlpHttpLogExporterTestPeer : public ::testing::Test, } }; +class MockOtlpHttpClient : public OtlpHttpClient +{ +public: + MockOtlpHttpClient(OtlpHttpClientOptions &&options) : OtlpHttpClient(std::move(options)) {} + MOCK_METHOD(sdk::common::ExportResult, + Export, + (const google::protobuf::Message &), + (noexcept, override)); +}; + +MockOtlpHttpClient *GetMockOtlpHttpClient(HttpRequestContentType content_type) +{ + OtlpHttpLogExporterOptions options; + options.content_type = content_type; + options.console_debug = true; + options.http_headers.insert( + std::make_pair("Custom-Header-Key", "Custom-Header-Value")); + OtlpHttpClientOptions otlpHttpClientOptions( + options.url, options.content_type, options.json_bytes_mapping, options.use_json_name, + options.console_debug, options.timeout, options.http_headers); + return new MockOtlpHttpClient(std::move(otlpHttpClientOptions)); +} + // Create log records, let processor call Export() TEST_F(OtlpHttpLogExporterTestPeer, ExportJsonIntegrationTest) { - size_t old_count = getCurrentRequestCount(); - auto exporter = GetExporter(HttpRequestContentType::kJson); + auto mockOtlpHttpClient = GetMockOtlpHttpClient(HttpRequestContentType::kJson); + auto exporter = GetExporter(std::unique_ptr{mockOtlpHttpClient}); bool attribute_storage_bool_value[] = {true, false, true}; int32_t attribute_storage_int32_value[] = {1, 2}; @@ -225,6 +116,9 @@ TEST_F(OtlpHttpLogExporterTestPeer, ExportJsonIntegrationTest) const std::string schema_url{"https://opentelemetry.io/schemas/1.2.0"}; auto logger = provider->GetLogger("test", "", "opentelelemtry_library", "", schema_url); + EXPECT_CALL(*mockOtlpHttpClient, Export(_)) + .Times(Exactly(1)) + .WillOnce(Return(sdk::common::ExportResult::kSuccess)); logger->Log(opentelemetry::logs::Severity::kInfo, "Log name", "Log message", {{"service.name", "unit_test_service"}, {"tenant.id", "test_user"}, @@ -251,46 +145,13 @@ TEST_F(OtlpHttpLogExporterTestPeer, ExportJsonIntegrationTest) span_id.ToLowerBase16(MakeSpan(span_id_hex)); report_span_id.assign(span_id_hex, sizeof(span_id_hex)); } - - ASSERT_TRUE(waitForRequests(30, old_count + 1)); - auto check_json = received_requests_json_.back(); - auto resource_logs = *check_json["resource_logs"].begin(); - auto instrumentation_library_span = *resource_logs["instrumentation_library_logs"].begin(); - auto log = *instrumentation_library_span["logs"].begin(); - auto received_trace_id = log["trace_id"].get(); - auto received_span_id = log["span_id"].get(); - EXPECT_EQ(received_trace_id, report_trace_id); - EXPECT_EQ(received_span_id, report_span_id); - EXPECT_EQ("Log name", log["name"].get()); - EXPECT_EQ("Log message", log["body"]["string_value"].get()); - EXPECT_LE(15, log["attributes"].size()); - bool check_service_name = false; - for (auto attribute : log["attributes"]) - { - if ("service.name" == attribute["key"].get()) - { - check_service_name = true; - EXPECT_EQ("unit_test_service", attribute["value"]["string_value"].get()); - } - } - ASSERT_TRUE(check_service_name); - - { - auto custom_header = received_requests_headers_.find("Custom-Header-Key"); - ASSERT_TRUE(custom_header != received_requests_headers_.end()); - if (custom_header != received_requests_headers_.end()) - { - EXPECT_EQ("Custom-Header-Value", custom_header->second); - } - } } // Create log records, let processor call Export() TEST_F(OtlpHttpLogExporterTestPeer, ExportBinaryIntegrationTest) { - size_t old_count = getCurrentRequestCount(); - - auto exporter = GetExporter(HttpRequestContentType::kBinary); + auto mockOtlpHttpClient = GetMockOtlpHttpClient(HttpRequestContentType::kJson); + auto exporter = GetExporter(std::unique_ptr{mockOtlpHttpClient}); bool attribute_storage_bool_value[] = {true, false, true}; int32_t attribute_storage_int32_value[] = {1, 2}; @@ -316,6 +177,9 @@ TEST_F(OtlpHttpLogExporterTestPeer, ExportBinaryIntegrationTest) const std::string schema_url{"https://opentelemetry.io/schemas/1.2.0"}; auto logger = provider->GetLogger("test", "", "opentelelemtry_library", "", schema_url); + EXPECT_CALL(*mockOtlpHttpClient, Export(_)) + .Times(Exactly(1)) + .WillOnce(Return(sdk::common::ExportResult::kSuccess)); logger->Log(opentelemetry::logs::Severity::kInfo, "Log name", "Log message", {{"service.name", "unit_test_service"}, {"tenant.id", "test_user"}, @@ -339,25 +203,6 @@ TEST_F(OtlpHttpLogExporterTestPeer, ExportBinaryIntegrationTest) report_trace_id.assign(reinterpret_cast(trace_id_bin), sizeof(trace_id_bin)); report_span_id.assign(reinterpret_cast(span_id_bin), sizeof(span_id_bin)); } - - ASSERT_TRUE(waitForRequests(30, old_count + 1)); - auto received_log = - received_requests_binary_.back().resource_logs(0).instrumentation_library_logs(0).logs(0); - EXPECT_EQ(received_log.trace_id(), report_trace_id); - EXPECT_EQ(received_log.span_id(), report_span_id); - EXPECT_EQ("Log name", received_log.name()); - EXPECT_EQ("Log message", received_log.body().string_value()); - EXPECT_LE(15, received_log.attributes_size()); - bool check_service_name = false; - for (auto &attribute : received_log.attributes()) - { - if ("service.name" == attribute.key()) - { - check_service_name = true; - EXPECT_EQ("unit_test_service", attribute.value().string_value()); - } - } - ASSERT_TRUE(check_service_name); } // Test exporter configuration options From aeb88ab144ab42c441b129883b2c58af842e9449 Mon Sep 17 00:00:00 2001 From: Oblivion Date: Sun, 30 Jan 2022 12:27:06 +0000 Subject: [PATCH 05/35] client message matcher --- .../exporters/otlp/otlp_http_client.h | 7 + exporters/otlp/src/otlp_http_client.cc | 19 ++- .../otlp/test/otlp_http_exporter_test.cc | 137 ++++++++++++------ 3 files changed, 111 insertions(+), 52 deletions(-) diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_client.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_client.h index 679f96e252..e5b376ab0d 100644 --- a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_client.h +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_client.h @@ -13,6 +13,7 @@ #include "opentelemetry/ext/http/client/http_client.h" #include "opentelemetry/sdk/common/exporter_utils.h" +#include "nlohmann/json.hpp" #include "opentelemetry/exporters/otlp/otlp_environment.h" #include @@ -121,6 +122,12 @@ class OtlpHttpClient */ bool Shutdown(std::chrono::microseconds timeout = std::chrono::microseconds(0)) noexcept; + static void ConvertGenericMessageToJson(nlohmann::json &value, + const google::protobuf::Message &message, + const OtlpHttpClientOptions &options); + static bool SerializeToHttpBody(opentelemetry::ext::http::client::Body &output, + const google::protobuf::Message &message); + private: // Stores if this HTTP client had its Shutdown() method called bool is_shutdown_ = false; diff --git a/exporters/otlp/src/otlp_http_client.cc b/exporters/otlp/src/otlp_http_client.cc index fd86b1e136..317a0b1967 100644 --- a/exporters/otlp/src/otlp_http_client.cc +++ b/exporters/otlp/src/otlp_http_client.cc @@ -12,8 +12,6 @@ #include "opentelemetry/ext/http/client/http_client_factory.h" #include "opentelemetry/ext/http/common/url_parser.h" -#include "nlohmann/json.hpp" - #include "opentelemetry/exporters/otlp/protobuf_include_prefix.h" #include @@ -267,6 +265,8 @@ class ResponseHandler : public http_client::EventHandler bool console_debug_ = false; }; +} // namespace + static inline char HexEncode(unsigned char byte) { #if defined(HAVE_GSL) @@ -340,9 +340,9 @@ static void ConvertListFieldToJson(nlohmann::json &value, const google::protobuf::FieldDescriptor *field_descriptor, const OtlpHttpClientOptions &options); -static void ConvertGenericMessageToJson(nlohmann::json &value, - const google::protobuf::Message &message, - const OtlpHttpClientOptions &options) +void OtlpHttpClient::ConvertGenericMessageToJson(nlohmann::json &value, + const google::protobuf::Message &message, + const OtlpHttpClientOptions &options) { std::vector fields_with_data; message.GetReflection()->ListFields(message, &fields_with_data); @@ -362,7 +362,8 @@ static void ConvertGenericMessageToJson(nlohmann::json &value, } } -static bool SerializeToHttpBody(http_client::Body &output, const google::protobuf::Message &message) +bool OtlpHttpClient::SerializeToHttpBody(http_client::Body &output, + const google::protobuf::Message &message) { auto body_size = message.ByteSizeLong(); if (body_size > 0) @@ -416,7 +417,7 @@ void ConvertGenericFieldToJson(nlohmann::json &value, break; } case google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE: { - ConvertGenericMessageToJson( + OtlpHttpClient::ConvertGenericMessageToJson( value, message.GetReflection()->GetMessage(message, field_descriptor, nullptr), options); break; } @@ -514,7 +515,7 @@ void ConvertListFieldToJson(nlohmann::json &value, for (int i = 0; i < field_size; ++i) { nlohmann::json sub_value; - ConvertGenericMessageToJson( + OtlpHttpClient::ConvertGenericMessageToJson( sub_value, message.GetReflection()->GetRepeatedMessage(message, field_descriptor, i), options); value.push_back(std::move(sub_value)); @@ -560,8 +561,6 @@ void ConvertListFieldToJson(nlohmann::json &value, } } -} // namespace - OtlpHttpClient::OtlpHttpClient(OtlpHttpClientOptions &&options) : options_(options), http_client_(http_client::HttpClientFactory::Create()) {} diff --git a/exporters/otlp/test/otlp_http_exporter_test.cc b/exporters/otlp/test/otlp_http_exporter_test.cc index 515cdb5003..9a61d13ec6 100755 --- a/exporters/otlp/test/otlp_http_exporter_test.cc +++ b/exporters/otlp/test/otlp_http_exporter_test.cc @@ -19,8 +19,8 @@ # include # include "gmock/gmock.h" +# include # include "nlohmann/json.hpp" - # if defined(_MSC_VER) # include "opentelemetry/sdk/common/env_variables.h" using opentelemetry::sdk::common::setenv; @@ -81,10 +81,67 @@ MockOtlpHttpClient *GetMockOtlpHttpClient(HttpRequestContentType content_type) return new MockOtlpHttpClient(std::move(otlpHttpClientOptions)); } +class IsValidMessageMatcher +{ +public: + IsValidMessageMatcher(const std::string &trace_id) : trace_id_(trace_id) {} + template + bool MatchAndExplain(const T &p, MatchResultListener * /* listener */) const + { + OtlpHttpExporterOptions options; + options.content_type = HttpRequestContentType::kJson; + options.console_debug = true; + options.http_headers.insert( + std::make_pair("Custom-Header-Key", "Custom-Header-Value")); + OtlpHttpClientOptions otlpHttpClientOptions( + options.url, options.content_type, options.json_bytes_mapping, options.use_json_name, + options.console_debug, options.timeout, options.http_headers); + nlohmann::json check_json; + OtlpHttpClient::ConvertGenericMessageToJson(check_json, p, otlpHttpClientOptions); + auto resource_span = *check_json["resource_spans"].begin(); + auto instrumentation_library_span = *resource_span["instrumentation_library_spans"].begin(); + auto span = *instrumentation_library_span["spans"].begin(); + auto received_trace_id = span["trace_id"].get(); + + if (trace_id_ != received_trace_id) + { + opentelemetry::ext::http::client::Body body; + OtlpHttpClient::SerializeToHttpBody(body, p); + // received_trace_id = body.resource_spans(0) + // .instrumentation_library_spans(0) + // .spans(0) + // .trace_id(); + + auto msg = std::string(body.begin(), body.end()); + + std::puts(received_trace_id.c_str()); + std::puts(msg.c_str()); + std::puts("######"); + } + return trace_id_ == received_trace_id; + } + + // Describes the property of a value matching this matcher. + void DescribeTo(std::ostream *os) const { *os << "is not NULL"; } + + // Describes the property of a value NOT matching this matcher. + void DescribeNegationTo(std::ostream *os) const { *os << "is NULL"; } + +private: + std::string trace_id_; +}; + +// To construct a polymorphic matcher, pass an instance of the class +// to MakePolymorphicMatcher(). Note the return type. +PolymorphicMatcher IsValidMessage(const std::string &trace_id) +{ + return MakePolymorphicMatcher(IsValidMessageMatcher(trace_id)); +} + // Create spans, let processor call Export() TEST_F(OtlpHttpExporterTestPeer, ExportJsonIntegrationTest) { - auto mockOtlpHttpClient = GetMockOtlpHttpClient(HttpRequestContentType::kJson); + auto mockOtlpHttpClient = GetMockOtlpHttpClient(HttpRequestContentType::kBinary); auto exporter = GetExporter(std::unique_ptr{mockOtlpHttpClient}); resource::ResourceAttributes resource_attributes = {{"service.name", "unit_test_service"}, @@ -114,26 +171,24 @@ TEST_F(OtlpHttpExporterTestPeer, ExportJsonIntegrationTest) new sdk::trace::TracerProvider(std::move(processor), resource)); std::string report_trace_id; - { - char trace_id_hex[2 * trace_api::TraceId::kSize] = {0}; - auto tracer = provider->GetTracer("test"); - auto parent_span = tracer->StartSpan("Test parent span"); - - trace_api::StartSpanOptions child_span_opts = {}; - child_span_opts.parent = parent_span->GetContext(); - - auto child_span = tracer->StartSpan("Test child span", child_span_opts); - EXPECT_CALL(*mockOtlpHttpClient, Export(_)) - .Times(Exactly(1)) - .WillOnce(Return(sdk::common::ExportResult::kSuccess)); - child_span->End(); - parent_span->End(); - - nostd::get(child_span_opts.parent) - .trace_id() - .ToLowerBase16(MakeSpan(trace_id_hex)); - report_trace_id.assign(trace_id_hex, sizeof(trace_id_hex)); - } + char trace_id_hex[2 * trace_api::TraceId::kSize] = {0}; + auto tracer = provider->GetTracer("test"); + auto parent_span = tracer->StartSpan("Test parent span"); + + trace_api::StartSpanOptions child_span_opts = {}; + child_span_opts.parent = parent_span->GetContext(); + + auto child_span = tracer->StartSpan("Test child span", child_span_opts); + + child_span->End(); + parent_span->End(); + nostd::get(child_span_opts.parent) + .trace_id() + .ToLowerBase16(MakeSpan(trace_id_hex)); + report_trace_id.assign(trace_id_hex, sizeof(trace_id_hex)); + EXPECT_CALL(*mockOtlpHttpClient, Export(IsValidMessage(report_trace_id))) + .Times(Exactly(1)) + .WillOnce(Return(sdk::common::ExportResult::kSuccess)); } // Create spans, let processor call Export() @@ -170,26 +225,24 @@ TEST_F(OtlpHttpExporterTestPeer, ExportBinaryIntegrationTest) new sdk::trace::TracerProvider(std::move(processor), resource)); std::string report_trace_id; - { - uint8_t trace_id_binary[trace_api::TraceId::kSize] = {0}; - auto tracer = provider->GetTracer("test"); - auto parent_span = tracer->StartSpan("Test parent span"); - - trace_api::StartSpanOptions child_span_opts = {}; - child_span_opts.parent = parent_span->GetContext(); - - auto child_span = tracer->StartSpan("Test child span", child_span_opts); - EXPECT_CALL(*mockOtlpHttpClient, Export(_)) - .Times(Exactly(1)) - .WillOnce(Return(sdk::common::ExportResult::kSuccess)); - child_span->End(); - parent_span->End(); - - nostd::get(child_span_opts.parent) - .trace_id() - .CopyBytesTo(MakeSpan(trace_id_binary)); - report_trace_id.assign(reinterpret_cast(trace_id_binary), sizeof(trace_id_binary)); - } + uint8_t trace_id_binary[trace_api::TraceId::kSize] = {0}; + auto tracer = provider->GetTracer("test"); + auto parent_span = tracer->StartSpan("Test parent span"); + + trace_api::StartSpanOptions child_span_opts = {}; + child_span_opts.parent = parent_span->GetContext(); + + auto child_span = tracer->StartSpan("Test child span", child_span_opts); + child_span->End(); + parent_span->End(); + + nostd::get(child_span_opts.parent) + .trace_id() + .CopyBytesTo(MakeSpan(trace_id_binary)); + report_trace_id.assign(reinterpret_cast(trace_id_binary), sizeof(trace_id_binary)); + // EXPECT_CALL(*mockOtlpHttpClient, Export(IsValidMessage(report_trace_id))) + // .Times(Exactly(1)) + // .WillOnce(Return(sdk::common::ExportResult::kSuccess)); } // Test exporter configuration options From 67f969a0684e00c8afde26b43e0f87ef5710980b Mon Sep 17 00:00:00 2001 From: Oblivion Date: Sun, 30 Jan 2022 12:39:01 +0000 Subject: [PATCH 06/35] otlp_http fix binary integeratoin --- exporters/otlp/test/otlp_http_exporter_test.cc | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/exporters/otlp/test/otlp_http_exporter_test.cc b/exporters/otlp/test/otlp_http_exporter_test.cc index 9a61d13ec6..81a6e7218c 100755 --- a/exporters/otlp/test/otlp_http_exporter_test.cc +++ b/exporters/otlp/test/otlp_http_exporter_test.cc @@ -141,7 +141,7 @@ PolymorphicMatcher IsValidMessage(const std::string &trac // Create spans, let processor call Export() TEST_F(OtlpHttpExporterTestPeer, ExportJsonIntegrationTest) { - auto mockOtlpHttpClient = GetMockOtlpHttpClient(HttpRequestContentType::kBinary); + auto mockOtlpHttpClient = GetMockOtlpHttpClient(HttpRequestContentType::kJson); auto exporter = GetExporter(std::unique_ptr{mockOtlpHttpClient}); resource::ResourceAttributes resource_attributes = {{"service.name", "unit_test_service"}, @@ -225,9 +225,9 @@ TEST_F(OtlpHttpExporterTestPeer, ExportBinaryIntegrationTest) new sdk::trace::TracerProvider(std::move(processor), resource)); std::string report_trace_id; - uint8_t trace_id_binary[trace_api::TraceId::kSize] = {0}; - auto tracer = provider->GetTracer("test"); - auto parent_span = tracer->StartSpan("Test parent span"); + char trace_id_hex[2 * trace_api::TraceId::kSize] = {0}; + auto tracer = provider->GetTracer("test"); + auto parent_span = tracer->StartSpan("Test parent span"); trace_api::StartSpanOptions child_span_opts = {}; child_span_opts.parent = parent_span->GetContext(); @@ -238,11 +238,11 @@ TEST_F(OtlpHttpExporterTestPeer, ExportBinaryIntegrationTest) nostd::get(child_span_opts.parent) .trace_id() - .CopyBytesTo(MakeSpan(trace_id_binary)); - report_trace_id.assign(reinterpret_cast(trace_id_binary), sizeof(trace_id_binary)); - // EXPECT_CALL(*mockOtlpHttpClient, Export(IsValidMessage(report_trace_id))) - // .Times(Exactly(1)) - // .WillOnce(Return(sdk::common::ExportResult::kSuccess)); + .ToLowerBase16(MakeSpan(trace_id_hex)); + report_trace_id.assign(reinterpret_cast(trace_id_hex), sizeof(trace_id_hex)); + EXPECT_CALL(*mockOtlpHttpClient, Export(IsValidMessage(report_trace_id))) + .Times(Exactly(1)) + .WillOnce(Return(sdk::common::ExportResult::kSuccess)); } // Test exporter configuration options From 1da777954e87845c969f4699d4b7491ce755c01b Mon Sep 17 00:00:00 2001 From: Oblivion Date: Sun, 30 Jan 2022 12:44:01 +0000 Subject: [PATCH 07/35] clean up --- .../otlp/test/otlp_http_exporter_test.cc | 22 ++----------------- 1 file changed, 2 insertions(+), 20 deletions(-) diff --git a/exporters/otlp/test/otlp_http_exporter_test.cc b/exporters/otlp/test/otlp_http_exporter_test.cc index 81a6e7218c..37ad3f7425 100755 --- a/exporters/otlp/test/otlp_http_exporter_test.cc +++ b/exporters/otlp/test/otlp_http_exporter_test.cc @@ -102,30 +102,12 @@ class IsValidMessageMatcher auto instrumentation_library_span = *resource_span["instrumentation_library_spans"].begin(); auto span = *instrumentation_library_span["spans"].begin(); auto received_trace_id = span["trace_id"].get(); - - if (trace_id_ != received_trace_id) - { - opentelemetry::ext::http::client::Body body; - OtlpHttpClient::SerializeToHttpBody(body, p); - // received_trace_id = body.resource_spans(0) - // .instrumentation_library_spans(0) - // .spans(0) - // .trace_id(); - - auto msg = std::string(body.begin(), body.end()); - - std::puts(received_trace_id.c_str()); - std::puts(msg.c_str()); - std::puts("######"); - } return trace_id_ == received_trace_id; } - // Describes the property of a value matching this matcher. - void DescribeTo(std::ostream *os) const { *os << "is not NULL"; } + void DescribeTo(std::ostream *os) const { *os << "received trace_id matches"; } - // Describes the property of a value NOT matching this matcher. - void DescribeNegationTo(std::ostream *os) const { *os << "is NULL"; } + void DescribeNegationTo(std::ostream *os) const { *os << "received trace_id does not matche"; } private: std::string trace_id_; From a4256dbdc568422983cbfcc2bcb9b24d42925762 Mon Sep 17 00:00:00 2001 From: Oblivion Date: Sun, 30 Jan 2022 13:19:43 +0000 Subject: [PATCH 08/35] otlp_http_log_exporter --- .../otlp/test/otlp_http_exporter_test.cc | 2 - .../otlp/test/otlp_http_log_exporter_test.cc | 194 +++++++++++------- 2 files changed, 117 insertions(+), 79 deletions(-) diff --git a/exporters/otlp/test/otlp_http_exporter_test.cc b/exporters/otlp/test/otlp_http_exporter_test.cc index 37ad3f7425..2cf0ee21c6 100755 --- a/exporters/otlp/test/otlp_http_exporter_test.cc +++ b/exporters/otlp/test/otlp_http_exporter_test.cc @@ -113,8 +113,6 @@ class IsValidMessageMatcher std::string trace_id_; }; -// To construct a polymorphic matcher, pass an instance of the class -// to MakePolymorphicMatcher(). Note the return type. PolymorphicMatcher IsValidMessage(const std::string &trace_id) { return MakePolymorphicMatcher(IsValidMessageMatcher(trace_id)); diff --git a/exporters/otlp/test/otlp_http_log_exporter_test.cc b/exporters/otlp/test/otlp_http_log_exporter_test.cc index fe929ac502..418c5d41e7 100755 --- a/exporters/otlp/test/otlp_http_log_exporter_test.cc +++ b/exporters/otlp/test/otlp_http_log_exporter_test.cc @@ -84,6 +84,43 @@ MockOtlpHttpClient *GetMockOtlpHttpClient(HttpRequestContentType content_type) return new MockOtlpHttpClient(std::move(otlpHttpClientOptions)); } +class IsValidMessageMatcher +{ +public: + IsValidMessageMatcher(const std::string &trace_id) : trace_id_(trace_id) {} + template + bool MatchAndExplain(const T &p, MatchResultListener * /* listener */) const + { + OtlpHttpLogExporterOptions options; + options.content_type = HttpRequestContentType::kJson; + options.console_debug = true; + options.http_headers.insert( + std::make_pair("Custom-Header-Key", "Custom-Header-Value")); + OtlpHttpClientOptions otlpHttpClientOptions( + options.url, options.content_type, options.json_bytes_mapping, options.use_json_name, + options.console_debug, options.timeout, options.http_headers); + nlohmann::json check_json; + OtlpHttpClient::ConvertGenericMessageToJson(check_json, p, otlpHttpClientOptions); + auto resource_span = *check_json["resource_logs"].begin(); + auto instrumentation_library_span = *resource_span["instrumentation_library_logs"].begin(); + auto span = *instrumentation_library_span["logs"].begin(); + auto received_trace_id = span["trace_id"].get(); + return trace_id_ == received_trace_id; + } + + void DescribeTo(std::ostream *os) const { *os << "received trace_id matches"; } + + void DescribeNegationTo(std::ostream *os) const { *os << "received trace_id does not matche"; } + +private: + std::string trace_id_; +}; + +PolymorphicMatcher IsValidMessage(const std::string &trace_id) +{ + return MakePolymorphicMatcher(IsValidMessageMatcher(trace_id)); +} + // Create log records, let processor call Export() TEST_F(OtlpHttpLogExporterTestPeer, ExportJsonIntegrationTest) { @@ -104,47 +141,46 @@ TEST_F(OtlpHttpLogExporterTestPeer, ExportJsonIntegrationTest) std::string report_trace_id; std::string report_span_id; - { - uint8_t trace_id_bin[opentelemetry::trace::TraceId::kSize] = { - '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; - char trace_id_hex[2 * opentelemetry::trace::TraceId::kSize] = {0}; - opentelemetry::trace::TraceId trace_id{trace_id_bin}; - uint8_t span_id_bin[opentelemetry::trace::SpanId::kSize] = {'7', '6', '5', '4', - '3', '2', '1', '0'}; - char span_id_hex[2 * opentelemetry::trace::SpanId::kSize] = {0}; - opentelemetry::trace::SpanId span_id{span_id_bin}; - - const std::string schema_url{"https://opentelemetry.io/schemas/1.2.0"}; - auto logger = provider->GetLogger("test", "", "opentelelemtry_library", "", schema_url); - EXPECT_CALL(*mockOtlpHttpClient, Export(_)) - .Times(Exactly(1)) - .WillOnce(Return(sdk::common::ExportResult::kSuccess)); - logger->Log(opentelemetry::logs::Severity::kInfo, "Log name", "Log message", - {{"service.name", "unit_test_service"}, - {"tenant.id", "test_user"}, - {"bool_value", true}, - {"int32_value", static_cast(1)}, - {"uint32_value", static_cast(2)}, - {"int64_value", static_cast(0x1100000000LL)}, - {"uint64_value", static_cast(0x1200000000ULL)}, - {"double_value", static_cast(3.1)}, - {"vec_bool_value", attribute_storage_bool_value}, - {"vec_int32_value", attribute_storage_int32_value}, - {"vec_uint32_value", attribute_storage_uint32_value}, - {"vec_int64_value", attribute_storage_int64_value}, - {"vec_uint64_value", attribute_storage_uint64_value}, - {"vec_double_value", attribute_storage_double_value}, - {"vec_string_value", attribute_storage_string_value}}, - trace_id, span_id, - opentelemetry::trace::TraceFlags{opentelemetry::trace::TraceFlags::kIsSampled}, - std::chrono::system_clock::now()); - - trace_id.ToLowerBase16(MakeSpan(trace_id_hex)); - report_trace_id.assign(trace_id_hex, sizeof(trace_id_hex)); - - span_id.ToLowerBase16(MakeSpan(span_id_hex)); - report_span_id.assign(span_id_hex, sizeof(span_id_hex)); - } + uint8_t trace_id_bin[opentelemetry::trace::TraceId::kSize] = { + '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; + char trace_id_hex[2 * opentelemetry::trace::TraceId::kSize] = {0}; + opentelemetry::trace::TraceId trace_id{trace_id_bin}; + uint8_t span_id_bin[opentelemetry::trace::SpanId::kSize] = {'7', '6', '5', '4', + '3', '2', '1', '0'}; + char span_id_hex[2 * opentelemetry::trace::SpanId::kSize] = {0}; + opentelemetry::trace::SpanId span_id{span_id_bin}; + + const std::string schema_url{"https://opentelemetry.io/schemas/1.2.0"}; + auto logger = provider->GetLogger("test", "", "opentelelemtry_library", "", schema_url); + logger->Log(opentelemetry::logs::Severity::kInfo, "Log name", "Log message", + {{"service.name", "unit_test_service"}, + {"tenant.id", "test_user"}, + {"bool_value", true}, + {"int32_value", static_cast(1)}, + {"uint32_value", static_cast(2)}, + {"int64_value", static_cast(0x1100000000LL)}, + {"uint64_value", static_cast(0x1200000000ULL)}, + {"double_value", static_cast(3.1)}, + {"vec_bool_value", attribute_storage_bool_value}, + {"vec_int32_value", attribute_storage_int32_value}, + {"vec_uint32_value", attribute_storage_uint32_value}, + {"vec_int64_value", attribute_storage_int64_value}, + {"vec_uint64_value", attribute_storage_uint64_value}, + {"vec_double_value", attribute_storage_double_value}, + {"vec_string_value", attribute_storage_string_value}}, + trace_id, span_id, + opentelemetry::trace::TraceFlags{opentelemetry::trace::TraceFlags::kIsSampled}, + std::chrono::system_clock::now()); + + trace_id.ToLowerBase16(MakeSpan(trace_id_hex)); + report_trace_id.assign(trace_id_hex, sizeof(trace_id_hex)); + + span_id.ToLowerBase16(MakeSpan(span_id_hex)); + report_span_id.assign(span_id_hex, sizeof(span_id_hex)); + + EXPECT_CALL(*mockOtlpHttpClient, Export(IsValidMessage(report_trace_id))) + .Times(Exactly(1)) + .WillOnce(Return(sdk::common::ExportResult::kSuccess)); } // Create log records, let processor call Export() @@ -167,42 +203,46 @@ TEST_F(OtlpHttpLogExporterTestPeer, ExportBinaryIntegrationTest) std::string report_trace_id; std::string report_span_id; - { - uint8_t trace_id_bin[opentelemetry::trace::TraceId::kSize] = { - '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; - opentelemetry::trace::TraceId trace_id{trace_id_bin}; - uint8_t span_id_bin[opentelemetry::trace::SpanId::kSize] = {'7', '6', '5', '4', - '3', '2', '1', '0'}; - opentelemetry::trace::SpanId span_id{span_id_bin}; - - const std::string schema_url{"https://opentelemetry.io/schemas/1.2.0"}; - auto logger = provider->GetLogger("test", "", "opentelelemtry_library", "", schema_url); - EXPECT_CALL(*mockOtlpHttpClient, Export(_)) - .Times(Exactly(1)) - .WillOnce(Return(sdk::common::ExportResult::kSuccess)); - logger->Log(opentelemetry::logs::Severity::kInfo, "Log name", "Log message", - {{"service.name", "unit_test_service"}, - {"tenant.id", "test_user"}, - {"bool_value", true}, - {"int32_value", static_cast(1)}, - {"uint32_value", static_cast(2)}, - {"int64_value", static_cast(0x1100000000LL)}, - {"uint64_value", static_cast(0x1200000000ULL)}, - {"double_value", static_cast(3.1)}, - {"vec_bool_value", attribute_storage_bool_value}, - {"vec_int32_value", attribute_storage_int32_value}, - {"vec_uint32_value", attribute_storage_uint32_value}, - {"vec_int64_value", attribute_storage_int64_value}, - {"vec_uint64_value", attribute_storage_uint64_value}, - {"vec_double_value", attribute_storage_double_value}, - {"vec_string_value", attribute_storage_string_value}}, - trace_id, span_id, - opentelemetry::trace::TraceFlags{opentelemetry::trace::TraceFlags::kIsSampled}, - std::chrono::system_clock::now()); - - report_trace_id.assign(reinterpret_cast(trace_id_bin), sizeof(trace_id_bin)); - report_span_id.assign(reinterpret_cast(span_id_bin), sizeof(span_id_bin)); - } + uint8_t trace_id_bin[opentelemetry::trace::TraceId::kSize] = { + '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; + char trace_id_hex[2 * opentelemetry::trace::TraceId::kSize] = {0}; + opentelemetry::trace::TraceId trace_id{trace_id_bin}; + uint8_t span_id_bin[opentelemetry::trace::SpanId::kSize] = {'7', '6', '5', '4', + '3', '2', '1', '0'}; + char span_id_hex[2 * opentelemetry::trace::SpanId::kSize] = {0}; + opentelemetry::trace::SpanId span_id{span_id_bin}; + + const std::string schema_url{"https://opentelemetry.io/schemas/1.2.0"}; + auto logger = provider->GetLogger("test", "", "opentelelemtry_library", "", schema_url); + logger->Log(opentelemetry::logs::Severity::kInfo, "Log name", "Log message", + {{"service.name", "unit_test_service"}, + {"tenant.id", "test_user"}, + {"bool_value", true}, + {"int32_value", static_cast(1)}, + {"uint32_value", static_cast(2)}, + {"int64_value", static_cast(0x1100000000LL)}, + {"uint64_value", static_cast(0x1200000000ULL)}, + {"double_value", static_cast(3.1)}, + {"vec_bool_value", attribute_storage_bool_value}, + {"vec_int32_value", attribute_storage_int32_value}, + {"vec_uint32_value", attribute_storage_uint32_value}, + {"vec_int64_value", attribute_storage_int64_value}, + {"vec_uint64_value", attribute_storage_uint64_value}, + {"vec_double_value", attribute_storage_double_value}, + {"vec_string_value", attribute_storage_string_value}}, + trace_id, span_id, + opentelemetry::trace::TraceFlags{opentelemetry::trace::TraceFlags::kIsSampled}, + std::chrono::system_clock::now()); + + trace_id.ToLowerBase16(MakeSpan(trace_id_hex)); + report_trace_id.assign(trace_id_hex, sizeof(trace_id_hex)); + + span_id.ToLowerBase16(MakeSpan(span_id_hex)); + report_span_id.assign(span_id_hex, sizeof(span_id_hex)); + + EXPECT_CALL(*mockOtlpHttpClient, Export(IsValidMessage(report_trace_id))) + .Times(Exactly(1)) + .WillOnce(Return(sdk::common::ExportResult::kSuccess)); } // Test exporter configuration options From 230add4dead3a0df3b6adfc2dd9f6ff506b1e1ae Mon Sep 17 00:00:00 2001 From: Oblivion Date: Sun, 30 Jan 2022 13:50:45 +0000 Subject: [PATCH 09/35] otlp_http_log more checks --- .../otlp/test/otlp_http_log_exporter_test.cc | 38 ++++++++++++++----- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/exporters/otlp/test/otlp_http_log_exporter_test.cc b/exporters/otlp/test/otlp_http_log_exporter_test.cc index 418c5d41e7..fe728e5e42 100755 --- a/exporters/otlp/test/otlp_http_log_exporter_test.cc +++ b/exporters/otlp/test/otlp_http_log_exporter_test.cc @@ -87,9 +87,11 @@ MockOtlpHttpClient *GetMockOtlpHttpClient(HttpRequestContentType content_type) class IsValidMessageMatcher { public: - IsValidMessageMatcher(const std::string &trace_id) : trace_id_(trace_id) {} + IsValidMessageMatcher(const std::string &trace_id, const std::string &span_id) + : trace_id_(trace_id), span_id_(span_id) + {} template - bool MatchAndExplain(const T &p, MatchResultListener * /* listener */) const + bool MatchAndExplain(const T &p, MatchResultListener *) const { OtlpHttpLogExporterOptions options; options.content_type = HttpRequestContentType::kJson; @@ -103,9 +105,25 @@ class IsValidMessageMatcher OtlpHttpClient::ConvertGenericMessageToJson(check_json, p, otlpHttpClientOptions); auto resource_span = *check_json["resource_logs"].begin(); auto instrumentation_library_span = *resource_span["instrumentation_library_logs"].begin(); - auto span = *instrumentation_library_span["logs"].begin(); - auto received_trace_id = span["trace_id"].get(); - return trace_id_ == received_trace_id; + auto log = *instrumentation_library_span["logs"].begin(); + auto received_trace_id = log["trace_id"].get(); + auto received_span_id = log["span_id"].get(); + bool is_ok = trace_id_ == received_trace_id; + is_ok &= span_id_ == received_span_id; + is_ok &= "Log name" == log["name"].get(); + is_ok &= "Log message" == log["body"]["string_value"].get(); + is_ok &= 15 <= log["attributes"].size(); + bool check_service_name = false; + for (auto attribute : log["attributes"]) + { + if ("service.name" == attribute["key"].get()) + { + check_service_name = true; + EXPECT_EQ("unit_test_service", attribute["value"]["string_value"].get()); + } + } + is_ok &= check_service_name; + return is_ok; } void DescribeTo(std::ostream *os) const { *os << "received trace_id matches"; } @@ -114,11 +132,13 @@ class IsValidMessageMatcher private: std::string trace_id_; + std::string span_id_; }; -PolymorphicMatcher IsValidMessage(const std::string &trace_id) +PolymorphicMatcher IsValidMessage(const std::string &trace_id, + const std::string &span_id) { - return MakePolymorphicMatcher(IsValidMessageMatcher(trace_id)); + return MakePolymorphicMatcher(IsValidMessageMatcher(trace_id, span_id)); } // Create log records, let processor call Export() @@ -178,7 +198,7 @@ TEST_F(OtlpHttpLogExporterTestPeer, ExportJsonIntegrationTest) span_id.ToLowerBase16(MakeSpan(span_id_hex)); report_span_id.assign(span_id_hex, sizeof(span_id_hex)); - EXPECT_CALL(*mockOtlpHttpClient, Export(IsValidMessage(report_trace_id))) + EXPECT_CALL(*mockOtlpHttpClient, Export(IsValidMessage(report_trace_id, report_span_id))) .Times(Exactly(1)) .WillOnce(Return(sdk::common::ExportResult::kSuccess)); } @@ -240,7 +260,7 @@ TEST_F(OtlpHttpLogExporterTestPeer, ExportBinaryIntegrationTest) span_id.ToLowerBase16(MakeSpan(span_id_hex)); report_span_id.assign(span_id_hex, sizeof(span_id_hex)); - EXPECT_CALL(*mockOtlpHttpClient, Export(IsValidMessage(report_trace_id))) + EXPECT_CALL(*mockOtlpHttpClient, Export(IsValidMessage(report_trace_id, report_span_id))) .Times(Exactly(1)) .WillOnce(Return(sdk::common::ExportResult::kSuccess)); } From 8f4e600c292a5f282e6b34b7488423a4fc0c9bc8 Mon Sep 17 00:00:00 2001 From: Oblivion Date: Sun, 30 Jan 2022 14:18:44 +0000 Subject: [PATCH 10/35] clean up --- .../otlp/test/otlp_http_exporter_test.cc | 32 +++++++++--------- .../otlp/test/otlp_http_log_exporter_test.cc | 33 +++++++++---------- 2 files changed, 31 insertions(+), 34 deletions(-) diff --git a/exporters/otlp/test/otlp_http_exporter_test.cc b/exporters/otlp/test/otlp_http_exporter_test.cc index 2cf0ee21c6..0eca7acf69 100755 --- a/exporters/otlp/test/otlp_http_exporter_test.cc +++ b/exporters/otlp/test/otlp_http_exporter_test.cc @@ -43,6 +43,19 @@ static nostd::span MakeSpan(T (&array)[N]) return nostd::span(array); } +OtlpHttpClientOptions MakeOtlpHttpClientOptions(HttpRequestContentType content_type) +{ + OtlpHttpExporterOptions options; + options.content_type = content_type; + options.console_debug = true; + options.http_headers.insert( + std::make_pair("Custom-Header-Key", "Custom-Header-Value")); + OtlpHttpClientOptions otlpHttpClientOptions( + options.url, options.content_type, options.json_bytes_mapping, options.use_json_name, + options.console_debug, options.timeout, options.http_headers); + return otlpHttpClientOptions; +} + class OtlpHttpExporterTestPeer : public ::testing::Test { public: @@ -70,15 +83,7 @@ class MockOtlpHttpClient : public OtlpHttpClient MockOtlpHttpClient *GetMockOtlpHttpClient(HttpRequestContentType content_type) { - OtlpHttpExporterOptions options; - options.content_type = content_type; - options.console_debug = true; - options.http_headers.insert( - std::make_pair("Custom-Header-Key", "Custom-Header-Value")); - OtlpHttpClientOptions otlpHttpClientOptions( - options.url, options.content_type, options.json_bytes_mapping, options.use_json_name, - options.console_debug, options.timeout, options.http_headers); - return new MockOtlpHttpClient(std::move(otlpHttpClientOptions)); + return new MockOtlpHttpClient(MakeOtlpHttpClientOptions(content_type)); } class IsValidMessageMatcher @@ -88,14 +93,7 @@ class IsValidMessageMatcher template bool MatchAndExplain(const T &p, MatchResultListener * /* listener */) const { - OtlpHttpExporterOptions options; - options.content_type = HttpRequestContentType::kJson; - options.console_debug = true; - options.http_headers.insert( - std::make_pair("Custom-Header-Key", "Custom-Header-Value")); - OtlpHttpClientOptions otlpHttpClientOptions( - options.url, options.content_type, options.json_bytes_mapping, options.use_json_name, - options.console_debug, options.timeout, options.http_headers); + auto otlpHttpClientOptions = MakeOtlpHttpClientOptions(HttpRequestContentType::kJson); nlohmann::json check_json; OtlpHttpClient::ConvertGenericMessageToJson(check_json, p, otlpHttpClientOptions); auto resource_span = *check_json["resource_spans"].begin(); diff --git a/exporters/otlp/test/otlp_http_log_exporter_test.cc b/exporters/otlp/test/otlp_http_log_exporter_test.cc index fe728e5e42..92a3789f0e 100755 --- a/exporters/otlp/test/otlp_http_log_exporter_test.cc +++ b/exporters/otlp/test/otlp_http_log_exporter_test.cc @@ -46,6 +46,19 @@ static nostd::span MakeSpan(T (&array)[N]) return nostd::span(array); } +OtlpHttpClientOptions MakeOtlpHttpClientOptions(HttpRequestContentType content_type) +{ + OtlpHttpLogExporterOptions options; + options.content_type = content_type; + options.console_debug = true; + options.http_headers.insert( + std::make_pair("Custom-Header-Key", "Custom-Header-Value")); + OtlpHttpClientOptions otlpHttpClientOptions( + options.url, options.content_type, options.json_bytes_mapping, options.use_json_name, + options.console_debug, options.timeout, options.http_headers); + return otlpHttpClientOptions; +} + class OtlpHttpLogExporterTestPeer : public ::testing::Test { public: @@ -73,15 +86,8 @@ class MockOtlpHttpClient : public OtlpHttpClient MockOtlpHttpClient *GetMockOtlpHttpClient(HttpRequestContentType content_type) { - OtlpHttpLogExporterOptions options; - options.content_type = content_type; - options.console_debug = true; - options.http_headers.insert( - std::make_pair("Custom-Header-Key", "Custom-Header-Value")); - OtlpHttpClientOptions otlpHttpClientOptions( - options.url, options.content_type, options.json_bytes_mapping, options.use_json_name, - options.console_debug, options.timeout, options.http_headers); - return new MockOtlpHttpClient(std::move(otlpHttpClientOptions)); + + return new MockOtlpHttpClient(MakeOtlpHttpClientOptions(content_type)); } class IsValidMessageMatcher @@ -93,14 +99,7 @@ class IsValidMessageMatcher template bool MatchAndExplain(const T &p, MatchResultListener *) const { - OtlpHttpLogExporterOptions options; - options.content_type = HttpRequestContentType::kJson; - options.console_debug = true; - options.http_headers.insert( - std::make_pair("Custom-Header-Key", "Custom-Header-Value")); - OtlpHttpClientOptions otlpHttpClientOptions( - options.url, options.content_type, options.json_bytes_mapping, options.use_json_name, - options.console_debug, options.timeout, options.http_headers); + auto otlpHttpClientOptions = MakeOtlpHttpClientOptions(HttpRequestContentType::kJson); nlohmann::json check_json; OtlpHttpClient::ConvertGenericMessageToJson(check_json, p, otlpHttpClientOptions); auto resource_span = *check_json["resource_logs"].begin(); From 3ef15823b0b37083a1edfb0c41a1159624930081 Mon Sep 17 00:00:00 2001 From: Oblivion Date: Sun, 30 Jan 2022 15:51:53 +0000 Subject: [PATCH 11/35] zipkin matcher --- exporters/zipkin/test/zipkin_exporter_test.cc | 72 +++++++++++++------ 1 file changed, 50 insertions(+), 22 deletions(-) diff --git a/exporters/zipkin/test/zipkin_exporter_test.cc b/exporters/zipkin/test/zipkin_exporter_test.cc index d7e0a1007d..5fd1dc4f05 100644 --- a/exporters/zipkin/test/zipkin_exporter_test.cc +++ b/exporters/zipkin/test/zipkin_exporter_test.cc @@ -13,6 +13,7 @@ # include # include "gmock/gmock.h" +# include "nlohmann/json.hpp" # if defined(_MSC_VER) # include "opentelemetry/sdk/common/env_variables.h" @@ -68,6 +69,33 @@ class MockHttpClient : public opentelemetry::ext::http::client::HttpClientSync (noexcept, override)); }; +class IsValidMessageMatcher +{ +public: + IsValidMessageMatcher(const std::string &trace_id) : trace_id_(trace_id) {} + template + bool MatchAndExplain(const T &p, MatchResultListener * /* listener */) const + { + auto body = std::string(p.begin(), p.end()); + nlohmann::json check_json = nlohmann::json::parse(body); + auto trace_id_kv = check_json.at(0).find("traceId"); + auto received_trace_id = trace_id_kv.value().get(); + return trace_id_ == received_trace_id; + } + + void DescribeTo(std::ostream *os) const { *os << "received trace_id matches"; } + + void DescribeNegationTo(std::ostream *os) const { *os << "received trace_id does not matche"; } + +private: + std::string trace_id_; +}; + +PolymorphicMatcher IsValidMessage(const std::string &trace_id) +{ + return MakePolymorphicMatcher(IsValidMessageMatcher(trace_id)); +} + // Create spans, let processor call Export() TEST_F(ZipkinExporterTestPeer, ExportJsonIntegrationTest) { @@ -103,28 +131,28 @@ TEST_F(ZipkinExporterTestPeer, ExportJsonIntegrationTest) new sdk::trace::TracerProvider(std::move(processor), resource)); std::string report_trace_id; - { - char trace_id_hex[2 * trace_api::TraceId::kSize] = {0}; - auto tracer = provider->GetTracer("test"); - auto parent_span = tracer->StartSpan("Test parent span"); - - trace_api::StartSpanOptions child_span_opts = {}; - child_span_opts.parent = parent_span->GetContext(); - - auto child_span = tracer->StartSpan("Test child span", child_span_opts); - EXPECT_CALL(*mock_http_client, Post(_, _, _)) - .Times(Exactly(1)) - .WillOnce(Return(ByMove(std::move(ext::http::client::Result{ - std::unique_ptr{new ext::http::client::curl::Response()}, - ext::http::client::SessionState::Response})))); - child_span->End(); - parent_span->End(); - - nostd::get(child_span_opts.parent) - .trace_id() - .ToLowerBase16(MakeSpan(trace_id_hex)); - report_trace_id.assign(trace_id_hex, sizeof(trace_id_hex)); - } + char trace_id_hex[2 * trace_api::TraceId::kSize] = {0}; + auto tracer = provider->GetTracer("test"); + auto parent_span = tracer->StartSpan("Test parent span"); + + trace_api::StartSpanOptions child_span_opts = {}; + child_span_opts.parent = parent_span->GetContext(); + + auto child_span = tracer->StartSpan("Test child span", child_span_opts); + child_span->End(); + parent_span->End(); + + nostd::get(child_span_opts.parent) + .trace_id() + .ToLowerBase16(MakeSpan(trace_id_hex)); + report_trace_id.assign(trace_id_hex, sizeof(trace_id_hex)); + + auto expected_url = nostd::string_view{"http://localhost:9411/api/v2/spans"}; + EXPECT_CALL(*mock_http_client, Post(expected_url, IsValidMessage(report_trace_id), _)) + .Times(Exactly(1)) + .WillOnce(Return(ByMove(std::move(ext::http::client::Result{ + std::unique_ptr{new ext::http::client::curl::Response()}, + ext::http::client::SessionState::Response})))); } // Create spans, let processor call Export() From a47f576b27e08570493cf576b82585867ecf1abc Mon Sep 17 00:00:00 2001 From: Oblivion Date: Sun, 30 Jan 2022 16:22:10 +0000 Subject: [PATCH 12/35] cmake define --- exporters/otlp/CMakeLists.txt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/exporters/otlp/CMakeLists.txt b/exporters/otlp/CMakeLists.txt index 705fc97042..6dce7bb562 100755 --- a/exporters/otlp/CMakeLists.txt +++ b/exporters/otlp/CMakeLists.txt @@ -40,6 +40,9 @@ if(WITH_OTLP_GRPC) endif() if(WITH_OTLP_HTTP) + if(BUILD_TESTING) + add_definitions(-DTEST) + endif() find_package(CURL REQUIRED) add_library(opentelemetry_exporter_otlp_http_client src/otlp_http_client.cc) set_target_properties(opentelemetry_exporter_otlp_http_client From adcc7d3e87bbd9ea7cb8f32ed74759d5fb25b186 Mon Sep 17 00:00:00 2001 From: Ehsan Saei <71217171+esigo@users.noreply.github.com> Date: Sun, 30 Jan 2022 17:34:40 +0100 Subject: [PATCH 13/35] bazel test option --- ci/do_ci.ps1 | 2 +- ci/do_ci.sh | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/ci/do_ci.ps1 b/ci/do_ci.ps1 index da340c9624..966b5e230f 100644 --- a/ci/do_ci.ps1 +++ b/ci/do_ci.ps1 @@ -6,7 +6,7 @@ $action = $args[0] $SRC_DIR=(Get-Item -Path ".\").FullName $BAZEL_OPTIONS="--copt=-DENABLE_METRICS_PREVIEW --copt=-DENABLE_LOGS_PREVIEW --build_tag_filters=-jaeger" -$BAZEL_TEST_OPTIONS="$BAZEL_OPTIONS --test_output=errors" +$BAZEL_TEST_OPTIONS="$BAZEL_OPTIONS --copt=-DTEST --test_output=errors" if (!(test-path build)) { mkdir build diff --git a/ci/do_ci.sh b/ci/do_ci.sh index 368190b6ec..9f59532fd7 100755 --- a/ci/do_ci.sh +++ b/ci/do_ci.sh @@ -60,7 +60,7 @@ mkdir -p "${BUILD_DIR}" mkdir -p "${PLUGIN_DIR}" BAZEL_OPTIONS="--copt=-DENABLE_METRICS_PREVIEW --copt=-DENABLE_LOGS_PREVIEW" -BAZEL_TEST_OPTIONS="$BAZEL_OPTIONS --test_output=errors" +BAZEL_TEST_OPTIONS="$BAZEL_OPTIONS --copt=-DTEST --test_output=errors" # https://github.com/bazelbuild/bazel/issues/4341 BAZEL_MACOS_OPTIONS="$BAZEL_OPRIONS --features=-supports_dynamic_linker --build_tag_filters=-jaeger" From 8f06a91ddae64d35756c9c8a5e341ac02112ffd8 Mon Sep 17 00:00:00 2001 From: Ehsan Saei <71217171+esigo@users.noreply.github.com> Date: Sun, 30 Jan 2022 18:08:10 +0100 Subject: [PATCH 14/35] fix bazel CI --- ci/do_ci.ps1 | 4 ++-- ci/do_ci.sh | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/ci/do_ci.ps1 b/ci/do_ci.ps1 index 966b5e230f..ddd0d38bd7 100644 --- a/ci/do_ci.ps1 +++ b/ci/do_ci.ps1 @@ -5,8 +5,8 @@ $action = $args[0] $SRC_DIR=(Get-Item -Path ".\").FullName -$BAZEL_OPTIONS="--copt=-DENABLE_METRICS_PREVIEW --copt=-DENABLE_LOGS_PREVIEW --build_tag_filters=-jaeger" -$BAZEL_TEST_OPTIONS="$BAZEL_OPTIONS --copt=-DTEST --test_output=errors" +$BAZEL_OPTIONS="--copt=-DENABLE_METRICS_PREVIEW --copt=-DENABLE_LOGS_PREVIEW --build_tag_filters=-jaeger --copt=-DTEST" +$BAZEL_TEST_OPTIONS="$BAZEL_OPTIONS --test_output=errors" if (!(test-path build)) { mkdir build diff --git a/ci/do_ci.sh b/ci/do_ci.sh index 9f59532fd7..5b5f1b1693 100755 --- a/ci/do_ci.sh +++ b/ci/do_ci.sh @@ -59,8 +59,8 @@ mkdir -p "${BUILD_DIR}" [ -z "${PLUGIN_DIR}" ] && export PLUGIN_DIR=$HOME/plugin mkdir -p "${PLUGIN_DIR}" -BAZEL_OPTIONS="--copt=-DENABLE_METRICS_PREVIEW --copt=-DENABLE_LOGS_PREVIEW" -BAZEL_TEST_OPTIONS="$BAZEL_OPTIONS --copt=-DTEST --test_output=errors" +BAZEL_OPTIONS="--copt=-DENABLE_METRICS_PREVIEW --copt=-DENABLE_LOGS_PREVIEW --copt=-DTEST" +BAZEL_TEST_OPTIONS="$BAZEL_OPTIONS --test_output=errors" # https://github.com/bazelbuild/bazel/issues/4341 BAZEL_MACOS_OPTIONS="$BAZEL_OPRIONS --features=-supports_dynamic_linker --build_tag_filters=-jaeger" From 2bf1bd8cc5a65107cf4e9d252461a73515928257 Mon Sep 17 00:00:00 2001 From: Oblivion Date: Sun, 30 Jan 2022 19:19:53 +0000 Subject: [PATCH 15/35] bazel mac --- ci/do_ci.sh | 2 +- .../otlp/test/otlp_http_exporter_test.cc | 21 ++++++++++--------- .../otlp/test/otlp_http_log_exporter_test.cc | 21 +++++++++---------- 3 files changed, 22 insertions(+), 22 deletions(-) diff --git a/ci/do_ci.sh b/ci/do_ci.sh index 5b5f1b1693..c51e9c1ab6 100755 --- a/ci/do_ci.sh +++ b/ci/do_ci.sh @@ -63,7 +63,7 @@ BAZEL_OPTIONS="--copt=-DENABLE_METRICS_PREVIEW --copt=-DENABLE_LOGS_PREVIEW --co BAZEL_TEST_OPTIONS="$BAZEL_OPTIONS --test_output=errors" # https://github.com/bazelbuild/bazel/issues/4341 -BAZEL_MACOS_OPTIONS="$BAZEL_OPRIONS --features=-supports_dynamic_linker --build_tag_filters=-jaeger" +BAZEL_MACOS_OPTIONS="$BAZEL_OPTIONS --features=-supports_dynamic_linker --build_tag_filters=-jaeger" BAZEL_MACOS_TEST_OPTIONS="$BAZEL_MACOS_OPTIONS --test_output=errors" BAZEL_STARTUP_OPTIONS="--output_user_root=$HOME/.cache/bazel" diff --git a/exporters/otlp/test/otlp_http_exporter_test.cc b/exporters/otlp/test/otlp_http_exporter_test.cc index 0eca7acf69..430681f846 100755 --- a/exporters/otlp/test/otlp_http_exporter_test.cc +++ b/exporters/otlp/test/otlp_http_exporter_test.cc @@ -50,10 +50,10 @@ OtlpHttpClientOptions MakeOtlpHttpClientOptions(HttpRequestContentType content_t options.console_debug = true; options.http_headers.insert( std::make_pair("Custom-Header-Key", "Custom-Header-Value")); - OtlpHttpClientOptions otlpHttpClientOptions( + OtlpHttpClientOptions otlp_http_client_options( options.url, options.content_type, options.json_bytes_mapping, options.use_json_name, options.console_debug, options.timeout, options.http_headers); - return otlpHttpClientOptions; + return otlp_http_client_options; } class OtlpHttpExporterTestPeer : public ::testing::Test @@ -93,9 +93,9 @@ class IsValidMessageMatcher template bool MatchAndExplain(const T &p, MatchResultListener * /* listener */) const { - auto otlpHttpClientOptions = MakeOtlpHttpClientOptions(HttpRequestContentType::kJson); + auto otlp_http_client_options = MakeOtlpHttpClientOptions(HttpRequestContentType::kJson); nlohmann::json check_json; - OtlpHttpClient::ConvertGenericMessageToJson(check_json, p, otlpHttpClientOptions); + OtlpHttpClient::ConvertGenericMessageToJson(check_json, p, otlp_http_client_options); auto resource_span = *check_json["resource_spans"].begin(); auto instrumentation_library_span = *resource_span["instrumentation_library_spans"].begin(); auto span = *instrumentation_library_span["spans"].begin(); @@ -119,8 +119,8 @@ PolymorphicMatcher IsValidMessage(const std::string &trac // Create spans, let processor call Export() TEST_F(OtlpHttpExporterTestPeer, ExportJsonIntegrationTest) { - auto mockOtlpHttpClient = GetMockOtlpHttpClient(HttpRequestContentType::kJson); - auto exporter = GetExporter(std::unique_ptr{mockOtlpHttpClient}); + auto mock_otlp_http_client = GetMockOtlpHttpClient(HttpRequestContentType::kJson); + auto exporter = GetExporter(std::unique_ptr{mock_otlp_http_client}); resource::ResourceAttributes resource_attributes = {{"service.name", "unit_test_service"}, {"tenant.id", "test_user"}}; @@ -149,6 +149,7 @@ TEST_F(OtlpHttpExporterTestPeer, ExportJsonIntegrationTest) new sdk::trace::TracerProvider(std::move(processor), resource)); std::string report_trace_id; + char trace_id_hex[2 * trace_api::TraceId::kSize] = {0}; auto tracer = provider->GetTracer("test"); auto parent_span = tracer->StartSpan("Test parent span"); @@ -164,7 +165,7 @@ TEST_F(OtlpHttpExporterTestPeer, ExportJsonIntegrationTest) .trace_id() .ToLowerBase16(MakeSpan(trace_id_hex)); report_trace_id.assign(trace_id_hex, sizeof(trace_id_hex)); - EXPECT_CALL(*mockOtlpHttpClient, Export(IsValidMessage(report_trace_id))) + EXPECT_CALL(*mock_otlp_http_client, Export(IsValidMessage(report_trace_id))) .Times(Exactly(1)) .WillOnce(Return(sdk::common::ExportResult::kSuccess)); } @@ -172,8 +173,8 @@ TEST_F(OtlpHttpExporterTestPeer, ExportJsonIntegrationTest) // Create spans, let processor call Export() TEST_F(OtlpHttpExporterTestPeer, ExportBinaryIntegrationTest) { - auto mockOtlpHttpClient = GetMockOtlpHttpClient(HttpRequestContentType::kBinary); - auto exporter = GetExporter(std::unique_ptr{mockOtlpHttpClient}); + auto mock_otlp_http_client = GetMockOtlpHttpClient(HttpRequestContentType::kBinary); + auto exporter = GetExporter(std::unique_ptr{mock_otlp_http_client}); resource::ResourceAttributes resource_attributes = {{"service.name", "unit_test_service"}, {"tenant.id", "test_user"}}; @@ -218,7 +219,7 @@ TEST_F(OtlpHttpExporterTestPeer, ExportBinaryIntegrationTest) .trace_id() .ToLowerBase16(MakeSpan(trace_id_hex)); report_trace_id.assign(reinterpret_cast(trace_id_hex), sizeof(trace_id_hex)); - EXPECT_CALL(*mockOtlpHttpClient, Export(IsValidMessage(report_trace_id))) + EXPECT_CALL(*mock_otlp_http_client, Export(IsValidMessage(report_trace_id))) .Times(Exactly(1)) .WillOnce(Return(sdk::common::ExportResult::kSuccess)); } diff --git a/exporters/otlp/test/otlp_http_log_exporter_test.cc b/exporters/otlp/test/otlp_http_log_exporter_test.cc index 92a3789f0e..fa5ab78658 100755 --- a/exporters/otlp/test/otlp_http_log_exporter_test.cc +++ b/exporters/otlp/test/otlp_http_log_exporter_test.cc @@ -53,10 +53,10 @@ OtlpHttpClientOptions MakeOtlpHttpClientOptions(HttpRequestContentType content_t options.console_debug = true; options.http_headers.insert( std::make_pair("Custom-Header-Key", "Custom-Header-Value")); - OtlpHttpClientOptions otlpHttpClientOptions( + OtlpHttpClientOptions otlp_http_client_options( options.url, options.content_type, options.json_bytes_mapping, options.use_json_name, options.console_debug, options.timeout, options.http_headers); - return otlpHttpClientOptions; + return otlp_http_client_options; } class OtlpHttpLogExporterTestPeer : public ::testing::Test @@ -86,7 +86,6 @@ class MockOtlpHttpClient : public OtlpHttpClient MockOtlpHttpClient *GetMockOtlpHttpClient(HttpRequestContentType content_type) { - return new MockOtlpHttpClient(MakeOtlpHttpClientOptions(content_type)); } @@ -99,9 +98,9 @@ class IsValidMessageMatcher template bool MatchAndExplain(const T &p, MatchResultListener *) const { - auto otlpHttpClientOptions = MakeOtlpHttpClientOptions(HttpRequestContentType::kJson); + auto otlp_http_client_options = MakeOtlpHttpClientOptions(HttpRequestContentType::kJson); nlohmann::json check_json; - OtlpHttpClient::ConvertGenericMessageToJson(check_json, p, otlpHttpClientOptions); + OtlpHttpClient::ConvertGenericMessageToJson(check_json, p, otlp_http_client_options); auto resource_span = *check_json["resource_logs"].begin(); auto instrumentation_library_span = *resource_span["instrumentation_library_logs"].begin(); auto log = *instrumentation_library_span["logs"].begin(); @@ -143,8 +142,8 @@ PolymorphicMatcher IsValidMessage(const std::string &trac // Create log records, let processor call Export() TEST_F(OtlpHttpLogExporterTestPeer, ExportJsonIntegrationTest) { - auto mockOtlpHttpClient = GetMockOtlpHttpClient(HttpRequestContentType::kJson); - auto exporter = GetExporter(std::unique_ptr{mockOtlpHttpClient}); + auto mock_otlp_http_client = GetMockOtlpHttpClient(HttpRequestContentType::kJson); + auto exporter = GetExporter(std::unique_ptr{mock_otlp_http_client}); bool attribute_storage_bool_value[] = {true, false, true}; int32_t attribute_storage_int32_value[] = {1, 2}; @@ -197,7 +196,7 @@ TEST_F(OtlpHttpLogExporterTestPeer, ExportJsonIntegrationTest) span_id.ToLowerBase16(MakeSpan(span_id_hex)); report_span_id.assign(span_id_hex, sizeof(span_id_hex)); - EXPECT_CALL(*mockOtlpHttpClient, Export(IsValidMessage(report_trace_id, report_span_id))) + EXPECT_CALL(*mock_otlp_http_client, Export(IsValidMessage(report_trace_id, report_span_id))) .Times(Exactly(1)) .WillOnce(Return(sdk::common::ExportResult::kSuccess)); } @@ -205,8 +204,8 @@ TEST_F(OtlpHttpLogExporterTestPeer, ExportJsonIntegrationTest) // Create log records, let processor call Export() TEST_F(OtlpHttpLogExporterTestPeer, ExportBinaryIntegrationTest) { - auto mockOtlpHttpClient = GetMockOtlpHttpClient(HttpRequestContentType::kJson); - auto exporter = GetExporter(std::unique_ptr{mockOtlpHttpClient}); + auto mock_otlp_http_client = GetMockOtlpHttpClient(HttpRequestContentType::kJson); + auto exporter = GetExporter(std::unique_ptr{mock_otlp_http_client}); bool attribute_storage_bool_value[] = {true, false, true}; int32_t attribute_storage_int32_value[] = {1, 2}; @@ -259,7 +258,7 @@ TEST_F(OtlpHttpLogExporterTestPeer, ExportBinaryIntegrationTest) span_id.ToLowerBase16(MakeSpan(span_id_hex)); report_span_id.assign(span_id_hex, sizeof(span_id_hex)); - EXPECT_CALL(*mockOtlpHttpClient, Export(IsValidMessage(report_trace_id, report_span_id))) + EXPECT_CALL(*mock_otlp_http_client, Export(IsValidMessage(report_trace_id, report_span_id))) .Times(Exactly(1)) .WillOnce(Return(sdk::common::ExportResult::kSuccess)); } From 5c7ec0a6d5fe6a7a3b785ca928d0ef46a99d40c5 Mon Sep 17 00:00:00 2001 From: Oblivion Date: Sun, 30 Jan 2022 20:04:51 +0000 Subject: [PATCH 16/35] fix race --- exporters/otlp/test/otlp_http_exporter_test.cc | 10 ++++++---- exporters/zipkin/test/zipkin_exporter_test.cc | 6 +++--- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/exporters/otlp/test/otlp_http_exporter_test.cc b/exporters/otlp/test/otlp_http_exporter_test.cc index 430681f846..1e4004f7c3 100755 --- a/exporters/otlp/test/otlp_http_exporter_test.cc +++ b/exporters/otlp/test/otlp_http_exporter_test.cc @@ -159,8 +159,6 @@ TEST_F(OtlpHttpExporterTestPeer, ExportJsonIntegrationTest) auto child_span = tracer->StartSpan("Test child span", child_span_opts); - child_span->End(); - parent_span->End(); nostd::get(child_span_opts.parent) .trace_id() .ToLowerBase16(MakeSpan(trace_id_hex)); @@ -168,6 +166,9 @@ TEST_F(OtlpHttpExporterTestPeer, ExportJsonIntegrationTest) EXPECT_CALL(*mock_otlp_http_client, Export(IsValidMessage(report_trace_id))) .Times(Exactly(1)) .WillOnce(Return(sdk::common::ExportResult::kSuccess)); + + child_span->End(); + parent_span->End(); } // Create spans, let processor call Export() @@ -212,8 +213,6 @@ TEST_F(OtlpHttpExporterTestPeer, ExportBinaryIntegrationTest) child_span_opts.parent = parent_span->GetContext(); auto child_span = tracer->StartSpan("Test child span", child_span_opts); - child_span->End(); - parent_span->End(); nostd::get(child_span_opts.parent) .trace_id() @@ -222,6 +221,9 @@ TEST_F(OtlpHttpExporterTestPeer, ExportBinaryIntegrationTest) EXPECT_CALL(*mock_otlp_http_client, Export(IsValidMessage(report_trace_id))) .Times(Exactly(1)) .WillOnce(Return(sdk::common::ExportResult::kSuccess)); + + child_span->End(); + parent_span->End(); } // Test exporter configuration options diff --git a/exporters/zipkin/test/zipkin_exporter_test.cc b/exporters/zipkin/test/zipkin_exporter_test.cc index 5fd1dc4f05..a4e8c19b82 100644 --- a/exporters/zipkin/test/zipkin_exporter_test.cc +++ b/exporters/zipkin/test/zipkin_exporter_test.cc @@ -137,10 +137,7 @@ TEST_F(ZipkinExporterTestPeer, ExportJsonIntegrationTest) trace_api::StartSpanOptions child_span_opts = {}; child_span_opts.parent = parent_span->GetContext(); - auto child_span = tracer->StartSpan("Test child span", child_span_opts); - child_span->End(); - parent_span->End(); nostd::get(child_span_opts.parent) .trace_id() @@ -153,6 +150,9 @@ TEST_F(ZipkinExporterTestPeer, ExportJsonIntegrationTest) .WillOnce(Return(ByMove(std::move(ext::http::client::Result{ std::unique_ptr{new ext::http::client::curl::Response()}, ext::http::client::SessionState::Response})))); + + child_span->End(); + parent_span->End(); } // Create spans, let processor call Export() From c6c6b4bd5424691e565dd72fec442dc5f8d5f509 Mon Sep 17 00:00:00 2001 From: Ehsan Saei <71217171+esigo@users.noreply.github.com> Date: Sun, 30 Jan 2022 23:08:17 +0100 Subject: [PATCH 17/35] Windows TEST define --- ci/do_ci.ps1 | 2 +- ci/do_ci.sh | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/ci/do_ci.ps1 b/ci/do_ci.ps1 index ddd0d38bd7..49891ad7d7 100644 --- a/ci/do_ci.ps1 +++ b/ci/do_ci.ps1 @@ -5,7 +5,7 @@ $action = $args[0] $SRC_DIR=(Get-Item -Path ".\").FullName -$BAZEL_OPTIONS="--copt=-DENABLE_METRICS_PREVIEW --copt=-DENABLE_LOGS_PREVIEW --build_tag_filters=-jaeger --copt=-DTEST" +$BAZEL_OPTIONS="--copt=-DENABLE_METRICS_PREVIEW --copt=-DENABLE_LOGS_PREVIEW --build_tag_filters=-jaeger --copt=/DTEST" $BAZEL_TEST_OPTIONS="$BAZEL_OPTIONS --test_output=errors" if (!(test-path build)) { diff --git a/ci/do_ci.sh b/ci/do_ci.sh index c51e9c1ab6..a0f44628c6 100755 --- a/ci/do_ci.sh +++ b/ci/do_ci.sh @@ -63,7 +63,7 @@ BAZEL_OPTIONS="--copt=-DENABLE_METRICS_PREVIEW --copt=-DENABLE_LOGS_PREVIEW --co BAZEL_TEST_OPTIONS="$BAZEL_OPTIONS --test_output=errors" # https://github.com/bazelbuild/bazel/issues/4341 -BAZEL_MACOS_OPTIONS="$BAZEL_OPTIONS --features=-supports_dynamic_linker --build_tag_filters=-jaeger" +BAZEL_MACOS_OPTIONS="$BAZEL_OPRIONS --copt=-DTEST --features=-supports_dynamic_linker --build_tag_filters=-jaeger" BAZEL_MACOS_TEST_OPTIONS="$BAZEL_MACOS_OPTIONS --test_output=errors" BAZEL_STARTUP_OPTIONS="--output_user_root=$HOME/.cache/bazel" From 296de51eca6cd09eb56cd04c6bb2bf0f381b7a34 Mon Sep 17 00:00:00 2001 From: Oblivion Date: Mon, 31 Jan 2022 18:23:17 +0000 Subject: [PATCH 18/35] rename define --- ci/do_ci.ps1 | 2 +- ci/do_ci.sh | 2 +- exporters/otlp/CMakeLists.txt | 2 +- .../include/opentelemetry/exporters/otlp/otlp_http_client.h | 4 ++-- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/ci/do_ci.ps1 b/ci/do_ci.ps1 index ddd0d38bd7..cf253b6cd7 100644 --- a/ci/do_ci.ps1 +++ b/ci/do_ci.ps1 @@ -5,7 +5,7 @@ $action = $args[0] $SRC_DIR=(Get-Item -Path ".\").FullName -$BAZEL_OPTIONS="--copt=-DENABLE_METRICS_PREVIEW --copt=-DENABLE_LOGS_PREVIEW --build_tag_filters=-jaeger --copt=-DTEST" +$BAZEL_OPTIONS="--copt=-DENABLE_METRICS_PREVIEW --copt=-DENABLE_LOGS_PREVIEW --build_tag_filters=-jaeger --copt=-DENABLE_TEST" $BAZEL_TEST_OPTIONS="$BAZEL_OPTIONS --test_output=errors" if (!(test-path build)) { diff --git a/ci/do_ci.sh b/ci/do_ci.sh index c51e9c1ab6..4717e25940 100755 --- a/ci/do_ci.sh +++ b/ci/do_ci.sh @@ -59,7 +59,7 @@ mkdir -p "${BUILD_DIR}" [ -z "${PLUGIN_DIR}" ] && export PLUGIN_DIR=$HOME/plugin mkdir -p "${PLUGIN_DIR}" -BAZEL_OPTIONS="--copt=-DENABLE_METRICS_PREVIEW --copt=-DENABLE_LOGS_PREVIEW --copt=-DTEST" +BAZEL_OPTIONS="--copt=-DENABLE_METRICS_PREVIEW --copt=-DENABLE_LOGS_PREVIEW --copt=-DENABLE_TEST" BAZEL_TEST_OPTIONS="$BAZEL_OPTIONS --test_output=errors" # https://github.com/bazelbuild/bazel/issues/4341 diff --git a/exporters/otlp/CMakeLists.txt b/exporters/otlp/CMakeLists.txt index 6dce7bb562..6fedeefd35 100755 --- a/exporters/otlp/CMakeLists.txt +++ b/exporters/otlp/CMakeLists.txt @@ -41,7 +41,7 @@ endif() if(WITH_OTLP_HTTP) if(BUILD_TESTING) - add_definitions(-DTEST) + add_definitions(-DENABLE_TEST) endif() find_package(CURL REQUIRED) add_library(opentelemetry_exporter_otlp_http_client src/otlp_http_client.cc) diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_client.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_client.h index e5b376ab0d..c2a7e69a60 100644 --- a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_client.h +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_client.h @@ -89,7 +89,7 @@ struct OtlpHttpClientOptions {} }; -#ifdef TEST +#ifdef ENABLE_TEST # define VIRTUAL_TEST virtual #else # define VIRTUAL_TEST @@ -104,7 +104,7 @@ class OtlpHttpClient * Create an OtlpHttpClient using the given options. */ explicit OtlpHttpClient(OtlpHttpClientOptions &&options); -#ifdef TEST +#ifdef ENABLE_TEST VIRTUAL_TEST ~OtlpHttpClient() {} #endif From f5e84e7eb86f2a189a1dc1ed11b46da43bd550ac Mon Sep 17 00:00:00 2001 From: Ehsan Saei <71217171+esigo@users.noreply.github.com> Date: Mon, 31 Jan 2022 20:15:44 +0100 Subject: [PATCH 19/35] fix typo --- ci/do_ci.ps1 | 2 +- ci/do_ci.sh | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/ci/do_ci.ps1 b/ci/do_ci.ps1 index cf253b6cd7..95774fc3ee 100644 --- a/ci/do_ci.ps1 +++ b/ci/do_ci.ps1 @@ -22,7 +22,7 @@ $VCPKG_DIR="$SRC_DIR\vcpkg" switch ($action) { "bazel.build" { - bazel build $BAZEL_OPTIONS -- //... + bazel build --copt=-DENABLE_TEST $BAZEL_OPTIONS -- //... $exit = $LASTEXITCODE if ($exit -ne 0) { exit $exit diff --git a/ci/do_ci.sh b/ci/do_ci.sh index 0d5c145ce0..77911452be 100755 --- a/ci/do_ci.sh +++ b/ci/do_ci.sh @@ -63,7 +63,7 @@ BAZEL_OPTIONS="--copt=-DENABLE_METRICS_PREVIEW --copt=-DENABLE_LOGS_PREVIEW --co BAZEL_TEST_OPTIONS="$BAZEL_OPTIONS --test_output=errors" # https://github.com/bazelbuild/bazel/issues/4341 -BAZEL_MACOS_OPTIONS="$BAZEL_OPRIONS --copt=-DTEST --features=-supports_dynamic_linker --build_tag_filters=-jaeger" +BAZEL_MACOS_OPTIONS="$BAZEL_OPRIONS --copt=-DENABLE_TEST --features=-supports_dynamic_linker --build_tag_filters=-jaeger" BAZEL_MACOS_TEST_OPTIONS="$BAZEL_MACOS_OPTIONS --test_output=errors" BAZEL_STARTUP_OPTIONS="--output_user_root=$HOME/.cache/bazel" From 03777d570f417b34dfa0f7036090e11ed7cc5fbe Mon Sep 17 00:00:00 2001 From: Ehsan Saei <71217171+esigo@users.noreply.github.com> Date: Mon, 31 Jan 2022 21:03:15 +0100 Subject: [PATCH 20/35] clean --- exporters/otlp/test/otlp_http_exporter_test.cc | 2 +- exporters/zipkin/test/zipkin_exporter_test.cc | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/exporters/otlp/test/otlp_http_exporter_test.cc b/exporters/otlp/test/otlp_http_exporter_test.cc index 1e4004f7c3..df5c8f29dc 100755 --- a/exporters/otlp/test/otlp_http_exporter_test.cc +++ b/exporters/otlp/test/otlp_http_exporter_test.cc @@ -19,8 +19,8 @@ # include # include "gmock/gmock.h" -# include # include "nlohmann/json.hpp" + # if defined(_MSC_VER) # include "opentelemetry/sdk/common/env_variables.h" using opentelemetry::sdk::common::setenv; diff --git a/exporters/zipkin/test/zipkin_exporter_test.cc b/exporters/zipkin/test/zipkin_exporter_test.cc index a4e8c19b82..eec71f43d6 100644 --- a/exporters/zipkin/test/zipkin_exporter_test.cc +++ b/exporters/zipkin/test/zipkin_exporter_test.cc @@ -13,6 +13,7 @@ # include # include "gmock/gmock.h" + # include "nlohmann/json.hpp" # if defined(_MSC_VER) From aaa04f22fe3459a9531197b70eac6a14f77263e9 Mon Sep 17 00:00:00 2001 From: esigo Date: Sat, 5 Feb 2022 15:06:42 +0100 Subject: [PATCH 21/35] no_send client otlp_http_exporter --- .../exporters/otlp/otlp_http_client.h | 10 ++ exporters/otlp/src/otlp_http_client.cc | 5 + .../otlp/test/otlp_http_exporter_test.cc | 153 +++++++++++------- .../ext/http/client/curl/http_client_curl.h | 3 + 4 files changed, 115 insertions(+), 56 deletions(-) mode change 100755 => 100644 exporters/otlp/test/otlp_http_exporter_test.cc diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_client.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_client.h index c2a7e69a60..a4e40ca76e 100644 --- a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_client.h +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_client.h @@ -141,6 +141,16 @@ class OtlpHttpClient std::string http_uri_; mutable opentelemetry::common::SpinLockMutex lock_; bool isShutdown() const noexcept; + // For testing + friend class OtlpHttpExporterTestPeer; + /** + * Create an OtlpHttpClient using the specified http client. + * Only tests can call this constructor directly. + * @param options the Otlp http client options to be used for exporting + * @param http_client the http client to be used for exporting + */ + OtlpHttpClient(OtlpHttpClientOptions &&options, + std::shared_ptr http_client); }; } // namespace otlp } // namespace exporter diff --git a/exporters/otlp/src/otlp_http_client.cc b/exporters/otlp/src/otlp_http_client.cc index 317a0b1967..d26bc02290 100644 --- a/exporters/otlp/src/otlp_http_client.cc +++ b/exporters/otlp/src/otlp_http_client.cc @@ -565,6 +565,11 @@ OtlpHttpClient::OtlpHttpClient(OtlpHttpClientOptions &&options) : options_(options), http_client_(http_client::HttpClientFactory::Create()) {} +OtlpHttpClient::OtlpHttpClient(OtlpHttpClientOptions &&options, + std::shared_ptr http_client) + : options_(options), http_client_(http_client) +{} + // ----------------------------- HTTP Client methods ------------------------------ opentelemetry::sdk::common::ExportResult OtlpHttpClient::Export( const google::protobuf::Message &message) noexcept diff --git a/exporters/otlp/test/otlp_http_exporter_test.cc b/exporters/otlp/test/otlp_http_exporter_test.cc old mode 100755 new mode 100644 index df5c8f29dc..1b61f71290 --- a/exporters/otlp/test/otlp_http_exporter_test.cc +++ b/exporters/otlp/test/otlp_http_exporter_test.cc @@ -11,6 +11,7 @@ # include "opentelemetry/exporters/otlp/protobuf_include_suffix.h" +# include "opentelemetry/ext/http/client/curl/http_client_curl.h" # include "opentelemetry/ext/http/server/http_server.h" # include "opentelemetry/sdk/trace/batch_span_processor.h" # include "opentelemetry/sdk/trace/tracer_provider.h" @@ -48,6 +49,7 @@ OtlpHttpClientOptions MakeOtlpHttpClientOptions(HttpRequestContentType content_t OtlpHttpExporterOptions options; options.content_type = content_type; options.console_debug = true; + options.timeout = std::chrono::system_clock::duration::zero(); options.http_headers.insert( std::make_pair("Custom-Header-Key", "Custom-Header-Value")); OtlpHttpClientOptions otlp_http_client_options( @@ -56,6 +58,37 @@ OtlpHttpClientOptions MakeOtlpHttpClientOptions(HttpRequestContentType content_t return otlp_http_client_options; } +namespace http_client = opentelemetry::ext::http::client; + +class MockSession : public http_client::curl::Session +{ +public: + MockSession(http_client::curl::HttpClient &http_client) + : http_client::curl::Session(http_client, "http", "", 80) + {} + MOCK_METHOD(void, + SendRequest, + (opentelemetry::ext::http::client::EventHandler &), + (noexcept, override)); + bool FinishSession() noexcept override { return true; } +}; + +class NosendHttpClient : public http_client::curl::HttpClient +{ +public: + NosendHttpClient() + { + session_ = std::shared_ptr{new MockSession(*this)}; + } + std::shared_ptr CreateSession( + nostd::string_view url) noexcept override + { + return session_; + } + + std::shared_ptr session_; +}; + class OtlpHttpExporterTestPeer : public ::testing::Test { public: @@ -69,57 +102,22 @@ class OtlpHttpExporterTestPeer : public ::testing::Test { return exporter->options_; } -}; -class MockOtlpHttpClient : public OtlpHttpClient -{ -public: - MockOtlpHttpClient(OtlpHttpClientOptions &&options) : OtlpHttpClient(std::move(options)) {} - MOCK_METHOD(sdk::common::ExportResult, - Export, - (const google::protobuf::Message &), - (noexcept, override)); -}; - -MockOtlpHttpClient *GetMockOtlpHttpClient(HttpRequestContentType content_type) -{ - return new MockOtlpHttpClient(MakeOtlpHttpClientOptions(content_type)); -} - -class IsValidMessageMatcher -{ -public: - IsValidMessageMatcher(const std::string &trace_id) : trace_id_(trace_id) {} - template - bool MatchAndExplain(const T &p, MatchResultListener * /* listener */) const + static std::pair> + GetMockOtlpHttpClient(HttpRequestContentType content_type) { - auto otlp_http_client_options = MakeOtlpHttpClientOptions(HttpRequestContentType::kJson); - nlohmann::json check_json; - OtlpHttpClient::ConvertGenericMessageToJson(check_json, p, otlp_http_client_options); - auto resource_span = *check_json["resource_spans"].begin(); - auto instrumentation_library_span = *resource_span["instrumentation_library_spans"].begin(); - auto span = *instrumentation_library_span["spans"].begin(); - auto received_trace_id = span["trace_id"].get(); - return trace_id_ == received_trace_id; + std::shared_ptr http_client{new NosendHttpClient}; + return {new OtlpHttpClient(MakeOtlpHttpClientOptions(content_type), http_client), http_client}; } - - void DescribeTo(std::ostream *os) const { *os << "received trace_id matches"; } - - void DescribeNegationTo(std::ostream *os) const { *os << "received trace_id does not matche"; } - -private: - std::string trace_id_; }; -PolymorphicMatcher IsValidMessage(const std::string &trace_id) -{ - return MakePolymorphicMatcher(IsValidMessageMatcher(trace_id)); -} - // Create spans, let processor call Export() TEST_F(OtlpHttpExporterTestPeer, ExportJsonIntegrationTest) { - auto mock_otlp_http_client = GetMockOtlpHttpClient(HttpRequestContentType::kJson); + auto mock_otlp_client = + OtlpHttpExporterTestPeer::GetMockOtlpHttpClient(HttpRequestContentType::kJson); + auto mock_otlp_http_client = mock_otlp_client.first; + auto client = mock_otlp_client.second; auto exporter = GetExporter(std::unique_ptr{mock_otlp_http_client}); resource::ResourceAttributes resource_attributes = {{"service.name", "unit_test_service"}, @@ -163,9 +161,29 @@ TEST_F(OtlpHttpExporterTestPeer, ExportJsonIntegrationTest) .trace_id() .ToLowerBase16(MakeSpan(trace_id_hex)); report_trace_id.assign(trace_id_hex, sizeof(trace_id_hex)); - EXPECT_CALL(*mock_otlp_http_client, Export(IsValidMessage(report_trace_id))) - .Times(Exactly(1)) - .WillOnce(Return(sdk::common::ExportResult::kSuccess)); + + auto no_send_client = std::dynamic_pointer_cast(client); + auto mock_session = std::static_pointer_cast(no_send_client->session_); + EXPECT_CALL(*mock_session, SendRequest) + .WillOnce([&mock_session, + report_trace_id](opentelemetry::ext::http::client::EventHandler &callback) { + auto check_json = nlohmann::json::parse(mock_session->GetRequest()->body_, nullptr, false); + auto resource_span = *check_json["resource_spans"].begin(); + auto instrumentation_library_span = *resource_span["instrumentation_library_spans"].begin(); + auto span = *instrumentation_library_span["spans"].begin(); + auto received_trace_id = span["trace_id"].get(); + EXPECT_EQ(received_trace_id, report_trace_id); + + auto custom_header = mock_session->GetRequest()->headers_.find("Custom-Header-Key"); + ASSERT_TRUE(custom_header != mock_session->GetRequest()->headers_.end()); + if (custom_header != mock_session->GetRequest()->headers_.end()) + { + EXPECT_EQ("Custom-Header-Value", custom_header->second); + } + // let the otlp_http_client to continue + http_client::curl::Response response; + callback.OnResponse(response); + }); child_span->End(); parent_span->End(); @@ -174,7 +192,10 @@ TEST_F(OtlpHttpExporterTestPeer, ExportJsonIntegrationTest) // Create spans, let processor call Export() TEST_F(OtlpHttpExporterTestPeer, ExportBinaryIntegrationTest) { - auto mock_otlp_http_client = GetMockOtlpHttpClient(HttpRequestContentType::kBinary); + auto mock_otlp_client = + OtlpHttpExporterTestPeer::GetMockOtlpHttpClient(HttpRequestContentType::kBinary); + auto mock_otlp_http_client = mock_otlp_client.first; + auto client = mock_otlp_client.second; auto exporter = GetExporter(std::unique_ptr{mock_otlp_http_client}); resource::ResourceAttributes resource_attributes = {{"service.name", "unit_test_service"}, @@ -205,22 +226,42 @@ TEST_F(OtlpHttpExporterTestPeer, ExportBinaryIntegrationTest) new sdk::trace::TracerProvider(std::move(processor), resource)); std::string report_trace_id; - char trace_id_hex[2 * trace_api::TraceId::kSize] = {0}; - auto tracer = provider->GetTracer("test"); - auto parent_span = tracer->StartSpan("Test parent span"); + + uint8_t trace_id_binary[trace_api::TraceId::kSize] = {0}; + auto tracer = provider->GetTracer("test"); + auto parent_span = tracer->StartSpan("Test parent span"); trace_api::StartSpanOptions child_span_opts = {}; child_span_opts.parent = parent_span->GetContext(); auto child_span = tracer->StartSpan("Test child span", child_span_opts); - nostd::get(child_span_opts.parent) .trace_id() - .ToLowerBase16(MakeSpan(trace_id_hex)); - report_trace_id.assign(reinterpret_cast(trace_id_hex), sizeof(trace_id_hex)); - EXPECT_CALL(*mock_otlp_http_client, Export(IsValidMessage(report_trace_id))) - .Times(Exactly(1)) - .WillOnce(Return(sdk::common::ExportResult::kSuccess)); + .CopyBytesTo(MakeSpan(trace_id_binary)); + report_trace_id.assign(reinterpret_cast(trace_id_binary), sizeof(trace_id_binary)); + + auto no_send_client = std::dynamic_pointer_cast(client); + auto mock_session = std::static_pointer_cast(no_send_client->session_); + EXPECT_CALL(*mock_session, SendRequest) + .WillOnce([&mock_session, + report_trace_id](opentelemetry::ext::http::client::EventHandler &callback) { + opentelemetry::proto::collector::trace::v1::ExportTraceServiceRequest request_body; + request_body.ParseFromArray(&mock_session->GetRequest()->body_[0], + static_cast(mock_session->GetRequest()->body_.size())); + auto received_trace_id = + request_body.resource_spans(0).instrumentation_library_spans(0).spans(0).trace_id(); + EXPECT_EQ(received_trace_id, report_trace_id); + + auto custom_header = mock_session->GetRequest()->headers_.find("Custom-Header-Key"); + ASSERT_TRUE(custom_header != mock_session->GetRequest()->headers_.end()); + if (custom_header != mock_session->GetRequest()->headers_.end()) + { + EXPECT_EQ("Custom-Header-Value", custom_header->second); + } + // let the otlp_http_client to continue + http_client::curl::Response response; + callback.OnResponse(response); + }); child_span->End(); parent_span->End(); diff --git a/ext/include/opentelemetry/ext/http/client/curl/http_client_curl.h b/ext/include/opentelemetry/ext/http/client/curl/http_client_curl.h index 32d0a96783..9f2f05f3f0 100644 --- a/ext/include/opentelemetry/ext/http/client/curl/http_client_curl.h +++ b/ext/include/opentelemetry/ext/http/client/curl/http_client_curl.h @@ -185,6 +185,9 @@ class Session : public opentelemetry::ext::http::client::Session */ const std::string &GetBaseUri() const { return host_; } +#ifdef ENABLE_TEST + std::shared_ptr GetRequest() { return http_request_; } +#endif private: std::shared_ptr http_request_; std::string host_; From 19ec9977640cc7867cc3a93e0846cf2b124daf19 Mon Sep 17 00:00:00 2001 From: esigo Date: Sat, 5 Feb 2022 16:14:52 +0100 Subject: [PATCH 22/35] no_send client otlp_http_log_exporter --- ci/do_ci.ps1 | 2 +- .../exporters/otlp/otlp_http_client.h | 1 + .../otlp/test/otlp_http_exporter_test.cc | 4 +- .../otlp/test/otlp_http_log_exporter_test.cc | 181 ++++++++++-------- 4 files changed, 105 insertions(+), 83 deletions(-) mode change 100755 => 100644 exporters/otlp/test/otlp_http_log_exporter_test.cc diff --git a/ci/do_ci.ps1 b/ci/do_ci.ps1 index 95774fc3ee..c97ca13381 100644 --- a/ci/do_ci.ps1 +++ b/ci/do_ci.ps1 @@ -5,7 +5,7 @@ $action = $args[0] $SRC_DIR=(Get-Item -Path ".\").FullName -$BAZEL_OPTIONS="--copt=-DENABLE_METRICS_PREVIEW --copt=-DENABLE_LOGS_PREVIEW --build_tag_filters=-jaeger --copt=-DENABLE_TEST" +$BAZEL_OPTIONS="--copt=-DENABLE_METRICS_PREVIEW --copt=-DENABLE_LOGS_PREVIEW --build_tag_filters=-jaeger" $BAZEL_TEST_OPTIONS="$BAZEL_OPTIONS --test_output=errors" if (!(test-path build)) { diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_client.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_client.h index a4e40ca76e..cc547554fc 100644 --- a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_client.h +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_client.h @@ -143,6 +143,7 @@ class OtlpHttpClient bool isShutdown() const noexcept; // For testing friend class OtlpHttpExporterTestPeer; + friend class OtlpHttpLogExporterTestPeer; /** * Create an OtlpHttpClient using the specified http client. * Only tests can call this constructor directly. diff --git a/exporters/otlp/test/otlp_http_exporter_test.cc b/exporters/otlp/test/otlp_http_exporter_test.cc index 1b61f71290..0c66a03f09 100644 --- a/exporters/otlp/test/otlp_http_exporter_test.cc +++ b/exporters/otlp/test/otlp_http_exporter_test.cc @@ -162,7 +162,7 @@ TEST_F(OtlpHttpExporterTestPeer, ExportJsonIntegrationTest) .ToLowerBase16(MakeSpan(trace_id_hex)); report_trace_id.assign(trace_id_hex, sizeof(trace_id_hex)); - auto no_send_client = std::dynamic_pointer_cast(client); + auto no_send_client = std::static_pointer_cast(client); auto mock_session = std::static_pointer_cast(no_send_client->session_); EXPECT_CALL(*mock_session, SendRequest) .WillOnce([&mock_session, @@ -240,7 +240,7 @@ TEST_F(OtlpHttpExporterTestPeer, ExportBinaryIntegrationTest) .CopyBytesTo(MakeSpan(trace_id_binary)); report_trace_id.assign(reinterpret_cast(trace_id_binary), sizeof(trace_id_binary)); - auto no_send_client = std::dynamic_pointer_cast(client); + auto no_send_client = std::static_pointer_cast(client); auto mock_session = std::static_pointer_cast(no_send_client->session_); EXPECT_CALL(*mock_session, SendRequest) .WillOnce([&mock_session, diff --git a/exporters/otlp/test/otlp_http_log_exporter_test.cc b/exporters/otlp/test/otlp_http_log_exporter_test.cc old mode 100755 new mode 100644 index fa5ab78658..f96d1644c4 --- a/exporters/otlp/test/otlp_http_log_exporter_test.cc +++ b/exporters/otlp/test/otlp_http_log_exporter_test.cc @@ -13,6 +13,7 @@ # include "opentelemetry/exporters/otlp/protobuf_include_suffix.h" # include "opentelemetry/common/key_value_iterable_view.h" +# include "opentelemetry/ext/http/client/curl/http_client_curl.h" # include "opentelemetry/ext/http/server/http_server.h" # include "opentelemetry/logs/provider.h" # include "opentelemetry/sdk/logs/batch_log_processor.h" @@ -59,6 +60,37 @@ OtlpHttpClientOptions MakeOtlpHttpClientOptions(HttpRequestContentType content_t return otlp_http_client_options; } +namespace http_client = opentelemetry::ext::http::client; + +class MockSession : public http_client::curl::Session +{ +public: + MockSession(http_client::curl::HttpClient &http_client) + : http_client::curl::Session(http_client, "http", "", 80) + {} + MOCK_METHOD(void, + SendRequest, + (opentelemetry::ext::http::client::EventHandler &), + (noexcept, override)); + bool FinishSession() noexcept override { return true; } +}; + +class NosendHttpClient : public http_client::curl::HttpClient +{ +public: + NosendHttpClient() + { + session_ = std::shared_ptr{new MockSession(*this)}; + } + std::shared_ptr CreateSession( + nostd::string_view url) noexcept override + { + return session_; + } + + std::shared_ptr session_; +}; + class OtlpHttpLogExporterTestPeer : public ::testing::Test { public: @@ -72,77 +104,21 @@ class OtlpHttpLogExporterTestPeer : public ::testing::Test { return exporter->options_; } -}; - -class MockOtlpHttpClient : public OtlpHttpClient -{ -public: - MockOtlpHttpClient(OtlpHttpClientOptions &&options) : OtlpHttpClient(std::move(options)) {} - MOCK_METHOD(sdk::common::ExportResult, - Export, - (const google::protobuf::Message &), - (noexcept, override)); -}; - -MockOtlpHttpClient *GetMockOtlpHttpClient(HttpRequestContentType content_type) -{ - return new MockOtlpHttpClient(MakeOtlpHttpClientOptions(content_type)); -} - -class IsValidMessageMatcher -{ -public: - IsValidMessageMatcher(const std::string &trace_id, const std::string &span_id) - : trace_id_(trace_id), span_id_(span_id) - {} - template - bool MatchAndExplain(const T &p, MatchResultListener *) const + static std::pair> + GetMockOtlpHttpClient(HttpRequestContentType content_type) { - auto otlp_http_client_options = MakeOtlpHttpClientOptions(HttpRequestContentType::kJson); - nlohmann::json check_json; - OtlpHttpClient::ConvertGenericMessageToJson(check_json, p, otlp_http_client_options); - auto resource_span = *check_json["resource_logs"].begin(); - auto instrumentation_library_span = *resource_span["instrumentation_library_logs"].begin(); - auto log = *instrumentation_library_span["logs"].begin(); - auto received_trace_id = log["trace_id"].get(); - auto received_span_id = log["span_id"].get(); - bool is_ok = trace_id_ == received_trace_id; - is_ok &= span_id_ == received_span_id; - is_ok &= "Log name" == log["name"].get(); - is_ok &= "Log message" == log["body"]["string_value"].get(); - is_ok &= 15 <= log["attributes"].size(); - bool check_service_name = false; - for (auto attribute : log["attributes"]) - { - if ("service.name" == attribute["key"].get()) - { - check_service_name = true; - EXPECT_EQ("unit_test_service", attribute["value"]["string_value"].get()); - } - } - is_ok &= check_service_name; - return is_ok; + std::shared_ptr http_client{new NosendHttpClient}; + return {new OtlpHttpClient(MakeOtlpHttpClientOptions(content_type), http_client), http_client}; } - - void DescribeTo(std::ostream *os) const { *os << "received trace_id matches"; } - - void DescribeNegationTo(std::ostream *os) const { *os << "received trace_id does not matche"; } - -private: - std::string trace_id_; - std::string span_id_; }; -PolymorphicMatcher IsValidMessage(const std::string &trace_id, - const std::string &span_id) -{ - return MakePolymorphicMatcher(IsValidMessageMatcher(trace_id, span_id)); -} - // Create log records, let processor call Export() TEST_F(OtlpHttpLogExporterTestPeer, ExportJsonIntegrationTest) { - auto mock_otlp_http_client = GetMockOtlpHttpClient(HttpRequestContentType::kJson); + auto mock_otlp_client = + OtlpHttpLogExporterTestPeer::GetMockOtlpHttpClient(HttpRequestContentType::kJson); + auto mock_otlp_http_client = mock_otlp_client.first; + auto client = mock_otlp_client.second; auto exporter = GetExporter(std::unique_ptr{mock_otlp_http_client}); bool attribute_storage_bool_value[] = {true, false, true}; @@ -196,15 +172,41 @@ TEST_F(OtlpHttpLogExporterTestPeer, ExportJsonIntegrationTest) span_id.ToLowerBase16(MakeSpan(span_id_hex)); report_span_id.assign(span_id_hex, sizeof(span_id_hex)); - EXPECT_CALL(*mock_otlp_http_client, Export(IsValidMessage(report_trace_id, report_span_id))) - .Times(Exactly(1)) - .WillOnce(Return(sdk::common::ExportResult::kSuccess)); + auto no_send_client = std::static_pointer_cast(client); + auto mock_session = std::static_pointer_cast(no_send_client->session_); + EXPECT_CALL(*mock_session, SendRequest) + .WillOnce([&mock_session, report_trace_id, + report_span_id](opentelemetry::ext::http::client::EventHandler &callback) { + auto check_json = nlohmann::json::parse(mock_session->GetRequest()->body_, nullptr, false); + auto resource_logs = *check_json["resource_logs"].begin(); + auto instrumentation_library_span = *resource_logs["instrumentation_library_logs"].begin(); + auto log = *instrumentation_library_span["logs"].begin(); + auto received_trace_id = log["trace_id"].get(); + auto received_span_id = log["span_id"].get(); + EXPECT_EQ(received_trace_id, report_trace_id); + EXPECT_EQ(received_span_id, report_span_id); + EXPECT_EQ("Log name", log["name"].get()); + EXPECT_EQ("Log message", log["body"]["string_value"].get()); + EXPECT_LE(15, log["attributes"].size()); + auto custom_header = mock_session->GetRequest()->headers_.find("Custom-Header-Key"); + ASSERT_TRUE(custom_header != mock_session->GetRequest()->headers_.end()); + if (custom_header != mock_session->GetRequest()->headers_.end()) + { + EXPECT_EQ("Custom-Header-Value", custom_header->second); + } + // let the otlp_http_client to continue + http_client::curl::Response response; + callback.OnResponse(response); + }); } // Create log records, let processor call Export() TEST_F(OtlpHttpLogExporterTestPeer, ExportBinaryIntegrationTest) { - auto mock_otlp_http_client = GetMockOtlpHttpClient(HttpRequestContentType::kJson); + auto mock_otlp_client = + OtlpHttpLogExporterTestPeer::GetMockOtlpHttpClient(HttpRequestContentType::kBinary); + auto mock_otlp_http_client = mock_otlp_client.first; + auto client = mock_otlp_client.second; auto exporter = GetExporter(std::unique_ptr{mock_otlp_http_client}); bool attribute_storage_bool_value[] = {true, false, true}; @@ -223,11 +225,9 @@ TEST_F(OtlpHttpLogExporterTestPeer, ExportBinaryIntegrationTest) std::string report_span_id; uint8_t trace_id_bin[opentelemetry::trace::TraceId::kSize] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; - char trace_id_hex[2 * opentelemetry::trace::TraceId::kSize] = {0}; opentelemetry::trace::TraceId trace_id{trace_id_bin}; - uint8_t span_id_bin[opentelemetry::trace::SpanId::kSize] = {'7', '6', '5', '4', + uint8_t span_id_bin[opentelemetry::trace::SpanId::kSize] = {'7', '6', '5', '4', '3', '2', '1', '0'}; - char span_id_hex[2 * opentelemetry::trace::SpanId::kSize] = {0}; opentelemetry::trace::SpanId span_id{span_id_bin}; const std::string schema_url{"https://opentelemetry.io/schemas/1.2.0"}; @@ -252,15 +252,36 @@ TEST_F(OtlpHttpLogExporterTestPeer, ExportBinaryIntegrationTest) opentelemetry::trace::TraceFlags{opentelemetry::trace::TraceFlags::kIsSampled}, std::chrono::system_clock::now()); - trace_id.ToLowerBase16(MakeSpan(trace_id_hex)); - report_trace_id.assign(trace_id_hex, sizeof(trace_id_hex)); - - span_id.ToLowerBase16(MakeSpan(span_id_hex)); - report_span_id.assign(span_id_hex, sizeof(span_id_hex)); - - EXPECT_CALL(*mock_otlp_http_client, Export(IsValidMessage(report_trace_id, report_span_id))) - .Times(Exactly(1)) - .WillOnce(Return(sdk::common::ExportResult::kSuccess)); + report_trace_id.assign(reinterpret_cast(trace_id_bin), sizeof(trace_id_bin)); + report_span_id.assign(reinterpret_cast(span_id_bin), sizeof(span_id_bin)); + + auto no_send_client = std::static_pointer_cast(client); + auto mock_session = std::static_pointer_cast(no_send_client->session_); + EXPECT_CALL(*mock_session, SendRequest) + .WillOnce([&mock_session, report_trace_id, + report_span_id](opentelemetry::ext::http::client::EventHandler &callback) { + opentelemetry::proto::collector::logs::v1::ExportLogsServiceRequest request_body; + request_body.ParseFromArray(&mock_session->GetRequest()->body_[0], + static_cast(mock_session->GetRequest()->body_.size())); + auto received_log = request_body.resource_logs(0).instrumentation_library_logs(0).logs(0); + EXPECT_EQ(received_log.trace_id(), report_trace_id); + EXPECT_EQ(received_log.span_id(), report_span_id); + EXPECT_EQ("Log name", received_log.name()); + EXPECT_EQ("Log message", received_log.body().string_value()); + EXPECT_LE(15, received_log.attributes_size()); + bool check_service_name = false; + for (auto &attribute : received_log.attributes()) + { + if ("service.name" == attribute.key()) + { + check_service_name = true; + EXPECT_EQ("unit_test_service", attribute.value().string_value()); + } + } + ASSERT_TRUE(check_service_name); + http_client::curl::Response response; + callback.OnResponse(response); + }); } // Test exporter configuration options From 62cc1ddd99cb3fbaa669eee6a7561cb3a0df9519 Mon Sep 17 00:00:00 2001 From: esigo Date: Sat, 5 Feb 2022 16:31:22 +0100 Subject: [PATCH 23/35] revert unnecessary changes --- .../exporters/otlp/otlp_http_client.h | 17 +---------------- exporters/otlp/src/otlp_http_client.cc | 14 +++++++------- 2 files changed, 8 insertions(+), 23 deletions(-) diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_client.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_client.h index cc547554fc..00a484e04f 100644 --- a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_client.h +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_client.h @@ -13,7 +13,6 @@ #include "opentelemetry/ext/http/client/http_client.h" #include "opentelemetry/sdk/common/exporter_utils.h" -#include "nlohmann/json.hpp" #include "opentelemetry/exporters/otlp/otlp_environment.h" #include @@ -89,11 +88,6 @@ struct OtlpHttpClientOptions {} }; -#ifdef ENABLE_TEST -# define VIRTUAL_TEST virtual -#else -# define VIRTUAL_TEST -#endif /** * The OTLP HTTP client exports span data in OpenTelemetry Protocol (OTLP) format. */ @@ -104,16 +98,13 @@ class OtlpHttpClient * Create an OtlpHttpClient using the given options. */ explicit OtlpHttpClient(OtlpHttpClientOptions &&options); -#ifdef ENABLE_TEST - VIRTUAL_TEST ~OtlpHttpClient() {} -#endif /** * Export * @param message message to export, it should be ExportTraceServiceRequest, * ExportMetricsServiceRequest or ExportLogsServiceRequest */ - VIRTUAL_TEST sdk::common::ExportResult Export(const google::protobuf::Message &message) noexcept; + sdk::common::ExportResult Export(const google::protobuf::Message &message) noexcept; /** * Shut down the HTTP client. * @param timeout an optional timeout, the default timeout of 0 means that no @@ -122,12 +113,6 @@ class OtlpHttpClient */ bool Shutdown(std::chrono::microseconds timeout = std::chrono::microseconds(0)) noexcept; - static void ConvertGenericMessageToJson(nlohmann::json &value, - const google::protobuf::Message &message, - const OtlpHttpClientOptions &options); - static bool SerializeToHttpBody(opentelemetry::ext::http::client::Body &output, - const google::protobuf::Message &message); - private: // Stores if this HTTP client had its Shutdown() method called bool is_shutdown_ = false; diff --git a/exporters/otlp/src/otlp_http_client.cc b/exporters/otlp/src/otlp_http_client.cc index d26bc02290..467c9077ba 100644 --- a/exporters/otlp/src/otlp_http_client.cc +++ b/exporters/otlp/src/otlp_http_client.cc @@ -18,6 +18,7 @@ #include "google/protobuf/message.h" #include "google/protobuf/reflection.h" #include "google/protobuf/stubs/common.h" +#include "nlohmann/json.hpp" #if defined(GOOGLE_PROTOBUF_VERSION) && GOOGLE_PROTOBUF_VERSION >= 3007000 # include "google/protobuf/stubs/strutil.h" @@ -340,9 +341,9 @@ static void ConvertListFieldToJson(nlohmann::json &value, const google::protobuf::FieldDescriptor *field_descriptor, const OtlpHttpClientOptions &options); -void OtlpHttpClient::ConvertGenericMessageToJson(nlohmann::json &value, - const google::protobuf::Message &message, - const OtlpHttpClientOptions &options) +static void ConvertGenericMessageToJson(nlohmann::json &value, + const google::protobuf::Message &message, + const OtlpHttpClientOptions &options) { std::vector fields_with_data; message.GetReflection()->ListFields(message, &fields_with_data); @@ -362,8 +363,7 @@ void OtlpHttpClient::ConvertGenericMessageToJson(nlohmann::json &value, } } -bool OtlpHttpClient::SerializeToHttpBody(http_client::Body &output, - const google::protobuf::Message &message) +bool SerializeToHttpBody(http_client::Body &output, const google::protobuf::Message &message) { auto body_size = message.ByteSizeLong(); if (body_size > 0) @@ -417,7 +417,7 @@ void ConvertGenericFieldToJson(nlohmann::json &value, break; } case google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE: { - OtlpHttpClient::ConvertGenericMessageToJson( + ConvertGenericMessageToJson( value, message.GetReflection()->GetMessage(message, field_descriptor, nullptr), options); break; } @@ -515,7 +515,7 @@ void ConvertListFieldToJson(nlohmann::json &value, for (int i = 0; i < field_size; ++i) { nlohmann::json sub_value; - OtlpHttpClient::ConvertGenericMessageToJson( + ConvertGenericMessageToJson( sub_value, message.GetReflection()->GetRepeatedMessage(message, field_descriptor, i), options); value.push_back(std::move(sub_value)); From 4e3b68da9feb113cbeb7e3c2f55d5d702465a7c3 Mon Sep 17 00:00:00 2001 From: esigo Date: Sat, 5 Feb 2022 16:50:47 +0100 Subject: [PATCH 24/35] revert unnecessary changes --- .../include/opentelemetry/exporters/otlp/otlp_http_client.h | 1 + exporters/otlp/src/otlp_http_client.cc | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_client.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_client.h index 00a484e04f..1a199bed48 100644 --- a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_client.h +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_client.h @@ -105,6 +105,7 @@ class OtlpHttpClient * ExportMetricsServiceRequest or ExportLogsServiceRequest */ sdk::common::ExportResult Export(const google::protobuf::Message &message) noexcept; + /** * Shut down the HTTP client. * @param timeout an optional timeout, the default timeout of 0 means that no diff --git a/exporters/otlp/src/otlp_http_client.cc b/exporters/otlp/src/otlp_http_client.cc index 467c9077ba..544f74ca7c 100644 --- a/exporters/otlp/src/otlp_http_client.cc +++ b/exporters/otlp/src/otlp_http_client.cc @@ -266,8 +266,6 @@ class ResponseHandler : public http_client::EventHandler bool console_debug_ = false; }; -} // namespace - static inline char HexEncode(unsigned char byte) { #if defined(HAVE_GSL) @@ -561,6 +559,8 @@ void ConvertListFieldToJson(nlohmann::json &value, } } +} // namespace + OtlpHttpClient::OtlpHttpClient(OtlpHttpClientOptions &&options) : options_(options), http_client_(http_client::HttpClientFactory::Create()) {} From fe5dd6fa3aec29b08c22a759239a20bf1706f860 Mon Sep 17 00:00:00 2001 From: esigo Date: Sat, 12 Feb 2022 19:55:16 +0100 Subject: [PATCH 25/35] http_client_nosend --- exporters/otlp/BUILD | 4 +- .../otlp/test/otlp_http_exporter_test.cc | 48 ++--- .../otlp/test/otlp_http_log_exporter_test.cc | 48 ++--- .../http/client/nosend/http_client_nosend.h | 183 ++++++++++++++++++ ext/src/http/client/BUILD | 16 ++ ext/src/http/client/curl/BUILD | 1 - ...factory_curl.cc => http_client_factory.cc} | 9 +- ext/src/http/client/nosend/BUILD | 18 ++ .../http/client/nosend/http_client_nosend.cc | 70 +++++++ 9 files changed, 319 insertions(+), 78 deletions(-) create mode 100644 ext/include/opentelemetry/ext/http/client/nosend/http_client_nosend.h create mode 100644 ext/src/http/client/BUILD rename ext/src/http/client/{curl/http_client_factory_curl.cc => http_client_factory.cc} (77%) create mode 100644 ext/src/http/client/nosend/BUILD create mode 100644 ext/src/http/client/nosend/http_client_nosend.cc diff --git a/exporters/otlp/BUILD b/exporters/otlp/BUILD index 1a39a82baf..d85b79a84e 100644 --- a/exporters/otlp/BUILD +++ b/exporters/otlp/BUILD @@ -98,7 +98,7 @@ cc_library( ], deps = [ "//api", - "//ext/src/http/client/curl:http_client_curl", + "//ext/src/http/client:http_client_factory", "//sdk:headers", "@com_github_opentelemetry_proto//:common_proto_cc", "@github_nlohmann_json//:json", @@ -234,6 +234,7 @@ cc_test( deps = [ ":otlp_http_exporter", "//api", + "//ext/src/http/client/nosend:http_client_nosend", "@com_google_googletest//:gtest_main", ], ) @@ -249,6 +250,7 @@ cc_test( deps = [ ":otlp_http_log_exporter", "//api", + "//ext/src/http/client/nosend:http_client_nosend", "@com_google_googletest//:gtest_main", ], ) diff --git a/exporters/otlp/test/otlp_http_exporter_test.cc b/exporters/otlp/test/otlp_http_exporter_test.cc index 0c66a03f09..5929301ac8 100644 --- a/exporters/otlp/test/otlp_http_exporter_test.cc +++ b/exporters/otlp/test/otlp_http_exporter_test.cc @@ -11,7 +11,8 @@ # include "opentelemetry/exporters/otlp/protobuf_include_suffix.h" -# include "opentelemetry/ext/http/client/curl/http_client_curl.h" +# include "opentelemetry/ext/http/client/http_client_factory.h" +# include "opentelemetry/ext/http/client/nosend/http_client_nosend.h" # include "opentelemetry/ext/http/server/http_server.h" # include "opentelemetry/sdk/trace/batch_span_processor.h" # include "opentelemetry/sdk/trace/tracer_provider.h" @@ -60,35 +61,6 @@ OtlpHttpClientOptions MakeOtlpHttpClientOptions(HttpRequestContentType content_t namespace http_client = opentelemetry::ext::http::client; -class MockSession : public http_client::curl::Session -{ -public: - MockSession(http_client::curl::HttpClient &http_client) - : http_client::curl::Session(http_client, "http", "", 80) - {} - MOCK_METHOD(void, - SendRequest, - (opentelemetry::ext::http::client::EventHandler &), - (noexcept, override)); - bool FinishSession() noexcept override { return true; } -}; - -class NosendHttpClient : public http_client::curl::HttpClient -{ -public: - NosendHttpClient() - { - session_ = std::shared_ptr{new MockSession(*this)}; - } - std::shared_ptr CreateSession( - nostd::string_view url) noexcept override - { - return session_; - } - - std::shared_ptr session_; -}; - class OtlpHttpExporterTestPeer : public ::testing::Test { public: @@ -106,7 +78,7 @@ class OtlpHttpExporterTestPeer : public ::testing::Test static std::pair> GetMockOtlpHttpClient(HttpRequestContentType content_type) { - std::shared_ptr http_client{new NosendHttpClient}; + auto http_client = http_client::HttpClientFactory::Create(); return {new OtlpHttpClient(MakeOtlpHttpClientOptions(content_type), http_client), http_client}; } }; @@ -162,8 +134,9 @@ TEST_F(OtlpHttpExporterTestPeer, ExportJsonIntegrationTest) .ToLowerBase16(MakeSpan(trace_id_hex)); report_trace_id.assign(trace_id_hex, sizeof(trace_id_hex)); - auto no_send_client = std::static_pointer_cast(client); - auto mock_session = std::static_pointer_cast(no_send_client->session_); + auto no_send_client = std::static_pointer_cast(client); + auto mock_session = + std::static_pointer_cast(no_send_client->session_); EXPECT_CALL(*mock_session, SendRequest) .WillOnce([&mock_session, report_trace_id](opentelemetry::ext::http::client::EventHandler &callback) { @@ -181,7 +154,7 @@ TEST_F(OtlpHttpExporterTestPeer, ExportJsonIntegrationTest) EXPECT_EQ("Custom-Header-Value", custom_header->second); } // let the otlp_http_client to continue - http_client::curl::Response response; + http_client::nosend::Response response; callback.OnResponse(response); }); @@ -240,8 +213,9 @@ TEST_F(OtlpHttpExporterTestPeer, ExportBinaryIntegrationTest) .CopyBytesTo(MakeSpan(trace_id_binary)); report_trace_id.assign(reinterpret_cast(trace_id_binary), sizeof(trace_id_binary)); - auto no_send_client = std::static_pointer_cast(client); - auto mock_session = std::static_pointer_cast(no_send_client->session_); + auto no_send_client = std::static_pointer_cast(client); + auto mock_session = + std::static_pointer_cast(no_send_client->session_); EXPECT_CALL(*mock_session, SendRequest) .WillOnce([&mock_session, report_trace_id](opentelemetry::ext::http::client::EventHandler &callback) { @@ -259,7 +233,7 @@ TEST_F(OtlpHttpExporterTestPeer, ExportBinaryIntegrationTest) EXPECT_EQ("Custom-Header-Value", custom_header->second); } // let the otlp_http_client to continue - http_client::curl::Response response; + http_client::nosend::Response response; callback.OnResponse(response); }); diff --git a/exporters/otlp/test/otlp_http_log_exporter_test.cc b/exporters/otlp/test/otlp_http_log_exporter_test.cc index f96d1644c4..8ebdf7aba2 100644 --- a/exporters/otlp/test/otlp_http_log_exporter_test.cc +++ b/exporters/otlp/test/otlp_http_log_exporter_test.cc @@ -13,7 +13,8 @@ # include "opentelemetry/exporters/otlp/protobuf_include_suffix.h" # include "opentelemetry/common/key_value_iterable_view.h" -# include "opentelemetry/ext/http/client/curl/http_client_curl.h" +# include "opentelemetry/ext/http/client/http_client_factory.h" +# include "opentelemetry/ext/http/client/nosend/http_client_nosend.h" # include "opentelemetry/ext/http/server/http_server.h" # include "opentelemetry/logs/provider.h" # include "opentelemetry/sdk/logs/batch_log_processor.h" @@ -62,35 +63,6 @@ OtlpHttpClientOptions MakeOtlpHttpClientOptions(HttpRequestContentType content_t namespace http_client = opentelemetry::ext::http::client; -class MockSession : public http_client::curl::Session -{ -public: - MockSession(http_client::curl::HttpClient &http_client) - : http_client::curl::Session(http_client, "http", "", 80) - {} - MOCK_METHOD(void, - SendRequest, - (opentelemetry::ext::http::client::EventHandler &), - (noexcept, override)); - bool FinishSession() noexcept override { return true; } -}; - -class NosendHttpClient : public http_client::curl::HttpClient -{ -public: - NosendHttpClient() - { - session_ = std::shared_ptr{new MockSession(*this)}; - } - std::shared_ptr CreateSession( - nostd::string_view url) noexcept override - { - return session_; - } - - std::shared_ptr session_; -}; - class OtlpHttpLogExporterTestPeer : public ::testing::Test { public: @@ -107,7 +79,7 @@ class OtlpHttpLogExporterTestPeer : public ::testing::Test static std::pair> GetMockOtlpHttpClient(HttpRequestContentType content_type) { - std::shared_ptr http_client{new NosendHttpClient}; + auto http_client = http_client::HttpClientFactory::Create(); return {new OtlpHttpClient(MakeOtlpHttpClientOptions(content_type), http_client), http_client}; } }; @@ -172,8 +144,9 @@ TEST_F(OtlpHttpLogExporterTestPeer, ExportJsonIntegrationTest) span_id.ToLowerBase16(MakeSpan(span_id_hex)); report_span_id.assign(span_id_hex, sizeof(span_id_hex)); - auto no_send_client = std::static_pointer_cast(client); - auto mock_session = std::static_pointer_cast(no_send_client->session_); + auto no_send_client = std::static_pointer_cast(client); + auto mock_session = + std::static_pointer_cast(no_send_client->session_); EXPECT_CALL(*mock_session, SendRequest) .WillOnce([&mock_session, report_trace_id, report_span_id](opentelemetry::ext::http::client::EventHandler &callback) { @@ -195,7 +168,7 @@ TEST_F(OtlpHttpLogExporterTestPeer, ExportJsonIntegrationTest) EXPECT_EQ("Custom-Header-Value", custom_header->second); } // let the otlp_http_client to continue - http_client::curl::Response response; + http_client::nosend::Response response; callback.OnResponse(response); }); } @@ -255,8 +228,9 @@ TEST_F(OtlpHttpLogExporterTestPeer, ExportBinaryIntegrationTest) report_trace_id.assign(reinterpret_cast(trace_id_bin), sizeof(trace_id_bin)); report_span_id.assign(reinterpret_cast(span_id_bin), sizeof(span_id_bin)); - auto no_send_client = std::static_pointer_cast(client); - auto mock_session = std::static_pointer_cast(no_send_client->session_); + auto no_send_client = std::static_pointer_cast(client); + auto mock_session = + std::static_pointer_cast(no_send_client->session_); EXPECT_CALL(*mock_session, SendRequest) .WillOnce([&mock_session, report_trace_id, report_span_id](opentelemetry::ext::http::client::EventHandler &callback) { @@ -279,7 +253,7 @@ TEST_F(OtlpHttpLogExporterTestPeer, ExportBinaryIntegrationTest) } } ASSERT_TRUE(check_service_name); - http_client::curl::Response response; + http_client::nosend::Response response; callback.OnResponse(response); }); } diff --git a/ext/include/opentelemetry/ext/http/client/nosend/http_client_nosend.h b/ext/include/opentelemetry/ext/http/client/nosend/http_client_nosend.h new file mode 100644 index 0000000000..f92dec0ee3 --- /dev/null +++ b/ext/include/opentelemetry/ext/http/client/nosend/http_client_nosend.h @@ -0,0 +1,183 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once + +#ifdef ENABLE_TEST +# include "opentelemetry/ext/http/client/http_client.h" +# include "opentelemetry/ext/http/common/url_parser.h" +# include "opentelemetry/version.h" + +# include +# include +# include + +# include +# include "gmock/gmock.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace ext +{ +namespace http +{ +namespace client +{ +namespace nosend +{ + +const opentelemetry::ext::http::client::StatusCode Http_Ok = 200; + +class Request : public opentelemetry::ext::http::client::Request +{ +public: + Request() : method_(opentelemetry::ext::http::client::Method::Get), uri_("/") {} + + void SetMethod(opentelemetry::ext::http::client::Method method) noexcept override + { + method_ = method; + } + + void SetBody(opentelemetry::ext::http::client::Body &body) noexcept override + { + body_ = std::move(body); + } + + void AddHeader(nostd::string_view name, nostd::string_view value) noexcept override + { + headers_.insert(std::pair(static_cast(name), + static_cast(value))); + } + + void ReplaceHeader(nostd::string_view name, nostd::string_view value) noexcept override; + + virtual void SetUri(nostd::string_view uri) noexcept override + { + uri_ = static_cast(uri); + } + + void SetTimeoutMs(std::chrono::milliseconds timeout_ms) noexcept override + { + timeout_ms_ = timeout_ms; + } + +public: + opentelemetry::ext::http::client::Method method_; + opentelemetry::ext::http::client::Body body_; + opentelemetry::ext::http::client::Headers headers_; + std::string uri_; + std::chrono::milliseconds timeout_ms_{5000}; // ms +}; + +class Response : public opentelemetry::ext::http::client::Response +{ +public: + Response() : status_code_(Http_Ok) {} + + virtual const opentelemetry::ext::http::client::Body &GetBody() const noexcept override + { + return body_; + } + + virtual bool ForEachHeader( + nostd::function_ref callable) + const noexcept override; + + virtual bool ForEachHeader( + const nostd::string_view &name, + nostd::function_ref callable) + const noexcept override; + + virtual opentelemetry::ext::http::client::StatusCode GetStatusCode() const noexcept override + { + return status_code_; + } + +public: + Headers headers_; + opentelemetry::ext::http::client::Body body_; + opentelemetry::ext::http::client::StatusCode status_code_; +}; + +class HttpClient; + +class Session : public opentelemetry::ext::http::client::Session +{ +public: + Session(HttpClient &http_client, + std::string scheme = "http", + const std::string &host = "", + uint16_t port = 80) + : http_client_(http_client), is_session_active_(false) + { + host_ = scheme + "://" + host + ":" + std::to_string(port) + "/"; + } + + std::shared_ptr CreateRequest() noexcept override + { + http_request_.reset(new Request()); + return http_request_; + } + + MOCK_METHOD(void, + SendRequest, + (opentelemetry::ext::http::client::EventHandler &), + (noexcept, override)); + + virtual bool CancelSession() noexcept override; + + virtual bool FinishSession() noexcept override; + + virtual bool IsSessionActive() noexcept override { return is_session_active_; } + + void SetId(uint64_t session_id) { session_id_ = session_id; } + + /** + * Returns the base URI. + * @return the base URI as a string consisting of scheme, host and port. + */ + const std::string &GetBaseUri() const { return host_; } + + std::shared_ptr GetRequest() { return http_request_; } + +private: + std::shared_ptr http_request_; + std::string host_; + uint64_t session_id_; + HttpClient &http_client_; + bool is_session_active_; +}; + +class HttpClient : public opentelemetry::ext::http::client::HttpClient +{ +public: + HttpClient() { session_ = std::shared_ptr{new Session(*this)}; } + + std::shared_ptr CreateSession( + nostd::string_view) noexcept override + { + return session_; + } + + bool CancelAllSessions() noexcept override + { + session_->CancelSession(); + return true; + } + + bool FinishAllSessions() noexcept override + { + session_->FinishSession(); + return true; + } + + void CleanupSession(uint64_t session_id) {} + + std::shared_ptr session_; +}; + +} // namespace nosend +} // namespace client +} // namespace http +} // namespace ext +OPENTELEMETRY_END_NAMESPACE +#endif diff --git a/ext/src/http/client/BUILD b/ext/src/http/client/BUILD new file mode 100644 index 0000000000..dfbdaf95e2 --- /dev/null +++ b/ext/src/http/client/BUILD @@ -0,0 +1,16 @@ +package(default_visibility = ["//visibility:public"]) + +cc_library( + name = "http_client_factory", + srcs = [ + "http_client_factory.cc", + ], + include_prefix = "src/http/client", + deps = [ + "//api", + "//ext:headers", + "//ext/src/http/client/curl:http_client_curl", + "//ext/src/http/client/nosend:http_client_nosend", + "//sdk:headers", + ], +) diff --git a/ext/src/http/client/curl/BUILD b/ext/src/http/client/curl/BUILD index 33ab814b91..1e5f34f156 100644 --- a/ext/src/http/client/curl/BUILD +++ b/ext/src/http/client/curl/BUILD @@ -4,7 +4,6 @@ cc_library( name = "http_client_curl", srcs = [ "http_client_curl.cc", - "http_client_factory_curl.cc", ], copts = [ "-DWITH_CURL", diff --git a/ext/src/http/client/curl/http_client_factory_curl.cc b/ext/src/http/client/http_client_factory.cc similarity index 77% rename from ext/src/http/client/curl/http_client_factory_curl.cc rename to ext/src/http/client/http_client_factory.cc index 262dfde63c..54821403ec 100644 --- a/ext/src/http/client/curl/http_client_factory_curl.cc +++ b/ext/src/http/client/http_client_factory.cc @@ -1,14 +1,19 @@ // Copyright The OpenTelemetry Authors // SPDX-License-Identifier: Apache-2.0 +#include "opentelemetry/ext/http/client/http_client_factory.h" #include "opentelemetry/ext/http/client/curl/http_client_curl.h" #include "opentelemetry/ext/http/client/http_client.h" -#include "opentelemetry/ext/http/client/http_client_factory.h" - +#ifdef ENABLE_TEST +# include "opentelemetry/ext/http/client/nosend/http_client_nosend.h" +#endif namespace http_client = opentelemetry::ext::http::client; std::shared_ptr http_client::HttpClientFactory::Create() { +#ifdef ENABLE_TEST + return std::make_shared(); +#endif return std::make_shared(); } diff --git a/ext/src/http/client/nosend/BUILD b/ext/src/http/client/nosend/BUILD new file mode 100644 index 0000000000..5e3079c394 --- /dev/null +++ b/ext/src/http/client/nosend/BUILD @@ -0,0 +1,18 @@ +package(default_visibility = ["//visibility:public"]) + +cc_library( + name = "http_client_nosend", + srcs = [ + "http_client_nosend.cc", + ], + include_prefix = "src/http/client/nosend", + tags = [ + "test", + ], + deps = [ + "//api", + "//ext:headers", + "//sdk:headers", + "@com_google_googletest//:gtest_main", + ], +) diff --git a/ext/src/http/client/nosend/http_client_nosend.cc b/ext/src/http/client/nosend/http_client_nosend.cc new file mode 100644 index 0000000000..a92224741c --- /dev/null +++ b/ext/src/http/client/nosend/http_client_nosend.cc @@ -0,0 +1,70 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#ifdef ENABLE_TEST +# include "opentelemetry/ext/http/client/nosend/http_client_nosend.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace ext +{ +namespace http +{ +namespace client +{ +namespace nosend +{ +void Request::ReplaceHeader(nostd::string_view name, nostd::string_view value) noexcept +{ + // erase matching headers + auto range = headers_.equal_range(static_cast(name)); + headers_.erase(range.first, range.second); + AddHeader(name, value); +} + +bool Response::ForEachHeader( + nostd::function_ref callable) + const noexcept +{ + for (const auto &header : headers_) + { + if (!callable(header.first, header.second)) + { + return false; + } + } + return true; +} + +bool Response::ForEachHeader( + const nostd::string_view &name, + nostd::function_ref callable) + const noexcept +{ + auto range = headers_.equal_range(static_cast(name)); + for (auto it = range.first; it != range.second; ++it) + { + if (!callable(it->first, it->second)) + { + return false; + } + } + return true; +} +bool Session::CancelSession() noexcept +{ + http_client_.CleanupSession(session_id_); + return true; +} + +bool Session::FinishSession() noexcept +{ + http_client_.CleanupSession(session_id_); + return true; +} + +} // namespace nosend +} // namespace client +} // namespace http +} // namespace ext +OPENTELEMETRY_END_NAMESPACE +#endif From f724e1a9f797816416ac4bfa6532d448bb9cf08c Mon Sep 17 00:00:00 2001 From: esigo Date: Sat, 12 Feb 2022 23:12:07 +0100 Subject: [PATCH 26/35] cmake --- CMakeLists.txt | 1 + exporters/otlp/CMakeLists.txt | 12 +++++--- .../otlp/test/otlp_http_exporter_test.cc | 2 +- .../otlp/test/otlp_http_log_exporter_test.cc | 2 +- .../ext/http/client/http_client_factory.h | 9 +++++- .../http/client/nosend/http_client_nosend.h | 1 + ext/src/CMakeLists.txt | 2 +- ext/src/http/client/CMakeLists.txt | 5 ++++ ext/src/http/client/curl/CMakeLists.txt | 2 +- ext/src/http/client/http_client_factory.cc | 8 +++-- ext/src/http/client/nosend/CMakeLists.txt | 29 +++++++++++++++++++ .../http/client/nosend/http_client_nosend.cc | 1 + ext/test/http/CMakeLists.txt | 4 +-- ext/test/http/curl_http_test.cc | 2 +- 14 files changed, 66 insertions(+), 14 deletions(-) create mode 100644 ext/src/http/client/CMakeLists.txt create mode 100644 ext/src/http/client/nosend/CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt index e49dcf6302..a9714bdbfa 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -390,6 +390,7 @@ list(APPEND CMAKE_PREFIX_PATH "${CMAKE_BINARY_DIR}") include(CTest) if(BUILD_TESTING) + add_definitions(-DENABLE_TEST) if(EXISTS ${CMAKE_BINARY_DIR}/lib/libgtest.a) # Prefer GTest from build tree. GTest is not always working with # CMAKE_PREFIX_PATH diff --git a/exporters/otlp/CMakeLists.txt b/exporters/otlp/CMakeLists.txt index 6fedeefd35..0bf685fe0f 100755 --- a/exporters/otlp/CMakeLists.txt +++ b/exporters/otlp/CMakeLists.txt @@ -173,7 +173,7 @@ if(BUILD_TESTING) add_executable(otlp_http_exporter_test test/otlp_http_exporter_test.cc) target_link_libraries( otlp_http_exporter_test ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} - ${GMOCK_LIB} opentelemetry_exporter_otlp_http) + ${GMOCK_LIB} opentelemetry_exporter_otlp_http http_client_factory) gtest_add_tests( TARGET otlp_http_exporter_test TEST_PREFIX exporter.otlp. @@ -183,9 +183,13 @@ if(BUILD_TESTING) add_executable(otlp_http_log_exporter_test test/otlp_http_log_exporter_test.cc) target_link_libraries( - otlp_http_log_exporter_test ${GTEST_BOTH_LIBRARIES} - ${CMAKE_THREAD_LIBS_INIT} ${GMOCK_LIB} - opentelemetry_exporter_otlp_http_log opentelemetry_logs) + otlp_http_log_exporter_test + ${GTEST_BOTH_LIBRARIES} + ${CMAKE_THREAD_LIBS_INIT} + ${GMOCK_LIB} + opentelemetry_exporter_otlp_http_log + opentelemetry_logs + http_client_factory) gtest_add_tests( TARGET otlp_http_log_exporter_test TEST_PREFIX exporter.otlp. diff --git a/exporters/otlp/test/otlp_http_exporter_test.cc b/exporters/otlp/test/otlp_http_exporter_test.cc index 5929301ac8..5cd8d90e9d 100644 --- a/exporters/otlp/test/otlp_http_exporter_test.cc +++ b/exporters/otlp/test/otlp_http_exporter_test.cc @@ -78,7 +78,7 @@ class OtlpHttpExporterTestPeer : public ::testing::Test static std::pair> GetMockOtlpHttpClient(HttpRequestContentType content_type) { - auto http_client = http_client::HttpClientFactory::Create(); + auto http_client = http_client::HttpClientFactory::Create(http_client::ClientType::Nosend); return {new OtlpHttpClient(MakeOtlpHttpClientOptions(content_type), http_client), http_client}; } }; diff --git a/exporters/otlp/test/otlp_http_log_exporter_test.cc b/exporters/otlp/test/otlp_http_log_exporter_test.cc index 8ebdf7aba2..c32e3682a9 100644 --- a/exporters/otlp/test/otlp_http_log_exporter_test.cc +++ b/exporters/otlp/test/otlp_http_log_exporter_test.cc @@ -79,7 +79,7 @@ class OtlpHttpLogExporterTestPeer : public ::testing::Test static std::pair> GetMockOtlpHttpClient(HttpRequestContentType content_type) { - auto http_client = http_client::HttpClientFactory::Create(); + auto http_client = http_client::HttpClientFactory::Create(http_client::ClientType::Nosend); return {new OtlpHttpClient(MakeOtlpHttpClientOptions(content_type), http_client), http_client}; } }; diff --git a/ext/include/opentelemetry/ext/http/client/http_client_factory.h b/ext/include/opentelemetry/ext/http/client/http_client_factory.h index 49504d4aac..2fbf0735d2 100644 --- a/ext/include/opentelemetry/ext/http/client/http_client_factory.h +++ b/ext/include/opentelemetry/ext/http/client/http_client_factory.h @@ -11,12 +11,19 @@ namespace http { namespace client { + +enum class ClientType +{ + Curl, + Nosend +}; + class HttpClientFactory { public: static std::shared_ptr CreateSync(); - static std::shared_ptr Create(); + static std::shared_ptr Create(const ClientType client_type = ClientType::Curl); }; } // namespace client } // namespace http diff --git a/ext/include/opentelemetry/ext/http/client/nosend/http_client_nosend.h b/ext/include/opentelemetry/ext/http/client/nosend/http_client_nosend.h index f92dec0ee3..02433d75ce 100644 --- a/ext/include/opentelemetry/ext/http/client/nosend/http_client_nosend.h +++ b/ext/include/opentelemetry/ext/http/client/nosend/http_client_nosend.h @@ -15,6 +15,7 @@ # include # include "gmock/gmock.h" +using namespace testing; OPENTELEMETRY_BEGIN_NAMESPACE namespace ext { diff --git a/ext/src/CMakeLists.txt b/ext/src/CMakeLists.txt index 191a9e7bdf..eb47794ca8 100644 --- a/ext/src/CMakeLists.txt +++ b/ext/src/CMakeLists.txt @@ -1,4 +1,4 @@ if(WITH_ZPAGES) add_subdirectory(zpages) endif() -add_subdirectory(http/client/curl) +add_subdirectory(http/client) diff --git a/ext/src/http/client/CMakeLists.txt b/ext/src/http/client/CMakeLists.txt new file mode 100644 index 0000000000..b4ce94483a --- /dev/null +++ b/ext/src/http/client/CMakeLists.txt @@ -0,0 +1,5 @@ +add_subdirectory(curl) +add_subdirectory(nosend) + +add_library(http_client_factory http_client_factory.cc) +target_link_libraries(http_client_factory http_client_curl http_client_nosend) diff --git a/ext/src/http/client/curl/CMakeLists.txt b/ext/src/http/client/curl/CMakeLists.txt index 64486b96db..6b65d10ea6 100644 --- a/ext/src/http/client/curl/CMakeLists.txt +++ b/ext/src/http/client/curl/CMakeLists.txt @@ -1,6 +1,6 @@ find_package(CURL) if(CURL_FOUND) - add_library(http_client_curl http_client_factory_curl.cc http_client_curl.cc) + add_library(http_client_curl http_client_curl.cc) set_target_properties(http_client_curl PROPERTIES EXPORT_NAME http_client_curl) diff --git a/ext/src/http/client/http_client_factory.cc b/ext/src/http/client/http_client_factory.cc index 54821403ec..39c881603b 100644 --- a/ext/src/http/client/http_client_factory.cc +++ b/ext/src/http/client/http_client_factory.cc @@ -9,10 +9,14 @@ #endif namespace http_client = opentelemetry::ext::http::client; -std::shared_ptr http_client::HttpClientFactory::Create() +std::shared_ptr http_client::HttpClientFactory::Create( + const ClientType client_type) { #ifdef ENABLE_TEST - return std::make_shared(); + if (client_type == ClientType::Nosend) + { + return std::make_shared(); + } #endif return std::make_shared(); } diff --git a/ext/src/http/client/nosend/CMakeLists.txt b/ext/src/http/client/nosend/CMakeLists.txt new file mode 100644 index 0000000000..a554aede96 --- /dev/null +++ b/ext/src/http/client/nosend/CMakeLists.txt @@ -0,0 +1,29 @@ +add_library( + http_client_nosend + # "${CMAKE_SOURCE_DIR}/ext/include/opentelemetry/ext/http/client/nosend/http_client_nosend.h" + http_client_nosend.cc) + +set_target_properties(http_client_nosend PROPERTIES EXPORT_NAME + http_client_nosend) + +if(MSVC) + # Explicitly specify that we consume GTest from shared library. The rest of + # code logic below determines whether we link Release or Debug flavor of the + # library. These flavors have different prefix on Windows, gmock and gmockd + # respectively. + add_definitions(-DGTEST_LINKED_AS_SHARED_LIBRARY=1) + if(GMOCK_LIB) + # unset GMOCK_LIB to force find_library to redo the lookup, as the cached + # entry could cause linking to incorrect flavor of gmock and leading to + # runtime error. + unset(GMOCK_LIB CACHE) + endif() +endif() +if(MSVC AND CMAKE_BUILD_TYPE STREQUAL "Debug") + find_library(GMOCK_LIB gmockd PATH_SUFFIXES lib) +else() + find_library(GMOCK_LIB gmock PATH_SUFFIXES lib) +endif() + +target_link_libraries(http_client_nosend ${GMOCK_LIB} opentelemetry_api + opentelemetry_ext) diff --git a/ext/src/http/client/nosend/http_client_nosend.cc b/ext/src/http/client/nosend/http_client_nosend.cc index a92224741c..c2b1c6acf9 100644 --- a/ext/src/http/client/nosend/http_client_nosend.cc +++ b/ext/src/http/client/nosend/http_client_nosend.cc @@ -50,6 +50,7 @@ bool Response::ForEachHeader( } return true; } + bool Session::CancelSession() noexcept { http_client_.CleanupSession(session_id_); diff --git a/ext/test/http/CMakeLists.txt b/ext/test/http/CMakeLists.txt index 13b7c8206e..933673e6b5 100644 --- a/ext/test/http/CMakeLists.txt +++ b/ext/test/http/CMakeLists.txt @@ -7,10 +7,10 @@ if(CURL_FOUND) ${CMAKE_THREAD_LIBS_INIT}) if(TARGET CURL::libcurl) - target_link_libraries(${FILENAME} CURL::libcurl http_client_curl) + target_link_libraries(${FILENAME} CURL::libcurl http_client_factory) else() include_directories(${CURL_INCLUDE_DIRS}) - target_link_libraries(${FILENAME} ${CURL_LIBRARIES} http_client_curl) + target_link_libraries(${FILENAME} ${CURL_LIBRARIES} http_client_factory) endif() gtest_add_tests( TARGET ${FILENAME} diff --git a/ext/test/http/curl_http_test.cc b/ext/test/http/curl_http_test.cc index bc89b3277e..c9a6312888 100644 --- a/ext/test/http/curl_http_test.cc +++ b/ext/test/http/curl_http_test.cc @@ -1,7 +1,7 @@ // Copyright The OpenTelemetry Authors // SPDX-License-Identifier: Apache-2.0 -#include "opentelemetry/ext//http/client/curl//http_client_curl.h" +#include "opentelemetry/ext//http/client/curl/http_client_curl.h" #include "opentelemetry/ext/http/client/http_client_factory.h" #include "opentelemetry/ext/http/server/http_server.h" From f500f4d9e6412591babf7e75d34abc09522e9025 Mon Sep 17 00:00:00 2001 From: esigo Date: Sat, 12 Feb 2022 23:25:30 +0100 Subject: [PATCH 27/35] http_client_factory bazel --- examples/http/BUILD | 2 +- exporters/elasticsearch/BUILD | 2 +- exporters/jaeger/BUILD | 2 +- exporters/zipkin/BUILD | 2 +- ext/test/http/BUILD | 2 +- ext/test/w3c_tracecontext_test/BUILD | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/examples/http/BUILD b/examples/http/BUILD index ac36bc0cee..60a6724a8d 100644 --- a/examples/http/BUILD +++ b/examples/http/BUILD @@ -20,7 +20,7 @@ cc_binary( "//api", "//exporters/ostream:ostream_span_exporter", "//ext:headers", - "//ext/src/http/client/curl:http_client_curl", + "//ext/src/http/client:http_client_factory", "//sdk/src/trace", ], ) diff --git a/exporters/elasticsearch/BUILD b/exporters/elasticsearch/BUILD index 78ff94d466..2909917f21 100644 --- a/exporters/elasticsearch/BUILD +++ b/exporters/elasticsearch/BUILD @@ -21,7 +21,7 @@ cc_library( tags = ["es"], deps = [ "//ext:headers", - "//ext/src/http/client/curl:http_client_curl", + "//ext/src/http/client:http_client_factory", "//sdk/src/logs", "@curl", "@github_nlohmann_json//:json", diff --git a/exporters/jaeger/BUILD b/exporters/jaeger/BUILD index 49e60cfb1d..b2cb0f4b08 100644 --- a/exporters/jaeger/BUILD +++ b/exporters/jaeger/BUILD @@ -55,7 +55,7 @@ cc_library( tags = ["jaeger"], deps = [ ":thrift", - "//ext/src/http/client/curl:http_client_curl", + "//ext/src/http/client:http_client_factory", ], ) diff --git a/exporters/zipkin/BUILD b/exporters/zipkin/BUILD index 6cd52b2d05..2e2ba8f470 100644 --- a/exporters/zipkin/BUILD +++ b/exporters/zipkin/BUILD @@ -32,7 +32,7 @@ cc_library( tags = ["zipkin"], deps = [ ":zipkin_recordable", - "//ext/src/http/client/curl:http_client_curl", + "//ext/src/http/client:http_client_factory", ], ) diff --git a/ext/test/http/BUILD b/ext/test/http/BUILD index d1818ca0e3..0192f86097 100644 --- a/ext/test/http/BUILD +++ b/ext/test/http/BUILD @@ -6,7 +6,7 @@ cc_test( tags = ["test"], deps = [ "//ext:headers", - "//ext/src/http/client/curl:http_client_curl", + "//ext/src/http/client:http_client_factory", "//sdk/src/trace", "@com_google_googletest//:gtest_main", "@curl", diff --git a/ext/test/w3c_tracecontext_test/BUILD b/ext/test/w3c_tracecontext_test/BUILD index e52c943fe3..352cd0e7aa 100644 --- a/ext/test/w3c_tracecontext_test/BUILD +++ b/ext/test/w3c_tracecontext_test/BUILD @@ -15,7 +15,7 @@ cc_binary( "//api", "//exporters/ostream:ostream_span_exporter", "//ext:headers", - "//ext/src/http/client/curl:http_client_curl", + "//ext/src/http/client:http_client_factory", "//sdk/src/trace", "@curl", "@github_nlohmann_json//:json", From 75427bea1585e9d8fc11181280672d326d9094ca Mon Sep 17 00:00:00 2001 From: esigo Date: Sat, 12 Feb 2022 23:30:13 +0100 Subject: [PATCH 28/35] http_client_factory cmake --- examples/http/CMakeLists.txt | 8 ++++---- exporters/elasticsearch/CMakeLists.txt | 2 +- exporters/jaeger/CMakeLists.txt | 2 +- exporters/otlp/CMakeLists.txt | 2 +- exporters/zipkin/CMakeLists.txt | 2 +- ext/test/w3c_tracecontext_test/CMakeLists.txt | 2 +- 6 files changed, 9 insertions(+), 9 deletions(-) diff --git a/examples/http/CMakeLists.txt b/examples/http/CMakeLists.txt index 2eddcfe03f..ca54f4032a 100644 --- a/examples/http/CMakeLists.txt +++ b/examples/http/CMakeLists.txt @@ -10,10 +10,10 @@ else() add_executable(http_server server.cc) target_link_libraries( - http_client ${CMAKE_THREAD_LIBS_INIT} opentelemetry_trace http_client_curl - opentelemetry_exporter_ostream_span ${CURL_LIBRARIES}) + http_client ${CMAKE_THREAD_LIBS_INIT} opentelemetry_trace + http_client_factory opentelemetry_exporter_ostream_span ${CURL_LIBRARIES}) target_link_libraries( - http_server ${CMAKE_THREAD_LIBS_INIT} opentelemetry_trace http_client_curl - opentelemetry_exporter_ostream_span) + http_server ${CMAKE_THREAD_LIBS_INIT} opentelemetry_trace + http_client_factory opentelemetry_exporter_ostream_span) endif() diff --git a/exporters/elasticsearch/CMakeLists.txt b/exporters/elasticsearch/CMakeLists.txt index 1538a6f44a..a659a8ed45 100644 --- a/exporters/elasticsearch/CMakeLists.txt +++ b/exporters/elasticsearch/CMakeLists.txt @@ -10,7 +10,7 @@ target_include_directories( target_link_libraries( opentelemetry_exporter_elasticsearch_logs - PUBLIC opentelemetry_trace opentelemetry_logs http_client_curl) + PUBLIC opentelemetry_trace opentelemetry_logs http_client_factory) install( TARGETS opentelemetry_exporter_elasticsearch_logs diff --git a/exporters/jaeger/CMakeLists.txt b/exporters/jaeger/CMakeLists.txt index 0659c399b0..c0a57cb887 100644 --- a/exporters/jaeger/CMakeLists.txt +++ b/exporters/jaeger/CMakeLists.txt @@ -31,7 +31,7 @@ target_include_directories( target_link_libraries( opentelemetry_exporter_jaeger_trace - PUBLIC opentelemetry_resources http_client_curl + PUBLIC opentelemetry_resources http_client_factory PRIVATE thrift::thrift) if(MSVC) diff --git a/exporters/otlp/CMakeLists.txt b/exporters/otlp/CMakeLists.txt index 0bf685fe0f..4c20521a23 100755 --- a/exporters/otlp/CMakeLists.txt +++ b/exporters/otlp/CMakeLists.txt @@ -49,7 +49,7 @@ if(WITH_OTLP_HTTP) PROPERTIES EXPORT_NAME otlp_http_client) target_link_libraries( opentelemetry_exporter_otlp_http_client - PUBLIC opentelemetry_sdk opentelemetry_proto http_client_curl + PUBLIC opentelemetry_sdk opentelemetry_proto http_client_factory nlohmann_json::nlohmann_json) if(nlohmann_json_clone) add_dependencies(opentelemetry_exporter_otlp_http_client diff --git a/exporters/zipkin/CMakeLists.txt b/exporters/zipkin/CMakeLists.txt index b9591324fd..0e7aa6e7a6 100644 --- a/exporters/zipkin/CMakeLists.txt +++ b/exporters/zipkin/CMakeLists.txt @@ -20,7 +20,7 @@ add_library(opentelemetry_exporter_zipkin_trace src/zipkin_exporter.cc target_link_libraries( opentelemetry_exporter_zipkin_trace - PUBLIC opentelemetry_trace http_client_curl nlohmann_json::nlohmann_json) + PUBLIC opentelemetry_trace http_client_factory nlohmann_json::nlohmann_json) install( TARGETS opentelemetry_exporter_zipkin_trace diff --git a/ext/test/w3c_tracecontext_test/CMakeLists.txt b/ext/test/w3c_tracecontext_test/CMakeLists.txt index 30e2f5d0f3..dd1f5a9963 100644 --- a/ext/test/w3c_tracecontext_test/CMakeLists.txt +++ b/ext/test/w3c_tracecontext_test/CMakeLists.txt @@ -7,7 +7,7 @@ else() add_executable(w3c_tracecontext_test main.cc) target_link_libraries( w3c_tracecontext_test - PRIVATE ${CMAKE_THREAD_LIBS_INIT} opentelemetry_trace http_client_curl + PRIVATE ${CMAKE_THREAD_LIBS_INIT} opentelemetry_trace http_client_factory opentelemetry_exporter_ostream_span ${CURL_LIBRARIES} nlohmann_json::nlohmann_json) if(nlohmann_json_clone) From 1101015b97ff07d1c05ff671e34b57411523c000 Mon Sep 17 00:00:00 2001 From: esigo Date: Sun, 13 Feb 2022 01:08:38 +0100 Subject: [PATCH 29/35] fix cmake --- CMakeLists.txt | 1 + exporters/otlp/CMakeLists.txt | 3 -- ext/src/http/client/CMakeLists.txt | 16 +++++- ext/src/http/client/nosend/CMakeLists.txt | 59 +++++++++++++---------- 4 files changed, 50 insertions(+), 29 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a9714bdbfa..d631e09236 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -427,6 +427,7 @@ include_directories(api/include) add_subdirectory(api) if(NOT WITH_API_ONLY) + set(BUILD_TESTING ${BUILD_TESTING}) include_directories(sdk/include) include_directories(sdk) include_directories(ext/include) diff --git a/exporters/otlp/CMakeLists.txt b/exporters/otlp/CMakeLists.txt index 4c20521a23..7387d01817 100755 --- a/exporters/otlp/CMakeLists.txt +++ b/exporters/otlp/CMakeLists.txt @@ -40,9 +40,6 @@ if(WITH_OTLP_GRPC) endif() if(WITH_OTLP_HTTP) - if(BUILD_TESTING) - add_definitions(-DENABLE_TEST) - endif() find_package(CURL REQUIRED) add_library(opentelemetry_exporter_otlp_http_client src/otlp_http_client.cc) set_target_properties(opentelemetry_exporter_otlp_http_client diff --git a/ext/src/http/client/CMakeLists.txt b/ext/src/http/client/CMakeLists.txt index b4ce94483a..dd2491e46c 100644 --- a/ext/src/http/client/CMakeLists.txt +++ b/ext/src/http/client/CMakeLists.txt @@ -2,4 +2,18 @@ add_subdirectory(curl) add_subdirectory(nosend) add_library(http_client_factory http_client_factory.cc) -target_link_libraries(http_client_factory http_client_curl http_client_nosend) +set_target_properties(http_client_factory PROPERTIES EXPORT_NAME + http_client_factory) +set(http_client_factory_libs http_client_curl) +if(${BUILD_TESTING}) + set(http_client_factory_libs ${http_client_factory_libs} http_client_nosend) +endif() + +target_link_libraries(http_client_factory ${http_client_factory_libs}) + +install( + TARGETS http_client_factory + EXPORT "${PROJECT_NAME}-target" + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}) diff --git a/ext/src/http/client/nosend/CMakeLists.txt b/ext/src/http/client/nosend/CMakeLists.txt index a554aede96..62b1c5498d 100644 --- a/ext/src/http/client/nosend/CMakeLists.txt +++ b/ext/src/http/client/nosend/CMakeLists.txt @@ -1,29 +1,38 @@ -add_library( - http_client_nosend - # "${CMAKE_SOURCE_DIR}/ext/include/opentelemetry/ext/http/client/nosend/http_client_nosend.h" - http_client_nosend.cc) +if(${BUILD_TESTING}) + add_library( + http_client_nosend + # "${CMAKE_SOURCE_DIR}/ext/include/opentelemetry/ext/http/client/nosend/http_client_nosend.h" + http_client_nosend.cc) -set_target_properties(http_client_nosend PROPERTIES EXPORT_NAME - http_client_nosend) + set_target_properties(http_client_nosend PROPERTIES EXPORT_NAME + http_client_nosend) -if(MSVC) - # Explicitly specify that we consume GTest from shared library. The rest of - # code logic below determines whether we link Release or Debug flavor of the - # library. These flavors have different prefix on Windows, gmock and gmockd - # respectively. - add_definitions(-DGTEST_LINKED_AS_SHARED_LIBRARY=1) - if(GMOCK_LIB) - # unset GMOCK_LIB to force find_library to redo the lookup, as the cached - # entry could cause linking to incorrect flavor of gmock and leading to - # runtime error. - unset(GMOCK_LIB CACHE) + if(MSVC) + # Explicitly specify that we consume GTest from shared library. The rest of + # code logic below determines whether we link Release or Debug flavor of the + # library. These flavors have different prefix on Windows, gmock and gmockd + # respectively. + add_definitions(-DGTEST_LINKED_AS_SHARED_LIBRARY=1) + if(GMOCK_LIB) + # unset GMOCK_LIB to force find_library to redo the lookup, as the cached + # entry could cause linking to incorrect flavor of gmock and leading to + # runtime error. + unset(GMOCK_LIB CACHE) + endif() + endif() + if(MSVC AND CMAKE_BUILD_TYPE STREQUAL "Debug") + find_library(GMOCK_LIB gmockd PATH_SUFFIXES lib) + else() + find_library(GMOCK_LIB gmock PATH_SUFFIXES lib) endif() -endif() -if(MSVC AND CMAKE_BUILD_TYPE STREQUAL "Debug") - find_library(GMOCK_LIB gmockd PATH_SUFFIXES lib) -else() - find_library(GMOCK_LIB gmock PATH_SUFFIXES lib) -endif() -target_link_libraries(http_client_nosend ${GMOCK_LIB} opentelemetry_api - opentelemetry_ext) + target_link_libraries(http_client_nosend ${GMOCK_LIB} opentelemetry_api + opentelemetry_ext) + + install( + TARGETS http_client_nosend + EXPORT "${PROJECT_NAME}-target" + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}) +endif() From 7033b3007466aaebb7a3bbc0d51654e6fc17987a Mon Sep 17 00:00:00 2001 From: esigo Date: Sun, 13 Feb 2022 10:03:33 +0100 Subject: [PATCH 30/35] fix cmake link --- ext/src/http/client/nosend/CMakeLists.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ext/src/http/client/nosend/CMakeLists.txt b/ext/src/http/client/nosend/CMakeLists.txt index 62b1c5498d..b18f0740f7 100644 --- a/ext/src/http/client/nosend/CMakeLists.txt +++ b/ext/src/http/client/nosend/CMakeLists.txt @@ -26,8 +26,8 @@ if(${BUILD_TESTING}) find_library(GMOCK_LIB gmock PATH_SUFFIXES lib) endif() - target_link_libraries(http_client_nosend ${GMOCK_LIB} opentelemetry_api - opentelemetry_ext) + target_link_libraries(http_client_nosend ${GTEST_BOTH_LIBRARIES} ${GMOCK_LIB} + opentelemetry_api opentelemetry_ext) install( TARGETS http_client_nosend From 7f3773a97cf052d34c27cc797a0e8c764b0768d2 Mon Sep 17 00:00:00 2001 From: esigo Date: Sun, 13 Feb 2022 15:49:30 +0100 Subject: [PATCH 31/35] fix cmake Windows --- ext/src/http/client/CMakeLists.txt | 38 +++++++++++++++++++----------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/ext/src/http/client/CMakeLists.txt b/ext/src/http/client/CMakeLists.txt index dd2491e46c..fd2ea356e2 100644 --- a/ext/src/http/client/CMakeLists.txt +++ b/ext/src/http/client/CMakeLists.txt @@ -1,19 +1,29 @@ add_subdirectory(curl) add_subdirectory(nosend) -add_library(http_client_factory http_client_factory.cc) -set_target_properties(http_client_factory PROPERTIES EXPORT_NAME - http_client_factory) -set(http_client_factory_libs http_client_curl) -if(${BUILD_TESTING}) - set(http_client_factory_libs ${http_client_factory_libs} http_client_nosend) -endif() +find_package(CURL) +if(CURL_FOUND) + add_library(http_client_factory http_client_factory.cc) + set_target_properties(http_client_factory PROPERTIES EXPORT_NAME + http_client_factory) + set(http_client_factory_libs http_client_curl) + if(TARGET CURL::libcurl) + set(http_client_factory_libs ${http_client_factory_libs} CURL::libcurl) + else() + set(http_client_factory_libs ${http_client_factory_libs} ${CURL_LIBRARIES}) + target_include_directories(http_client_factory_libs + INTERFACE "${CURL_INCLUDE_DIRS}") + endif() + if(${BUILD_TESTING}) + set(http_client_factory_libs ${http_client_factory_libs} http_client_nosend) + endif() -target_link_libraries(http_client_factory ${http_client_factory_libs}) + target_link_libraries(http_client_factory PUBLIC ${http_client_factory_libs}) -install( - TARGETS http_client_factory - EXPORT "${PROJECT_NAME}-target" - RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} - LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} - ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}) + install( + TARGETS http_client_factory + EXPORT "${PROJECT_NAME}-target" + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}) +endif() From 486fa33c3a2d18482abd408b04cb11b6de600d92 Mon Sep 17 00:00:00 2001 From: Ehsan Saei <71217171+esigo@users.noreply.github.com> Date: Wed, 16 Feb 2022 17:08:33 +0100 Subject: [PATCH 32/35] revert client factory --- examples/http/BUILD | 2 +- examples/http/CMakeLists.txt | 8 ++++---- exporters/elasticsearch/BUILD | 2 +- exporters/elasticsearch/CMakeLists.txt | 2 +- exporters/jaeger/BUILD | 2 +- exporters/jaeger/CMakeLists.txt | 2 +- exporters/otlp/BUILD | 2 +- exporters/otlp/CMakeLists.txt | 4 ++-- exporters/zipkin/BUILD | 2 +- exporters/zipkin/CMakeLists.txt | 2 +- .../ext/http/client/http_client_factory.h | 13 ++++--------- ext/src/http/client/curl/BUILD | 1 + ext/src/http/client/curl/CMakeLists.txt | 2 +- .../http_client_factory_curl.cc} | 17 ++++------------- .../client/nosend/http_client_factory_nosend.cc | 13 +++++++++++++ ext/test/http/BUILD | 2 +- ext/test/http/CMakeLists.txt | 4 ++-- ext/test/w3c_tracecontext_test/BUILD | 2 +- ext/test/w3c_tracecontext_test/CMakeLists.txt | 2 +- 19 files changed, 42 insertions(+), 42 deletions(-) rename ext/src/http/client/{http_client_factory.cc => curl/http_client_factory_curl.cc} (67%) create mode 100644 ext/src/http/client/nosend/http_client_factory_nosend.cc diff --git a/examples/http/BUILD b/examples/http/BUILD index 60a6724a8d..ac36bc0cee 100644 --- a/examples/http/BUILD +++ b/examples/http/BUILD @@ -20,7 +20,7 @@ cc_binary( "//api", "//exporters/ostream:ostream_span_exporter", "//ext:headers", - "//ext/src/http/client:http_client_factory", + "//ext/src/http/client/curl:http_client_curl", "//sdk/src/trace", ], ) diff --git a/examples/http/CMakeLists.txt b/examples/http/CMakeLists.txt index ca54f4032a..2eddcfe03f 100644 --- a/examples/http/CMakeLists.txt +++ b/examples/http/CMakeLists.txt @@ -10,10 +10,10 @@ else() add_executable(http_server server.cc) target_link_libraries( - http_client ${CMAKE_THREAD_LIBS_INIT} opentelemetry_trace - http_client_factory opentelemetry_exporter_ostream_span ${CURL_LIBRARIES}) + http_client ${CMAKE_THREAD_LIBS_INIT} opentelemetry_trace http_client_curl + opentelemetry_exporter_ostream_span ${CURL_LIBRARIES}) target_link_libraries( - http_server ${CMAKE_THREAD_LIBS_INIT} opentelemetry_trace - http_client_factory opentelemetry_exporter_ostream_span) + http_server ${CMAKE_THREAD_LIBS_INIT} opentelemetry_trace http_client_curl + opentelemetry_exporter_ostream_span) endif() diff --git a/exporters/elasticsearch/BUILD b/exporters/elasticsearch/BUILD index 2909917f21..78ff94d466 100644 --- a/exporters/elasticsearch/BUILD +++ b/exporters/elasticsearch/BUILD @@ -21,7 +21,7 @@ cc_library( tags = ["es"], deps = [ "//ext:headers", - "//ext/src/http/client:http_client_factory", + "//ext/src/http/client/curl:http_client_curl", "//sdk/src/logs", "@curl", "@github_nlohmann_json//:json", diff --git a/exporters/elasticsearch/CMakeLists.txt b/exporters/elasticsearch/CMakeLists.txt index a659a8ed45..1538a6f44a 100644 --- a/exporters/elasticsearch/CMakeLists.txt +++ b/exporters/elasticsearch/CMakeLists.txt @@ -10,7 +10,7 @@ target_include_directories( target_link_libraries( opentelemetry_exporter_elasticsearch_logs - PUBLIC opentelemetry_trace opentelemetry_logs http_client_factory) + PUBLIC opentelemetry_trace opentelemetry_logs http_client_curl) install( TARGETS opentelemetry_exporter_elasticsearch_logs diff --git a/exporters/jaeger/BUILD b/exporters/jaeger/BUILD index b2cb0f4b08..49e60cfb1d 100644 --- a/exporters/jaeger/BUILD +++ b/exporters/jaeger/BUILD @@ -55,7 +55,7 @@ cc_library( tags = ["jaeger"], deps = [ ":thrift", - "//ext/src/http/client:http_client_factory", + "//ext/src/http/client/curl:http_client_curl", ], ) diff --git a/exporters/jaeger/CMakeLists.txt b/exporters/jaeger/CMakeLists.txt index c0a57cb887..0659c399b0 100644 --- a/exporters/jaeger/CMakeLists.txt +++ b/exporters/jaeger/CMakeLists.txt @@ -31,7 +31,7 @@ target_include_directories( target_link_libraries( opentelemetry_exporter_jaeger_trace - PUBLIC opentelemetry_resources http_client_factory + PUBLIC opentelemetry_resources http_client_curl PRIVATE thrift::thrift) if(MSVC) diff --git a/exporters/otlp/BUILD b/exporters/otlp/BUILD index d85b79a84e..4968191385 100644 --- a/exporters/otlp/BUILD +++ b/exporters/otlp/BUILD @@ -98,7 +98,7 @@ cc_library( ], deps = [ "//api", - "//ext/src/http/client:http_client_factory", + "//ext/src/http/client/curl:http_client_curl", "//sdk:headers", "@com_github_opentelemetry_proto//:common_proto_cc", "@github_nlohmann_json//:json", diff --git a/exporters/otlp/CMakeLists.txt b/exporters/otlp/CMakeLists.txt index 7387d01817..c3bdc159b0 100755 --- a/exporters/otlp/CMakeLists.txt +++ b/exporters/otlp/CMakeLists.txt @@ -46,7 +46,7 @@ if(WITH_OTLP_HTTP) PROPERTIES EXPORT_NAME otlp_http_client) target_link_libraries( opentelemetry_exporter_otlp_http_client - PUBLIC opentelemetry_sdk opentelemetry_proto http_client_factory + PUBLIC opentelemetry_sdk opentelemetry_proto http_client_curl nlohmann_json::nlohmann_json) if(nlohmann_json_clone) add_dependencies(opentelemetry_exporter_otlp_http_client @@ -186,7 +186,7 @@ if(BUILD_TESTING) ${GMOCK_LIB} opentelemetry_exporter_otlp_http_log opentelemetry_logs - http_client_factory) + http_client_nosend) gtest_add_tests( TARGET otlp_http_log_exporter_test TEST_PREFIX exporter.otlp. diff --git a/exporters/zipkin/BUILD b/exporters/zipkin/BUILD index 2e2ba8f470..6cd52b2d05 100644 --- a/exporters/zipkin/BUILD +++ b/exporters/zipkin/BUILD @@ -32,7 +32,7 @@ cc_library( tags = ["zipkin"], deps = [ ":zipkin_recordable", - "//ext/src/http/client:http_client_factory", + "//ext/src/http/client/curl:http_client_curl", ], ) diff --git a/exporters/zipkin/CMakeLists.txt b/exporters/zipkin/CMakeLists.txt index 0e7aa6e7a6..b9591324fd 100644 --- a/exporters/zipkin/CMakeLists.txt +++ b/exporters/zipkin/CMakeLists.txt @@ -20,7 +20,7 @@ add_library(opentelemetry_exporter_zipkin_trace src/zipkin_exporter.cc target_link_libraries( opentelemetry_exporter_zipkin_trace - PUBLIC opentelemetry_trace http_client_factory nlohmann_json::nlohmann_json) + PUBLIC opentelemetry_trace http_client_curl nlohmann_json::nlohmann_json) install( TARGETS opentelemetry_exporter_zipkin_trace diff --git a/ext/include/opentelemetry/ext/http/client/http_client_factory.h b/ext/include/opentelemetry/ext/http/client/http_client_factory.h index 2fbf0735d2..f03c1a0b64 100644 --- a/ext/include/opentelemetry/ext/http/client/http_client_factory.h +++ b/ext/include/opentelemetry/ext/http/client/http_client_factory.h @@ -11,21 +11,16 @@ namespace http { namespace client { - -enum class ClientType -{ - Curl, - Nosend -}; - class HttpClientFactory { public: static std::shared_ptr CreateSync(); - static std::shared_ptr Create(const ClientType client_type = ClientType::Curl); + static std::shared_ptr Create(); + + static std::shared_ptr CreateNoSend(); }; } // namespace client } // namespace http } // namespace ext -OPENTELEMETRY_END_NAMESPACE \ No newline at end of file +OPENTELEMETRY_END_NAMESPACE diff --git a/ext/src/http/client/curl/BUILD b/ext/src/http/client/curl/BUILD index 1e5f34f156..33ab814b91 100644 --- a/ext/src/http/client/curl/BUILD +++ b/ext/src/http/client/curl/BUILD @@ -4,6 +4,7 @@ cc_library( name = "http_client_curl", srcs = [ "http_client_curl.cc", + "http_client_factory_curl.cc", ], copts = [ "-DWITH_CURL", diff --git a/ext/src/http/client/curl/CMakeLists.txt b/ext/src/http/client/curl/CMakeLists.txt index 6b65d10ea6..64486b96db 100644 --- a/ext/src/http/client/curl/CMakeLists.txt +++ b/ext/src/http/client/curl/CMakeLists.txt @@ -1,6 +1,6 @@ find_package(CURL) if(CURL_FOUND) - add_library(http_client_curl http_client_curl.cc) + add_library(http_client_curl http_client_factory_curl.cc http_client_curl.cc) set_target_properties(http_client_curl PROPERTIES EXPORT_NAME http_client_curl) diff --git a/ext/src/http/client/http_client_factory.cc b/ext/src/http/client/curl/http_client_factory_curl.cc similarity index 67% rename from ext/src/http/client/http_client_factory.cc rename to ext/src/http/client/curl/http_client_factory_curl.cc index 39c881603b..f6266c2931 100644 --- a/ext/src/http/client/http_client_factory.cc +++ b/ext/src/http/client/curl/http_client_factory_curl.cc @@ -1,27 +1,18 @@ // Copyright The OpenTelemetry Authors // SPDX-License-Identifier: Apache-2.0 -#include "opentelemetry/ext/http/client/http_client_factory.h" #include "opentelemetry/ext/http/client/curl/http_client_curl.h" #include "opentelemetry/ext/http/client/http_client.h" -#ifdef ENABLE_TEST -# include "opentelemetry/ext/http/client/nosend/http_client_nosend.h" -#endif +#include "opentelemetry/ext/http/client/http_client_factory.h" + namespace http_client = opentelemetry::ext::http::client; -std::shared_ptr http_client::HttpClientFactory::Create( - const ClientType client_type) +std::shared_ptr http_client::HttpClientFactory::Create() { -#ifdef ENABLE_TEST - if (client_type == ClientType::Nosend) - { - return std::make_shared(); - } -#endif return std::make_shared(); } std::shared_ptr http_client::HttpClientFactory::CreateSync() { return std::make_shared(); -} \ No newline at end of file +} diff --git a/ext/src/http/client/nosend/http_client_factory_nosend.cc b/ext/src/http/client/nosend/http_client_factory_nosend.cc new file mode 100644 index 0000000000..841dd2d8eb --- /dev/null +++ b/ext/src/http/client/nosend/http_client_factory_nosend.cc @@ -0,0 +1,13 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#include "opentelemetry/ext/http/client/http_client.h" +#include "opentelemetry/ext/http/client/http_client_factory.h" +#include "opentelemetry/ext/http/client/nosend/http_client_nosend.h" + +namespace http_client = opentelemetry::ext::http::client; + +std::shared_ptr http_client::HttpClientFactory::CreateNoSend() +{ + return std::make_shared(); +} diff --git a/ext/test/http/BUILD b/ext/test/http/BUILD index 0192f86097..d1818ca0e3 100644 --- a/ext/test/http/BUILD +++ b/ext/test/http/BUILD @@ -6,7 +6,7 @@ cc_test( tags = ["test"], deps = [ "//ext:headers", - "//ext/src/http/client:http_client_factory", + "//ext/src/http/client/curl:http_client_curl", "//sdk/src/trace", "@com_google_googletest//:gtest_main", "@curl", diff --git a/ext/test/http/CMakeLists.txt b/ext/test/http/CMakeLists.txt index 933673e6b5..13b7c8206e 100644 --- a/ext/test/http/CMakeLists.txt +++ b/ext/test/http/CMakeLists.txt @@ -7,10 +7,10 @@ if(CURL_FOUND) ${CMAKE_THREAD_LIBS_INIT}) if(TARGET CURL::libcurl) - target_link_libraries(${FILENAME} CURL::libcurl http_client_factory) + target_link_libraries(${FILENAME} CURL::libcurl http_client_curl) else() include_directories(${CURL_INCLUDE_DIRS}) - target_link_libraries(${FILENAME} ${CURL_LIBRARIES} http_client_factory) + target_link_libraries(${FILENAME} ${CURL_LIBRARIES} http_client_curl) endif() gtest_add_tests( TARGET ${FILENAME} diff --git a/ext/test/w3c_tracecontext_test/BUILD b/ext/test/w3c_tracecontext_test/BUILD index 352cd0e7aa..e52c943fe3 100644 --- a/ext/test/w3c_tracecontext_test/BUILD +++ b/ext/test/w3c_tracecontext_test/BUILD @@ -15,7 +15,7 @@ cc_binary( "//api", "//exporters/ostream:ostream_span_exporter", "//ext:headers", - "//ext/src/http/client:http_client_factory", + "//ext/src/http/client/curl:http_client_curl", "//sdk/src/trace", "@curl", "@github_nlohmann_json//:json", diff --git a/ext/test/w3c_tracecontext_test/CMakeLists.txt b/ext/test/w3c_tracecontext_test/CMakeLists.txt index dd1f5a9963..30e2f5d0f3 100644 --- a/ext/test/w3c_tracecontext_test/CMakeLists.txt +++ b/ext/test/w3c_tracecontext_test/CMakeLists.txt @@ -7,7 +7,7 @@ else() add_executable(w3c_tracecontext_test main.cc) target_link_libraries( w3c_tracecontext_test - PRIVATE ${CMAKE_THREAD_LIBS_INIT} opentelemetry_trace http_client_factory + PRIVATE ${CMAKE_THREAD_LIBS_INIT} opentelemetry_trace http_client_curl opentelemetry_exporter_ostream_span ${CURL_LIBRARIES} nlohmann_json::nlohmann_json) if(nlohmann_json_clone) From 7b18c4cf2331e9359433dc0caab17647441b7a18 Mon Sep 17 00:00:00 2001 From: Oblivion Date: Wed, 16 Feb 2022 17:00:38 +0000 Subject: [PATCH 33/35] fix CI --- exporters/otlp/CMakeLists.txt | 2 +- .../otlp/test/otlp_http_exporter_test.cc | 2 +- .../otlp/test/otlp_http_log_exporter_test.cc | 2 +- ext/src/CMakeLists.txt | 6 +++- ext/src/http/client/BUILD | 16 ---------- ext/src/http/client/CMakeLists.txt | 29 ------------------- ext/src/http/client/nosend/BUILD | 1 + ext/src/http/client/nosend/CMakeLists.txt | 6 ++-- 8 files changed, 11 insertions(+), 53 deletions(-) delete mode 100644 ext/src/http/client/BUILD delete mode 100644 ext/src/http/client/CMakeLists.txt diff --git a/exporters/otlp/CMakeLists.txt b/exporters/otlp/CMakeLists.txt index c3bdc159b0..0332267922 100755 --- a/exporters/otlp/CMakeLists.txt +++ b/exporters/otlp/CMakeLists.txt @@ -170,7 +170,7 @@ if(BUILD_TESTING) add_executable(otlp_http_exporter_test test/otlp_http_exporter_test.cc) target_link_libraries( otlp_http_exporter_test ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} - ${GMOCK_LIB} opentelemetry_exporter_otlp_http http_client_factory) + ${GMOCK_LIB} opentelemetry_exporter_otlp_http http_client_nosend) gtest_add_tests( TARGET otlp_http_exporter_test TEST_PREFIX exporter.otlp. diff --git a/exporters/otlp/test/otlp_http_exporter_test.cc b/exporters/otlp/test/otlp_http_exporter_test.cc index 5cd8d90e9d..ef0b5a509e 100644 --- a/exporters/otlp/test/otlp_http_exporter_test.cc +++ b/exporters/otlp/test/otlp_http_exporter_test.cc @@ -78,7 +78,7 @@ class OtlpHttpExporterTestPeer : public ::testing::Test static std::pair> GetMockOtlpHttpClient(HttpRequestContentType content_type) { - auto http_client = http_client::HttpClientFactory::Create(http_client::ClientType::Nosend); + auto http_client = http_client::HttpClientFactory::CreateNoSend(); return {new OtlpHttpClient(MakeOtlpHttpClientOptions(content_type), http_client), http_client}; } }; diff --git a/exporters/otlp/test/otlp_http_log_exporter_test.cc b/exporters/otlp/test/otlp_http_log_exporter_test.cc index c32e3682a9..9be4af0983 100644 --- a/exporters/otlp/test/otlp_http_log_exporter_test.cc +++ b/exporters/otlp/test/otlp_http_log_exporter_test.cc @@ -79,7 +79,7 @@ class OtlpHttpLogExporterTestPeer : public ::testing::Test static std::pair> GetMockOtlpHttpClient(HttpRequestContentType content_type) { - auto http_client = http_client::HttpClientFactory::Create(http_client::ClientType::Nosend); + auto http_client = http_client::HttpClientFactory::CreateNoSend(); return {new OtlpHttpClient(MakeOtlpHttpClientOptions(content_type), http_client), http_client}; } }; diff --git a/ext/src/CMakeLists.txt b/ext/src/CMakeLists.txt index eb47794ca8..a976882ff0 100644 --- a/ext/src/CMakeLists.txt +++ b/ext/src/CMakeLists.txt @@ -1,4 +1,8 @@ if(WITH_ZPAGES) add_subdirectory(zpages) endif() -add_subdirectory(http/client) + +add_subdirectory(http/client/curl) +if(BUILD_TESTING) + add_subdirectory(http/client/nosend) +endif() diff --git a/ext/src/http/client/BUILD b/ext/src/http/client/BUILD deleted file mode 100644 index dfbdaf95e2..0000000000 --- a/ext/src/http/client/BUILD +++ /dev/null @@ -1,16 +0,0 @@ -package(default_visibility = ["//visibility:public"]) - -cc_library( - name = "http_client_factory", - srcs = [ - "http_client_factory.cc", - ], - include_prefix = "src/http/client", - deps = [ - "//api", - "//ext:headers", - "//ext/src/http/client/curl:http_client_curl", - "//ext/src/http/client/nosend:http_client_nosend", - "//sdk:headers", - ], -) diff --git a/ext/src/http/client/CMakeLists.txt b/ext/src/http/client/CMakeLists.txt deleted file mode 100644 index fd2ea356e2..0000000000 --- a/ext/src/http/client/CMakeLists.txt +++ /dev/null @@ -1,29 +0,0 @@ -add_subdirectory(curl) -add_subdirectory(nosend) - -find_package(CURL) -if(CURL_FOUND) - add_library(http_client_factory http_client_factory.cc) - set_target_properties(http_client_factory PROPERTIES EXPORT_NAME - http_client_factory) - set(http_client_factory_libs http_client_curl) - if(TARGET CURL::libcurl) - set(http_client_factory_libs ${http_client_factory_libs} CURL::libcurl) - else() - set(http_client_factory_libs ${http_client_factory_libs} ${CURL_LIBRARIES}) - target_include_directories(http_client_factory_libs - INTERFACE "${CURL_INCLUDE_DIRS}") - endif() - if(${BUILD_TESTING}) - set(http_client_factory_libs ${http_client_factory_libs} http_client_nosend) - endif() - - target_link_libraries(http_client_factory PUBLIC ${http_client_factory_libs}) - - install( - TARGETS http_client_factory - EXPORT "${PROJECT_NAME}-target" - RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} - LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} - ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}) -endif() diff --git a/ext/src/http/client/nosend/BUILD b/ext/src/http/client/nosend/BUILD index 5e3079c394..b27106a164 100644 --- a/ext/src/http/client/nosend/BUILD +++ b/ext/src/http/client/nosend/BUILD @@ -3,6 +3,7 @@ package(default_visibility = ["//visibility:public"]) cc_library( name = "http_client_nosend", srcs = [ + "http_client_factory_nosend.cc", "http_client_nosend.cc", ], include_prefix = "src/http/client/nosend", diff --git a/ext/src/http/client/nosend/CMakeLists.txt b/ext/src/http/client/nosend/CMakeLists.txt index b18f0740f7..9118abbfb5 100644 --- a/ext/src/http/client/nosend/CMakeLists.txt +++ b/ext/src/http/client/nosend/CMakeLists.txt @@ -1,8 +1,6 @@ if(${BUILD_TESTING}) - add_library( - http_client_nosend - # "${CMAKE_SOURCE_DIR}/ext/include/opentelemetry/ext/http/client/nosend/http_client_nosend.h" - http_client_nosend.cc) + add_library(http_client_nosend http_client_factory_nosend.cc + http_client_nosend.cc) set_target_properties(http_client_nosend PROPERTIES EXPORT_NAME http_client_nosend) From 6fd0201df8db5d26975da8143c5883fe67fef891 Mon Sep 17 00:00:00 2001 From: Ehsan Saei <71217171+esigo@users.noreply.github.com> Date: Sat, 19 Feb 2022 09:27:15 +0100 Subject: [PATCH 34/35] remove dup code --- .../opentelemetry/exporters/otlp/otlp_http_log_exporter.h | 3 --- 1 file changed, 3 deletions(-) diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_log_exporter.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_log_exporter.h index f2167539df..d330e62be4 100644 --- a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_log_exporter.h +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_log_exporter.h @@ -90,9 +90,6 @@ class OtlpHttpLogExporter final : public opentelemetry::sdk::logs::LogExporter bool Shutdown(std::chrono::microseconds timeout = std::chrono::microseconds(0)) noexcept override; private: - // For testing - friend class OtlpHttpLogExporterTestPeer; - // Configuration options for the exporter const OtlpHttpLogExporterOptions options_; From a643d1a5f75055575368e612a49f0ef4ad7e36b0 Mon Sep 17 00:00:00 2001 From: Ehsan Saei <71217171+esigo@users.noreply.github.com> Date: Sat, 19 Feb 2022 09:30:49 +0100 Subject: [PATCH 35/35] clean code dup --- .../include/opentelemetry/exporters/otlp/otlp_http_exporter.h | 3 --- 1 file changed, 3 deletions(-) diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_exporter.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_exporter.h index 9d85f1539c..3e6a521194 100644 --- a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_exporter.h +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_exporter.h @@ -91,9 +91,6 @@ class OtlpHttpExporter final : public opentelemetry::sdk::trace::SpanExporter bool Shutdown(std::chrono::microseconds timeout = std::chrono::microseconds(0)) noexcept override; private: - // For testing - friend class OtlpHttpExporterTestPeer; - // The configuration options associated with this exporter. const OtlpHttpExporterOptions options_;