-
Notifications
You must be signed in to change notification settings - Fork 417
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: owent <admin@owent.net>
- Loading branch information
Showing
9 changed files
with
246 additions
and
261 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
exporters/otlp/include/opentelemetry/exporters/otlp/otlp_grpc_client.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.