@@ -78,14 +78,18 @@ class OPENTELEMETRY_LOCAL_SYMBOL OtlpGrpcAsyncCallData : public OtlpGrpcAsyncCal
7878 virtual ~OtlpGrpcAsyncCallData () {}
7979};
8080} // namespace
81+ #endif
8182
8283struct OtlpGrpcClientAsyncData
8384{
85+
8486 std::chrono::system_clock::duration export_timeout = std::chrono::seconds{10 };
8587
8688 // The best performance trade-off of gRPC is having numcpu's threads and one completion queue
8789 // per thread, but this exporter should not cost a lot resource and we don't want to create
88- // too many threads in the process. So we use one completion queue.
90+ // too many threads in the process. So we use one completion queue and shared context.
91+ std::shared_ptr<grpc::Channel> channel;
92+ #ifdef ENABLE_ASYNC_EXPORT
8993 grpc::CompletionQueue cq;
9094
9195 // Running requests, this is used to limit the number of concurrent requests.
@@ -98,6 +102,7 @@ struct OtlpGrpcClientAsyncData
98102 // Condition variable and mutex to control the concurrency count of running requests.
99103 std::mutex session_waker_lock;
100104 std::condition_variable session_waker;
105+ #endif
101106
102107 // Reference count of OtlpGrpcClient
103108 std::atomic<int64_t > reference_count{0 };
@@ -107,7 +112,6 @@ struct OtlpGrpcClientAsyncData
107112 // https://stackoverflow.com/questions/53408962/try-to-understand-compiler-error-message-default-member-initializer-required-be
108113 OtlpGrpcClientAsyncData () {}
109114};
110- #endif
111115
112116namespace
113117{
@@ -243,24 +247,23 @@ static sdk::common::ExportResult InternalDelegateAsyncExport(
243247#endif
244248} // namespace
245249
246- #ifdef ENABLE_ASYNC_EXPORT
247250OtlpGrpcClientReferenceGuard::OtlpGrpcClientReferenceGuard () noexcept : has_value_{false } {}
248251
249252OtlpGrpcClientReferenceGuard::~OtlpGrpcClientReferenceGuard () noexcept {}
250- #endif
251253
252- OtlpGrpcClient::OtlpGrpcClient ()
253- # ifdef ENABLE_ASYNC_EXPORT
254- : is_shutdown_( false )
255- # endif
256- { }
254+ OtlpGrpcClient::OtlpGrpcClient (const OtlpGrpcClientOptions &options) : is_shutdown_( false )
255+ {
256+ std::shared_ptr<OtlpGrpcClientAsyncData> async_data = MutableAsyncData (options);
257+ async_data-> channel = MakeChannel (options);
258+ }
257259
258260OtlpGrpcClient::~OtlpGrpcClient ()
259261{
260- # ifdef ENABLE_ASYNC_EXPORT
262+
261263 std::shared_ptr<OtlpGrpcClientAsyncData> async_data;
262264 async_data.swap (async_data_);
263265
266+ #ifdef ENABLE_ASYNC_EXPORT
264267 while (async_data && async_data->running_requests .load (std::memory_order_acquire) > 0 )
265268 {
266269 std::unique_lock<std::mutex> lock{async_data->session_waker_lock };
@@ -359,21 +362,33 @@ std::unique_ptr<grpc::ClientContext> OtlpGrpcClient::MakeClientContext(
359362}
360363
361364std::unique_ptr<proto::collector::trace::v1::TraceService::StubInterface>
362- OtlpGrpcClient::MakeTraceServiceStub (const OtlpGrpcClientOptions &options )
365+ OtlpGrpcClient::MakeTraceServiceStub ()
363366{
364- return proto::collector::trace::v1::TraceService::NewStub (MakeChannel (options));
367+ if (!async_data_ || !async_data_->channel )
368+ {
369+ return nullptr ;
370+ }
371+ return proto::collector::trace::v1::TraceService::NewStub (async_data_->channel );
365372}
366373
367374std::unique_ptr<proto::collector::metrics::v1::MetricsService::StubInterface>
368- OtlpGrpcClient::MakeMetricsServiceStub (const OtlpGrpcClientOptions &options )
375+ OtlpGrpcClient::MakeMetricsServiceStub ()
369376{
370- return proto::collector::metrics::v1::MetricsService::NewStub (MakeChannel (options));
377+ if (!async_data_ || !async_data_->channel )
378+ {
379+ return nullptr ;
380+ }
381+ return proto::collector::metrics::v1::MetricsService::NewStub (async_data_->channel );
371382}
372383
373384std::unique_ptr<proto::collector::logs::v1::LogsService::StubInterface>
374- OtlpGrpcClient::MakeLogsServiceStub (const OtlpGrpcClientOptions &options )
385+ OtlpGrpcClient::MakeLogsServiceStub ()
375386{
376- return proto::collector::logs::v1::LogsService::NewStub (MakeChannel (options));
387+ if (!async_data_ || !async_data_->channel )
388+ {
389+ return nullptr ;
390+ }
391+ return proto::collector::logs::v1::LogsService::NewStub (async_data_->channel );
377392}
378393
379394grpc::Status OtlpGrpcClient::DelegateExport (
@@ -406,8 +421,6 @@ grpc::Status OtlpGrpcClient::DelegateExport(
406421 return stub->Export (context.get (), request, response);
407422}
408423
409- #ifdef ENABLE_ASYNC_EXPORT
410-
411424void OtlpGrpcClient::AddReference (OtlpGrpcClientReferenceGuard &guard,
412425 const OtlpGrpcClientOptions &options) noexcept
413426{
@@ -437,6 +450,8 @@ bool OtlpGrpcClient::RemoveReference(OtlpGrpcClientReferenceGuard &guard) noexce
437450 return true ;
438451}
439452
453+ #ifdef ENABLE_ASYNC_EXPORT
454+
440455/* *
441456 * Async export
442457 * @param options Options used to message to create gRPC context and stub(if necessary)
@@ -538,6 +553,8 @@ sdk::common::ExportResult OtlpGrpcClient::DelegateAsyncExport(
538553 " log(s)" );
539554}
540555
556+ #endif
557+
541558std::shared_ptr<OtlpGrpcClientAsyncData> OtlpGrpcClient::MutableAsyncData (
542559 const OtlpGrpcClientOptions &options)
543560{
@@ -556,13 +573,15 @@ bool OtlpGrpcClient::IsShutdown() const noexcept
556573 return is_shutdown_.load (std::memory_order_acquire);
557574}
558575
559- bool OtlpGrpcClient::ForceFlush (std::chrono::microseconds timeout) noexcept
576+ bool OtlpGrpcClient::ForceFlush (
577+ OPENTELEMETRY_MAYBE_UNUSED std::chrono::microseconds timeout) noexcept
560578{
561579 if (!async_data_)
562580 {
563581 return true ;
564582 }
565583
584+ #ifdef ENABLE_ASYNC_EXPORT
566585 std::size_t request_counter = async_data_->start_request_counter .load (std::memory_order_acquire);
567586 if (request_counter <= async_data_->finished_request_counter .load (std::memory_order_acquire))
568587 {
@@ -601,16 +620,20 @@ bool OtlpGrpcClient::ForceFlush(std::chrono::microseconds timeout) noexcept
601620 }
602621
603622 return timeout_steady > std::chrono::steady_clock::duration::zero ();
623+ #else
624+ return true ;
625+ #endif
604626}
605627
606628bool OtlpGrpcClient::Shutdown (OtlpGrpcClientReferenceGuard &guard,
607- std::chrono::microseconds timeout) noexcept
629+ OPENTELEMETRY_MAYBE_UNUSED std::chrono::microseconds timeout) noexcept
608630{
609631 if (!async_data_)
610632 {
611633 return true ;
612634 }
613635
636+ #ifdef ENABLE_ASYNC_EXPORT
614637 bool last_reference_removed = RemoveReference (guard);
615638 bool force_flush_result;
616639 if (last_reference_removed && false == is_shutdown_.exchange (true , std::memory_order_acq_rel))
@@ -625,9 +648,11 @@ bool OtlpGrpcClient::Shutdown(OtlpGrpcClientReferenceGuard &guard,
625648 }
626649
627650 return force_flush_result;
628- }
629-
651+ #else
652+ RemoveReference (guard);
653+ return true ;
630654#endif
655+ }
631656
632657} // namespace otlp
633658} // namespace exporter
0 commit comments