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
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions exporters/otlp/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ cc_library(
strip_include_prefix = "include",
deps = [
":otlp_recordable",
"//ext:headers",
"//sdk/src/trace",

# For gRPC
Expand Down
21 changes: 19 additions & 2 deletions exporters/otlp/src/otlp_grpc_exporter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "opentelemetry/exporters/otlp/otlp_grpc_exporter.h"
#include "opentelemetry/exporters/otlp/otlp_recordable.h"
#include "opentelemetry/ext/http/common/url_parser.h"
#include "opentelemetry/sdk_config.h"

#include <grpcpp/grpcpp.h>
Expand Down Expand Up @@ -64,6 +65,22 @@ 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.
//

ext::http::common::UrlParser url(options.endpoint);
if (!url.success_)
{
OTEL_INTERNAL_LOG_ERROR("[OTLP Exporter] invalid endpoint: " << options.endpoint);

return nullptr;
}

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 ?

std::string grpc_target = url.host_ + ":" + std::to_string(static_cast<int>(url.port_));

if (options.use_ssl_credentials)
{
grpc::SslCredentialsOptions ssl_opts;
Expand All @@ -75,11 +92,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