Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Http client add url support #833

Merged
merged 17 commits into from
Jun 11, 2021
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ Increment the:

## [Unreleased]

* [INSTRUMENTATION] HTTPClient: Add 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))


## [1.0.0-rc1] 2021-06-04

* [BUILD] Enable Jaeger exporter build in Windows ([#815](https://github.com/open-telemetry/opentelemetry-cpp/pull/815))
Expand Down
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 @@ -120,7 +121,7 @@ class Session : public http_client::Session
{
if (host.rfind("http://", 0) != 0 && host.rfind("https://", 0) != 0)
{
host_ = "http://" + host; // TODO - https support
host_ = "http://" + host;
}
else
{
Expand Down Expand Up @@ -259,6 +260,20 @@ class HttpClient : public http_client::HttpClient
return session;
}

std::shared_ptr<http_client::Session> CreateSession(nostd::string_view url) noexcept override
{
auto parsedUrl = common::UrlParser(std::string(url));
if (!parsedUrl.success_)
{
return std::make_shared<Session>(*this, "", 80);
}
auto session = std::make_shared<Session>(*this, parsedUrl.host_, parsedUrl.port_);
auto session_id = ++next_session_id_;
session->SetId(session_id);
sessions_.insert({session_id, session});
return session;
}

bool CancelAllSessions() noexcept override
{
for (auto &session : sessions_)
Expand Down
7 changes: 5 additions & 2 deletions ext/include/opentelemetry/ext/http/client/http_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,11 @@ class HttpClient
{
public:
virtual std::shared_ptr<Session> CreateSession(nostd::string_view host,
uint16_t port = 80) noexcept = 0;
virtual bool CancelAllSessions() noexcept = 0;
uint16_t port) noexcept = 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we still need the port parameter here, if the URL itself can contain scheme and port?
Should we rename the host parameter to url or uri ?


virtual std::shared_ptr<Session> CreateSession(nostd::string_view url) noexcept = 0;
Copy link
Contributor

@maxgolov maxgolov Jun 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I see what you are doing. But I am not sure - maybe we should just deprecate the old method? I think this new one is much better. If we look at Javascript API for the open method, it only requires the URL parameter. In Java - this is also completely outsourced to java.net.URL . It's even more logical to assume that the session itself accepts URL object or string, rather than trying to implement multi-parameter parsing from host, port. Because it's rather [ scheme, host, port, location ] than simply [ host, port ] pair. It'd be best we just collapse all of it into single string parameter, then parse it using URL class.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes exactly, I just overloaded the function because that is what the @lalitb (the original issue creator) said might have been good. If its alright I can just simply remove the old parameter and just leave the new one since its also consistent with the GET and POST commands. But that is definitely a decision for the maintainers so when they give their input we can see whats the best course of action. @lalitb @reyang

Copy link
Member

@lalitb lalitb Jun 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it makes sense to remove the original method as @maxgolov suggested.


virtual bool CancelAllSessions() noexcept = 0;

virtual bool FinishAllSessions() noexcept = 0;

Expand Down
2 changes: 1 addition & 1 deletion 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 Down