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

Remove scheme from OTLP endpoint before passing to gRPC #988

Merged
merged 5 commits into from
Sep 21, 2021
Merged
Changes from 2 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
25 changes: 23 additions & 2 deletions exporters/otlp/src/otlp_grpc_exporter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,27 @@ std::unique_ptr<proto::collector::trace::v1::TraceService::Stub> MakeServiceStub
{
std::shared_ptr<grpc::Channel> channel;

//
// Scheme is allowed in OTLP endpoint definition, but is not allowed for creating gRPC channel.
// Passing URI with scheme to grpc::CreateChannel could resolve the endpoint to some unexpected
// address, so remove schme from the endpoint before passing it to gRPC.
//
std::string grpc_target;
const char *http_scheme = "http://";
const char *https_scheme = "https://";
if (options.endpoint.rfind(http_scheme, 0) == 0)
{
grpc_target = options.endpoint.substr(strlen(http_scheme));
}
else if (options.endpoint.rfind(https_scheme, 0) == 0)
{
grpc_target = options.endpoint.substr(strlen(https_scheme));
}
else
{
grpc_target = options.endpoint;
}

Copy link
Member

@lalitb lalitb Sep 21, 2021

Choose a reason for hiding this comment

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

Nit, we can also use UrlParser :

UrlParser url(options.endpoint);
if (url.success_) {
  grpc_target = url.host_ + ":" + url.port_;
}

This will validate that url is in correct format, and remove path and query_string if added ?

if (options.use_ssl_credentials)
{
grpc::SslCredentialsOptions ssl_opts;
Expand All @@ -75,11 +96,11 @@ std::unique_ptr<proto::collector::trace::v1::TraceService::Stub> MakeServiceStub
{
ssl_opts.pem_root_certs = get_file_contents((options.ssl_credentials_cacert_path).c_str());
}
channel = grpc::CreateChannel(options.endpoint, grpc::SslCredentials(ssl_opts));
channel = grpc::CreateChannel(grpc_target, grpc::SslCredentials(ssl_opts));
}
else
{
channel = grpc::CreateChannel(options.endpoint, grpc::InsecureChannelCredentials());
channel = grpc::CreateChannel(grpc_target, grpc::InsecureChannelCredentials());
}
return proto::collector::trace::v1::TraceService::NewStub(channel);
}
Expand Down