Skip to content

Commit

Permalink
Refactored GRPC options.
Browse files Browse the repository at this point in the history
  • Loading branch information
marcalff committed Oct 28, 2023
1 parent 6db88e2 commit 25dc909
Show file tree
Hide file tree
Showing 22 changed files with 265 additions and 77 deletions.
14 changes: 10 additions & 4 deletions examples/otlp/grpc_log_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
// SPDX-License-Identifier: Apache-2.0

#include "opentelemetry/exporters/otlp/otlp_grpc_exporter_factory.h"
#include "opentelemetry/exporters/otlp/otlp_grpc_exporter_options.h"
#include "opentelemetry/exporters/otlp/otlp_grpc_log_record_exporter_factory.h"
#include "opentelemetry/exporters/otlp/otlp_grpc_log_record_exporter_options.h"
#include "opentelemetry/logs/provider.h"
#include "opentelemetry/sdk/logs/exporter.h"
#include "opentelemetry/sdk/logs/logger_provider_factory.h"
Expand Down Expand Up @@ -37,6 +39,7 @@ namespace trace_sdk = opentelemetry::sdk::trace;
namespace
{
opentelemetry::exporter::otlp::OtlpGrpcExporterOptions opts;
opentelemetry::exporter::otlp::OtlpGrpcLogRecordExporterOptions log_opts;
void InitTracer()
{
// Create OTLP exporter instance
Expand Down Expand Up @@ -65,7 +68,7 @@ void CleanupTracer()
void InitLogger()
{
// Create OTLP exporter instance
auto exporter = otlp::OtlpGrpcLogRecordExporterFactory::Create(opts);
auto exporter = otlp::OtlpGrpcLogRecordExporterFactory::Create(log_opts);
auto processor = logs_sdk::SimpleLogRecordProcessorFactory::Create(std::move(exporter));
nostd::shared_ptr<logs::LoggerProvider> provider(
logs_sdk::LoggerProviderFactory::Create(std::move(processor)));
Expand All @@ -92,11 +95,14 @@ int main(int argc, char *argv[])
{
if (argc > 1)
{
opts.endpoint = argv[1];
opts.endpoint = argv[1];
log_opts.endpoint = argv[1];
if (argc > 2)
{
opts.use_ssl_credentials = true;
opts.ssl_credentials_cacert_path = argv[2];
opts.use_ssl_credentials = true;
log_opts.use_ssl_credentials = true;
opts.ssl_credentials_cacert_path = argv[2];
log_opts.ssl_credentials_cacert_path = argv[2];
}
}
InitLogger();
Expand Down
5 changes: 3 additions & 2 deletions exporters/otlp/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ cc_library(
srcs = [
"src/otlp_grpc_exporter.cc",
"src/otlp_grpc_exporter_factory.cc",
"src/otlp_grpc_exporter_options.cc",
],
hdrs = [
"include/opentelemetry/exporters/otlp/otlp_environment.h",
Expand Down Expand Up @@ -175,7 +176,6 @@ cc_library(
],
hdrs = [
"include/opentelemetry/exporters/otlp/otlp_environment.h",
"include/opentelemetry/exporters/otlp/otlp_grpc_exporter_options.h",
"include/opentelemetry/exporters/otlp/otlp_grpc_metric_exporter.h",
"include/opentelemetry/exporters/otlp/otlp_grpc_metric_exporter_factory.h",
"include/opentelemetry/exporters/otlp/otlp_grpc_metric_exporter_options.h",
Expand Down Expand Up @@ -259,12 +259,13 @@ cc_library(
srcs = [
"src/otlp_grpc_log_record_exporter.cc",
"src/otlp_grpc_log_record_exporter_factory.cc",
"src/otlp_grpc_log_record_exporter_options.cc",
],
hdrs = [
"include/opentelemetry/exporters/otlp/otlp_environment.h",
"include/opentelemetry/exporters/otlp/otlp_grpc_exporter_options.h",
"include/opentelemetry/exporters/otlp/otlp_grpc_log_record_exporter.h",
"include/opentelemetry/exporters/otlp/otlp_grpc_log_record_exporter_factory.h",
"include/opentelemetry/exporters/otlp/otlp_grpc_log_record_exporter_options.h",
"include/opentelemetry/exporters/otlp/protobuf_include_prefix.h",
"include/opentelemetry/exporters/otlp/protobuf_include_suffix.h",
],
Expand Down
9 changes: 6 additions & 3 deletions exporters/otlp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ if(WITH_OTLP_GRPC)
opentelemetry_exporter_otlp_grpc_client)

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

set_target_properties(opentelemetry_exporter_otlp_grpc
PROPERTIES EXPORT_NAME otlp_grpc_exporter)
Expand All @@ -73,7 +74,8 @@ if(WITH_OTLP_GRPC)
add_library(
opentelemetry_exporter_otlp_grpc_log
src/otlp_grpc_log_record_exporter.cc
src/otlp_grpc_log_record_exporter_factory.cc)
src/otlp_grpc_log_record_exporter_factory.cc
src/otlp_grpc_log_record_exporter_options.cc)

set_target_properties(opentelemetry_exporter_otlp_grpc_log
PROPERTIES EXPORT_NAME otlp_grpc_log_record_exporter)
Expand All @@ -88,7 +90,8 @@ if(WITH_OTLP_GRPC)

add_library(
opentelemetry_exporter_otlp_grpc_metrics
src/otlp_grpc_metric_exporter.cc src/otlp_grpc_metric_exporter_factory.cc)
src/otlp_grpc_metric_exporter.cc src/otlp_grpc_metric_exporter_factory.cc
src/otlp_grpc_metric_exporter_options.cc)

set_target_properties(opentelemetry_exporter_otlp_grpc_metrics
PROPERTIES EXPORT_NAME otlp_grpc_metrics_exporter)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ namespace exporter
namespace otlp
{

struct OtlpGrpcClientOptions;

/**
* The OTLP gRPC client contains utility functions of gRPC.
*/
Expand All @@ -32,13 +34,13 @@ class OtlpGrpcClient
/**
* Create gRPC channel from the exporter options.
*/
static std::shared_ptr<grpc::Channel> MakeChannel(const OtlpGrpcExporterOptions &options);
static std::shared_ptr<grpc::Channel> MakeChannel(const OtlpGrpcClientOptions &options);

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

/**
* Create gRPC CompletionQueue to async call RPC.
Expand All @@ -49,19 +51,19 @@ class OtlpGrpcClient
* Create trace service stub to communicate with the OpenTelemetry Collector.
*/
static std::unique_ptr<proto::collector::trace::v1::TraceService::StubInterface>
MakeTraceServiceStub(const OtlpGrpcExporterOptions &options);
MakeTraceServiceStub(const OtlpGrpcClientOptions &options);

/**
* Create metrics service stub to communicate with the OpenTelemetry Collector.
*/
static std::unique_ptr<proto::collector::metrics::v1::MetricsService::StubInterface>
MakeMetricsServiceStub(const OtlpGrpcExporterOptions &options);
MakeMetricsServiceStub(const OtlpGrpcClientOptions &options);

/**
* Create logs service stub to communicate with the OpenTelemetry Collector.
*/
static std::unique_ptr<proto::collector::logs::v1::LogsService::StubInterface>
MakeLogsServiceStub(const OtlpGrpcExporterOptions &options);
MakeLogsServiceStub(const OtlpGrpcClientOptions &options);

static grpc::Status DelegateExport(
proto::collector::trace::v1::TraceService::StubInterface *stub,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#pragma once

#include "opentelemetry/exporters/otlp/otlp_environment.h"
#include "opentelemetry/version.h"

#include <chrono>
#include <string>

OPENTELEMETRY_BEGIN_NAMESPACE
namespace exporter
{
namespace otlp
{

struct OtlpGrpcClientOptions
{
/** The endpoint to export to. */
std::string endpoint;

/** Use SSL. */
bool use_ssl_credentials;

/** CA CERT, path to a file. */
std::string ssl_credentials_cacert_path;

/** CA CERT, as a string. */
std::string ssl_credentials_cacert_as_string;

#ifdef ENABLE_OTLP_GRPC_SSL_MTLS_PREVIEW
/** CLIENT KEY, path to a file. */
std::string ssl_client_key_path;

/** CLIENT KEY, as a string. */
std::string ssl_client_key_string;

/** CLIENT CERT, path to a file. */
std::string ssl_client_cert_path;

/** CLIENT CERT, as a string. */
std::string ssl_client_cert_string;
#endif

/** Export timeout. */
std::chrono::system_clock::duration timeout;

/** Additional HTTP headers. */
OtlpHeaders metadata;

/** User agent. */
std::string user_agent;
};

} // namespace otlp
} // namespace exporter
OPENTELEMETRY_END_NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@

#pragma once

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

#include <memory>
#include "opentelemetry/exporters/otlp/otlp_grpc_client_options.h"
#include "opentelemetry/version.h"

OPENTELEMETRY_BEGIN_NAMESPACE
namespace exporter
Expand All @@ -15,38 +14,16 @@ namespace otlp

/**
* Struct to hold OTLP GRPC traces exporter options.
*
* See
* https://github.com/open-telemetry/opentelemetry-proto/blob/main/docs/specification.md#otlpgrpc
*
* See
* https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/protocol/exporter.md
*/
struct OtlpGrpcExporterOptions
struct OtlpGrpcExporterOptions : public OtlpGrpcClientOptions
{
// The endpoint to export to. By default the OpenTelemetry Collector's default endpoint.
std::string endpoint = GetOtlpDefaultGrpcEndpoint();
// By default when false, uses grpc::InsecureChannelCredentials(); If true,
// uses ssl_credentials_cacert_path if non-empty, else uses ssl_credentials_cacert_as_string
bool use_ssl_credentials = GetOtlpDefaultIsSslEnable();
// ssl_credentials_cacert_path specifies path to .pem file to be used for SSL encryption.
std::string ssl_credentials_cacert_path = GetOtlpDefaultSslCertificatePath();
// ssl_credentials_cacert_as_string in-memory string representation of .pem file to be used for
// SSL encryption.
std::string ssl_credentials_cacert_as_string = GetOtlpDefaultSslCertificateString();

#ifdef ENABLE_OTLP_GRPC_SSL_MTLS_PREVIEW
// At most one of ssl_client_key_* should be non-empty. If use_ssl_credentials, they will
// be read to allow for mTLS.
std::string ssl_client_key_path = GetOtlpDefaultTracesSslClientKeyPath();
std::string ssl_client_key_string = GetOtlpDefaultTracesSslClientKeyString();

// At most one of ssl_client_cert_* should be non-empty. If use_ssl_credentials, they will
// be read to allow for mTLS.
std::string ssl_client_cert_path = GetOtlpDefaultTracesSslClientCertificatePath();
std::string ssl_client_cert_string = GetOtlpDefaultTracesSslClientCertificateString();
#endif

// Timeout for grpc deadline
std::chrono::system_clock::duration timeout = GetOtlpDefaultTimeout();
// Additional HTTP headers
OtlpHeaders metadata = GetOtlpDefaultHeaders();
// User agent
std::string user_agent = GetOtlpDefaultUserAgent();
OtlpGrpcExporterOptions();
};

} // namespace otlp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// clang-format on

#include "opentelemetry/exporters/otlp/otlp_environment.h"
#include "opentelemetry/exporters/otlp/otlp_grpc_exporter_options.h"
#include "opentelemetry/exporters/otlp/otlp_grpc_log_record_exporter_options.h"
#include "opentelemetry/sdk/logs/exporter.h"

OPENTELEMETRY_BEGIN_NAMESPACE
Expand All @@ -37,7 +37,7 @@ class OtlpGrpcLogRecordExporter : public opentelemetry::sdk::logs::LogRecordExpo
* Create an OtlpGrpcLogRecordExporter with user specified options.
* @param options An object containing the user's configuration options.
*/
OtlpGrpcLogRecordExporter(const OtlpGrpcExporterOptions &options);
OtlpGrpcLogRecordExporter(const OtlpGrpcLogRecordExporterOptions &options);

/**
* Creates a recordable that stores the data in protobuf.
Expand Down Expand Up @@ -71,7 +71,7 @@ class OtlpGrpcLogRecordExporter : public opentelemetry::sdk::logs::LogRecordExpo

private:
// Configuration options for the exporter
const OtlpGrpcExporterOptions options_;
const OtlpGrpcLogRecordExporterOptions options_;

// For testing
friend class OtlpGrpcLogRecordExporterTestPeer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#pragma once

#include "opentelemetry/exporters/otlp/otlp_grpc_exporter_options.h"
#include "opentelemetry/exporters/otlp/otlp_grpc_log_record_exporter_options.h"
#include "opentelemetry/sdk/logs/exporter.h"

OPENTELEMETRY_BEGIN_NAMESPACE
Expand All @@ -27,7 +27,7 @@ class OPENTELEMETRY_EXPORT OtlpGrpcLogRecordExporterFactory
* Create a OtlpGrpcLogRecordExporter.
*/
static std::unique_ptr<opentelemetry::sdk::logs::LogRecordExporter> Create(
const OtlpGrpcExporterOptions &options);
const OtlpGrpcLogRecordExporterOptions &options);
};

} // namespace otlp
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#pragma once

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

OPENTELEMETRY_BEGIN_NAMESPACE
namespace exporter
{
namespace otlp
{

/**
* Struct to hold OTLP GRPC log record exporter options.
*
* See
* https://github.com/open-telemetry/opentelemetry-proto/blob/main/docs/specification.md#otlpgrpc
*
* See
* https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/protocol/exporter.md
*/
struct OtlpGrpcLogRecordExporterOptions : public OtlpGrpcClientOptions
{
OtlpGrpcLogRecordExporterOptions();
};

} // namespace otlp
} // namespace exporter
OPENTELEMETRY_END_NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@

#pragma once

#include "opentelemetry/exporters/otlp/otlp_grpc_exporter_options.h"
#include "opentelemetry/exporters/otlp/otlp_grpc_client_options.h"
#include "opentelemetry/exporters/otlp/otlp_preferred_temporality.h"
#include "opentelemetry/sdk/metrics/instruments.h"

#include <memory>
#include "opentelemetry/version.h"

OPENTELEMETRY_BEGIN_NAMESPACE
namespace exporter
Expand All @@ -17,13 +15,19 @@ namespace otlp

/**
* Struct to hold OTLP GRPC metrics exporter options.
*
* See
* https://github.com/open-telemetry/opentelemetry-proto/blob/main/docs/specification.md#otlpgrpc
*
* See
* https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/protocol/exporter.md
*/
struct OtlpGrpcMetricExporterOptions : public OtlpGrpcExporterOptions
struct OtlpGrpcMetricExporterOptions : public OtlpGrpcClientOptions
{
OtlpGrpcMetricExporterOptions();

// Preferred Aggregation Temporality
PreferredAggregationTemporality aggregation_temporality =
PreferredAggregationTemporality::kCumulative;
/** Preferred Aggregation Temporality. */
PreferredAggregationTemporality aggregation_temporality;
};

} // namespace otlp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace otlp
{

/**
* Struct to hold OTLP HTTP logs exporter options.
* Struct to hold OTLP HTTP log record exporter options.
*
* See
* https://github.com/open-telemetry/opentelemetry-proto/blob/main/docs/specification.md#otlphttp
Expand Down
Loading

0 comments on commit 25dc909

Please sign in to comment.