Skip to content

Commit

Permalink
Http client add url support (#833)
Browse files Browse the repository at this point in the history
  • Loading branch information
ohamuy authored Jun 11, 2021
1 parent 3741a8f commit 703a2a5
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 26 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
2 changes: 1 addition & 1 deletion exporters/elasticsearch/src/es_log_exporter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 14 additions & 13 deletions ext/include/opentelemetry/ext/http/client/curl/http_client_curl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <map>
Expand Down Expand Up @@ -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<http_client::Request> CreateRequest() noexcept override
Expand Down Expand Up @@ -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<http_client::Session> CreateSession(nostd::string_view host,
uint16_t port = 80) noexcept override
std::shared_ptr<http_client::Session> CreateSession(nostd::string_view url) noexcept override
{
auto session = std::make_shared<Session>(*this, std::string(host), port);
auto parsedUrl = common::UrlParser(std::string(url));
if (!parsedUrl.success_)
{
return std::make_shared<Session>(*this);
}
auto session =
std::make_shared<Session>(*this, parsedUrl.scheme_, parsedUrl.host_, parsedUrl.port_);
auto session_id = ++next_session_id_;
session->SetId(session_id);
sessions_.insert({session_id, session});
Expand Down
8 changes: 4 additions & 4 deletions ext/include/opentelemetry/ext/http/client/http_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -226,9 +226,9 @@ class Session
class HttpClient
{
public:
virtual std::shared_ptr<Session> CreateSession(nostd::string_view host,
uint16_t port = 80) noexcept = 0;
virtual bool CancelAllSessions() noexcept = 0;
virtual std::shared_ptr<Session> CreateSession(nostd::string_view url) noexcept = 0;

virtual bool CancelAllSessions() noexcept = 0;

virtual bool FinishAllSessions() noexcept = 0;

Expand Down
13 changes: 6 additions & 7 deletions ext/test/http/curl_http_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
Expand All @@ -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();
Expand Down Expand Up @@ -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<curl::Session>(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<curl::Session>(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<curl::Session>(session)->GetBaseUri(),
"http://127.0.0.1:31339/");
}
2 changes: 1 addition & 1 deletion ext/test/w3c_tracecontext_test/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 703a2a5

Please sign in to comment.