Skip to content

Commit d99593f

Browse files
authored
[EXPORTER] support unix sockets in grpc client (#3410)
1 parent 214950e commit d99593f

File tree

7 files changed

+98
-16
lines changed

7 files changed

+98
-16
lines changed

bazel/extra_deps.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44
# Load prometheus C++ dependencies.
55

6-
load("@com_github_jupp0r_prometheus_cpp//bazel:repositories.bzl", "prometheus_cpp_repositories")
76
load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace")
7+
load("@com_github_jupp0r_prometheus_cpp//bazel:repositories.bzl", "prometheus_cpp_repositories")
88
load("@rules_foreign_cc//foreign_cc:repositories.bzl", "rules_foreign_cc_dependencies")
99

1010
def opentelemetry_extra_deps():

examples/grpc/BUILD

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# Copyright The OpenTelemetry Authors
22
# SPDX-License-Identifier: Apache-2.0
33

4-
package(default_visibility = ["//visibility:public"])
5-
6-
load("@rules_proto//proto:defs.bzl", "proto_library")
74
load("@com_github_grpc_grpc//bazel:cc_grpc_library.bzl", "cc_grpc_library")
5+
load("@rules_proto//proto:defs.bzl", "proto_library")
6+
7+
package(default_visibility = ["//visibility:public"])
88

99
proto_library(
1010
name = "messages_proto",

exporters/otlp/BUILD

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# Copyright The OpenTelemetry Authors
22
# SPDX-License-Identifier: Apache-2.0
33

4-
package(default_visibility = ["//visibility:public"])
5-
64
load("//bazel:otel_cc_benchmark.bzl", "otel_cc_benchmark")
75

6+
package(default_visibility = ["//visibility:public"])
7+
88
cc_library(
99
name = "otlp_recordable",
1010
srcs = [
@@ -474,6 +474,20 @@ cc_test(
474474
],
475475
)
476476

477+
cc_test(
478+
name = "otlp_grpc_target_test",
479+
srcs = ["test/otlp_grpc_target_test.cc"],
480+
tags = [
481+
"otlp",
482+
"otlp_grpc",
483+
"test",
484+
],
485+
deps = [
486+
":otlp_grpc_client",
487+
"@com_google_googletest//:gtest_main",
488+
],
489+
)
490+
477491
cc_test(
478492
name = "otlp_grpc_exporter_factory_test",
479493
srcs = ["test/otlp_grpc_exporter_factory_test.cc"],

exporters/otlp/CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,15 @@ if(BUILD_TESTING)
377377
TEST_PREFIX exporter.otlp.
378378
TEST_LIST otlp_grpc_exporter_test)
379379

380+
add_executable(otlp_grpc_target_test test/otlp_grpc_target_test.cc)
381+
target_link_libraries(
382+
otlp_grpc_target_test ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT}
383+
${GMOCK_LIB} opentelemetry_exporter_otlp_grpc)
384+
gtest_add_tests(
385+
TARGET otlp_grpc_target_test
386+
TEST_PREFIX exporter.otlp.
387+
TEST_LIST otlp_grpc_target_test)
388+
380389
add_executable(otlp_grpc_exporter_factory_test
381390
test/otlp_grpc_exporter_factory_test.cc)
382391
target_link_libraries(

exporters/otlp/include/opentelemetry/exporters/otlp/otlp_grpc_client.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ class OtlpGrpcClient
6262

6363
~OtlpGrpcClient();
6464

65+
static std::string GetGrpcTarget(const std::string &endpoint);
66+
6567
/**
6668
* Create gRPC channel from the exporter options.
6769
*/

exporters/otlp/src/otlp_grpc_client.cc

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,32 @@ OtlpGrpcClient::~OtlpGrpcClient()
309309
#endif
310310
}
311311

312+
std::string OtlpGrpcClient::GetGrpcTarget(const std::string &endpoint)
313+
{
314+
//
315+
// Scheme is allowed in OTLP endpoint definition, but is not allowed for creating gRPC
316+
// channel. Passing URI with scheme to grpc::CreateChannel could resolve the endpoint to some
317+
// unexpected address.
318+
//
319+
ext::http::common::UrlParser url(endpoint);
320+
if (!url.success_)
321+
{
322+
OTEL_INTERNAL_LOG_ERROR("[OTLP GRPC Client] invalid endpoint: " << endpoint);
323+
return "";
324+
}
325+
326+
std::string grpc_target;
327+
if (url.scheme_ == "unix")
328+
{
329+
grpc_target = "unix:" + url.path_;
330+
}
331+
else
332+
{
333+
grpc_target = url.host_ + ":" + std::to_string(static_cast<int>(url.port_));
334+
}
335+
return grpc_target;
336+
}
337+
312338
std::shared_ptr<grpc::Channel> OtlpGrpcClient::MakeChannel(const OtlpGrpcClientOptions &options)
313339
{
314340

@@ -318,22 +344,16 @@ std::shared_ptr<grpc::Channel> OtlpGrpcClient::MakeChannel(const OtlpGrpcClientO
318344

319345
return nullptr;
320346
}
321-
//
322-
// Scheme is allowed in OTLP endpoint definition, but is not allowed for creating gRPC
323-
// channel. Passing URI with scheme to grpc::CreateChannel could resolve the endpoint to some
324-
// unexpected address.
325-
//
326347

327-
ext::http::common::UrlParser url(options.endpoint);
328-
if (!url.success_)
348+
std::shared_ptr<grpc::Channel> channel;
349+
std::string grpc_target = GetGrpcTarget(options.endpoint);
350+
351+
if (grpc_target.empty())
329352
{
330353
OTEL_INTERNAL_LOG_ERROR("[OTLP GRPC Client] invalid endpoint: " << options.endpoint);
331-
332354
return nullptr;
333355
}
334356

335-
std::shared_ptr<grpc::Channel> channel;
336-
std::string grpc_target = url.host_ + ":" + std::to_string(static_cast<int>(url.port_));
337357
grpc::ChannelArguments grpc_arguments;
338358
grpc_arguments.SetUserAgentPrefix(options.user_agent);
339359

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
#include <grpcpp/grpcpp.h>
5+
#include <gtest/gtest.h>
6+
7+
#include "opentelemetry/exporters/otlp/otlp_grpc_client.h"
8+
9+
OPENTELEMETRY_BEGIN_NAMESPACE
10+
namespace exporter
11+
{
12+
namespace otlp
13+
{
14+
15+
TEST(OtlpGrpcClientEndpointTest, GrpcClientTest)
16+
{
17+
OtlpGrpcClientOptions opts1;
18+
opts1.endpoint = "unix:///tmp/otel1.sock";
19+
20+
OtlpGrpcClientOptions opts2;
21+
opts2.endpoint = "unix:tmp/otel2.sock";
22+
23+
OtlpGrpcClientOptions opts3;
24+
opts3.endpoint = "localhost:4317";
25+
26+
auto target1 = OtlpGrpcClient::GetGrpcTarget(opts1.endpoint);
27+
auto target2 = OtlpGrpcClient::GetGrpcTarget(opts2.endpoint);
28+
auto target3 = OtlpGrpcClient::GetGrpcTarget(opts3.endpoint);
29+
30+
EXPECT_EQ(target1, "unix:/tmp/otel1.sock");
31+
EXPECT_EQ(target2, "");
32+
EXPECT_EQ(target3, "localhost:4317");
33+
}
34+
35+
} // namespace otlp
36+
} // namespace exporter
37+
OPENTELEMETRY_END_NAMESPACE

0 commit comments

Comments
 (0)