From 945ce591ee0a3c787a37a952ba501be48de40b75 Mon Sep 17 00:00:00 2001 From: Oblivion Date: Sat, 26 Nov 2022 16:47:31 +0000 Subject: [PATCH 1/4] move client::nosend under test_common --- CMakeLists.txt | 3 + exporters/otlp/BUILD | 12 +- exporters/otlp/CMakeLists.txt | 4 +- .../otlp/test/otlp_http_exporter_test.cc | 2 +- .../otlp_http_log_record_exporter_test.cc | 2 +- .../test/otlp_http_metric_exporter_test.cc | 2 +- .../ext/http/client/http_client_factory.h | 2 + ext/src/CMakeLists.txt | 3 - test_common/BUILD | 7 + test_common/CMakeLists.txt | 13 ++ .../http/client/nosend/http_client_nosend.h | 179 ++++++++++++++++++ test_common/src/CMakeLists.txt | 3 + test_common/src/http/client/nosend/BUILD | 20 ++ .../src/http/client/nosend/CMakeLists.txt | 31 +++ .../nosend/http_client_factory_nosend.cc | 13 ++ .../http/client/nosend/http_client_nosend.cc | 99 ++++++++++ 16 files changed, 381 insertions(+), 14 deletions(-) create mode 100644 test_common/BUILD create mode 100644 test_common/CMakeLists.txt create mode 100644 test_common/include/opentelemetry/test_common/ext/http/client/nosend/http_client_nosend.h create mode 100644 test_common/src/CMakeLists.txt create mode 100644 test_common/src/http/client/nosend/BUILD create mode 100644 test_common/src/http/client/nosend/CMakeLists.txt create mode 100644 test_common/src/http/client/nosend/http_client_factory_nosend.cc create mode 100644 test_common/src/http/client/nosend/http_client_nosend.cc diff --git a/CMakeLists.txt b/CMakeLists.txt index 9b9710d9bc..f1a6d1cd17 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -501,6 +501,9 @@ if(NOT WITH_API_ONLY) add_subdirectory(sdk) add_subdirectory(ext) add_subdirectory(exporters) + if(BUILD_TESTING) + add_subdirectory(test_common) + endif() if(WITH_EXAMPLES) add_subdirectory(examples) endif() diff --git a/exporters/otlp/BUILD b/exporters/otlp/BUILD index 2aab339e8d..8bb1ae2ff6 100644 --- a/exporters/otlp/BUILD +++ b/exporters/otlp/BUILD @@ -370,7 +370,7 @@ cc_test( deps = [ ":otlp_http_exporter", "//api", - "//ext/src/http/client/nosend:http_client_nosend", + "//test_common/src/http/client/nosend:http_client_nosend", "@com_google_googletest//:gtest_main", ], ) @@ -386,7 +386,7 @@ cc_test( deps = [ ":otlp_http_exporter", "//api", - "//ext/src/http/client/nosend:http_client_nosend", + "//test_common/src/http/client/nosend:http_client_nosend", "@com_google_googletest//:gtest_main", ], ) @@ -402,7 +402,7 @@ cc_test( deps = [ ":otlp_http_log_record_exporter", "//api", - "//ext/src/http/client/nosend:http_client_nosend", + "//test_common/src/http/client/nosend:http_client_nosend", "@com_google_googletest//:gtest_main", ], ) @@ -418,7 +418,7 @@ cc_test( deps = [ ":otlp_http_log_record_exporter", "//api", - "//ext/src/http/client/nosend:http_client_nosend", + "//test_common/src/http/client/nosend:http_client_nosend", "@com_google_googletest//:gtest_main", ], ) @@ -481,7 +481,7 @@ cc_test( deps = [ ":otlp_http_metric_exporter", "//api", - "//ext/src/http/client/nosend:http_client_nosend", + "//test_common/src/http/client/nosend:http_client_nosend", "@com_google_googletest//:gtest_main", ], ) @@ -497,7 +497,7 @@ cc_test( deps = [ ":otlp_http_metric_exporter", "//api", - "//ext/src/http/client/nosend:http_client_nosend", + "//test_common/src/http/client/nosend:http_client_nosend", "@com_google_googletest//:gtest_main", ], ) diff --git a/exporters/otlp/CMakeLists.txt b/exporters/otlp/CMakeLists.txt index 1ab0c7cb33..b00e00d462 100644 --- a/exporters/otlp/CMakeLists.txt +++ b/exporters/otlp/CMakeLists.txt @@ -306,7 +306,7 @@ if(BUILD_TESTING) ${GMOCK_LIB} opentelemetry_exporter_otlp_http_log opentelemetry_logs - http_client_nosend) + opentelemetry_http_client_nosend) gtest_add_tests( TARGET otlp_http_log_record_exporter_test TEST_PREFIX exporter.otlp. @@ -333,7 +333,7 @@ if(BUILD_TESTING) ${GMOCK_LIB} opentelemetry_exporter_otlp_http_metric opentelemetry_metrics - http_client_nosend) + opentelemetry_http_client_nosend) gtest_add_tests( TARGET otlp_http_metric_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 fe2dd37e07..d7d53d5839 100644 --- a/exporters/otlp/test/otlp_http_exporter_test.cc +++ b/exporters/otlp/test/otlp_http_exporter_test.cc @@ -15,10 +15,10 @@ # include "opentelemetry/exporters/otlp/protobuf_include_suffix.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" +# include "opentelemetry/test_common/ext/http/client/nosend/http_client_nosend.h" # include "opentelemetry/trace/provider.h" # include diff --git a/exporters/otlp/test/otlp_http_log_record_exporter_test.cc b/exporters/otlp/test/otlp_http_log_record_exporter_test.cc index e1ab896083..6dc8ef8960 100644 --- a/exporters/otlp/test/otlp_http_log_record_exporter_test.cc +++ b/exporters/otlp/test/otlp_http_log_record_exporter_test.cc @@ -17,7 +17,6 @@ # include "opentelemetry/common/key_value_iterable_view.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_record_processor.h" @@ -25,6 +24,7 @@ # include "opentelemetry/sdk/logs/log_record.h" # include "opentelemetry/sdk/logs/logger_provider.h" # include "opentelemetry/sdk/resource/resource.h" +# include "opentelemetry/test_common/ext/http/client/nosend/http_client_nosend.h" # include # include diff --git a/exporters/otlp/test/otlp_http_metric_exporter_test.cc b/exporters/otlp/test/otlp_http_metric_exporter_test.cc index 7d4f2a1ebd..5dbb4cc2a7 100644 --- a/exporters/otlp/test/otlp_http_metric_exporter_test.cc +++ b/exporters/otlp/test/otlp_http_metric_exporter_test.cc @@ -15,13 +15,13 @@ #include "opentelemetry/common/key_value_iterable_view.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/metrics/aggregation/default_aggregation.h" #include "opentelemetry/sdk/metrics/aggregation/histogram_aggregation.h" #include "opentelemetry/sdk/metrics/data/metric_data.h" #include "opentelemetry/sdk/metrics/instruments.h" #include "opentelemetry/sdk/resource/resource.h" +#include "opentelemetry/test_common/ext/http/client/nosend/http_client_nosend.h" #include #include 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 f03c1a0b64..8df1e578d2 100644 --- a/ext/include/opentelemetry/ext/http/client/http_client_factory.h +++ b/ext/include/opentelemetry/ext/http/client/http_client_factory.h @@ -18,7 +18,9 @@ class HttpClientFactory static std::shared_ptr Create(); +#ifdef ENABLE_TEST static std::shared_ptr CreateNoSend(); +#endif }; } // namespace client } // namespace http diff --git a/ext/src/CMakeLists.txt b/ext/src/CMakeLists.txt index a976882ff0..6d7a14be41 100644 --- a/ext/src/CMakeLists.txt +++ b/ext/src/CMakeLists.txt @@ -3,6 +3,3 @@ if(WITH_ZPAGES) endif() add_subdirectory(http/client/curl) -if(BUILD_TESTING) - add_subdirectory(http/client/nosend) -endif() diff --git a/test_common/BUILD b/test_common/BUILD new file mode 100644 index 0000000000..cc62431b53 --- /dev/null +++ b/test_common/BUILD @@ -0,0 +1,7 @@ +package(default_visibility = ["//visibility:public"]) + +cc_library( + name = "headers", + hdrs = glob(["include/**/*.h"]), + strip_include_prefix = "include", +) diff --git a/test_common/CMakeLists.txt b/test_common/CMakeLists.txt new file mode 100644 index 0000000000..0ef4b9dab0 --- /dev/null +++ b/test_common/CMakeLists.txt @@ -0,0 +1,13 @@ +if(BUILD_TESTING) + add_library(opentelemetry_test_common INTERFACE) + target_include_directories( + opentelemetry_test_common + INTERFACE "$" + "$") + + set_target_properties(opentelemetry_test_common PROPERTIES EXPORT_NAME + "teset_common") + target_link_libraries(opentelemetry_test_common INTERFACE opentelemetry_api) + + add_subdirectory(src) +endif() diff --git a/test_common/include/opentelemetry/test_common/ext/http/client/nosend/http_client_nosend.h b/test_common/include/opentelemetry/test_common/ext/http/client/nosend/http_client_nosend.h new file mode 100644 index 0000000000..4325885a96 --- /dev/null +++ b/test_common/include/opentelemetry/test_common/ext/http/client/nosend/http_client_nosend.h @@ -0,0 +1,179 @@ +// 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; + + 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) {} + + const opentelemetry::ext::http::client::Body &GetBody() const noexcept override { return body_; } + + bool ForEachHeader(nostd::function_ref + callable) const noexcept override; + + bool ForEachHeader(const nostd::string_view &name, + nostd::function_ref + callable) const noexcept override; + + opentelemetry::ext::http::client::StatusCode GetStatusCode() const noexcept override + { + return status_code_; + } + + void Finish(opentelemetry::ext::http::client::EventHandler &callback) noexcept + { + callback.OnEvent(opentelemetry::ext::http::client::SessionState::Created, ""); + callback.OnEvent(opentelemetry::ext::http::client::SessionState::Connecting, ""); + callback.OnEvent(opentelemetry::ext::http::client::SessionState::Connected, ""); + callback.OnEvent(opentelemetry::ext::http::client::SessionState::Sending, ""); + + // let the otlp_http_client to continue + callback.OnResponse(*this); + + callback.OnEvent(opentelemetry::ext::http::client::SessionState::Response, ""); + } + +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, + (std::shared_ptr), + (noexcept, override)); + + bool CancelSession() noexcept override; + + bool FinishSession() noexcept override; + + 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(); + + std::shared_ptr CreateSession( + nostd::string_view) noexcept override; + + bool CancelAllSessions() noexcept override; + + bool FinishAllSessions() noexcept override; + + void SetMaxSessionsPerConnection(std::size_t max_requests_per_connection) noexcept override; + + 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/test_common/src/CMakeLists.txt b/test_common/src/CMakeLists.txt new file mode 100644 index 0000000000..4b1ee36afa --- /dev/null +++ b/test_common/src/CMakeLists.txt @@ -0,0 +1,3 @@ +if(BUILD_TESTING) + add_subdirectory(http/client/nosend) +endif() diff --git a/test_common/src/http/client/nosend/BUILD b/test_common/src/http/client/nosend/BUILD new file mode 100644 index 0000000000..6053c967f4 --- /dev/null +++ b/test_common/src/http/client/nosend/BUILD @@ -0,0 +1,20 @@ +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", + tags = [ + "test", + ], + deps = [ + "//api", + "//ext:headers", + "//sdk:headers", + "//test_common:headers", + "@com_google_googletest//:gtest_main", + ], +) diff --git a/test_common/src/http/client/nosend/CMakeLists.txt b/test_common/src/http/client/nosend/CMakeLists.txt new file mode 100644 index 0000000000..648944abfc --- /dev/null +++ b/test_common/src/http/client/nosend/CMakeLists.txt @@ -0,0 +1,31 @@ +if(${BUILD_TESTING}) + add_library(opentelemetry_http_client_nosend http_client_factory_nosend.cc + http_client_nosend.cc) + + set_target_properties(opentelemetry_http_client_nosend + PROPERTIES EXPORT_NAME opentelemetry_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( + opentelemetry_http_client_nosend ${GTEST_BOTH_LIBRARIES} ${GMOCK_LIB} + opentelemetry_ext opentelemetry_test_common) + +endif() diff --git a/test_common/src/http/client/nosend/http_client_factory_nosend.cc b/test_common/src/http/client/nosend/http_client_factory_nosend.cc new file mode 100644 index 0000000000..d30e2b0d7b --- /dev/null +++ b/test_common/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/test_common/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/test_common/src/http/client/nosend/http_client_nosend.cc b/test_common/src/http/client/nosend/http_client_nosend.cc new file mode 100644 index 0000000000..dd14e4404a --- /dev/null +++ b/test_common/src/http/client/nosend/http_client_nosend.cc @@ -0,0 +1,99 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#ifdef ENABLE_TEST +# include "opentelemetry/test_common/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; +} + +HttpClient::HttpClient() +{ + session_ = std::shared_ptr{new Session(*this)}; +} + +std::shared_ptr HttpClient::CreateSession( + nostd::string_view) noexcept +{ + return session_; +} + +bool HttpClient::CancelAllSessions() noexcept +{ + session_->CancelSession(); + return true; +} + +bool HttpClient::FinishAllSessions() noexcept +{ + session_->FinishSession(); + return true; +} + +void HttpClient::SetMaxSessionsPerConnection(std::size_t /* max_requests_per_connection */) noexcept +{} + +void HttpClient::CleanupSession(uint64_t /* session_id */) {} + +} // namespace nosend +} // namespace client +} // namespace http +} // namespace ext +OPENTELEMETRY_END_NAMESPACE +#endif From adfbadd0cb75d79d5a9a96f21a33aa820fdc3039 Mon Sep 17 00:00:00 2001 From: Oblivion Date: Sat, 26 Nov 2022 16:53:09 +0000 Subject: [PATCH 2/4] clean --- .../http/client/nosend/http_client_nosend.h | 179 ------------------ ext/src/http/client/nosend/BUILD | 19 -- ext/src/http/client/nosend/CMakeLists.txt | 36 ---- .../nosend/http_client_factory_nosend.cc | 13 -- .../http/client/nosend/http_client_nosend.cc | 99 ---------- 5 files changed, 346 deletions(-) delete mode 100644 ext/include/opentelemetry/ext/http/client/nosend/http_client_nosend.h delete mode 100644 ext/src/http/client/nosend/BUILD delete mode 100644 ext/src/http/client/nosend/CMakeLists.txt delete mode 100644 ext/src/http/client/nosend/http_client_factory_nosend.cc delete mode 100644 ext/src/http/client/nosend/http_client_nosend.cc 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 deleted file mode 100644 index 4325885a96..0000000000 --- a/ext/include/opentelemetry/ext/http/client/nosend/http_client_nosend.h +++ /dev/null @@ -1,179 +0,0 @@ -// 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; - - 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) {} - - const opentelemetry::ext::http::client::Body &GetBody() const noexcept override { return body_; } - - bool ForEachHeader(nostd::function_ref - callable) const noexcept override; - - bool ForEachHeader(const nostd::string_view &name, - nostd::function_ref - callable) const noexcept override; - - opentelemetry::ext::http::client::StatusCode GetStatusCode() const noexcept override - { - return status_code_; - } - - void Finish(opentelemetry::ext::http::client::EventHandler &callback) noexcept - { - callback.OnEvent(opentelemetry::ext::http::client::SessionState::Created, ""); - callback.OnEvent(opentelemetry::ext::http::client::SessionState::Connecting, ""); - callback.OnEvent(opentelemetry::ext::http::client::SessionState::Connected, ""); - callback.OnEvent(opentelemetry::ext::http::client::SessionState::Sending, ""); - - // let the otlp_http_client to continue - callback.OnResponse(*this); - - callback.OnEvent(opentelemetry::ext::http::client::SessionState::Response, ""); - } - -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, - (std::shared_ptr), - (noexcept, override)); - - bool CancelSession() noexcept override; - - bool FinishSession() noexcept override; - - 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(); - - std::shared_ptr CreateSession( - nostd::string_view) noexcept override; - - bool CancelAllSessions() noexcept override; - - bool FinishAllSessions() noexcept override; - - void SetMaxSessionsPerConnection(std::size_t max_requests_per_connection) noexcept override; - - 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/nosend/BUILD b/ext/src/http/client/nosend/BUILD deleted file mode 100644 index b27106a164..0000000000 --- a/ext/src/http/client/nosend/BUILD +++ /dev/null @@ -1,19 +0,0 @@ -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", - tags = [ - "test", - ], - deps = [ - "//api", - "//ext:headers", - "//sdk:headers", - "@com_google_googletest//:gtest_main", - ], -) diff --git a/ext/src/http/client/nosend/CMakeLists.txt b/ext/src/http/client/nosend/CMakeLists.txt deleted file mode 100644 index 497daeb342..0000000000 --- a/ext/src/http/client/nosend/CMakeLists.txt +++ /dev/null @@ -1,36 +0,0 @@ -if(${BUILD_TESTING}) - 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) - - 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 ${GTEST_BOTH_LIBRARIES} ${GMOCK_LIB} - 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() diff --git a/ext/src/http/client/nosend/http_client_factory_nosend.cc b/ext/src/http/client/nosend/http_client_factory_nosend.cc deleted file mode 100644 index 841dd2d8eb..0000000000 --- a/ext/src/http/client/nosend/http_client_factory_nosend.cc +++ /dev/null @@ -1,13 +0,0 @@ -// 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/src/http/client/nosend/http_client_nosend.cc b/ext/src/http/client/nosend/http_client_nosend.cc deleted file mode 100644 index 1c085e3fd4..0000000000 --- a/ext/src/http/client/nosend/http_client_nosend.cc +++ /dev/null @@ -1,99 +0,0 @@ -// 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; -} - -HttpClient::HttpClient() -{ - session_ = std::shared_ptr{new Session(*this)}; -} - -std::shared_ptr HttpClient::CreateSession( - nostd::string_view) noexcept -{ - return session_; -} - -bool HttpClient::CancelAllSessions() noexcept -{ - session_->CancelSession(); - return true; -} - -bool HttpClient::FinishAllSessions() noexcept -{ - session_->FinishSession(); - return true; -} - -void HttpClient::SetMaxSessionsPerConnection(std::size_t /* max_requests_per_connection */) noexcept -{} - -void HttpClient::CleanupSession(uint64_t /* session_id */) {} - -} // namespace nosend -} // namespace client -} // namespace http -} // namespace ext -OPENTELEMETRY_END_NAMESPACE -#endif From 66e5799272d1f1197937ec60ea3ea1827d41f5c6 Mon Sep 17 00:00:00 2001 From: Ehsan Saei <71217171+esigo@users.noreply.github.com> Date: Sat, 26 Nov 2022 17:55:08 +0100 Subject: [PATCH 3/4] codecov ignore --- .github/.codecov.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/.codecov.yaml b/.github/.codecov.yaml index c4bd839179..1b9921ac4c 100644 --- a/.github/.codecov.yaml +++ b/.github/.codecov.yaml @@ -42,6 +42,7 @@ ignore: - "cmake/**/*" - "buildscripts/**/*" - "third_party/**/*" + - "test_common/**/*" - "tools/**/*" - ".vscode/**/*" - ".github/**/*" From f83ac0e3f12aed0e090ae3504ecd534b8b1167ff Mon Sep 17 00:00:00 2001 From: Oblivion Date: Sat, 26 Nov 2022 18:14:31 +0000 Subject: [PATCH 4/4] fix cmake --- exporters/otlp/CMakeLists.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/exporters/otlp/CMakeLists.txt b/exporters/otlp/CMakeLists.txt index b00e00d462..a894af7374 100644 --- a/exporters/otlp/CMakeLists.txt +++ b/exporters/otlp/CMakeLists.txt @@ -280,7 +280,8 @@ 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_nosend) + ${GMOCK_LIB} opentelemetry_exporter_otlp_http + opentelemetry_http_client_nosend) gtest_add_tests( TARGET otlp_http_exporter_test TEST_PREFIX exporter.otlp.