Skip to content

Commit

Permalink
Add OtlpGrpcClient
Browse files Browse the repository at this point in the history
Signed-off-by: owent <admin@owent.net>
  • Loading branch information
owent committed Sep 9, 2022
1 parent 57fa8fd commit 31cbe8f
Show file tree
Hide file tree
Showing 9 changed files with 246 additions and 261 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ Increment the:
## [Unreleased]

* [BUILD] Upgrade opentelemetry-proto to v0.19.0 [#1579](https://github.com/open-telemetry/opentelemetry-cpp/pull/1579)
* [METRICS EXPORTER] Add `OtlpGrpcMetricExporterFactory` and `OtlpHttpMetricExporterFactory` [#1606](https://github.com/open-telemetry/opentelemetry-cpp/pull/1606)
* [METRICS EXPORTER] Add `OtlpGrpcClient` [#1606](https://github.com/open-telemetry/opentelemetry-cpp/pull/1606)

## [1.6.0] 2022-08-15

Expand Down
2 changes: 2 additions & 0 deletions cmake/opentelemetry-cpp-config.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
# opentelemetry-cpp::metrics - Imported target of opentelemetry-cpp::metrics
# opentelemetry-cpp::logs - Imported target of opentelemetry-cpp::logs
# opentelemetry-cpp::in_memory_span_exporter - Imported target of opentelemetry-cpp::in_memory_span_exporter
# opentelemetry-cpp::otlp_grpc_client - Imported target of opentelemetry-cpp::otlp_grpc_client
# opentelemetry-cpp::otlp_recordable - Imported target of opentelemetry-cpp::otlp_recordable
# opentelemetry-cpp::otlp_grpc_exporter - Imported target of opentelemetry-cpp::otlp_grpc_exporter
# opentelemetry-cpp::otlp_grpc_log_exporter - Imported target of opentelemetry-cpp::otlp_grpc_log_exporter
Expand Down Expand Up @@ -85,6 +86,7 @@ set(_OPENTELEMETRY_CPP_LIBRARIES_TEST_TARGETS
logs
in_memory_span_exporter
otlp_recordable
otlp_grpc_client
otlp_grpc_exporter
otlp_grpc_log_exporter
otlp_grpc_metrics_exporter
Expand Down
31 changes: 28 additions & 3 deletions exporters/otlp/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,31 @@ cc_library(
],
)

cc_library(
name = "otlp_grpc_client",
srcs = [
"src/otlp_grpc_client.cc",
],
hdrs = [
"include/opentelemetry/exporters/otlp/otlp_environment.h",
"include/opentelemetry/exporters/otlp/otlp_grpc_client.h",
"include/opentelemetry/exporters/otlp/otlp_grpc_exporter_options.h",
"include/opentelemetry/exporters/otlp/protobuf_include_prefix.h",
"include/opentelemetry/exporters/otlp/protobuf_include_suffix.h",
],
strip_include_prefix = "include",
tags = [
"otlp",
"otlp_grpc",
],
deps = [
"//ext:headers",
"//sdk/src/common:global_log_handler",
"@com_github_grpc_grpc//:grpc++",
"@com_github_opentelemetry_proto//:common_proto_cc",
],
)

cc_library(
name = "otlp_grpc_exporter",
srcs = [
Expand All @@ -67,12 +92,12 @@ cc_library(
],
deps = [
":otlp_recordable",
":otlp_grpc_client",
"//ext:headers",
"//sdk/src/trace",

# For gRPC
"@com_github_opentelemetry_proto//:trace_service_grpc_cc",
"@com_github_grpc_grpc//:grpc++",
],
)

Expand Down Expand Up @@ -162,12 +187,12 @@ cc_library(
],
deps = [
":otlp_recordable",
":otlp_grpc_client",
"//ext:headers",
"//sdk/src/metrics",

# For gRPC
"@com_github_opentelemetry_proto//:metrics_service_grpc_cc",
"@com_github_grpc_grpc//:grpc++",
],
)

Expand Down Expand Up @@ -246,12 +271,12 @@ cc_library(
],
deps = [
":otlp_recordable",
":otlp_grpc_client",
"//ext:headers",
"//sdk/src/logs",
"@com_github_opentelemetry_proto//:logs_service_proto_cc",
# For gRPC
"@com_github_opentelemetry_proto//:logs_service_grpc_cc",
"@com_github_grpc_grpc//:grpc++",
],
)

Expand Down
42 changes: 36 additions & 6 deletions exporters/otlp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,40 @@ endif()

if(WITH_OTLP_GRPC)
find_package(gRPC REQUIRED)
add_library(opentelemetry_exporter_otlp_grpc_client src/otlp_grpc_client.cc)
set_target_properties(opentelemetry_exporter_otlp_grpc_client
PROPERTIES EXPORT_NAME otlp_grpc_client)
target_link_libraries(
opentelemetry_exporter_otlp_grpc_client
PUBLIC opentelemetry_sdk opentelemetry_ext opentelemetry_proto)

target_link_libraries(opentelemetry_exporter_otlp_grpc_client
PRIVATE gRPC::grpc++)
get_target_property(GRPC_INCLUDE_DIRECTORY gRPC::grpc++
INTERFACE_INCLUDE_DIRECTORIES)
if(GRPC_INCLUDE_DIRECTORY)
target_include_directories(
opentelemetry_exporter_otlp_grpc_client
PUBLIC "$<BUILD_INTERFACE:${GRPC_INCLUDE_DIRECTORY}>")
endif()
target_include_directories(
opentelemetry_exporter_otlp_grpc_client
PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>"
"$<INSTALL_INTERFACE:include>")

list(APPEND OPENTELEMETRY_OTLP_TARGETS
opentelemetry_exporter_otlp_grpc_client)

add_library(opentelemetry_exporter_otlp_grpc
src/otlp_grpc_exporter.cc src/otlp_grpc_exporter_factory.cc)

set_target_properties(opentelemetry_exporter_otlp_grpc
PROPERTIES EXPORT_NAME otlp_grpc_exporter)

target_link_libraries(opentelemetry_exporter_otlp_grpc
PUBLIC opentelemetry_otlp_recordable gRPC::grpc++)
target_link_libraries(
opentelemetry_exporter_otlp_grpc
PUBLIC opentelemetry_otlp_recordable
opentelemetry_exporter_otlp_grpc_client)

list(APPEND OPENTELEMETRY_OTLP_TARGETS opentelemetry_exporter_otlp_grpc)

Expand All @@ -44,8 +70,10 @@ if(WITH_OTLP_GRPC)
set_target_properties(opentelemetry_exporter_otlp_grpc_log
PROPERTIES EXPORT_NAME otlp_grpc_log_exporter)

target_link_libraries(opentelemetry_exporter_otlp_grpc_log
PUBLIC opentelemetry_otlp_recordable gRPC::grpc++)
target_link_libraries(
opentelemetry_exporter_otlp_grpc_log
PUBLIC opentelemetry_otlp_recordable
opentelemetry_exporter_otlp_grpc_client)

list(APPEND OPENTELEMETRY_OTLP_TARGETS opentelemetry_exporter_otlp_grpc_log)

Expand All @@ -57,8 +85,10 @@ if(WITH_OTLP_GRPC)
set_target_properties(opentelemetry_exporter_otlp_grpc_metrics
PROPERTIES EXPORT_NAME otlp_grpc_metrics_exporter)

target_link_libraries(opentelemetry_exporter_otlp_grpc_metrics
PUBLIC opentelemetry_otlp_recordable gRPC::grpc++)
target_link_libraries(
opentelemetry_exporter_otlp_grpc_metrics
PUBLIC opentelemetry_otlp_recordable
opentelemetry_exporter_otlp_grpc_client)

list(APPEND OPENTELEMETRY_OTLP_TARGETS
opentelemetry_exporter_otlp_grpc_metrics)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#pragma once

#include <grpcpp/grpcpp.h>

#include <memory>

#include "opentelemetry/exporters/otlp/otlp_grpc_exporter_options.h"

OPENTELEMETRY_BEGIN_NAMESPACE
namespace exporter
{
namespace otlp
{

/**
* The OTLP gRPC client contains utility functions of gRPC.
*/
class OtlpGrpcClient
{
public:
/**
* Create gRPC channel from the exporter options.
*/
static std::shared_ptr<grpc::Channel> MakeChannel(const OtlpGrpcExporterOptions &options);

/**
* Create gRPC client context to call RPC.
*/
static std::unique_ptr<grpc::ClientContext> MakeClientContext(
const OtlpGrpcExporterOptions &options);

/**
* Create service stub to communicate with the OpenTelemetry Collector.
*/
template <class ServiceType>
static std::unique_ptr<typename ServiceType::Stub> MakeServiceStub(
const OtlpGrpcExporterOptions &options)
{
return ServiceType::NewStub(MakeChannel(options));
}
};
} // namespace otlp
} // namespace exporter
OPENTELEMETRY_END_NAMESPACE
104 changes: 104 additions & 0 deletions exporters/otlp/src/otlp_grpc_client.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

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

#if defined(HAVE_GSL)
# include <gsl/gsl>
#else
# include <assert.h>
#endif

#include <fstream>
#include <iterator>
#include <memory>
#include <string>

#include "opentelemetry/ext/http/common/url_parser.h"
#include "opentelemetry/sdk/common/global_log_handler.h"

OPENTELEMETRY_BEGIN_NAMESPACE
namespace exporter
{
namespace otlp
{

namespace
{
// ----------------------------- Helper functions ------------------------------
static std::string GetFileContents(const char *fpath)
{
std::ifstream finstream(fpath);
std::string contents;
contents.assign((std::istreambuf_iterator<char>(finstream)), std::istreambuf_iterator<char>());
finstream.close();
return contents;
}
} // namespace

std::shared_ptr<grpc::Channel> OtlpGrpcClient::MakeChannel(const OtlpGrpcExporterOptions &options)
{
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 GRPC Client] invalid endpoint: " << options.endpoint);

return nullptr;
}

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

if (options.use_ssl_credentials)
{
grpc::SslCredentialsOptions ssl_opts;
if (options.ssl_credentials_cacert_path.empty())
{
ssl_opts.pem_root_certs = options.ssl_credentials_cacert_as_string;
}
else
{
ssl_opts.pem_root_certs = GetFileContents((options.ssl_credentials_cacert_path).c_str());
}
channel = grpc::CreateChannel(grpc_target, grpc::SslCredentials(ssl_opts));
}
else
{
channel = grpc::CreateChannel(grpc_target, grpc::InsecureChannelCredentials());
}

return channel;
}

std::unique_ptr<grpc::ClientContext> OtlpGrpcClient::MakeClientContext(
const OtlpGrpcExporterOptions &options)
{
std::unique_ptr<grpc::ClientContext> context{new grpc::ClientContext()};
if (!context)
{
return context;
}

if (options.timeout.count() > 0)
{
context->set_deadline(std::chrono::system_clock::now() + options.timeout);
}

for (auto &header : options.metadata)
{
context->AddMetadata(header.first, header.second);
}

return context;
}

} // namespace otlp
} // namespace exporter
OPENTELEMETRY_END_NAMESPACE
Loading

0 comments on commit 31cbe8f

Please sign in to comment.