Skip to content

Commit 703a2a5

Browse files
authored
Http client add url support (#833)
1 parent 3741a8f commit 703a2a5

File tree

6 files changed

+27
-26
lines changed

6 files changed

+27
-26
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Increment the:
1515

1616
## [Unreleased]
1717

18+
* [INSTRUMENTATION] HTTPClient: Change support for full URL argument ([#833](https://github.com/open-telemetry/opentelemetry-cpp/pull/833))
1819
* [EXPORTER] Add OTLP/HTTP+JSON Protocol exporter ([#810](https://github.com/open-telemetry/opentelemetry-cpp/pull/810))
1920
* [EXPORTER] Rename `OtlpExporter` to `OtlpGrpcExporter`, rename `otlp_exporter.h` to `otlp_grpc_exporter.h` ([#810](https://github.com/open-telemetry/opentelemetry-cpp/pull/810))
2021

exporters/elasticsearch/src/es_log_exporter.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ sdk::common::ExportResult ElasticsearchLogExporter::Export(
140140
}
141141

142142
// Create a connection to the ElasticSearch instance
143-
auto session = http_client_->CreateSession(options_.host_, options_.port_);
143+
auto session = http_client_->CreateSession(options_.host_ + std::to_string(options_.port_));
144144
auto request = session->CreateRequest();
145145

146146
// Populate the request with headers and methods

ext/include/opentelemetry/ext/http/client/curl/http_client_curl.h

+14-13
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#include "http_operation_curl.h"
77
#include "opentelemetry/ext/http/client/http_client.h"
8+
#include "opentelemetry/ext/http/common/url_parser.h"
89
#include "opentelemetry/version.h"
910

1011
#include <map>
@@ -115,18 +116,13 @@ class HttpClient;
115116
class Session : public http_client::Session
116117
{
117118
public:
118-
Session(HttpClient &http_client, const std::string &host, uint16_t port = 80)
119+
Session(HttpClient &http_client,
120+
std::string scheme = "http",
121+
const std::string &host = "",
122+
uint16_t port = 80)
119123
: http_client_(http_client), is_session_active_(false)
120124
{
121-
if (host.rfind("http://", 0) != 0 && host.rfind("https://", 0) != 0)
122-
{
123-
host_ = "http://" + host; // TODO - https support
124-
}
125-
else
126-
{
127-
host_ = host;
128-
}
129-
host_ += ":" + std::to_string(port) + "/";
125+
host_ = scheme + "://" + host + ":" + std::to_string(port) + "/";
130126
}
131127

132128
std::shared_ptr<http_client::Request> CreateRequest() noexcept override
@@ -249,10 +245,15 @@ class HttpClient : public http_client::HttpClient
249245
// The call (curl_global_init) is not thread safe. Ensure this is called only once.
250246
HttpClient() : next_session_id_{0} { curl_global_init(CURL_GLOBAL_ALL); }
251247

252-
std::shared_ptr<http_client::Session> CreateSession(nostd::string_view host,
253-
uint16_t port = 80) noexcept override
248+
std::shared_ptr<http_client::Session> CreateSession(nostd::string_view url) noexcept override
254249
{
255-
auto session = std::make_shared<Session>(*this, std::string(host), port);
250+
auto parsedUrl = common::UrlParser(std::string(url));
251+
if (!parsedUrl.success_)
252+
{
253+
return std::make_shared<Session>(*this);
254+
}
255+
auto session =
256+
std::make_shared<Session>(*this, parsedUrl.scheme_, parsedUrl.host_, parsedUrl.port_);
256257
auto session_id = ++next_session_id_;
257258
session->SetId(session_id);
258259
sessions_.insert({session_id, session});

ext/include/opentelemetry/ext/http/client/http_client.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ Async Request:
4545
};
4646
4747
HttpClient httpClient; // implementer can provide singleton implementation for it
48-
auto session = httpClient.createSession("localhost", 8000);
48+
auto session = httpClient.createSession("localhost" + 8000);
4949
auto request = session->CreateRequest();
5050
request->AddHeader(..);
5151
SimpleResponseHandler res_handler;
@@ -226,9 +226,9 @@ class Session
226226
class HttpClient
227227
{
228228
public:
229-
virtual std::shared_ptr<Session> CreateSession(nostd::string_view host,
230-
uint16_t port = 80) noexcept = 0;
231-
virtual bool CancelAllSessions() noexcept = 0;
229+
virtual std::shared_ptr<Session> CreateSession(nostd::string_view url) noexcept = 0;
230+
231+
virtual bool CancelAllSessions() noexcept = 0;
232232

233233
virtual bool FinishAllSessions() noexcept = 0;
234234

ext/test/http/curl_http_test.cc

+6-7
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ TEST_F(BasicCurlHttpTests, SendGetRequest)
191191
auto session_manager = http_client::HttpClientFactory::Create();
192192
EXPECT_TRUE(session_manager != nullptr);
193193

194-
auto session = session_manager->CreateSession("127.0.0.1", HTTP_PORT);
194+
auto session = session_manager->CreateSession("http://127.0.0.1:19000");
195195
auto request = session->CreateRequest();
196196
request->SetUri("get/");
197197
GetEventHandler *handler = new GetEventHandler();
@@ -208,7 +208,7 @@ TEST_F(BasicCurlHttpTests, SendPostRequest)
208208
auto session_manager = http_client::HttpClientFactory::Create();
209209
EXPECT_TRUE(session_manager != nullptr);
210210

211-
auto session = session_manager->CreateSession("127.0.0.1", HTTP_PORT);
211+
auto session = session_manager->CreateSession("http://127.0.0.1:19000");
212212
auto request = session->CreateRequest();
213213
request->SetUri("post/");
214214
request->SetMethod(http_client::Method::Post);
@@ -235,8 +235,7 @@ TEST_F(BasicCurlHttpTests, RequestTimeout)
235235
auto session_manager = http_client::HttpClientFactory::Create();
236236
EXPECT_TRUE(session_manager != nullptr);
237237

238-
auto session =
239-
session_manager->CreateSession("222.222.222.200", HTTP_PORT); // Non Existing address
238+
auto session = session_manager->CreateSession("222.222.222.200:19000"); // Non Existing address
240239
auto request = session->CreateRequest();
241240
request->SetUri("get/");
242241
GetEventHandler *handler = new GetEventHandler();
@@ -309,14 +308,14 @@ TEST_F(BasicCurlHttpTests, GetBaseUri)
309308
{
310309
curl::HttpClient session_manager;
311310

312-
auto session = session_manager.CreateSession("127.0.0.1", 80);
311+
auto session = session_manager.CreateSession("127.0.0.1:80");
313312
ASSERT_EQ(std::static_pointer_cast<curl::Session>(session)->GetBaseUri(), "http://127.0.0.1:80/");
314313

315-
session = session_manager.CreateSession("https://127.0.0.1", 443);
314+
session = session_manager.CreateSession("https://127.0.0.1:443");
316315
ASSERT_EQ(std::static_pointer_cast<curl::Session>(session)->GetBaseUri(),
317316
"https://127.0.0.1:443/");
318317

319-
session = session_manager.CreateSession("http://127.0.0.1", 31339);
318+
session = session_manager.CreateSession("http://127.0.0.1:31339");
320319
ASSERT_EQ(std::static_pointer_cast<curl::Session>(session)->GetBaseUri(),
321320
"http://127.0.0.1:31339/");
322321
}

ext/test/w3c_tracecontext_test/main.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ void send_request(opentelemetry::ext::http::client::curl::HttpClient &client,
105105

106106
Uri uri{url};
107107

108-
auto session = client.CreateSession(uri.host, uri.port);
108+
auto session = client.CreateSession(url);
109109
auto request = session->CreateRequest();
110110

111111
request->SetMethod(opentelemetry::ext::http::client::Method::Post);

0 commit comments

Comments
 (0)