diff --git a/CHANGELOG.md b/CHANGELOG.md index 1c4f68df9a..afdb4ac1a3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ Increment the: ## [Unreleased] +* [INSTRUMENTATION] HTTPClient: Change support for full URL argument ([#833](https://github.com/open-telemetry/opentelemetry-cpp/pull/833)) * [EXPORTER] Add OTLP/HTTP+JSON Protocol exporter ([#810](https://github.com/open-telemetry/opentelemetry-cpp/pull/810)) * [EXPORTER] Rename `OtlpExporter` to `OtlpGrpcExporter`, rename `otlp_exporter.h` to `otlp_grpc_exporter.h` ([#810](https://github.com/open-telemetry/opentelemetry-cpp/pull/810)) diff --git a/exporters/elasticsearch/src/es_log_exporter.cc b/exporters/elasticsearch/src/es_log_exporter.cc index 28d59ac4f8..c7201db907 100644 --- a/exporters/elasticsearch/src/es_log_exporter.cc +++ b/exporters/elasticsearch/src/es_log_exporter.cc @@ -140,7 +140,7 @@ sdk::common::ExportResult ElasticsearchLogExporter::Export( } // Create a connection to the ElasticSearch instance - auto session = http_client_->CreateSession(options_.host_, options_.port_); + auto session = http_client_->CreateSession(options_.host_ + std::to_string(options_.port_)); auto request = session->CreateRequest(); // Populate the request with headers and methods diff --git a/ext/include/opentelemetry/ext/http/client/curl/http_client_curl.h b/ext/include/opentelemetry/ext/http/client/curl/http_client_curl.h index ffdb10985a..126b09acf8 100644 --- a/ext/include/opentelemetry/ext/http/client/curl/http_client_curl.h +++ b/ext/include/opentelemetry/ext/http/client/curl/http_client_curl.h @@ -5,6 +5,7 @@ #include "http_operation_curl.h" #include "opentelemetry/ext/http/client/http_client.h" +#include "opentelemetry/ext/http/common/url_parser.h" #include "opentelemetry/version.h" #include @@ -115,18 +116,13 @@ class HttpClient; class Session : public http_client::Session { public: - Session(HttpClient &http_client, const std::string &host, uint16_t port = 80) + Session(HttpClient &http_client, + std::string scheme = "http", + const std::string &host = "", + uint16_t port = 80) : http_client_(http_client), is_session_active_(false) { - if (host.rfind("http://", 0) != 0 && host.rfind("https://", 0) != 0) - { - host_ = "http://" + host; // TODO - https support - } - else - { - host_ = host; - } - host_ += ":" + std::to_string(port) + "/"; + host_ = scheme + "://" + host + ":" + std::to_string(port) + "/"; } std::shared_ptr CreateRequest() noexcept override @@ -249,10 +245,15 @@ class HttpClient : public http_client::HttpClient // The call (curl_global_init) is not thread safe. Ensure this is called only once. HttpClient() : next_session_id_{0} { curl_global_init(CURL_GLOBAL_ALL); } - std::shared_ptr CreateSession(nostd::string_view host, - uint16_t port = 80) noexcept override + std::shared_ptr CreateSession(nostd::string_view url) noexcept override { - auto session = std::make_shared(*this, std::string(host), port); + auto parsedUrl = common::UrlParser(std::string(url)); + if (!parsedUrl.success_) + { + return std::make_shared(*this); + } + auto session = + std::make_shared(*this, parsedUrl.scheme_, parsedUrl.host_, parsedUrl.port_); auto session_id = ++next_session_id_; session->SetId(session_id); sessions_.insert({session_id, session}); diff --git a/ext/include/opentelemetry/ext/http/client/http_client.h b/ext/include/opentelemetry/ext/http/client/http_client.h index 88938dcda5..4e3e9845e0 100644 --- a/ext/include/opentelemetry/ext/http/client/http_client.h +++ b/ext/include/opentelemetry/ext/http/client/http_client.h @@ -45,7 +45,7 @@ Async Request: }; HttpClient httpClient; // implementer can provide singleton implementation for it - auto session = httpClient.createSession("localhost", 8000); + auto session = httpClient.createSession("localhost" + 8000); auto request = session->CreateRequest(); request->AddHeader(..); SimpleResponseHandler res_handler; @@ -226,9 +226,9 @@ class Session class HttpClient { public: - virtual std::shared_ptr CreateSession(nostd::string_view host, - uint16_t port = 80) noexcept = 0; - virtual bool CancelAllSessions() noexcept = 0; + virtual std::shared_ptr CreateSession(nostd::string_view url) noexcept = 0; + + virtual bool CancelAllSessions() noexcept = 0; virtual bool FinishAllSessions() noexcept = 0; diff --git a/ext/test/http/curl_http_test.cc b/ext/test/http/curl_http_test.cc index a0678fd9ab..31c0d06570 100644 --- a/ext/test/http/curl_http_test.cc +++ b/ext/test/http/curl_http_test.cc @@ -191,7 +191,7 @@ TEST_F(BasicCurlHttpTests, SendGetRequest) auto session_manager = http_client::HttpClientFactory::Create(); EXPECT_TRUE(session_manager != nullptr); - auto session = session_manager->CreateSession("127.0.0.1", HTTP_PORT); + auto session = session_manager->CreateSession("http://127.0.0.1:19000"); auto request = session->CreateRequest(); request->SetUri("get/"); GetEventHandler *handler = new GetEventHandler(); @@ -208,7 +208,7 @@ TEST_F(BasicCurlHttpTests, SendPostRequest) auto session_manager = http_client::HttpClientFactory::Create(); EXPECT_TRUE(session_manager != nullptr); - auto session = session_manager->CreateSession("127.0.0.1", HTTP_PORT); + auto session = session_manager->CreateSession("http://127.0.0.1:19000"); auto request = session->CreateRequest(); request->SetUri("post/"); request->SetMethod(http_client::Method::Post); @@ -235,8 +235,7 @@ TEST_F(BasicCurlHttpTests, RequestTimeout) auto session_manager = http_client::HttpClientFactory::Create(); EXPECT_TRUE(session_manager != nullptr); - auto session = - session_manager->CreateSession("222.222.222.200", HTTP_PORT); // Non Existing address + auto session = session_manager->CreateSession("222.222.222.200:19000"); // Non Existing address auto request = session->CreateRequest(); request->SetUri("get/"); GetEventHandler *handler = new GetEventHandler(); @@ -309,14 +308,14 @@ TEST_F(BasicCurlHttpTests, GetBaseUri) { curl::HttpClient session_manager; - auto session = session_manager.CreateSession("127.0.0.1", 80); + auto session = session_manager.CreateSession("127.0.0.1:80"); ASSERT_EQ(std::static_pointer_cast(session)->GetBaseUri(), "http://127.0.0.1:80/"); - session = session_manager.CreateSession("https://127.0.0.1", 443); + session = session_manager.CreateSession("https://127.0.0.1:443"); ASSERT_EQ(std::static_pointer_cast(session)->GetBaseUri(), "https://127.0.0.1:443/"); - session = session_manager.CreateSession("http://127.0.0.1", 31339); + session = session_manager.CreateSession("http://127.0.0.1:31339"); ASSERT_EQ(std::static_pointer_cast(session)->GetBaseUri(), "http://127.0.0.1:31339/"); } diff --git a/ext/test/w3c_tracecontext_test/main.cc b/ext/test/w3c_tracecontext_test/main.cc index c2c834f317..bc0f9bd65d 100644 --- a/ext/test/w3c_tracecontext_test/main.cc +++ b/ext/test/w3c_tracecontext_test/main.cc @@ -105,7 +105,7 @@ void send_request(opentelemetry::ext::http::client::curl::HttpClient &client, Uri uri{url}; - auto session = client.CreateSession(uri.host, uri.port); + auto session = client.CreateSession(url); auto request = session->CreateRequest(); request->SetMethod(opentelemetry::ext::http::client::Method::Post);