diff --git a/CMakeLists.txt b/CMakeLists.txt index edf5036117..16ccd110cf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -274,15 +274,13 @@ include(GNUInstallDirs) # Do we need HTTP CLIENT CURL ? # -if(WITH_OTLP_HTTP - OR WITH_ELASTICSEARCH - OR WITH_ZIPKIN - OR BUILD_W3CTRACECONTEXT_TEST - OR WITH_EXAMPLES_HTTP) - set(WITH_HTTP_CLIENT_CURL ON) -else() - set(WITH_HTTP_CLIENT_CURL OFF) -endif() +include(CMakeDependentOption) +cmake_dependent_option( + WITH_HTTP_CLIENT_CURL + "Use the curl HTTP client backend. Defaults to ON when any HTTP exporter is enabled; set OFF to supply a custom transport." + ON + "WITH_OTLP_HTTP OR WITH_ELASTICSEARCH OR WITH_ZIPKIN OR BUILD_W3CTRACECONTEXT_TEST OR WITH_EXAMPLES_HTTP" + OFF) # # Do we need ZLIB ? diff --git a/ci/do_ci.sh b/ci/do_ci.sh index f1bb6d78e3..c6b85c988b 100755 --- a/ci/do_ci.sh +++ b/ci/do_ci.sh @@ -489,6 +489,7 @@ elif [[ "$1" == "cmake.install.test" ]]; then "sdk" "configuration" "ext_common" + "ext_http" "ext_http_curl" "exporters_in_memory" "exporters_ostream" diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 6052d2f04d..604c498913 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -37,3 +37,7 @@ endif() if(WITH_CONFIGURATION) add_subdirectory(configuration) endif() + +if(NOT WITH_HTTP_CLIENT_CURL AND WITH_OTLP_HTTP) + add_subdirectory(custom_http_client) +endif() diff --git a/examples/custom_http_client/CMakeLists.txt b/examples/custom_http_client/CMakeLists.txt new file mode 100644 index 0000000000..a727198fc4 --- /dev/null +++ b/examples/custom_http_client/CMakeLists.txt @@ -0,0 +1,11 @@ +# Copyright The OpenTelemetry Authors +# SPDX-License-Identifier: Apache-2.0 + +add_library(custom_http_client_stub STATIC custom_http_client.cc) +target_link_libraries(custom_http_client_stub PUBLIC opentelemetry_ext + opentelemetry_sdk) + +add_executable(example_custom_http_client main.cc) +target_link_libraries( + example_custom_http_client PRIVATE custom_http_client_stub + opentelemetry-cpp::otlp_http_exporter) diff --git a/examples/custom_http_client/custom_http_client.cc b/examples/custom_http_client/custom_http_client.cc new file mode 100644 index 0000000000..f35edbd732 --- /dev/null +++ b/examples/custom_http_client/custom_http_client.cc @@ -0,0 +1,149 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#include +#include + +#include "opentelemetry/ext/http/client/http_client.h" +#include "opentelemetry/ext/http/client/http_client_factory.h" +#include "opentelemetry/sdk/common/thread_instrumentation.h" + +#include "custom_http_client.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace ext +{ +namespace http +{ +namespace client +{ + +namespace +{ + +class LoggingRequest : public Request +{ +public: + void SetMethod(Method method) noexcept override + { + switch (method) + { + case Method::Get: + method_ = "GET"; + break; + case Method::Post: + method_ = "POST"; + break; + case Method::Put: + method_ = "PUT"; + break; + default: + method_ = "OTHER"; + break; + } + } + + void SetUri(nostd::string_view uri) noexcept override { uri_ = std::string(uri); } + + void SetBody(Body &body) noexcept override { body_size_ = body.size(); } + + void SetSslOptions(const HttpSslOptions &) noexcept override {} + void AddHeader(nostd::string_view, nostd::string_view) noexcept override {} + void ReplaceHeader(nostd::string_view, nostd::string_view) noexcept override {} + void SetTimeoutMs(std::chrono::milliseconds) noexcept override {} + void SetCompression(const Compression &) noexcept override {} + void EnableLogging(bool) noexcept override {} + void SetRetryPolicy(const RetryPolicy &) noexcept override {} + + const std::string &GetMethod() const { return method_; } + const std::string &GetUri() const { return uri_; } + std::size_t GetBodySize() const { return body_size_; } + +private: + std::string method_; + std::string uri_; + std::size_t body_size_ = 0; +}; + +class OkResponse : public NoopResponse +{ +public: + StatusCode GetStatusCode() const noexcept override { return 200; } +}; + +class LoggingSession : public Session +{ +public: + std::shared_ptr CreateRequest() noexcept override + { + request_ = std::make_shared(); + return request_; + } + + void SendRequest(std::shared_ptr handler) noexcept override + { + if (request_) + { + std::cout << "Custom HTTP client: " << request_->GetMethod() << " " << request_->GetUri() + << " (" << request_->GetBodySize() << " bytes)\n"; + } + if (!handler) + { + return; + } + OkResponse response; + handler->OnResponse(response); + handler->OnEvent(SessionState::Response, {}); + std::cout << "Custom HTTP client: export acknowledged\n"; + } + + bool IsSessionActive() noexcept override { return false; } + bool CancelSession() noexcept override { return true; } + bool FinishSession() noexcept override { return true; } + +private: + std::shared_ptr request_; +}; + +class LoggingHttpClient : public HttpClient +{ +public: + std::shared_ptr CreateSession(nostd::string_view url) noexcept override + { + std::cout << "Custom HTTP client: creating session for " << std::string(url) << "\n"; + return std::make_shared(); + } + + bool CancelAllSessions() noexcept override { return true; } + bool FinishAllSessions() noexcept override { return true; } + void SetMaxSessionsPerConnection(std::size_t) noexcept override {} +}; + +} // namespace + +std::shared_ptr LoggingHttpClientFactory::Create() +{ + return std::make_shared(); +} + +std::shared_ptr LoggingHttpClientFactory::Create( + const std::shared_ptr &) +{ + return std::make_shared(); +} + +std::shared_ptr LoggingHttpClientFactory::CreateSync() +{ + return nullptr; +} + +std::shared_ptr GetDefaultHttpClientFactory() +{ + static auto instance = std::make_shared(); + return instance; +} + +} // namespace client +} // namespace http +} // namespace ext +OPENTELEMETRY_END_NAMESPACE diff --git a/examples/custom_http_client/custom_http_client.h b/examples/custom_http_client/custom_http_client.h new file mode 100644 index 0000000000..bbcb0d488f --- /dev/null +++ b/examples/custom_http_client/custom_http_client.h @@ -0,0 +1,31 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once + +#include + +#include "opentelemetry/ext/http/client/http_client_factory.h" +#include "opentelemetry/version.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace ext +{ +namespace http +{ +namespace client +{ + +class LoggingHttpClientFactory : public HttpClientFactory +{ +public: + std::shared_ptr CreateSync() override; + std::shared_ptr Create() override; + std::shared_ptr Create( + const std::shared_ptr &thread_instrumentation) override; +}; + +} // namespace client +} // namespace http +} // namespace ext +OPENTELEMETRY_END_NAMESPACE diff --git a/examples/custom_http_client/main.cc b/examples/custom_http_client/main.cc new file mode 100644 index 0000000000..bb8f8b106a --- /dev/null +++ b/examples/custom_http_client/main.cc @@ -0,0 +1,35 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#include + +#include "custom_http_client.h" +#include "opentelemetry/exporters/otlp/otlp_http_exporter_factory.h" +#include "opentelemetry/exporters/otlp/otlp_http_exporter_options.h" +#include "opentelemetry/sdk/trace/simple_processor_factory.h" +#include "opentelemetry/sdk/trace/tracer_provider_factory.h" + +namespace trace_sdk = opentelemetry::sdk::trace; +namespace otlp = opentelemetry::exporter::otlp; +namespace http_client = opentelemetry::ext::http::client; + +int main() +{ + otlp::OtlpHttpExporterOptions options; + options.url = "http://localhost:4318/v1/traces"; + + auto factory = std::make_shared(); + auto exporter = otlp::OtlpHttpExporterFactory::Create(options, factory); + auto processor = trace_sdk::SimpleSpanProcessorFactory::Create(std::move(exporter)); + + std::shared_ptr provider = + trace_sdk::TracerProviderFactory::Create(std::move(processor)); + + auto tracer = provider->GetTracer("custom-http-client-example"); + auto span = tracer->StartSpan("custom-span"); + span->SetAttribute("example.key", "value"); + span->End(); + + provider->ForceFlush(); + return 0; +} diff --git a/examples/http/CMakeLists.txt b/examples/http/CMakeLists.txt index 0aff680302..ffc286354c 100644 --- a/examples/http/CMakeLists.txt +++ b/examples/http/CMakeLists.txt @@ -1,15 +1,17 @@ # Copyright The OpenTelemetry Authors # SPDX-License-Identifier: Apache-2.0 -add_executable(http_client client.cc) -add_executable(http_server server.cc) +if(WITH_HTTP_CLIENT_CURL) + add_executable(http_client client.cc) + add_executable(http_server server.cc) -target_link_libraries( - http_client - PRIVATE opentelemetry-cpp::trace opentelemetry-cpp::http_client_curl - opentelemetry-cpp::ostream_span_exporter CURL::libcurl) + target_link_libraries( + http_client + PRIVATE opentelemetry-cpp::trace opentelemetry-cpp::http_client_curl + opentelemetry-cpp::ostream_span_exporter CURL::libcurl) -target_link_libraries( - http_server - PRIVATE opentelemetry-cpp::trace opentelemetry-cpp::http_client_curl - opentelemetry-cpp::ostream_span_exporter) + target_link_libraries( + http_server + PRIVATE opentelemetry-cpp::trace opentelemetry-cpp::http_client_curl + opentelemetry-cpp::ostream_span_exporter) +endif() diff --git a/examples/http/client.cc b/examples/http/client.cc index 5f7a504d81..4736adbadf 100644 --- a/examples/http/client.cc +++ b/examples/http/client.cc @@ -40,7 +40,7 @@ namespace semconv = opentelemetry::semconv; void sendRequest(const std::string &url) { - auto http_client = http_client::HttpClientFactory::CreateSync(); + auto http_client = http_client::GetDefaultHttpClientFactory()->CreateSync(); // start active span StartSpanOptions options; diff --git a/exporters/elasticsearch/CMakeLists.txt b/exporters/elasticsearch/CMakeLists.txt index 7341ec4cfc..cfd4ce01db 100644 --- a/exporters/elasticsearch/CMakeLists.txt +++ b/exporters/elasticsearch/CMakeLists.txt @@ -17,7 +17,7 @@ target_include_directories( target_link_libraries( opentelemetry_exporter_elasticsearch_logs - PUBLIC opentelemetry_trace opentelemetry_logs opentelemetry_http_client_curl + PUBLIC opentelemetry_trace opentelemetry_logs opentelemetry_http_client nlohmann_json::nlohmann_json) otel_add_component( diff --git a/exporters/elasticsearch/include/opentelemetry/exporters/elasticsearch/es_log_record_exporter.h b/exporters/elasticsearch/include/opentelemetry/exporters/elasticsearch/es_log_record_exporter.h index 2558fe083d..6234df59a2 100644 --- a/exporters/elasticsearch/include/opentelemetry/exporters/elasticsearch/es_log_record_exporter.h +++ b/exporters/elasticsearch/include/opentelemetry/exporters/elasticsearch/es_log_record_exporter.h @@ -9,6 +9,7 @@ #include #include "opentelemetry/ext/http/client/http_client.h" +#include "opentelemetry/ext/http/client/http_client_factory.h" #include "opentelemetry/nostd/shared_ptr.h" #include "opentelemetry/sdk/logs/exporter.h" #include "opentelemetry/version.h" @@ -87,6 +88,23 @@ class ElasticsearchLogRecordExporter final : public opentelemetry::sdk::logs::Lo */ ElasticsearchLogRecordExporter(const ElasticsearchExporterOptions &options); + /** + * Create an ElasticsearchLogRecordExporter with user specified options and HTTP client factory. + * @param options An object containing the user's configuration options. + * @param factory the HTTP client factory used to create the underlying HTTP client + */ + ElasticsearchLogRecordExporter( + const ElasticsearchExporterOptions &options, + const std::shared_ptr &factory); + + /** + * Create an ElasticsearchLogRecordExporter with user specified options and HTTP client. + * @param options An object containing the user's configuration options. + * @param http_client the HTTP client to be used for exporting + */ + ElasticsearchLogRecordExporter(const ElasticsearchExporterOptions &options, + std::shared_ptr http_client); + /** * Creates a recordable that stores the data in a JSON object * @return a newly initialized Recordable object. diff --git a/exporters/elasticsearch/src/es_log_record_exporter.cc b/exporters/elasticsearch/src/es_log_record_exporter.cc index fcfaa6252e..1d7dbf9c98 100644 --- a/exporters/elasticsearch/src/es_log_record_exporter.cc +++ b/exporters/elasticsearch/src/es_log_record_exporter.cc @@ -320,8 +320,20 @@ ElasticsearchLogRecordExporter::ElasticsearchLogRecordExporter() ElasticsearchLogRecordExporter::ElasticsearchLogRecordExporter( const ElasticsearchExporterOptions &options) + : ElasticsearchLogRecordExporter(options, ext::http::client::GetDefaultHttpClientFactory()) +{} + +ElasticsearchLogRecordExporter::ElasticsearchLogRecordExporter( + const ElasticsearchExporterOptions &options, + const std::shared_ptr &factory) + : ElasticsearchLogRecordExporter(options, factory->Create()) +{} + +ElasticsearchLogRecordExporter::ElasticsearchLogRecordExporter( + const ElasticsearchExporterOptions &options, + std::shared_ptr http_client) : options_{options}, - http_client_{ext::http::client::HttpClientFactory::Create()} + http_client_{std::move(http_client)} #ifdef ENABLE_ASYNC_EXPORT , synchronization_data_(new SynchronizationData()) diff --git a/exporters/elasticsearch/test/es_log_record_exporter_test.cc b/exporters/elasticsearch/test/es_log_record_exporter_test.cc index 99cedfbaeb..a65c0b4c1c 100644 --- a/exporters/elasticsearch/test/es_log_record_exporter_test.cc +++ b/exporters/elasticsearch/test/es_log_record_exporter_test.cc @@ -28,6 +28,14 @@ namespace logs_api = opentelemetry::logs; namespace nostd = opentelemetry::nostd; namespace logs_exporter = opentelemetry::exporter::logs; +TEST(ElasticsearchLogsExporterTests, CustomClientConstructionSucceeds) +{ + logs_exporter::ElasticsearchExporterOptions opts; + auto exporter = std::unique_ptr( + new logs_exporter::ElasticsearchLogRecordExporter(opts)); + ASSERT_NE(exporter, nullptr); +} + // Attempt to write a log to an invalid host/port, test that the Export() returns failure TEST(DISABLED_ElasticsearchLogsExporterTests, InvalidEndpoint) { diff --git a/exporters/otlp/CMakeLists.txt b/exporters/otlp/CMakeLists.txt index 2d53476be6..1d2852dc73 100644 --- a/exporters/otlp/CMakeLists.txt +++ b/exporters/otlp/CMakeLists.txt @@ -316,15 +316,15 @@ if(WITH_OTLP_HTTP) target_link_libraries( opentelemetry_exporter_otlp_http_client - PUBLIC opentelemetry_sdk - opentelemetry_ext - # Links flags of opentelemetry_http_client_curl should be public when - # building internal components + PUBLIC opentelemetry_sdk opentelemetry_ext "$" PRIVATE "$" - "$" - "$" - "$") + "$") + + target_link_libraries( + opentelemetry_exporter_otlp_http_client + PUBLIC opentelemetry_http_client + PRIVATE opentelemetry_common) target_include_directories( opentelemetry_exporter_otlp_http_client @@ -1062,6 +1062,39 @@ if(BUILD_TESTING) TARGET otlp_http_metric_exporter_factory_test TEST_PREFIX exporter.otlp. TEST_LIST otlp_http_metric_exporter_factory_test) + + add_executable(otlp_http_exporter_custom_client_test + test/otlp_http_exporter_custom_client_test.cc) + target_link_libraries( + otlp_http_exporter_custom_client_test ${GTEST_BOTH_LIBRARIES} + ${CMAKE_THREAD_LIBS_INIT} ${GMOCK_LIB} opentelemetry_exporter_otlp_http + opentelemetry_http_client_nosend) + gtest_add_tests( + TARGET otlp_http_exporter_custom_client_test + TEST_PREFIX exporter.otlp. + TEST_LIST otlp_http_exporter_custom_client_test) + + add_executable(otlp_http_metric_exporter_custom_client_test + test/otlp_http_metric_exporter_custom_client_test.cc) + target_link_libraries( + otlp_http_metric_exporter_custom_client_test ${GTEST_BOTH_LIBRARIES} + ${CMAKE_THREAD_LIBS_INIT} opentelemetry_exporter_otlp_http_metric + opentelemetry_metrics opentelemetry_http_client_nosend) + gtest_add_tests( + TARGET otlp_http_metric_exporter_custom_client_test + TEST_PREFIX exporter.otlp. + TEST_LIST otlp_http_metric_exporter_custom_client_test) + + add_executable(otlp_http_log_record_exporter_custom_client_test + test/otlp_http_log_record_exporter_custom_client_test.cc) + target_link_libraries( + otlp_http_log_record_exporter_custom_client_test ${GTEST_BOTH_LIBRARIES} + ${CMAKE_THREAD_LIBS_INIT} opentelemetry_exporter_otlp_http_log + opentelemetry_logs opentelemetry_http_client_nosend) + gtest_add_tests( + TARGET otlp_http_log_record_exporter_custom_client_test + TEST_PREFIX exporter.otlp. + TEST_LIST otlp_http_log_record_exporter_custom_client_test) endif() if(WITH_OTLP_FILE) 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 15197cf4d6..e273c5da57 100644 --- a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_client.h +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_client.h @@ -17,6 +17,7 @@ #include "opentelemetry/exporters/otlp/otlp_environment.h" #include "opentelemetry/exporters/otlp/otlp_http.h" #include "opentelemetry/ext/http/client/http_client.h" +#include "opentelemetry/ext/http/client/http_client_factory.h" #include "opentelemetry/nostd/string_view.h" #include "opentelemetry/nostd/variant.h" #include "opentelemetry/sdk/common/exporter_utils.h" @@ -155,9 +156,18 @@ class OtlpHttpClient public: /** * Create an OtlpHttpClient using the given options. + * Uses the default HTTP client factory (curl when available). */ explicit OtlpHttpClient(OtlpHttpClientOptions &&options); + /** + * Create an OtlpHttpClient using the given options and HTTP client factory. + * @param options the Otlp http client options to be used for exporting + * @param factory the HTTP client factory used to create the underlying HTTP client + */ + OtlpHttpClient(OtlpHttpClientOptions &&options, + const std::shared_ptr &factory); + /** * Create an OtlpHttpClient using the specified http client. * @param options the Otlp http client options to be used for exporting 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 454959dd45..ad1639a04b 100644 --- a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_exporter.h +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_exporter.h @@ -9,6 +9,7 @@ #include "opentelemetry/exporters/otlp/otlp_http_client.h" #include "opentelemetry/exporters/otlp/otlp_http_exporter_options.h" #include "opentelemetry/exporters/otlp/otlp_http_exporter_runtime_options.h" +#include "opentelemetry/ext/http/client/http_client_factory.h" #include "opentelemetry/nostd/span.h" #include "opentelemetry/sdk/common/exporter_utils.h" #include "opentelemetry/sdk/trace/exporter.h" @@ -43,6 +44,42 @@ class OPENTELEMETRY_EXPORT OtlpHttpExporter final : public opentelemetry::sdk::t OtlpHttpExporter(const OtlpHttpExporterOptions &options, const OtlpHttpExporterRuntimeOptions &runtime_options); + /** + * Create an OtlpHttpExporter using the given options and HTTP client factory. + * @param options the exporter options + * @param factory the HTTP client factory used to create the underlying HTTP client + */ + OtlpHttpExporter(const OtlpHttpExporterOptions &options, + const std::shared_ptr &factory); + + /** + * Create an OtlpHttpExporter using the given options, runtime options, and HTTP client factory. + * @param options the exporter options + * @param runtime_options the runtime options (e.g. thread instrumentation) + * @param factory the HTTP client factory used to create the underlying HTTP client + */ + OtlpHttpExporter(const OtlpHttpExporterOptions &options, + const OtlpHttpExporterRuntimeOptions &runtime_options, + const std::shared_ptr &factory); + + /** + * Create an OtlpHttpExporter using the given options and HTTP client. + * @param options the exporter options + * @param http_client the HTTP client to be used for exporting + */ + OtlpHttpExporter(const OtlpHttpExporterOptions &options, + std::shared_ptr http_client); + + /** + * Create an OtlpHttpExporter using the given options, runtime options, and HTTP client. + * @param options the exporter options + * @param runtime_options the runtime options (e.g. thread instrumentation) + * @param http_client the HTTP client to be used for exporting + */ + OtlpHttpExporter(const OtlpHttpExporterOptions &options, + const OtlpHttpExporterRuntimeOptions &runtime_options, + std::shared_ptr http_client); + /** * Create a span recordable. * @return a newly initialized Recordable object @@ -84,6 +121,7 @@ class OPENTELEMETRY_EXPORT OtlpHttpExporter final : public opentelemetry::sdk::t std::unique_ptr http_client_; // For testing friend class OtlpHttpExporterTestPeer; + friend class OtlpHttpExporterCustomClientTestPeer; /** * Create an OtlpHttpExporter using the specified http client. * Only tests can call this constructor directly. diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_exporter_factory.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_exporter_factory.h index bf42d26354..4484db127d 100644 --- a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_exporter_factory.h +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_exporter_factory.h @@ -7,6 +7,8 @@ #include "opentelemetry/exporters/otlp/otlp_http_exporter_options.h" #include "opentelemetry/exporters/otlp/otlp_http_exporter_runtime_options.h" +#include "opentelemetry/ext/http/client/http_client.h" +#include "opentelemetry/ext/http/client/http_client_factory.h" #include "opentelemetry/sdk/trace/exporter.h" #include "opentelemetry/version.h" @@ -39,6 +41,46 @@ class OPENTELEMETRY_EXPORT OtlpHttpExporterFactory static std::unique_ptr Create( const OtlpHttpExporterOptions &options, const OtlpHttpExporterRuntimeOptions &runtime_options); + + /** + * Create an OtlpHttpExporter using the given options and HTTP client factory. + * @param options the exporter options + * @param factory the HTTP client factory used to create the underlying HTTP client + */ + static std::unique_ptr Create( + const OtlpHttpExporterOptions &options, + const std::shared_ptr &factory); + + /** + * Create an OtlpHttpExporter using the given options, runtime options, and HTTP client factory. + * @param options the exporter options + * @param runtime_options the runtime options (e.g. thread instrumentation) + * @param factory the HTTP client factory used to create the underlying HTTP client + */ + static std::unique_ptr Create( + const OtlpHttpExporterOptions &options, + const OtlpHttpExporterRuntimeOptions &runtime_options, + const std::shared_ptr &factory); + + /** + * Create an OtlpHttpExporter using the given options and HTTP client. + * @param options the exporter options + * @param http_client the HTTP client to be used for exporting + */ + static std::unique_ptr Create( + const OtlpHttpExporterOptions &options, + std::shared_ptr http_client); + + /** + * Create an OtlpHttpExporter using the given options, runtime options, and HTTP client. + * @param options the exporter options + * @param runtime_options the runtime options (e.g. thread instrumentation) + * @param http_client the HTTP client to be used for exporting + */ + static std::unique_ptr Create( + const OtlpHttpExporterOptions &options, + const OtlpHttpExporterRuntimeOptions &runtime_options, + std::shared_ptr http_client); }; } // namespace otlp diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_log_record_exporter.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_log_record_exporter.h index aba430522e..4259c28998 100644 --- a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_log_record_exporter.h +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_log_record_exporter.h @@ -9,6 +9,7 @@ #include "opentelemetry/exporters/otlp/otlp_http_client.h" #include "opentelemetry/exporters/otlp/otlp_http_log_record_exporter_options.h" #include "opentelemetry/exporters/otlp/otlp_http_log_record_exporter_runtime_options.h" +#include "opentelemetry/ext/http/client/http_client_factory.h" #include "opentelemetry/nostd/span.h" #include "opentelemetry/sdk/common/exporter_utils.h" #include "opentelemetry/sdk/logs/exporter.h" @@ -46,6 +47,44 @@ class OtlpHttpLogRecordExporter final : public opentelemetry::sdk::logs::LogReco OtlpHttpLogRecordExporter(const OtlpHttpLogRecordExporterOptions &options, const OtlpHttpLogRecordExporterRuntimeOptions &runtime_options); + /** + * Create an OtlpHttpLogRecordExporter with user specified options and HTTP client factory. + * @param options An object containing the user's configuration options. + * @param factory the HTTP client factory used to create the underlying HTTP client + */ + OtlpHttpLogRecordExporter(const OtlpHttpLogRecordExporterOptions &options, + const std::shared_ptr &factory); + + /** + * Create an OtlpHttpLogRecordExporter with user specified options, runtime options, and HTTP + * client factory. + * @param options An object containing the user's configuration options. + * @param runtime_options An object containing the user's runtime options. + * @param factory the HTTP client factory used to create the underlying HTTP client + */ + OtlpHttpLogRecordExporter(const OtlpHttpLogRecordExporterOptions &options, + const OtlpHttpLogRecordExporterRuntimeOptions &runtime_options, + const std::shared_ptr &factory); + + /** + * Create an OtlpHttpLogRecordExporter with user specified options and HTTP client. + * @param options An object containing the user's configuration options. + * @param http_client the HTTP client to be used for exporting + */ + OtlpHttpLogRecordExporter(const OtlpHttpLogRecordExporterOptions &options, + std::shared_ptr http_client); + + /** + * Create an OtlpHttpLogRecordExporter with user specified options, runtime options, and HTTP + * client. + * @param options An object containing the user's configuration options. + * @param runtime_options An object containing the user's runtime options. + * @param http_client the HTTP client to be used for exporting + */ + OtlpHttpLogRecordExporter(const OtlpHttpLogRecordExporterOptions &options, + const OtlpHttpLogRecordExporterRuntimeOptions &runtime_options, + std::shared_ptr http_client); + /** * Creates a recordable that stores the data in a JSON object */ diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_log_record_exporter_factory.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_log_record_exporter_factory.h index d01eb4259a..3669d923b5 100644 --- a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_log_record_exporter_factory.h +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_log_record_exporter_factory.h @@ -7,6 +7,8 @@ #include "opentelemetry/exporters/otlp/otlp_http_log_record_exporter_options.h" #include "opentelemetry/exporters/otlp/otlp_http_log_record_exporter_runtime_options.h" +#include "opentelemetry/ext/http/client/http_client.h" +#include "opentelemetry/ext/http/client/http_client_factory.h" #include "opentelemetry/sdk/logs/exporter.h" #include "opentelemetry/version.h" @@ -39,6 +41,47 @@ class OPENTELEMETRY_EXPORT OtlpHttpLogRecordExporterFactory static std::unique_ptr Create( const OtlpHttpLogRecordExporterOptions &options, const OtlpHttpLogRecordExporterRuntimeOptions &runtime_options); + + /** + * Create a OtlpHttpLogRecordExporter using the given options and HTTP client factory. + * @param options the exporter options + * @param factory the HTTP client factory used to create the underlying HTTP client + */ + static std::unique_ptr Create( + const OtlpHttpLogRecordExporterOptions &options, + const std::shared_ptr &factory); + + /** + * Create a OtlpHttpLogRecordExporter using the given options, runtime options, and HTTP client + * factory. + * @param options the exporter options + * @param runtime_options the runtime options (e.g. thread instrumentation) + * @param factory the HTTP client factory used to create the underlying HTTP client + */ + static std::unique_ptr Create( + const OtlpHttpLogRecordExporterOptions &options, + const OtlpHttpLogRecordExporterRuntimeOptions &runtime_options, + const std::shared_ptr &factory); + + /** + * Create a OtlpHttpLogRecordExporter using the given options and HTTP client. + * @param options the exporter options + * @param http_client the HTTP client to be used for exporting + */ + static std::unique_ptr Create( + const OtlpHttpLogRecordExporterOptions &options, + std::shared_ptr http_client); + + /** + * Create a OtlpHttpLogRecordExporter using the given options, runtime options, and HTTP client. + * @param options the exporter options + * @param runtime_options the runtime options (e.g. thread instrumentation) + * @param http_client the HTTP client to be used for exporting + */ + static std::unique_ptr Create( + const OtlpHttpLogRecordExporterOptions &options, + const OtlpHttpLogRecordExporterRuntimeOptions &runtime_options, + std::shared_ptr http_client); }; } // namespace otlp diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_metric_exporter.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_metric_exporter.h index 72774f3606..d8a25f95d8 100644 --- a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_metric_exporter.h +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_metric_exporter.h @@ -9,6 +9,7 @@ #include "opentelemetry/exporters/otlp/otlp_http_client.h" #include "opentelemetry/exporters/otlp/otlp_http_metric_exporter_options.h" #include "opentelemetry/exporters/otlp/otlp_http_metric_exporter_runtime_options.h" +#include "opentelemetry/ext/http/client/http_client_factory.h" #include "opentelemetry/sdk/common/exporter_utils.h" #include "opentelemetry/sdk/metrics/export/metric_producer.h" #include "opentelemetry/sdk/metrics/instruments.h" @@ -45,6 +46,43 @@ class OtlpHttpMetricExporter final : public opentelemetry::sdk::metrics::PushMet OtlpHttpMetricExporter(const OtlpHttpMetricExporterOptions &options, const OtlpHttpMetricExporterRuntimeOptions &runtime_options); + /** + * Create an OtlpHttpMetricExporter with user specified options and HTTP client factory. + * @param options An object containing the user's configuration options. + * @param factory the HTTP client factory used to create the underlying HTTP client + */ + OtlpHttpMetricExporter(const OtlpHttpMetricExporterOptions &options, + const std::shared_ptr &factory); + + /** + * Create an OtlpHttpMetricExporter with user specified options, runtime options, and HTTP client + * factory. + * @param options An object containing the user's configuration options. + * @param runtime_options An object containing the user's runtime options. + * @param factory the HTTP client factory used to create the underlying HTTP client + */ + OtlpHttpMetricExporter(const OtlpHttpMetricExporterOptions &options, + const OtlpHttpMetricExporterRuntimeOptions &runtime_options, + const std::shared_ptr &factory); + + /** + * Create an OtlpHttpMetricExporter with user specified options and HTTP client. + * @param options An object containing the user's configuration options. + * @param http_client the HTTP client to be used for exporting + */ + OtlpHttpMetricExporter(const OtlpHttpMetricExporterOptions &options, + std::shared_ptr http_client); + + /** + * Create an OtlpHttpMetricExporter with user specified options, runtime options, and HTTP client. + * @param options An object containing the user's configuration options. + * @param runtime_options An object containing the user's runtime options. + * @param http_client the HTTP client to be used for exporting + */ + OtlpHttpMetricExporter(const OtlpHttpMetricExporterOptions &options, + const OtlpHttpMetricExporterRuntimeOptions &runtime_options, + std::shared_ptr http_client); + /** * Get the AggregationTemporality for exporter * diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_metric_exporter_factory.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_metric_exporter_factory.h index cb026cbe1e..d60dfb2b7f 100644 --- a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_metric_exporter_factory.h +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_http_metric_exporter_factory.h @@ -7,6 +7,8 @@ #include "opentelemetry/exporters/otlp/otlp_http_metric_exporter_options.h" #include "opentelemetry/exporters/otlp/otlp_http_metric_exporter_runtime_options.h" +#include "opentelemetry/ext/http/client/http_client.h" +#include "opentelemetry/ext/http/client/http_client_factory.h" #include "opentelemetry/sdk/metrics/push_metric_exporter.h" #include "opentelemetry/version.h" @@ -39,6 +41,47 @@ class OPENTELEMETRY_EXPORT OtlpHttpMetricExporterFactory static std::unique_ptr Create( const OtlpHttpMetricExporterOptions &options, const OtlpHttpMetricExporterRuntimeOptions &runtime_options); + + /** + * Create a OtlpHttpMetricExporter using the given options and HTTP client factory. + * @param options the exporter options + * @param factory the HTTP client factory used to create the underlying HTTP client + */ + static std::unique_ptr Create( + const OtlpHttpMetricExporterOptions &options, + const std::shared_ptr &factory); + + /** + * Create a OtlpHttpMetricExporter using the given options, runtime options, and HTTP client + * factory. + * @param options the exporter options + * @param runtime_options the runtime options (e.g. thread instrumentation) + * @param factory the HTTP client factory used to create the underlying HTTP client + */ + static std::unique_ptr Create( + const OtlpHttpMetricExporterOptions &options, + const OtlpHttpMetricExporterRuntimeOptions &runtime_options, + const std::shared_ptr &factory); + + /** + * Create a OtlpHttpMetricExporter using the given options and HTTP client. + * @param options the exporter options + * @param http_client the HTTP client to be used for exporting + */ + static std::unique_ptr Create( + const OtlpHttpMetricExporterOptions &options, + std::shared_ptr http_client); + + /** + * Create a OtlpHttpMetricExporter using the given options, runtime options, and HTTP client. + * @param options the exporter options + * @param runtime_options the runtime options (e.g. thread instrumentation) + * @param http_client the HTTP client to be used for exporting + */ + static std::unique_ptr Create( + const OtlpHttpMetricExporterOptions &options, + const OtlpHttpMetricExporterRuntimeOptions &runtime_options, + std::shared_ptr http_client); }; } // namespace otlp diff --git a/exporters/otlp/src/otlp_http_client.cc b/exporters/otlp/src/otlp_http_client.cc index 8542c86de2..dd6075b808 100644 --- a/exporters/otlp/src/otlp_http_client.cc +++ b/exporters/otlp/src/otlp_http_client.cc @@ -661,9 +661,19 @@ void ConvertListFieldToJson(nlohmann::json &value, } // namespace OtlpHttpClient::OtlpHttpClient(OtlpHttpClientOptions &&options) + : OtlpHttpClient(std::move(options), ext::http::client::GetDefaultHttpClientFactory()) +{} + +OtlpHttpClient::OtlpHttpClient(OtlpHttpClientOptions &&options, + const std::shared_ptr &factory) + : OtlpHttpClient(std::move(options), factory->Create(options.thread_instrumentation)) +{} + +OtlpHttpClient::OtlpHttpClient(OtlpHttpClientOptions &&options, + std::shared_ptr http_client) : is_shutdown_(false), options_(std::move(options)), - http_client_(http_client::HttpClientFactory::Create(options_.thread_instrumentation)), + http_client_(std::move(http_client)), start_session_counter_(0), finished_session_counter_(0) { @@ -702,17 +712,6 @@ OtlpHttpClient::~OtlpHttpClient() ; } -OtlpHttpClient::OtlpHttpClient(OtlpHttpClientOptions &&options, - std::shared_ptr http_client) - : is_shutdown_(false), - options_(std::move(options)), - http_client_(std::move(http_client)), - start_session_counter_(0), - finished_session_counter_(0) -{ - http_client_->SetMaxSessionsPerConnection(options_.max_requests_per_connection); -} - // ----------------------------- HTTP Client methods ------------------------------ opentelemetry::sdk::common::ExportResult OtlpHttpClient::Export( const google::protobuf::Message &message) noexcept diff --git a/exporters/otlp/src/otlp_http_exporter.cc b/exporters/otlp/src/otlp_http_exporter.cc index 499d577185..50bf12f868 100644 --- a/exporters/otlp/src/otlp_http_exporter.cc +++ b/exporters/otlp/src/otlp_http_exporter.cc @@ -16,6 +16,7 @@ #include "opentelemetry/exporters/otlp/otlp_recordable.h" #include "opentelemetry/exporters/otlp/otlp_recordable_utils.h" #include "opentelemetry/ext/http/client/http_client.h" +#include "opentelemetry/ext/http/client/http_client_factory.h" #include "opentelemetry/nostd/span.h" #include "opentelemetry/nostd/string_view.h" #include "opentelemetry/sdk/common/exporter_utils.h" @@ -115,6 +116,160 @@ OtlpHttpExporter::OtlpHttpExporter(const OtlpHttpExporterOptions &options, ))) {} +OtlpHttpExporter::OtlpHttpExporter( + const OtlpHttpExporterOptions &options, + const std::shared_ptr &factory) + : options_(options), + runtime_options_(), + http_client_(new OtlpHttpClient( + OtlpHttpClientOptions(options.url, + options.ssl_insecure_skip_verify, + options.ssl_ca_cert_path, + options.ssl_ca_cert_string, + options.ssl_client_key_path, + options.ssl_client_key_string, + options.ssl_client_cert_path, + options.ssl_client_cert_string, + options.ssl_min_tls, + options.ssl_max_tls, + options.ssl_cipher, + options.ssl_cipher_suite, + options.content_type, + options.json_bytes_mapping, + options.compression, + options.use_json_name, + options.console_debug, + options.timeout, + options.http_headers, + options.retry_policy_max_attempts, + options.retry_policy_initial_backoff, + options.retry_policy_max_backoff, + options.retry_policy_backoff_multiplier, + std::shared_ptr{nullptr} +#ifdef ENABLE_ASYNC_EXPORT + , + options.max_concurrent_requests, + options.max_requests_per_connection +#endif + ), + factory)) +{} + +OtlpHttpExporter::OtlpHttpExporter( + const OtlpHttpExporterOptions &options, + const OtlpHttpExporterRuntimeOptions &runtime_options, + const std::shared_ptr &factory) + : options_(options), + runtime_options_(runtime_options), + http_client_(new OtlpHttpClient(OtlpHttpClientOptions(options.url, + options.ssl_insecure_skip_verify, + options.ssl_ca_cert_path, + options.ssl_ca_cert_string, + options.ssl_client_key_path, + options.ssl_client_key_string, + options.ssl_client_cert_path, + options.ssl_client_cert_string, + options.ssl_min_tls, + options.ssl_max_tls, + options.ssl_cipher, + options.ssl_cipher_suite, + options.content_type, + options.json_bytes_mapping, + options.compression, + options.use_json_name, + options.console_debug, + options.timeout, + options.http_headers, + options.retry_policy_max_attempts, + options.retry_policy_initial_backoff, + options.retry_policy_max_backoff, + options.retry_policy_backoff_multiplier, + runtime_options.thread_instrumentation +#ifdef ENABLE_ASYNC_EXPORT + , + options.max_concurrent_requests, + options.max_requests_per_connection +#endif + ), + factory)) +{} + +OtlpHttpExporter::OtlpHttpExporter(const OtlpHttpExporterOptions &options, + std::shared_ptr http_client) + : options_(options), + runtime_options_(), + http_client_(new OtlpHttpClient( + OtlpHttpClientOptions(options.url, + options.ssl_insecure_skip_verify, + options.ssl_ca_cert_path, + options.ssl_ca_cert_string, + options.ssl_client_key_path, + options.ssl_client_key_string, + options.ssl_client_cert_path, + options.ssl_client_cert_string, + options.ssl_min_tls, + options.ssl_max_tls, + options.ssl_cipher, + options.ssl_cipher_suite, + options.content_type, + options.json_bytes_mapping, + options.compression, + options.use_json_name, + options.console_debug, + options.timeout, + options.http_headers, + options.retry_policy_max_attempts, + options.retry_policy_initial_backoff, + options.retry_policy_max_backoff, + options.retry_policy_backoff_multiplier, + std::shared_ptr{nullptr} +#ifdef ENABLE_ASYNC_EXPORT + , + options.max_concurrent_requests, + options.max_requests_per_connection +#endif + ), + std::move(http_client))) +{} + +OtlpHttpExporter::OtlpHttpExporter(const OtlpHttpExporterOptions &options, + const OtlpHttpExporterRuntimeOptions &runtime_options, + std::shared_ptr http_client) + : options_(options), + runtime_options_(runtime_options), + http_client_(new OtlpHttpClient(OtlpHttpClientOptions(options.url, + options.ssl_insecure_skip_verify, + options.ssl_ca_cert_path, + options.ssl_ca_cert_string, + options.ssl_client_key_path, + options.ssl_client_key_string, + options.ssl_client_cert_path, + options.ssl_client_cert_string, + options.ssl_min_tls, + options.ssl_max_tls, + options.ssl_cipher, + options.ssl_cipher_suite, + options.content_type, + options.json_bytes_mapping, + options.compression, + options.use_json_name, + options.console_debug, + options.timeout, + options.http_headers, + options.retry_policy_max_attempts, + options.retry_policy_initial_backoff, + options.retry_policy_max_backoff, + options.retry_policy_backoff_multiplier, + runtime_options.thread_instrumentation +#ifdef ENABLE_ASYNC_EXPORT + , + options.max_concurrent_requests, + options.max_requests_per_connection +#endif + ), + std::move(http_client))) +{} + OtlpHttpExporter::OtlpHttpExporter(std::unique_ptr http_client) : options_(OtlpHttpExporterOptions()), http_client_(std::move(http_client)) { diff --git a/exporters/otlp/src/otlp_http_exporter_factory.cc b/exporters/otlp/src/otlp_http_exporter_factory.cc index eca3552146..4aa8d9e52b 100644 --- a/exporters/otlp/src/otlp_http_exporter_factory.cc +++ b/exporters/otlp/src/otlp_http_exporter_factory.cc @@ -1,10 +1,13 @@ // Copyright The OpenTelemetry Authors // SPDX-License-Identifier: Apache-2.0 -#include "opentelemetry/exporters/otlp/otlp_http_exporter_factory.h" +#include + #include "opentelemetry/exporters/otlp/otlp_http_exporter.h" +#include "opentelemetry/exporters/otlp/otlp_http_exporter_factory.h" #include "opentelemetry/exporters/otlp/otlp_http_exporter_options.h" #include "opentelemetry/exporters/otlp/otlp_http_exporter_runtime_options.h" +#include "opentelemetry/ext/http/client/http_client.h" OPENTELEMETRY_BEGIN_NAMESPACE namespace exporter @@ -34,6 +37,40 @@ std::unique_ptr OtlpHttpExporterFactory return exporter; } +std::unique_ptr OtlpHttpExporterFactory::Create( + const OtlpHttpExporterOptions &options, + const std::shared_ptr &factory) +{ + return std::unique_ptr( + new OtlpHttpExporter(options, factory)); +} + +std::unique_ptr OtlpHttpExporterFactory::Create( + const OtlpHttpExporterOptions &options, + const OtlpHttpExporterRuntimeOptions &runtime_options, + const std::shared_ptr &factory) +{ + return std::unique_ptr( + new OtlpHttpExporter(options, runtime_options, factory)); +} + +std::unique_ptr OtlpHttpExporterFactory::Create( + const OtlpHttpExporterOptions &options, + std::shared_ptr http_client) +{ + return std::unique_ptr( + new OtlpHttpExporter(options, std::move(http_client))); +} + +std::unique_ptr OtlpHttpExporterFactory::Create( + const OtlpHttpExporterOptions &options, + const OtlpHttpExporterRuntimeOptions &runtime_options, + std::shared_ptr http_client) +{ + return std::unique_ptr( + new OtlpHttpExporter(options, runtime_options, std::move(http_client))); +} + } // namespace otlp } // namespace exporter OPENTELEMETRY_END_NAMESPACE diff --git a/exporters/otlp/src/otlp_http_log_record_exporter.cc b/exporters/otlp/src/otlp_http_log_record_exporter.cc index b787ae3a46..e062d35146 100644 --- a/exporters/otlp/src/otlp_http_log_record_exporter.cc +++ b/exporters/otlp/src/otlp_http_log_record_exporter.cc @@ -16,6 +16,7 @@ #include "opentelemetry/exporters/otlp/otlp_log_recordable.h" #include "opentelemetry/exporters/otlp/otlp_recordable_utils.h" #include "opentelemetry/ext/http/client/http_client.h" +#include "opentelemetry/ext/http/client/http_client_factory.h" #include "opentelemetry/nostd/span.h" #include "opentelemetry/nostd/string_view.h" #include "opentelemetry/sdk/common/exporter_utils.h" @@ -119,6 +120,162 @@ OtlpHttpLogRecordExporter::OtlpHttpLogRecordExporter( ))) {} +OtlpHttpLogRecordExporter::OtlpHttpLogRecordExporter( + const OtlpHttpLogRecordExporterOptions &options, + const std::shared_ptr &factory) + : options_(options), + runtime_options_(), + http_client_(new OtlpHttpClient( + OtlpHttpClientOptions(options.url, + options.ssl_insecure_skip_verify, + options.ssl_ca_cert_path, + options.ssl_ca_cert_string, + options.ssl_client_key_path, + options.ssl_client_key_string, + options.ssl_client_cert_path, + options.ssl_client_cert_string, + options.ssl_min_tls, + options.ssl_max_tls, + options.ssl_cipher, + options.ssl_cipher_suite, + options.content_type, + options.json_bytes_mapping, + options.compression, + options.use_json_name, + options.console_debug, + options.timeout, + options.http_headers, + options.retry_policy_max_attempts, + options.retry_policy_initial_backoff, + options.retry_policy_max_backoff, + options.retry_policy_backoff_multiplier, + std::shared_ptr{nullptr} +#ifdef ENABLE_ASYNC_EXPORT + , + options.max_concurrent_requests, + options.max_requests_per_connection +#endif + ), + factory)) +{} + +OtlpHttpLogRecordExporter::OtlpHttpLogRecordExporter( + const OtlpHttpLogRecordExporterOptions &options, + const OtlpHttpLogRecordExporterRuntimeOptions &runtime_options, + const std::shared_ptr &factory) + : options_(options), + runtime_options_(runtime_options), + http_client_(new OtlpHttpClient(OtlpHttpClientOptions(options.url, + options.ssl_insecure_skip_verify, + options.ssl_ca_cert_path, + options.ssl_ca_cert_string, + options.ssl_client_key_path, + options.ssl_client_key_string, + options.ssl_client_cert_path, + options.ssl_client_cert_string, + options.ssl_min_tls, + options.ssl_max_tls, + options.ssl_cipher, + options.ssl_cipher_suite, + options.content_type, + options.json_bytes_mapping, + options.compression, + options.use_json_name, + options.console_debug, + options.timeout, + options.http_headers, + options.retry_policy_max_attempts, + options.retry_policy_initial_backoff, + options.retry_policy_max_backoff, + options.retry_policy_backoff_multiplier, + runtime_options.thread_instrumentation +#ifdef ENABLE_ASYNC_EXPORT + , + options.max_concurrent_requests, + options.max_requests_per_connection +#endif + ), + factory)) +{} + +OtlpHttpLogRecordExporter::OtlpHttpLogRecordExporter( + const OtlpHttpLogRecordExporterOptions &options, + std::shared_ptr http_client) + : options_(options), + runtime_options_(), + http_client_(new OtlpHttpClient( + OtlpHttpClientOptions(options.url, + options.ssl_insecure_skip_verify, + options.ssl_ca_cert_path, + options.ssl_ca_cert_string, + options.ssl_client_key_path, + options.ssl_client_key_string, + options.ssl_client_cert_path, + options.ssl_client_cert_string, + options.ssl_min_tls, + options.ssl_max_tls, + options.ssl_cipher, + options.ssl_cipher_suite, + options.content_type, + options.json_bytes_mapping, + options.compression, + options.use_json_name, + options.console_debug, + options.timeout, + options.http_headers, + options.retry_policy_max_attempts, + options.retry_policy_initial_backoff, + options.retry_policy_max_backoff, + options.retry_policy_backoff_multiplier, + std::shared_ptr{nullptr} +#ifdef ENABLE_ASYNC_EXPORT + , + options.max_concurrent_requests, + options.max_requests_per_connection +#endif + ), + std::move(http_client))) +{} + +OtlpHttpLogRecordExporter::OtlpHttpLogRecordExporter( + const OtlpHttpLogRecordExporterOptions &options, + const OtlpHttpLogRecordExporterRuntimeOptions &runtime_options, + std::shared_ptr http_client) + : options_(options), + runtime_options_(runtime_options), + http_client_(new OtlpHttpClient(OtlpHttpClientOptions(options.url, + options.ssl_insecure_skip_verify, + options.ssl_ca_cert_path, + options.ssl_ca_cert_string, + options.ssl_client_key_path, + options.ssl_client_key_string, + options.ssl_client_cert_path, + options.ssl_client_cert_string, + options.ssl_min_tls, + options.ssl_max_tls, + options.ssl_cipher, + options.ssl_cipher_suite, + options.content_type, + options.json_bytes_mapping, + options.compression, + options.use_json_name, + options.console_debug, + options.timeout, + options.http_headers, + options.retry_policy_max_attempts, + options.retry_policy_initial_backoff, + options.retry_policy_max_backoff, + options.retry_policy_backoff_multiplier, + runtime_options.thread_instrumentation +#ifdef ENABLE_ASYNC_EXPORT + , + options.max_concurrent_requests, + options.max_requests_per_connection +#endif + ), + std::move(http_client))) +{} + OtlpHttpLogRecordExporter::OtlpHttpLogRecordExporter(std::unique_ptr http_client) : options_(OtlpHttpLogRecordExporterOptions()), http_client_(std::move(http_client)) { diff --git a/exporters/otlp/src/otlp_http_log_record_exporter_factory.cc b/exporters/otlp/src/otlp_http_log_record_exporter_factory.cc index 54e78bf4e9..7b22231ad6 100644 --- a/exporters/otlp/src/otlp_http_log_record_exporter_factory.cc +++ b/exporters/otlp/src/otlp_http_log_record_exporter_factory.cc @@ -1,10 +1,13 @@ // Copyright The OpenTelemetry Authors // SPDX-License-Identifier: Apache-2.0 -#include "opentelemetry/exporters/otlp/otlp_http_log_record_exporter_factory.h" +#include + #include "opentelemetry/exporters/otlp/otlp_http_log_record_exporter.h" +#include "opentelemetry/exporters/otlp/otlp_http_log_record_exporter_factory.h" #include "opentelemetry/exporters/otlp/otlp_http_log_record_exporter_options.h" #include "opentelemetry/exporters/otlp/otlp_http_log_record_exporter_runtime_options.h" +#include "opentelemetry/ext/http/client/http_client.h" OPENTELEMETRY_BEGIN_NAMESPACE namespace exporter @@ -36,6 +39,44 @@ OtlpHttpLogRecordExporterFactory::Create( return exporter; } +std::unique_ptr +OtlpHttpLogRecordExporterFactory::Create( + const OtlpHttpLogRecordExporterOptions &options, + const std::shared_ptr &factory) +{ + return std::unique_ptr( + new OtlpHttpLogRecordExporter(options, factory)); +} + +std::unique_ptr +OtlpHttpLogRecordExporterFactory::Create( + const OtlpHttpLogRecordExporterOptions &options, + const OtlpHttpLogRecordExporterRuntimeOptions &runtime_options, + const std::shared_ptr &factory) +{ + return std::unique_ptr( + new OtlpHttpLogRecordExporter(options, runtime_options, factory)); +} + +std::unique_ptr +OtlpHttpLogRecordExporterFactory::Create( + const OtlpHttpLogRecordExporterOptions &options, + std::shared_ptr http_client) +{ + return std::unique_ptr( + new OtlpHttpLogRecordExporter(options, std::move(http_client))); +} + +std::unique_ptr +OtlpHttpLogRecordExporterFactory::Create( + const OtlpHttpLogRecordExporterOptions &options, + const OtlpHttpLogRecordExporterRuntimeOptions &runtime_options, + std::shared_ptr http_client) +{ + return std::unique_ptr( + new OtlpHttpLogRecordExporter(options, runtime_options, std::move(http_client))); +} + } // namespace otlp } // namespace exporter OPENTELEMETRY_END_NAMESPACE diff --git a/exporters/otlp/src/otlp_http_metric_exporter.cc b/exporters/otlp/src/otlp_http_metric_exporter.cc index 38f5a8eb93..780483b910 100644 --- a/exporters/otlp/src/otlp_http_metric_exporter.cc +++ b/exporters/otlp/src/otlp_http_metric_exporter.cc @@ -16,6 +16,7 @@ #include "opentelemetry/exporters/otlp/otlp_http_metric_exporter_runtime_options.h" #include "opentelemetry/exporters/otlp/otlp_metric_utils.h" #include "opentelemetry/ext/http/client/http_client.h" +#include "opentelemetry/ext/http/client/http_client_factory.h" #include "opentelemetry/nostd/string_view.h" #include "opentelemetry/sdk/common/exporter_utils.h" #include "opentelemetry/sdk/common/global_log_handler.h" @@ -121,6 +122,170 @@ OtlpHttpMetricExporter::OtlpHttpMetricExporter( ))) {} +OtlpHttpMetricExporter::OtlpHttpMetricExporter( + const OtlpHttpMetricExporterOptions &options, + const std::shared_ptr &factory) + : options_(options), + runtime_options_(), + aggregation_temporality_selector_{ + OtlpMetricUtils::ChooseTemporalitySelector(options_.aggregation_temporality)}, + http_client_(new OtlpHttpClient( + OtlpHttpClientOptions(options.url, + options.ssl_insecure_skip_verify, + options.ssl_ca_cert_path, + options.ssl_ca_cert_string, + options.ssl_client_key_path, + options.ssl_client_key_string, + options.ssl_client_cert_path, + options.ssl_client_cert_string, + options.ssl_min_tls, + options.ssl_max_tls, + options.ssl_cipher, + options.ssl_cipher_suite, + options.content_type, + options.json_bytes_mapping, + options.compression, + options.use_json_name, + options.console_debug, + options.timeout, + options.http_headers, + options.retry_policy_max_attempts, + options.retry_policy_initial_backoff, + options.retry_policy_max_backoff, + options.retry_policy_backoff_multiplier, + std::shared_ptr{nullptr} +#ifdef ENABLE_ASYNC_EXPORT + , + options.max_concurrent_requests, + options.max_requests_per_connection +#endif + ), + factory)) +{} + +OtlpHttpMetricExporter::OtlpHttpMetricExporter( + const OtlpHttpMetricExporterOptions &options, + const OtlpHttpMetricExporterRuntimeOptions &runtime_options, + const std::shared_ptr &factory) + : options_(options), + runtime_options_(runtime_options), + aggregation_temporality_selector_{ + OtlpMetricUtils::ChooseTemporalitySelector(options_.aggregation_temporality)}, + http_client_(new OtlpHttpClient(OtlpHttpClientOptions(options.url, + options.ssl_insecure_skip_verify, + options.ssl_ca_cert_path, + options.ssl_ca_cert_string, + options.ssl_client_key_path, + options.ssl_client_key_string, + options.ssl_client_cert_path, + options.ssl_client_cert_string, + options.ssl_min_tls, + options.ssl_max_tls, + options.ssl_cipher, + options.ssl_cipher_suite, + options.content_type, + options.json_bytes_mapping, + options.compression, + options.use_json_name, + options.console_debug, + options.timeout, + options.http_headers, + options.retry_policy_max_attempts, + options.retry_policy_initial_backoff, + options.retry_policy_max_backoff, + options.retry_policy_backoff_multiplier, + runtime_options.thread_instrumentation +#ifdef ENABLE_ASYNC_EXPORT + , + options.max_concurrent_requests, + options.max_requests_per_connection +#endif + ), + factory)) +{} + +OtlpHttpMetricExporter::OtlpHttpMetricExporter( + const OtlpHttpMetricExporterOptions &options, + std::shared_ptr http_client) + : options_(options), + runtime_options_(), + aggregation_temporality_selector_{ + OtlpMetricUtils::ChooseTemporalitySelector(options_.aggregation_temporality)}, + http_client_(new OtlpHttpClient( + OtlpHttpClientOptions(options.url, + options.ssl_insecure_skip_verify, + options.ssl_ca_cert_path, + options.ssl_ca_cert_string, + options.ssl_client_key_path, + options.ssl_client_key_string, + options.ssl_client_cert_path, + options.ssl_client_cert_string, + options.ssl_min_tls, + options.ssl_max_tls, + options.ssl_cipher, + options.ssl_cipher_suite, + options.content_type, + options.json_bytes_mapping, + options.compression, + options.use_json_name, + options.console_debug, + options.timeout, + options.http_headers, + options.retry_policy_max_attempts, + options.retry_policy_initial_backoff, + options.retry_policy_max_backoff, + options.retry_policy_backoff_multiplier, + std::shared_ptr{nullptr} +#ifdef ENABLE_ASYNC_EXPORT + , + options.max_concurrent_requests, + options.max_requests_per_connection +#endif + ), + std::move(http_client))) +{} + +OtlpHttpMetricExporter::OtlpHttpMetricExporter( + const OtlpHttpMetricExporterOptions &options, + const OtlpHttpMetricExporterRuntimeOptions &runtime_options, + std::shared_ptr http_client) + : options_(options), + runtime_options_(runtime_options), + aggregation_temporality_selector_{ + OtlpMetricUtils::ChooseTemporalitySelector(options_.aggregation_temporality)}, + http_client_(new OtlpHttpClient(OtlpHttpClientOptions(options.url, + options.ssl_insecure_skip_verify, + options.ssl_ca_cert_path, + options.ssl_ca_cert_string, + options.ssl_client_key_path, + options.ssl_client_key_string, + options.ssl_client_cert_path, + options.ssl_client_cert_string, + options.ssl_min_tls, + options.ssl_max_tls, + options.ssl_cipher, + options.ssl_cipher_suite, + options.content_type, + options.json_bytes_mapping, + options.compression, + options.use_json_name, + options.console_debug, + options.timeout, + options.http_headers, + options.retry_policy_max_attempts, + options.retry_policy_initial_backoff, + options.retry_policy_max_backoff, + options.retry_policy_backoff_multiplier, + runtime_options.thread_instrumentation +#ifdef ENABLE_ASYNC_EXPORT + , + options.max_concurrent_requests, + options.max_requests_per_connection +#endif + ), + std::move(http_client))) +{} + OtlpHttpMetricExporter::OtlpHttpMetricExporter(std::unique_ptr http_client) : options_(OtlpHttpMetricExporterOptions()), aggregation_temporality_selector_{ diff --git a/exporters/otlp/src/otlp_http_metric_exporter_factory.cc b/exporters/otlp/src/otlp_http_metric_exporter_factory.cc index 1bd6d8a644..f819b4f9c2 100644 --- a/exporters/otlp/src/otlp_http_metric_exporter_factory.cc +++ b/exporters/otlp/src/otlp_http_metric_exporter_factory.cc @@ -1,10 +1,13 @@ // Copyright The OpenTelemetry Authors // SPDX-License-Identifier: Apache-2.0 -#include "opentelemetry/exporters/otlp/otlp_http_metric_exporter_factory.h" +#include + #include "opentelemetry/exporters/otlp/otlp_http_metric_exporter.h" +#include "opentelemetry/exporters/otlp/otlp_http_metric_exporter_factory.h" #include "opentelemetry/exporters/otlp/otlp_http_metric_exporter_options.h" #include "opentelemetry/exporters/otlp/otlp_http_metric_exporter_runtime_options.h" +#include "opentelemetry/ext/http/client/http_client.h" OPENTELEMETRY_BEGIN_NAMESPACE namespace exporter @@ -35,6 +38,44 @@ OtlpHttpMetricExporterFactory::Create(const OtlpHttpMetricExporterOptions &optio return exporter; } +std::unique_ptr +OtlpHttpMetricExporterFactory::Create( + const OtlpHttpMetricExporterOptions &options, + const std::shared_ptr &factory) +{ + return std::unique_ptr( + new OtlpHttpMetricExporter(options, factory)); +} + +std::unique_ptr +OtlpHttpMetricExporterFactory::Create( + const OtlpHttpMetricExporterOptions &options, + const OtlpHttpMetricExporterRuntimeOptions &runtime_options, + const std::shared_ptr &factory) +{ + return std::unique_ptr( + new OtlpHttpMetricExporter(options, runtime_options, factory)); +} + +std::unique_ptr +OtlpHttpMetricExporterFactory::Create( + const OtlpHttpMetricExporterOptions &options, + std::shared_ptr http_client) +{ + return std::unique_ptr( + new OtlpHttpMetricExporter(options, std::move(http_client))); +} + +std::unique_ptr +OtlpHttpMetricExporterFactory::Create( + const OtlpHttpMetricExporterOptions &options, + const OtlpHttpMetricExporterRuntimeOptions &runtime_options, + std::shared_ptr http_client) +{ + return std::unique_ptr( + new OtlpHttpMetricExporter(options, runtime_options, std::move(http_client))); +} + } // namespace otlp } // namespace exporter OPENTELEMETRY_END_NAMESPACE diff --git a/exporters/otlp/test/otlp_http_exporter_custom_client_test.cc b/exporters/otlp/test/otlp_http_exporter_custom_client_test.cc new file mode 100644 index 0000000000..e90088be7b --- /dev/null +++ b/exporters/otlp/test/otlp_http_exporter_custom_client_test.cc @@ -0,0 +1,152 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#ifndef OPENTELEMETRY_STL_VERSION + +# include + +# include "opentelemetry/exporters/otlp/otlp_http_client.h" +# include "opentelemetry/exporters/otlp/otlp_http_exporter.h" +# include "opentelemetry/exporters/otlp/otlp_http_exporter_factory.h" +# include "opentelemetry/exporters/otlp/otlp_http_exporter_options.h" +# include "opentelemetry/exporters/otlp/otlp_http_exporter_runtime_options.h" +# include "opentelemetry/ext/http/client/http_client_factory.h" +# include "opentelemetry/sdk/trace/batch_span_processor.h" +# include "opentelemetry/sdk/trace/batch_span_processor_options.h" +# include "opentelemetry/sdk/trace/tracer_provider.h" +# include "opentelemetry/test_common/ext/http/client/http_client_test_factory.h" +# include "opentelemetry/test_common/ext/http/client/nosend/http_client_factory_nosend.h" +# include "opentelemetry/test_common/ext/http/client/nosend/http_client_nosend.h" + +# include +# include "gmock/gmock.h" + +using namespace testing; + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace exporter +{ +namespace otlp +{ + +namespace http_client = opentelemetry::ext::http::client; +using NosendHttpClientFactory = + opentelemetry::test_common::ext::http::client::nosend::HttpClientFactoryNosend; + +static OtlpHttpClientOptions MakeOtlpHttpClientOptions() +{ + std::shared_ptr not_instrumented; + OtlpHttpExporterOptions options; + options.console_debug = true; + options.timeout = std::chrono::system_clock::duration::zero(); + options.retry_policy_max_attempts = 0U; + options.retry_policy_initial_backoff = std::chrono::duration::zero(); + options.retry_policy_max_backoff = std::chrono::duration::zero(); + options.retry_policy_backoff_multiplier = 0.0f; + OtlpHttpClientOptions otlp_http_client_options( + options.url, false, /* ssl_insecure_skip_verify */ + "", /* ssl_ca_cert_path */ + "", /* ssl_ca_cert_string */ + "", /* ssl_client_key_path */ + "", /* ssl_client_key_string */ + "", /* ssl_client_cert_path */ + "", /* ssl_client_cert_string */ + "", /* ssl_min_tls */ + "", /* ssl_max_tls */ + "", /* ssl_cipher */ + "", /* ssl_cipher_suite */ + options.content_type, options.json_bytes_mapping, options.compression, options.use_json_name, + options.console_debug, options.timeout, options.http_headers, + options.retry_policy_max_attempts, options.retry_policy_initial_backoff, + options.retry_policy_max_backoff, options.retry_policy_backoff_multiplier, not_instrumented); + otlp_http_client_options.max_concurrent_requests = 0; + return otlp_http_client_options; +} + +class OtlpHttpExporterCustomClientTestPeer : public ::testing::Test +{ +public: + std::unique_ptr GetExporter(std::unique_ptr http_client) + { + return std::unique_ptr(new OtlpHttpExporter(std::move(http_client))); + } + + static std::pair> + GetMockOtlpHttpClient() + { + auto http_client = http_client::HttpClientTestFactory::Create(); + return {new OtlpHttpClient(MakeOtlpHttpClientOptions(), http_client), http_client}; + } +}; + +TEST_F(OtlpHttpExporterCustomClientTestPeer, FactoryInjectionCreatesExporter) +{ + OtlpHttpExporterOptions opts; + auto factory = std::make_shared(); + auto exporter = OtlpHttpExporterFactory::Create(opts, std::move(factory)); + ASSERT_NE(exporter, nullptr); +} + +TEST_F(OtlpHttpExporterCustomClientTestPeer, HttpClientInjectionCreatesExporter) +{ + OtlpHttpExporterOptions opts; + auto client = http_client::HttpClientTestFactory::Create(); + auto exporter = OtlpHttpExporterFactory::Create(opts, std::move(client)); + ASSERT_NE(exporter, nullptr); +} + +TEST_F(OtlpHttpExporterCustomClientTestPeer, RuntimeOptionsWithFactoryCreatesExporter) +{ + OtlpHttpExporterOptions opts; + OtlpHttpExporterRuntimeOptions runtime_opts; + auto factory = std::make_shared(); + auto exporter = OtlpHttpExporterFactory::Create(opts, runtime_opts, std::move(factory)); + ASSERT_NE(exporter, nullptr); +} + +TEST_F(OtlpHttpExporterCustomClientTestPeer, RuntimeOptionsWithHttpClientCreatesExporter) +{ + OtlpHttpExporterOptions opts; + OtlpHttpExporterRuntimeOptions runtime_opts; + auto client = http_client::HttpClientTestFactory::Create(); + auto exporter = OtlpHttpExporterFactory::Create(opts, runtime_opts, std::move(client)); + ASSERT_NE(exporter, nullptr); +} + +TEST_F(OtlpHttpExporterCustomClientTestPeer, ExportCallsSendRequest) +{ + auto mock_otlp_client = GetMockOtlpHttpClient(); + auto client = mock_otlp_client.second; + auto exporter = GetExporter(std::unique_ptr{mock_otlp_client.first}); + + sdk::trace::BatchSpanProcessorOptions processor_opts; + processor_opts.max_export_batch_size = 5; + processor_opts.max_queue_size = 5; + processor_opts.schedule_delay_millis = std::chrono::milliseconds(256); + + auto processor = std::unique_ptr( + new sdk::trace::BatchSpanProcessor(std::move(exporter), processor_opts)); + auto provider = nostd::shared_ptr( + new sdk::trace::TracerProvider(std::move(processor))); + + auto tracer = provider->GetTracer("test"); + auto span = tracer->StartSpan("custom-client-span"); + span->End(); + + auto no_send_client = std::static_pointer_cast(client); + EXPECT_CALL(*std::static_pointer_cast(no_send_client->session_), + SendRequest) + .WillRepeatedly( + [](const std::shared_ptr &callback) { + http_client::nosend::Response response; + response.Finish(*callback); + }); + + provider->ForceFlush(); +} + +} // namespace otlp +} // namespace exporter +OPENTELEMETRY_END_NAMESPACE + +#endif // OPENTELEMETRY_STL_VERSION diff --git a/exporters/otlp/test/otlp_http_log_record_exporter_custom_client_test.cc b/exporters/otlp/test/otlp_http_log_record_exporter_custom_client_test.cc new file mode 100644 index 0000000000..82645ab3e1 --- /dev/null +++ b/exporters/otlp/test/otlp_http_log_record_exporter_custom_client_test.cc @@ -0,0 +1,64 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#ifndef OPENTELEMETRY_STL_VERSION + +# include "opentelemetry/exporters/otlp/otlp_http_log_record_exporter_factory.h" +# include "opentelemetry/exporters/otlp/otlp_http_log_record_exporter_options.h" +# include "opentelemetry/exporters/otlp/otlp_http_log_record_exporter_runtime_options.h" +# include "opentelemetry/ext/http/client/http_client_factory.h" +# include "opentelemetry/test_common/ext/http/client/http_client_test_factory.h" +# include "opentelemetry/test_common/ext/http/client/nosend/http_client_factory_nosend.h" + +# include + +namespace http_client = opentelemetry::ext::http::client; + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace exporter +{ +namespace otlp +{ + +using NosendHttpClientFactory = + opentelemetry::test_common::ext::http::client::nosend::HttpClientFactoryNosend; + +TEST(OtlpHttpLogRecordExporterCustomClientTest, FactoryInjectionCreatesExporter) +{ + OtlpHttpLogRecordExporterOptions opts; + auto factory = std::make_shared(); + auto exporter = OtlpHttpLogRecordExporterFactory::Create(opts, std::move(factory)); + ASSERT_NE(exporter, nullptr); +} + +TEST(OtlpHttpLogRecordExporterCustomClientTest, HttpClientInjectionCreatesExporter) +{ + OtlpHttpLogRecordExporterOptions opts; + auto client = http_client::HttpClientTestFactory::Create(); + auto exporter = OtlpHttpLogRecordExporterFactory::Create(opts, std::move(client)); + ASSERT_NE(exporter, nullptr); +} + +TEST(OtlpHttpLogRecordExporterCustomClientTest, RuntimeOptionsWithFactoryCreatesExporter) +{ + OtlpHttpLogRecordExporterOptions opts; + OtlpHttpLogRecordExporterRuntimeOptions runtime_opts; + auto factory = std::make_shared(); + auto exporter = OtlpHttpLogRecordExporterFactory::Create(opts, runtime_opts, std::move(factory)); + ASSERT_NE(exporter, nullptr); +} + +TEST(OtlpHttpLogRecordExporterCustomClientTest, RuntimeOptionsWithHttpClientCreatesExporter) +{ + OtlpHttpLogRecordExporterOptions opts; + OtlpHttpLogRecordExporterRuntimeOptions runtime_opts; + auto client = http_client::HttpClientTestFactory::Create(); + auto exporter = OtlpHttpLogRecordExporterFactory::Create(opts, runtime_opts, std::move(client)); + ASSERT_NE(exporter, nullptr); +} + +} // namespace otlp +} // namespace exporter +OPENTELEMETRY_END_NAMESPACE + +#endif // OPENTELEMETRY_STL_VERSION diff --git a/exporters/otlp/test/otlp_http_metric_exporter_custom_client_test.cc b/exporters/otlp/test/otlp_http_metric_exporter_custom_client_test.cc new file mode 100644 index 0000000000..58cbfe28e8 --- /dev/null +++ b/exporters/otlp/test/otlp_http_metric_exporter_custom_client_test.cc @@ -0,0 +1,64 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#ifndef OPENTELEMETRY_STL_VERSION + +# include "opentelemetry/exporters/otlp/otlp_http_metric_exporter_factory.h" +# include "opentelemetry/exporters/otlp/otlp_http_metric_exporter_options.h" +# include "opentelemetry/exporters/otlp/otlp_http_metric_exporter_runtime_options.h" +# include "opentelemetry/ext/http/client/http_client_factory.h" +# include "opentelemetry/test_common/ext/http/client/http_client_test_factory.h" +# include "opentelemetry/test_common/ext/http/client/nosend/http_client_factory_nosend.h" + +# include + +namespace http_client = opentelemetry::ext::http::client; + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace exporter +{ +namespace otlp +{ + +using NosendHttpClientFactory = + opentelemetry::test_common::ext::http::client::nosend::HttpClientFactoryNosend; + +TEST(OtlpHttpMetricExporterCustomClientTest, FactoryInjectionCreatesExporter) +{ + OtlpHttpMetricExporterOptions opts; + auto factory = std::make_shared(); + auto exporter = OtlpHttpMetricExporterFactory::Create(opts, std::move(factory)); + ASSERT_NE(exporter, nullptr); +} + +TEST(OtlpHttpMetricExporterCustomClientTest, HttpClientInjectionCreatesExporter) +{ + OtlpHttpMetricExporterOptions opts; + auto client = http_client::HttpClientTestFactory::Create(); + auto exporter = OtlpHttpMetricExporterFactory::Create(opts, std::move(client)); + ASSERT_NE(exporter, nullptr); +} + +TEST(OtlpHttpMetricExporterCustomClientTest, RuntimeOptionsWithFactoryCreatesExporter) +{ + OtlpHttpMetricExporterOptions opts; + OtlpHttpMetricExporterRuntimeOptions runtime_opts; + auto factory = std::make_shared(); + auto exporter = OtlpHttpMetricExporterFactory::Create(opts, runtime_opts, std::move(factory)); + ASSERT_NE(exporter, nullptr); +} + +TEST(OtlpHttpMetricExporterCustomClientTest, RuntimeOptionsWithHttpClientCreatesExporter) +{ + OtlpHttpMetricExporterOptions opts; + OtlpHttpMetricExporterRuntimeOptions runtime_opts; + auto client = http_client::HttpClientTestFactory::Create(); + auto exporter = OtlpHttpMetricExporterFactory::Create(opts, runtime_opts, std::move(client)); + ASSERT_NE(exporter, nullptr); +} + +} // namespace otlp +} // namespace exporter +OPENTELEMETRY_END_NAMESPACE + +#endif // OPENTELEMETRY_STL_VERSION diff --git a/exporters/zipkin/BUILD b/exporters/zipkin/BUILD index bfa91014d3..b79294c955 100644 --- a/exporters/zipkin/BUILD +++ b/exporters/zipkin/BUILD @@ -68,6 +68,7 @@ cc_test( deps = [ ":zipkin_exporter", ":zipkin_recordable", + "//test_common/src/http/client/nosend:http_client_nosend", "@com_google_googletest//:gtest_main", ], ) diff --git a/exporters/zipkin/CMakeLists.txt b/exporters/zipkin/CMakeLists.txt index 9955d8c4cb..b90b1177ce 100644 --- a/exporters/zipkin/CMakeLists.txt +++ b/exporters/zipkin/CMakeLists.txt @@ -23,7 +23,7 @@ set_target_version(opentelemetry_exporter_zipkin_trace) target_link_libraries( opentelemetry_exporter_zipkin_trace - PUBLIC opentelemetry_trace opentelemetry_http_client_curl + PUBLIC opentelemetry_trace opentelemetry_http_client nlohmann_json::nlohmann_json) otel_add_component( @@ -62,19 +62,22 @@ if(BUILD_TESTING) TEST_PREFIX exporter. TEST_LIST zipkin_recordable_test) - add_executable(zipkin_exporter_test test/zipkin_exporter_test.cc) - - target_link_libraries( - zipkin_exporter_test - ${GTEST_BOTH_LIBRARIES} - ${CMAKE_THREAD_LIBS_INIT} - ${GMOCK_LIB} - opentelemetry_exporter_zipkin_trace - opentelemetry_resources - CURL::libcurl) - - gtest_add_tests( - TARGET zipkin_exporter_test - TEST_PREFIX exporter. - TEST_LIST zipkin_exporter_test) + if(WITH_HTTP_CLIENT_CURL) + add_executable(zipkin_exporter_test test/zipkin_exporter_test.cc) + + target_link_libraries( + zipkin_exporter_test + ${GTEST_BOTH_LIBRARIES} + ${CMAKE_THREAD_LIBS_INIT} + ${GMOCK_LIB} + opentelemetry_exporter_zipkin_trace + opentelemetry_resources + opentelemetry_http_client_nosend + CURL::libcurl) + + gtest_add_tests( + TARGET zipkin_exporter_test + TEST_PREFIX exporter. + TEST_LIST zipkin_exporter_test) + endif() endif() # BUILD_TESTING diff --git a/exporters/zipkin/include/opentelemetry/exporters/zipkin/zipkin_exporter.h b/exporters/zipkin/include/opentelemetry/exporters/zipkin/zipkin_exporter.h index 19e7af9cf6..cb4d8f099b 100644 --- a/exporters/zipkin/include/opentelemetry/exporters/zipkin/zipkin_exporter.h +++ b/exporters/zipkin/include/opentelemetry/exporters/zipkin/zipkin_exporter.h @@ -11,6 +11,7 @@ #include "opentelemetry/exporters/zipkin/zipkin_exporter_options.h" #include "opentelemetry/ext/http/client/http_client.h" +#include "opentelemetry/ext/http/client/http_client_factory.h" #include "opentelemetry/ext/http/common/url_parser.h" #include "opentelemetry/nostd/span.h" #include "opentelemetry/sdk/common/exporter_utils.h" @@ -32,14 +33,33 @@ class ZipkinExporter final : public opentelemetry::sdk::trace::SpanExporter public: /** * Create a ZipkinExporter using all default options. + * Uses the default HTTP client factory (curl when available). */ ZipkinExporter(); /** * Create a ZipkinExporter using the given options. + * Uses the default HTTP client factory (curl when available). */ explicit ZipkinExporter(const ZipkinExporterOptions &options); + /** + * Create a ZipkinExporter using the given options and HTTP client factory. + * @param options the exporter options + * @param factory the HTTP client factory used to create the underlying HTTP client + */ + ZipkinExporter( + const ZipkinExporterOptions &options, + const std::shared_ptr &factory); + + /** + * Create a ZipkinExporter using the given options and HTTP client. + * @param options the exporter options + * @param http_client the HTTP client to be used for exporting + */ + ZipkinExporter(const ZipkinExporterOptions &options, + std::shared_ptr http_client); + /** * Create a span recordable. * @return a newly initialized Recordable object diff --git a/exporters/zipkin/include/opentelemetry/exporters/zipkin/zipkin_exporter_factory.h b/exporters/zipkin/include/opentelemetry/exporters/zipkin/zipkin_exporter_factory.h index b1564a7977..91e7ae4efc 100644 --- a/exporters/zipkin/include/opentelemetry/exporters/zipkin/zipkin_exporter_factory.h +++ b/exporters/zipkin/include/opentelemetry/exporters/zipkin/zipkin_exporter_factory.h @@ -6,6 +6,8 @@ #include #include "opentelemetry/exporters/zipkin/zipkin_exporter_options.h" +#include "opentelemetry/ext/http/client/http_client.h" +#include "opentelemetry/ext/http/client/http_client_factory.h" #include "opentelemetry/sdk/trace/exporter.h" #include "opentelemetry/version.h" @@ -31,6 +33,24 @@ class ZipkinExporterFactory */ static std::unique_ptr Create( const ZipkinExporterOptions &options); + + /** + * Create a ZipkinExporter using the given options and HTTP client factory. + * @param options the exporter options + * @param factory the HTTP client factory used to create the underlying HTTP client + */ + static std::unique_ptr Create( + const ZipkinExporterOptions &options, + const std::shared_ptr &factory); + + /** + * Create a ZipkinExporter using the given options and HTTP client. + * @param options the exporter options + * @param http_client the HTTP client to be used for exporting + */ + static std::unique_ptr Create( + const ZipkinExporterOptions &options, + std::shared_ptr http_client); }; } // namespace zipkin diff --git a/exporters/zipkin/src/zipkin_exporter.cc b/exporters/zipkin/src/zipkin_exporter.cc index f28e9f4d37..15766e7170 100644 --- a/exporters/zipkin/src/zipkin_exporter.cc +++ b/exporters/zipkin/src/zipkin_exporter.cc @@ -36,30 +36,29 @@ namespace zipkin // -------------------------------- Constructors -------------------------------- ZipkinExporter::ZipkinExporter(const ZipkinExporterOptions &options) - : options_(options), - http_client_(ext::http::client::HttpClientFactory::CreateSync()), - url_parser_(options_.endpoint) -{ - InitializeLocalEndpoint(); -} + : ZipkinExporter(options, ext::http::client::GetDefaultHttpClientFactory()) +{} ZipkinExporter::ZipkinExporter() - : options_(ZipkinExporterOptions()), - http_client_(ext::http::client::HttpClientFactory::CreateSync()), - url_parser_(options_.endpoint) + : ZipkinExporter(ZipkinExporterOptions(), ext::http::client::GetDefaultHttpClientFactory()) +{} + +ZipkinExporter::ZipkinExporter(const ZipkinExporterOptions &options, + const std::shared_ptr &factory) + : ZipkinExporter(options, factory->CreateSync()) +{} + +ZipkinExporter::ZipkinExporter(const ZipkinExporterOptions &options, + std::shared_ptr http_client) + : options_(options), http_client_(std::move(http_client)), url_parser_(options_.endpoint) { InitializeLocalEndpoint(); } ZipkinExporter::ZipkinExporter( std::shared_ptr http_client) - : options_(ZipkinExporterOptions()), - http_client_(std::move(http_client)), - url_parser_(options_.endpoint) -{ - - InitializeLocalEndpoint(); -} + : ZipkinExporter(ZipkinExporterOptions(), std::move(http_client)) +{} // ----------------------------- Exporter methods ------------------------------ diff --git a/exporters/zipkin/src/zipkin_exporter_factory.cc b/exporters/zipkin/src/zipkin_exporter_factory.cc index aaa8173cc4..d793831cd5 100644 --- a/exporters/zipkin/src/zipkin_exporter_factory.cc +++ b/exporters/zipkin/src/zipkin_exporter_factory.cc @@ -1,9 +1,13 @@ // Copyright The OpenTelemetry Authors // SPDX-License-Identifier: Apache-2.0 -#include "opentelemetry/exporters/zipkin/zipkin_exporter_factory.h" +#include + #include "opentelemetry/exporters/zipkin/zipkin_exporter.h" +#include "opentelemetry/exporters/zipkin/zipkin_exporter_factory.h" #include "opentelemetry/exporters/zipkin/zipkin_exporter_options.h" +#include "opentelemetry/ext/http/client/http_client.h" +#include "opentelemetry/ext/http/client/http_client_factory.h" #include "opentelemetry/version.h" OPENTELEMETRY_BEGIN_NAMESPACE @@ -25,6 +29,22 @@ std::unique_ptr ZipkinExporterFactory:: return exporter; } +std::unique_ptr ZipkinExporterFactory::Create( + const ZipkinExporterOptions &options, + const std::shared_ptr &factory) +{ + return std::unique_ptr( + new ZipkinExporter(options, factory)); +} + +std::unique_ptr ZipkinExporterFactory::Create( + const ZipkinExporterOptions &options, + std::shared_ptr http_client) +{ + return std::unique_ptr( + new ZipkinExporter(options, std::move(http_client))); +} + } // namespace zipkin } // namespace exporter OPENTELEMETRY_END_NAMESPACE diff --git a/exporters/zipkin/test/zipkin_exporter_test.cc b/exporters/zipkin/test/zipkin_exporter_test.cc index 36a78edf9c..086357c07f 100644 --- a/exporters/zipkin/test/zipkin_exporter_test.cc +++ b/exporters/zipkin/test/zipkin_exporter_test.cc @@ -4,11 +4,13 @@ #ifndef OPENTELEMETRY_STL_VERSION # include "opentelemetry/exporters/zipkin/zipkin_exporter.h" +# include "opentelemetry/exporters/zipkin/zipkin_exporter_factory.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/batch_span_processor_options.h" # include "opentelemetry/sdk/trace/tracer_provider.h" +# include "opentelemetry/test_common/ext/http/client/nosend/http_client_factory_nosend.h" # include "opentelemetry/trace/provider.h" # include @@ -224,6 +226,24 @@ TEST_F(ZipkinExporterTestPeer, ConfigFromEnv) # endif // NO_GETENV +TEST_F(ZipkinExporterTestPeer, FactoryInjectionCreatesExporter) +{ + ZipkinExporterOptions opts; + auto factory = std::make_shared< + opentelemetry::test_common::ext::http::client::nosend::HttpClientFactoryNosend>(); + auto exporter = ZipkinExporterFactory::Create(opts, std::move(factory)); + ASSERT_NE(exporter, nullptr); +} + +TEST_F(ZipkinExporterTestPeer, HttpClientSyncInjectionCreatesExporter) +{ + ZipkinExporterOptions opts; + auto client = + std::shared_ptr(new MockHttpClient); + auto exporter = ZipkinExporterFactory::Create(opts, std::move(client)); + ASSERT_NE(exporter, nullptr); +} + } // namespace zipkin } // namespace exporter OPENTELEMETRY_END_NAMESPACE diff --git a/ext/include/opentelemetry/ext/http/client/curl/http_client_factory_curl.h b/ext/include/opentelemetry/ext/http/client/curl/http_client_factory_curl.h new file mode 100644 index 0000000000..d23caee948 --- /dev/null +++ b/ext/include/opentelemetry/ext/http/client/curl/http_client_factory_curl.h @@ -0,0 +1,35 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once + +#include + +#include "opentelemetry/ext/http/client/http_client_factory.h" +#include "opentelemetry/version.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace ext +{ +namespace http +{ +namespace client +{ +namespace curl +{ + +class HttpCurlClientFactory : public HttpClientFactory +{ +public: + std::shared_ptr CreateSync() override; + + std::shared_ptr Create() override; + std::shared_ptr Create( + const std::shared_ptr &thread_instrumentation) override; +}; + +} // namespace curl +} // namespace client +} // namespace http +} // namespace ext +OPENTELEMETRY_END_NAMESPACE 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 c1a326b7e6..4ea37e7bac 100644 --- a/ext/include/opentelemetry/ext/http/client/http_client_factory.h +++ b/ext/include/opentelemetry/ext/http/client/http_client_factory.h @@ -3,6 +3,8 @@ #pragma once +#include + #include "opentelemetry/ext/http/client/http_client.h" #include "opentelemetry/sdk/common/thread_instrumentation.h" #include "opentelemetry/version.h" @@ -17,12 +19,26 @@ namespace client class HttpClientFactory { public: - static std::shared_ptr CreateSync(); + HttpClientFactory() = default; + HttpClientFactory(const HttpClientFactory &) = delete; + HttpClientFactory(HttpClientFactory &&) = delete; + HttpClientFactory &operator=(const HttpClientFactory &) = delete; + HttpClientFactory &operator=(HttpClientFactory &&) = delete; + + virtual ~HttpClientFactory() = default; + + virtual std::shared_ptr CreateSync() = 0; - static std::shared_ptr Create(); - static std::shared_ptr Create( - const std::shared_ptr &thread_instrumentation); + virtual std::shared_ptr Create() = 0; + virtual std::shared_ptr Create( + const std::shared_ptr &thread_instrumentation) = 0; }; + +// Returns an HttpCurlClientFactory instance when compiled with curl support. +// Not defined otherwise — binaries that link any exporter must provide their +// own definition or use the factory/client constructor overloads exclusively. +std::shared_ptr GetDefaultHttpClientFactory(); + } // namespace client } // namespace http } // namespace ext diff --git a/ext/src/CMakeLists.txt b/ext/src/CMakeLists.txt index b3d45a719d..d984ce6a61 100644 --- a/ext/src/CMakeLists.txt +++ b/ext/src/CMakeLists.txt @@ -5,6 +5,24 @@ if(WITH_HTTP_CLIENT_CURL) add_subdirectory(http/client/curl) endif() +if(WITH_HTTP_CLIENT_CURL + OR WITH_OTLP_HTTP + OR WITH_ELASTICSEARCH + OR WITH_ZIPKIN + OR BUILD_W3CTRACECONTEXT_TEST + OR WITH_EXAMPLES_HTTP) + add_library(opentelemetry_http_client INTERFACE) + target_link_libraries(opentelemetry_http_client INTERFACE opentelemetry_ext) + if(WITH_HTTP_CLIENT_CURL) + target_link_libraries(opentelemetry_http_client + INTERFACE opentelemetry_http_client_curl) + endif() + + set_target_properties(opentelemetry_http_client PROPERTIES EXPORT_NAME + http_client) + otel_add_component(COMPONENT ext_http TARGETS opentelemetry_http_client) +endif() + if(DEFINED OPENTELEMETRY_BUILD_DLL) add_subdirectory(dll) endif() diff --git a/ext/src/http/client/curl/http_client_factory_curl.cc b/ext/src/http/client/curl/http_client_factory_curl.cc index b339c2d93c..b0c5f131b4 100644 --- a/ext/src/http/client/curl/http_client_factory_curl.cc +++ b/ext/src/http/client/curl/http_client_factory_curl.cc @@ -4,24 +4,30 @@ #include #include "opentelemetry/ext/http/client/curl/http_client_curl.h" -#include "opentelemetry/ext/http/client/http_client.h" +#include "opentelemetry/ext/http/client/curl/http_client_factory_curl.h" #include "opentelemetry/ext/http/client/http_client_factory.h" -#include "opentelemetry/sdk/common/thread_instrumentation.h" namespace http_client = opentelemetry::ext::http::client; -std::shared_ptr http_client::HttpClientFactory::Create() +std::shared_ptr http_client::curl::HttpCurlClientFactory::Create() { return std::make_shared(); } -std::shared_ptr http_client::HttpClientFactory::Create( - const std::shared_ptr &thread_instrumentation) +std::shared_ptr http_client::curl::HttpCurlClientFactory::Create( + const std::shared_ptr + &thread_instrumentation) { return std::make_shared(thread_instrumentation); } -std::shared_ptr http_client::HttpClientFactory::CreateSync() +std::shared_ptr http_client::curl::HttpCurlClientFactory::CreateSync() { return std::make_shared(); } + +std::shared_ptr http_client::GetDefaultHttpClientFactory() +{ + static auto instance = std::make_shared(); + return instance; +} diff --git a/ext/test/http/curl_http_test.cc b/ext/test/http/curl_http_test.cc index 284a69fd51..d69fa32843 100644 --- a/ext/test/http/curl_http_test.cc +++ b/ext/test/http/curl_http_test.cc @@ -275,7 +275,7 @@ TEST_F(BasicCurlHttpTests, HttpResponse) TEST_F(BasicCurlHttpTests, SendGetRequest) { received_requests_.clear(); - auto session_manager = http_client::HttpClientFactory::Create(); + auto session_manager = http_client::GetDefaultHttpClientFactory()->Create(); EXPECT_TRUE(session_manager != nullptr); auto session = session_manager->CreateSession("http://127.0.0.1:19000"); @@ -292,7 +292,7 @@ TEST_F(BasicCurlHttpTests, SendGetRequest) TEST_F(BasicCurlHttpTests, SendPostRequest) { received_requests_.clear(); - auto session_manager = http_client::HttpClientFactory::Create(); + auto session_manager = http_client::GetDefaultHttpClientFactory()->Create(); EXPECT_TRUE(session_manager != nullptr); auto session = session_manager->CreateSession("http://127.0.0.1:19000"); @@ -318,7 +318,7 @@ TEST_F(BasicCurlHttpTests, SendPostRequest) TEST_F(BasicCurlHttpTests, RequestTimeout) { received_requests_.clear(); - auto session_manager = http_client::HttpClientFactory::Create(); + auto session_manager = http_client::GetDefaultHttpClientFactory()->Create(); EXPECT_TRUE(session_manager != nullptr); auto session = session_manager->CreateSession("192.0.2.0:19000"); // RFC 5737 TEST-NET-1 @@ -654,7 +654,7 @@ TEST_F(BasicCurlHttpTests, FinishInAsyncCallback) TEST_F(BasicCurlHttpTests, ElegantQuitQuick) { - auto http_client = http_client::HttpClientFactory::Create(); + auto http_client = http_client::GetDefaultHttpClientFactory()->Create(); std::static_pointer_cast(http_client)->MaybeSpawnBackgroundThread(); // start background first, then test it could wakeup auto session = http_client->CreateSession("http://127.0.0.1:19000/get/"); @@ -720,7 +720,7 @@ struct GzipEventHandler : public CustomEventHandler TEST_F(BasicCurlHttpTests, GzipCompressibleData) { received_requests_.clear(); - auto session_manager = http_client::HttpClientFactory::Create(); + auto session_manager = http_client::GetDefaultHttpClientFactory()->Create(); EXPECT_TRUE(session_manager != nullptr); auto session = session_manager->CreateSession("http://127.0.0.1:19000"); @@ -754,7 +754,7 @@ TEST_F(BasicCurlHttpTests, GzipCompressibleData) TEST_F(BasicCurlHttpTests, GzipIncompressibleData) { received_requests_.clear(); - auto session_manager = http_client::HttpClientFactory::Create(); + auto session_manager = http_client::GetDefaultHttpClientFactory()->Create(); EXPECT_TRUE(session_manager != nullptr); auto session = session_manager->CreateSession("http://127.0.0.1:19000"); diff --git a/ext/test/w3c_tracecontext_http_test_server/CMakeLists.txt b/ext/test/w3c_tracecontext_http_test_server/CMakeLists.txt index 095da7ad80..064b186037 100644 --- a/ext/test/w3c_tracecontext_http_test_server/CMakeLists.txt +++ b/ext/test/w3c_tracecontext_http_test_server/CMakeLists.txt @@ -1,9 +1,11 @@ # Copyright The OpenTelemetry Authors # SPDX-License-Identifier: Apache-2.0 -add_executable(w3c_tracecontext_http_test_server main.cc) -target_link_libraries( - w3c_tracecontext_http_test_server - PRIVATE ${CMAKE_THREAD_LIBS_INIT} opentelemetry_trace - opentelemetry_http_client_curl opentelemetry_exporter_ostream_span - CURL::libcurl nlohmann_json::nlohmann_json) +if(WITH_HTTP_CLIENT_CURL) + add_executable(w3c_tracecontext_http_test_server main.cc) + target_link_libraries( + w3c_tracecontext_http_test_server + PRIVATE ${CMAKE_THREAD_LIBS_INIT} opentelemetry_trace + opentelemetry_http_client_curl opentelemetry_exporter_ostream_span + CURL::libcurl nlohmann_json::nlohmann_json) +endif() diff --git a/install/test/cmake/component_tests/ext_http/CMakeLists.txt b/install/test/cmake/component_tests/ext_http/CMakeLists.txt new file mode 100644 index 0000000000..a4997abf3c --- /dev/null +++ b/install/test/cmake/component_tests/ext_http/CMakeLists.txt @@ -0,0 +1,16 @@ +# Copyright The OpenTelemetry Authors +# SPDX-License-Identifier: Apache-2.0 + +cmake_minimum_required(VERSION 3.16) +project(opentelemetry-cpp-ext_http-install-test LANGUAGES CXX) + +find_package(opentelemetry-cpp REQUIRED COMPONENTS ext_http) + +find_package(GTest CONFIG REQUIRED) +include(GoogleTest) + +add_executable(ext_http_test ${INSTALL_TEST_SRC_DIR}/test_ext_http.cc) +target_link_libraries(ext_http_test PRIVATE opentelemetry-cpp::http_client + GTest::gtest GTest::gtest_main) + +gtest_discover_tests(ext_http_test) diff --git a/install/test/cmake/examples_test/CMakeLists.txt b/install/test/cmake/examples_test/CMakeLists.txt index e9c11f5063..798a9124f2 100644 --- a/install/test/cmake/examples_test/CMakeLists.txt +++ b/install/test/cmake/examples_test/CMakeLists.txt @@ -48,6 +48,7 @@ endif() if("ext_http_curl" IN_LIST OPENTELEMETRY_CPP_COMPONENTS_INSTALLED) set(WITH_EXAMPLES_HTTP ON) + set(WITH_HTTP_CLIENT_CURL ON) endif() if("configuration" IN_LIST OPENTELEMETRY_CPP_COMPONENTS_INSTALLED) diff --git a/install/test/src/test_ext_http.cc b/install/test/src/test_ext_http.cc new file mode 100644 index 0000000000..edce76bc08 --- /dev/null +++ b/install/test/src/test_ext_http.cc @@ -0,0 +1,13 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#include + +#include + +TEST(ExtHttpInstall, Headers) +{ + using opentelemetry::ext::http::client::HttpClient; + using opentelemetry::ext::http::client::HttpClientSync; + SUCCEED(); +} diff --git a/install/test/src/test_ext_http_curl.cc b/install/test/src/test_ext_http_curl.cc index 799ee4732a..e3b2316ddc 100644 --- a/install/test/src/test_ext_http_curl.cc +++ b/install/test/src/test_ext_http_curl.cc @@ -8,6 +8,6 @@ TEST(ExtHttpCurlInstall, HttpClient) { - auto client = opentelemetry::ext::http::client::HttpClientFactory::Create(); + auto client = opentelemetry::ext::http::client::GetDefaultHttpClientFactory()->Create(); ASSERT_TRUE(client != nullptr); } \ No newline at end of file diff --git a/test_common/include/opentelemetry/test_common/ext/http/client/nosend/http_client_factory_nosend.h b/test_common/include/opentelemetry/test_common/ext/http/client/nosend/http_client_factory_nosend.h new file mode 100644 index 0000000000..7a71b24db5 --- /dev/null +++ b/test_common/include/opentelemetry/test_common/ext/http/client/nosend/http_client_factory_nosend.h @@ -0,0 +1,48 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once + +#include + +#include "opentelemetry/ext/http/client/http_client_factory.h" +#include "opentelemetry/test_common/ext/http/client/nosend/http_client_nosend.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace test_common +{ +namespace ext +{ +namespace http +{ +namespace client +{ +namespace nosend +{ + +class HttpClientFactoryNosend : public opentelemetry::ext::http::client::HttpClientFactory +{ +public: + std::shared_ptr Create() override + { + return std::make_shared(); + } + + std::shared_ptr Create( + const std::shared_ptr &) override + { + return std::make_shared(); + } + + std::shared_ptr CreateSync() override + { + return nullptr; + } +}; + +} // namespace nosend +} // namespace client +} // namespace http +} // namespace ext +} // namespace test_common +OPENTELEMETRY_END_NAMESPACE