From fad6d144b2d627177d6220607ac77b715cdc3241 Mon Sep 17 00:00:00 2001 From: Tom Tan Date: Tue, 21 Sep 2021 01:59:11 -0700 Subject: [PATCH 1/3] Remove scheme from OTLP endpoint before passing to gRPC --- exporters/otlp/src/otlp_grpc_exporter.cc | 25 ++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/exporters/otlp/src/otlp_grpc_exporter.cc b/exporters/otlp/src/otlp_grpc_exporter.cc index dec65fadda..b6ee1cd3f4 100644 --- a/exporters/otlp/src/otlp_grpc_exporter.cc +++ b/exporters/otlp/src/otlp_grpc_exporter.cc @@ -60,6 +60,27 @@ std::unique_ptr MakeServiceStub { std::shared_ptr 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; + } + if (options.use_ssl_credentials) { grpc::SslCredentialsOptions ssl_opts; @@ -71,11 +92,11 @@ std::unique_ptr 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); } From da3c9dd7a3db66d9854521a9d4f4d6f31fd25551 Mon Sep 17 00:00:00 2001 From: Tom Tan Date: Tue, 21 Sep 2021 13:49:59 -0700 Subject: [PATCH 2/3] Address feedback and adopt UrlPath. --- exporters/otlp/src/otlp_grpc_exporter.cc | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/exporters/otlp/src/otlp_grpc_exporter.cc b/exporters/otlp/src/otlp_grpc_exporter.cc index d097d7540a..c4121d7dd8 100644 --- a/exporters/otlp/src/otlp_grpc_exporter.cc +++ b/exporters/otlp/src/otlp_grpc_exporter.cc @@ -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 @@ -67,24 +68,19 @@ std::unique_ptr MakeServiceStub // // 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. + // address. // - 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 + + ext::http::common::UrlParser url(options.endpoint); + if (!url.success_) { - grpc_target = options.endpoint; + OTEL_INTERNAL_LOG_ERROR("[OTLP Exporter] invalid endpoint: " << options.endpoint); + + return nullptr; } + std::string grpc_target = url.host_ + ":" + std::to_string(static_cast(url.port_)); + if (options.use_ssl_credentials) { grpc::SslCredentialsOptions ssl_opts; From 49da152d9ee1af0133548e09da180105828ef79e Mon Sep 17 00:00:00 2001 From: Tom Tan Date: Tue, 21 Sep 2021 14:02:32 -0700 Subject: [PATCH 3/3] Add ext header to bazel build of otlp gRPC exporter --- exporters/otlp/BUILD | 1 + 1 file changed, 1 insertion(+) diff --git a/exporters/otlp/BUILD b/exporters/otlp/BUILD index 02b888f1b6..0ef968ef46 100644 --- a/exporters/otlp/BUILD +++ b/exporters/otlp/BUILD @@ -48,6 +48,7 @@ cc_library( strip_include_prefix = "include", deps = [ ":otlp_recordable", + "//ext:headers", "//sdk/src/trace", # For gRPC