From 8ebc8781b21e7498833b35a1e268c6aad804aba8 Mon Sep 17 00:00:00 2001 From: owent Date: Thu, 30 Jan 2025 01:41:47 +0800 Subject: [PATCH 1/2] Add document and sample for sharing gRPC Client --- docs/cpp-sdk-factory-design.md | 39 ++++++++++++++++++++++++++++++++++ examples/otlp/grpc_log_main.cc | 16 ++++++++------ 2 files changed, 49 insertions(+), 6 deletions(-) diff --git a/docs/cpp-sdk-factory-design.md b/docs/cpp-sdk-factory-design.md index a761d6653d..cbf9b6b24d 100644 --- a/docs/cpp-sdk-factory-design.md +++ b/docs/cpp-sdk-factory-design.md @@ -94,6 +94,45 @@ This property makes it possible to: - deploy a new SDK shared library - keep the application unchanged +### Case study, using Factory and shared gRPC client between OTLP gRPC exporters + +To reduce the cost of gRPC, the SDK allow users to shared gRPC clients between +OTLP gRPC exporters when these exporters have the same settings. This can be +used as follows from the application code: + +```cpp +// Include follows headers +#include "opentelemetry/exporters/otlp/otlp_grpc_client_factory.h" +#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" + +// Create exporters with shared gRPC Client +namespace otlp = opentelemetry::exporter::otlp; + +void SetupOtlp() { + otlp::OtlpGrpcExporterOptions trace_opts; + otlp::OtlpGrpcLogRecordExporterOptions log_opts; + + // Setting trace_opts and log_opts + std::shared_ptr shared_client = + otlp::OtlpGrpcClientFactory::Create(trace_opts); + + // Create exporters + auto trace_exporter = + otlp::OtlpGrpcExporterFactory::Create(trace_opts, shared_client); + auto log_exporter = + otlp::OtlpGrpcLogRecordExporterFactory::Create(log_opts, shared_client); + + // Other initialization codes ... +} +``` + +Be careful, create OTLP exporters with a existed `OtlpGrpcClient` will ignore +the options of gRPC when pass the `OtlpGrpcExporterOptions` or other option +object. + ## SDK extension Applications owners who want to extend existing SDK classes are expected diff --git a/examples/otlp/grpc_log_main.cc b/examples/otlp/grpc_log_main.cc index 81a6aa611e..8727c01bf6 100644 --- a/examples/otlp/grpc_log_main.cc +++ b/examples/otlp/grpc_log_main.cc @@ -1,6 +1,7 @@ // Copyright The OpenTelemetry Authors // SPDX-License-Identifier: Apache-2.0 +#include "opentelemetry/exporters/otlp/otlp_grpc_client_factory.h" #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" @@ -41,10 +42,10 @@ opentelemetry::exporter::otlp::OtlpGrpcLogRecordExporterOptions log_opts; std::shared_ptr tracer_provider; std::shared_ptr logger_provider; -void InitTracer() +void InitTracer(const std::shared_ptr &shared_client) { // Create OTLP exporter instance - auto exporter = otlp::OtlpGrpcExporterFactory::Create(opts); + auto exporter = otlp::OtlpGrpcExporterFactory::Create(opts, shared_client); auto processor = trace_sdk::SimpleSpanProcessorFactory::Create(std::move(exporter)); tracer_provider = trace_sdk::TracerProviderFactory::Create(std::move(processor)); @@ -66,10 +67,10 @@ void CleanupTracer() trace::Provider::SetTracerProvider(none); } -void InitLogger() +void InitLogger(const std::shared_ptr &shared_client) { // Create OTLP exporter instance - auto exporter = otlp::OtlpGrpcLogRecordExporterFactory::Create(log_opts); + auto exporter = otlp::OtlpGrpcLogRecordExporterFactory::Create(log_opts, shared_client); auto processor = logs_sdk::SimpleLogRecordProcessorFactory::Create(std::move(exporter)); logger_provider = logs_sdk::LoggerProviderFactory::Create(std::move(processor)); @@ -106,8 +107,11 @@ int main(int argc, char *argv[]) log_opts.ssl_credentials_cacert_path = argv[2]; } } - InitLogger(); - InitTracer(); + + std::shared_ptr shared_client = otlp::OtlpGrpcClientFactory::Create(opts); + + InitLogger(shared_client); + InitTracer(shared_client); foo_library(); CleanupTracer(); CleanupLogger(); From 6bc2c4cbb63b4f520f1a90ecea0a4525c0b36d19 Mon Sep 17 00:00:00 2001 From: owent Date: Fri, 31 Jan 2025 11:35:43 +0800 Subject: [PATCH 2/2] Fix nit --- docs/cpp-sdk-factory-design.md | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/docs/cpp-sdk-factory-design.md b/docs/cpp-sdk-factory-design.md index cbf9b6b24d..90d2506dd9 100644 --- a/docs/cpp-sdk-factory-design.md +++ b/docs/cpp-sdk-factory-design.md @@ -96,12 +96,12 @@ This property makes it possible to: ### Case study, using Factory and shared gRPC client between OTLP gRPC exporters -To reduce the cost of gRPC, the SDK allow users to shared gRPC clients between +To reduce the cost of gRPC, the SDK allow users to share gRPC clients between OTLP gRPC exporters when these exporters have the same settings. This can be used as follows from the application code: ```cpp -// Include follows headers +// Include following headers #include "opentelemetry/exporters/otlp/otlp_grpc_client_factory.h" #include "opentelemetry/exporters/otlp/otlp_grpc_exporter_factory.h" #include "opentelemetry/exporters/otlp/otlp_grpc_exporter_options.h" @@ -112,12 +112,17 @@ used as follows from the application code: namespace otlp = opentelemetry::exporter::otlp; void SetupOtlp() { + otlp::OtlpGrpcClientOptions client_opts; otlp::OtlpGrpcExporterOptions trace_opts; otlp::OtlpGrpcLogRecordExporterOptions log_opts; - // Setting trace_opts and log_opts + // Setting client_opts, trace_opts and log_opts + // client_opts.endpoint = "localhost:1234"; + // Or we can use client_opts = trace_opts; to copy options from environment of + // trace OTLP exporter. + std::shared_ptr shared_client = - otlp::OtlpGrpcClientFactory::Create(trace_opts); + otlp::OtlpGrpcClientFactory::Create(client_opts); // Create exporters auto trace_exporter = @@ -129,8 +134,8 @@ void SetupOtlp() { } ``` -Be careful, create OTLP exporters with a existed `OtlpGrpcClient` will ignore -the options of gRPC when pass the `OtlpGrpcExporterOptions` or other option +Be careful, create OTLP exporters with an existing `OtlpGrpcClient` will ignore +the options of gRPC when passing the `OtlpGrpcExporterOptions` or other option object. ## SDK extension