Skip to content
Merged
2 changes: 1 addition & 1 deletion bazel/extra_deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
6 changes: 3 additions & 3 deletions examples/grpc/BUILD
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
18 changes: 16 additions & 2 deletions exporters/otlp/BUILD
Original file line number Diff line number Diff line change
@@ -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 = [
Expand Down Expand Up @@ -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"],
Expand Down
9 changes: 9 additions & 0 deletions exporters/otlp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ class OtlpGrpcClient

~OtlpGrpcClient();

static std::string GetGrpcTarget(const std::string &endpoint);

/**
* Create gRPC channel from the exporter options.
*/
Expand Down
40 changes: 30 additions & 10 deletions exporters/otlp/src/otlp_grpc_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Copy link
Member

Choose a reason for hiding this comment

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

Could you please add a test for unix socket?

Copy link
Contributor

Choose a reason for hiding this comment

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

Also add some example on the unix scheme in the comment to help understanding?

{
grpc_target = "unix:" + url.path_;
}
else
{
grpc_target = url.host_ + ":" + std::to_string(static_cast<int>(url.port_));
}
return grpc_target;
}

std::shared_ptr<grpc::Channel> OtlpGrpcClient::MakeChannel(const OtlpGrpcClientOptions &options)
{

Expand All @@ -318,22 +344,16 @@ std::shared_ptr<grpc::Channel> 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<grpc::Channel> 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<grpc::Channel> channel;
std::string grpc_target = url.host_ + ":" + std::to_string(static_cast<int>(url.port_));
grpc::ChannelArguments grpc_arguments;
grpc_arguments.SetUserAgentPrefix(options.user_agent);

Expand Down
37 changes: 37 additions & 0 deletions exporters/otlp/test/otlp_grpc_target_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#include <grpcpp/grpcpp.h>
#include <gtest/gtest.h>

#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
Loading