Skip to content

Commit edb8937

Browse files
authored
[DOC] Add document and example for sharing gRPC Client (#3260)
1 parent 52a80b5 commit edb8937

File tree

2 files changed

+54
-6
lines changed

2 files changed

+54
-6
lines changed

docs/cpp-sdk-factory-design.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,50 @@ This property makes it possible to:
9494
- deploy a new SDK shared library
9595
- keep the application unchanged
9696
97+
### Case study, using Factory and shared gRPC client between OTLP gRPC exporters
98+
99+
To reduce the cost of gRPC, the SDK allow users to share gRPC clients between
100+
OTLP gRPC exporters when these exporters have the same settings. This can be
101+
used as follows from the application code:
102+
103+
```cpp
104+
// Include following headers
105+
#include "opentelemetry/exporters/otlp/otlp_grpc_client_factory.h"
106+
#include "opentelemetry/exporters/otlp/otlp_grpc_exporter_factory.h"
107+
#include "opentelemetry/exporters/otlp/otlp_grpc_exporter_options.h"
108+
#include "opentelemetry/exporters/otlp/otlp_grpc_log_record_exporter_factory.h"
109+
#include "opentelemetry/exporters/otlp/otlp_grpc_log_record_exporter_options.h"
110+
111+
// Create exporters with shared gRPC Client
112+
namespace otlp = opentelemetry::exporter::otlp;
113+
114+
void SetupOtlp() {
115+
otlp::OtlpGrpcClientOptions client_opts;
116+
otlp::OtlpGrpcExporterOptions trace_opts;
117+
otlp::OtlpGrpcLogRecordExporterOptions log_opts;
118+
119+
// Setting client_opts, trace_opts and log_opts
120+
// client_opts.endpoint = "localhost:1234";
121+
// Or we can use client_opts = trace_opts; to copy options from environment of
122+
// trace OTLP exporter.
123+
124+
std::shared_ptr<otlp::OtlpGrpcClient> shared_client =
125+
otlp::OtlpGrpcClientFactory::Create(client_opts);
126+
127+
// Create exporters
128+
auto trace_exporter =
129+
otlp::OtlpGrpcExporterFactory::Create(trace_opts, shared_client);
130+
auto log_exporter =
131+
otlp::OtlpGrpcLogRecordExporterFactory::Create(log_opts, shared_client);
132+
133+
// Other initialization codes ...
134+
}
135+
```
136+
137+
Be careful, create OTLP exporters with an existing `OtlpGrpcClient` will ignore
138+
the options of gRPC when passing the `OtlpGrpcExporterOptions` or other option
139+
object.
140+
97141
## SDK extension
98142

99143
Applications owners who want to extend existing SDK classes are expected

examples/otlp/grpc_log_main.cc

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

4+
#include "opentelemetry/exporters/otlp/otlp_grpc_client_factory.h"
45
#include "opentelemetry/exporters/otlp/otlp_grpc_exporter_factory.h"
56
#include "opentelemetry/exporters/otlp/otlp_grpc_exporter_options.h"
67
#include "opentelemetry/exporters/otlp/otlp_grpc_log_record_exporter_factory.h"
@@ -41,10 +42,10 @@ opentelemetry::exporter::otlp::OtlpGrpcLogRecordExporterOptions log_opts;
4142
std::shared_ptr<opentelemetry::sdk::trace::TracerProvider> tracer_provider;
4243
std::shared_ptr<opentelemetry::sdk::logs::LoggerProvider> logger_provider;
4344

44-
void InitTracer()
45+
void InitTracer(const std::shared_ptr<otlp::OtlpGrpcClient> &shared_client)
4546
{
4647
// Create OTLP exporter instance
47-
auto exporter = otlp::OtlpGrpcExporterFactory::Create(opts);
48+
auto exporter = otlp::OtlpGrpcExporterFactory::Create(opts, shared_client);
4849
auto processor = trace_sdk::SimpleSpanProcessorFactory::Create(std::move(exporter));
4950
tracer_provider = trace_sdk::TracerProviderFactory::Create(std::move(processor));
5051

@@ -66,10 +67,10 @@ void CleanupTracer()
6667
trace::Provider::SetTracerProvider(none);
6768
}
6869

69-
void InitLogger()
70+
void InitLogger(const std::shared_ptr<otlp::OtlpGrpcClient> &shared_client)
7071
{
7172
// Create OTLP exporter instance
72-
auto exporter = otlp::OtlpGrpcLogRecordExporterFactory::Create(log_opts);
73+
auto exporter = otlp::OtlpGrpcLogRecordExporterFactory::Create(log_opts, shared_client);
7374
auto processor = logs_sdk::SimpleLogRecordProcessorFactory::Create(std::move(exporter));
7475
logger_provider = logs_sdk::LoggerProviderFactory::Create(std::move(processor));
7576

@@ -106,8 +107,11 @@ int main(int argc, char *argv[])
106107
log_opts.ssl_credentials_cacert_path = argv[2];
107108
}
108109
}
109-
InitLogger();
110-
InitTracer();
110+
111+
std::shared_ptr<otlp::OtlpGrpcClient> shared_client = otlp::OtlpGrpcClientFactory::Create(opts);
112+
113+
InitLogger(shared_client);
114+
InitTracer(shared_client);
111115
foo_library();
112116
CleanupTracer();
113117
CleanupLogger();

0 commit comments

Comments
 (0)