diff --git a/bazel/extra_deps.bzl b/bazel/extra_deps.bzl index 00a32c50bf..f68bc0f19e 100644 --- a/bazel/extra_deps.bzl +++ b/bazel/extra_deps.bzl @@ -3,8 +3,8 @@ # Load prometheus C++ dependencies. -load("@com_github_jupp0r_prometheus_cpp//bazel:repositories.bzl", "prometheus_cpp_repositories") load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace") +load("@com_github_jupp0r_prometheus_cpp//bazel:repositories.bzl", "prometheus_cpp_repositories") load("@rules_foreign_cc//foreign_cc:repositories.bzl", "rules_foreign_cc_dependencies") def opentelemetry_extra_deps(): diff --git a/examples/grpc/BUILD b/examples/grpc/BUILD index b1d04e9fbd..5fe8898ac8 100644 --- a/examples/grpc/BUILD +++ b/examples/grpc/BUILD @@ -1,10 +1,10 @@ # Copyright The OpenTelemetry Authors # SPDX-License-Identifier: Apache-2.0 -package(default_visibility = ["//visibility:public"]) - -load("@rules_proto//proto:defs.bzl", "proto_library") load("@com_github_grpc_grpc//bazel:cc_grpc_library.bzl", "cc_grpc_library") +load("@rules_proto//proto:defs.bzl", "proto_library") + +package(default_visibility = ["//visibility:public"]) proto_library( name = "messages_proto", diff --git a/exporters/otlp/BUILD b/exporters/otlp/BUILD index ba55ba72c7..6198e39fe4 100644 --- a/exporters/otlp/BUILD +++ b/exporters/otlp/BUILD @@ -1,10 +1,10 @@ # Copyright The OpenTelemetry Authors # SPDX-License-Identifier: Apache-2.0 -package(default_visibility = ["//visibility:public"]) - load("//bazel:otel_cc_benchmark.bzl", "otel_cc_benchmark") +package(default_visibility = ["//visibility:public"]) + cc_library( name = "otlp_recordable", srcs = [ @@ -474,6 +474,20 @@ cc_test( ], ) +cc_test( + name = "otlp_grpc_target_test", + srcs = ["test/otlp_grpc_target_test.cc"], + tags = [ + "otlp", + "otlp_grpc", + "test", + ], + deps = [ + ":otlp_grpc_client", + "@com_google_googletest//:gtest_main", + ], +) + cc_test( name = "otlp_grpc_exporter_factory_test", srcs = ["test/otlp_grpc_exporter_factory_test.cc"], diff --git a/exporters/otlp/CMakeLists.txt b/exporters/otlp/CMakeLists.txt index 08303fe359..3ec20517e0 100644 --- a/exporters/otlp/CMakeLists.txt +++ b/exporters/otlp/CMakeLists.txt @@ -377,6 +377,15 @@ if(BUILD_TESTING) TEST_PREFIX exporter.otlp. TEST_LIST otlp_grpc_exporter_test) + add_executable(otlp_grpc_target_test test/otlp_grpc_target_test.cc) + target_link_libraries( + otlp_grpc_target_test ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} + ${GMOCK_LIB} opentelemetry_exporter_otlp_grpc) + gtest_add_tests( + TARGET otlp_grpc_target_test + TEST_PREFIX exporter.otlp. + TEST_LIST otlp_grpc_target_test) + add_executable(otlp_grpc_exporter_factory_test test/otlp_grpc_exporter_factory_test.cc) target_link_libraries( diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_grpc_client.h b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_grpc_client.h index 3ded3a16eb..b83c721281 100644 --- a/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_grpc_client.h +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/otlp_grpc_client.h @@ -62,6 +62,8 @@ class OtlpGrpcClient ~OtlpGrpcClient(); + static std::string GetGrpcTarget(const std::string &endpoint); + /** * Create gRPC channel from the exporter options. */ diff --git a/exporters/otlp/src/otlp_grpc_client.cc b/exporters/otlp/src/otlp_grpc_client.cc index 4d2fb6d38e..b520d1bd06 100644 --- a/exporters/otlp/src/otlp_grpc_client.cc +++ b/exporters/otlp/src/otlp_grpc_client.cc @@ -309,6 +309,32 @@ OtlpGrpcClient::~OtlpGrpcClient() #endif } +std::string OtlpGrpcClient::GetGrpcTarget(const std::string &endpoint) +{ + // + // 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(endpoint); + if (!url.success_) + { + OTEL_INTERNAL_LOG_ERROR("[OTLP GRPC Client] invalid endpoint: " << endpoint); + return ""; + } + + std::string grpc_target; + if (url.scheme_ == "unix") + { + grpc_target = "unix:" + url.path_; + } + else + { + grpc_target = url.host_ + ":" + std::to_string(static_cast(url.port_)); + } + return grpc_target; +} + std::shared_ptr OtlpGrpcClient::MakeChannel(const OtlpGrpcClientOptions &options) { @@ -318,22 +344,16 @@ std::shared_ptr OtlpGrpcClient::MakeChannel(const OtlpGrpcClientO return nullptr; } - // - // 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_) + std::shared_ptr channel; + std::string grpc_target = GetGrpcTarget(options.endpoint); + + if (grpc_target.empty()) { OTEL_INTERNAL_LOG_ERROR("[OTLP GRPC Client] invalid endpoint: " << options.endpoint); - return nullptr; } - std::shared_ptr channel; - std::string grpc_target = url.host_ + ":" + std::to_string(static_cast(url.port_)); grpc::ChannelArguments grpc_arguments; grpc_arguments.SetUserAgentPrefix(options.user_agent); diff --git a/exporters/otlp/test/otlp_grpc_target_test.cc b/exporters/otlp/test/otlp_grpc_target_test.cc new file mode 100644 index 0000000000..0073aeedbc --- /dev/null +++ b/exporters/otlp/test/otlp_grpc_target_test.cc @@ -0,0 +1,37 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#include +#include + +#include "opentelemetry/exporters/otlp/otlp_grpc_client.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace exporter +{ +namespace otlp +{ + +TEST(OtlpGrpcClientEndpointTest, GrpcClientTest) +{ + OtlpGrpcClientOptions opts1; + opts1.endpoint = "unix:///tmp/otel1.sock"; + + OtlpGrpcClientOptions opts2; + opts2.endpoint = "unix:tmp/otel2.sock"; + + OtlpGrpcClientOptions opts3; + opts3.endpoint = "localhost:4317"; + + auto target1 = OtlpGrpcClient::GetGrpcTarget(opts1.endpoint); + auto target2 = OtlpGrpcClient::GetGrpcTarget(opts2.endpoint); + auto target3 = OtlpGrpcClient::GetGrpcTarget(opts3.endpoint); + + EXPECT_EQ(target1, "unix:/tmp/otel1.sock"); + EXPECT_EQ(target2, ""); + EXPECT_EQ(target3, "localhost:4317"); +} + +} // namespace otlp +} // namespace exporter +OPENTELEMETRY_END_NAMESPACE