Skip to content

Commit 213e3cf

Browse files
committed
Allow to share gRPC clients between OTLP exporters.
1 parent a920898 commit 213e3cf

22 files changed

+815
-57
lines changed

exporters/otlp/BUILD

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,13 @@ cc_library(
4242
name = "otlp_grpc_client",
4343
srcs = [
4444
"src/otlp_grpc_client.cc",
45+
"src/otlp_grpc_client_factory.cc",
4546
"src/otlp_grpc_utils.cc",
4647
],
4748
hdrs = [
4849
"include/opentelemetry/exporters/otlp/otlp_environment.h",
4950
"include/opentelemetry/exporters/otlp/otlp_grpc_client.h",
51+
"include/opentelemetry/exporters/otlp/otlp_grpc_client_factory.h",
5052
"include/opentelemetry/exporters/otlp/otlp_grpc_client_options.h",
5153
"include/opentelemetry/exporters/otlp/otlp_grpc_utils.h",
5254
"include/opentelemetry/exporters/otlp/protobuf_include_prefix.h",

exporters/otlp/CMakeLists.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@ target_link_libraries(opentelemetry_otlp_recordable
2323

2424
if(WITH_OTLP_GRPC)
2525
find_package(gRPC REQUIRED)
26-
add_library(opentelemetry_exporter_otlp_grpc_client src/otlp_grpc_client.cc
27-
src/otlp_grpc_utils.cc)
26+
add_library(
27+
opentelemetry_exporter_otlp_grpc_client
28+
src/otlp_grpc_client.cc src/otlp_grpc_client_factory.cc
29+
src/otlp_grpc_utils.cc)
2830
set_target_properties(opentelemetry_exporter_otlp_grpc_client
2931
PROPERTIES EXPORT_NAME otlp_grpc_client)
3032
set_target_version(opentelemetry_exporter_otlp_grpc_client)

exporters/otlp/include/opentelemetry/exporters/otlp/otlp_grpc_client.h

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,27 @@ namespace exporter
2828
namespace otlp
2929
{
3030

31+
class OtlpGrpcClient;
3132
struct OtlpGrpcClientOptions;
3233

3334
#ifdef ENABLE_ASYNC_EXPORT
3435
struct OtlpGrpcClientAsyncData;
36+
37+
class OtlpGrpcClientReferenceGuard
38+
{
39+
public:
40+
OtlpGrpcClientReferenceGuard() noexcept;
41+
~OtlpGrpcClientReferenceGuard() noexcept;
42+
43+
OtlpGrpcClientReferenceGuard(const OtlpGrpcClientReferenceGuard &) = delete;
44+
OtlpGrpcClientReferenceGuard(OtlpGrpcClientReferenceGuard &&) = delete;
45+
OtlpGrpcClientReferenceGuard &operator=(const OtlpGrpcClientReferenceGuard &) = delete;
46+
OtlpGrpcClientReferenceGuard &operator=(OtlpGrpcClientReferenceGuard &&) = delete;
47+
48+
private:
49+
friend class OtlpGrpcClient;
50+
std::atomic<bool> has_value_;
51+
};
3552
#endif
3653

3754
/**
@@ -96,6 +113,17 @@ class OtlpGrpcClient
96113

97114
#ifdef ENABLE_ASYNC_EXPORT
98115

116+
void AddReference(OtlpGrpcClientReferenceGuard &guard,
117+
const OtlpGrpcClientOptions &options) noexcept;
118+
119+
/**
120+
* Reomve reference fro a guard object
121+
*
122+
* @param guard guard object to remove reference from
123+
* @return true if there is no more reference to this gRPC client
124+
*/
125+
bool RemoveReference(OtlpGrpcClientReferenceGuard &guard) noexcept;
126+
99127
/**
100128
* Async export
101129
* @param options Options used to message to create gRPC context and stub(if necessary)
@@ -167,10 +195,13 @@ class OtlpGrpcClient
167195
* timeout is applied.
168196
* @return return the status of this operation
169197
*/
170-
bool Shutdown(std::chrono::microseconds timeout = std::chrono::microseconds(0)) noexcept;
198+
bool Shutdown(OtlpGrpcClientReferenceGuard &guard,
199+
std::chrono::microseconds timeout = std::chrono::microseconds(0)) noexcept;
171200

172201
std::shared_ptr<OtlpGrpcClientAsyncData> MutableAsyncData(const OtlpGrpcClientOptions &options);
173202

203+
bool IsShutdown() const noexcept;
204+
174205
private:
175206
// Stores if this gRPC client had its Shutdown() method called
176207
std::atomic<bool> is_shutdown_;
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
#pragma once
5+
6+
#include <memory>
7+
8+
#include "opentelemetry/nostd/shared_ptr.h"
9+
10+
OPENTELEMETRY_BEGIN_NAMESPACE
11+
namespace exporter
12+
{
13+
namespace otlp
14+
{
15+
16+
#ifdef ENABLE_ASYNC_EXPORT
17+
class OtlpGrpcClientReferenceGuard;
18+
#endif
19+
class OtlpGrpcClient;
20+
21+
/**
22+
* Factory class for OtlpGrpcClient.
23+
*/
24+
class OPENTELEMETRY_EXPORT OtlpGrpcClientFactory
25+
{
26+
public:
27+
/**
28+
* Create an OtlpGrpcClient using all default options.
29+
*/
30+
static nostd::shared_ptr<OtlpGrpcClient> Create();
31+
32+
#ifdef ENABLE_ASYNC_EXPORT
33+
static nostd::shared_ptr<OtlpGrpcClientReferenceGuard> CreateReferenceGuard();
34+
#endif
35+
};
36+
37+
} // namespace otlp
38+
} // namespace exporter
39+
OPENTELEMETRY_END_NAMESPACE

exporters/otlp/include/opentelemetry/exporters/otlp/otlp_grpc_exporter.h

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,17 @@
66
#include <atomic>
77
#include <chrono>
88

9+
// clang-format off
910
#include "opentelemetry/exporters/otlp/protobuf_include_prefix.h"
11+
// clang-format on
1012

1113
#include "opentelemetry/proto/collector/trace/v1/trace_service.grpc.pb.h"
1214

15+
// clang-format off
1316
#include "opentelemetry/exporters/otlp/protobuf_include_suffix.h"
17+
// clang-format on
1418

19+
#include "opentelemetry/nostd/shared_ptr.h"
1520
#include "opentelemetry/sdk/trace/exporter.h"
1621

1722
#include "opentelemetry/exporters/otlp/otlp_environment.h"
@@ -23,6 +28,9 @@ namespace exporter
2328
namespace otlp
2429
{
2530

31+
#ifdef ENABLE_ASYNC_EXPORT
32+
class OtlpGrpcClientReferenceGuard;
33+
#endif
2634
class OtlpGrpcClient;
2735

2836
/**
@@ -36,11 +44,24 @@ class OtlpGrpcExporter final : public opentelemetry::sdk::trace::SpanExporter
3644
*/
3745
OtlpGrpcExporter();
3846

47+
#ifdef ENABLE_ASYNC_EXPORT
48+
/**
49+
* Create an OtlpGrpcExporter using specified OtlpGrpcClient.
50+
*
51+
* @param options options to create exporter
52+
* @param client the gRPC client to use
53+
*/
54+
OtlpGrpcExporter(const OtlpGrpcExporterOptions &options,
55+
nostd::shared_ptr<OtlpGrpcClient> client);
56+
#endif
57+
3958
/**
4059
* Create an OtlpGrpcExporter using the given options.
4160
*/
4261
explicit OtlpGrpcExporter(const OtlpGrpcExporterOptions &options);
4362

63+
~OtlpGrpcExporter() override;
64+
4465
/**
4566
* Create a span recordable.
4667
* @return a newly initialized Recordable object
@@ -71,27 +92,49 @@ class OtlpGrpcExporter final : public opentelemetry::sdk::trace::SpanExporter
7192
bool Shutdown(
7293
std::chrono::microseconds timeout = (std::chrono::microseconds::max)()) noexcept override;
7394

95+
#ifdef ENABLE_ASYNC_EXPORT
96+
/**
97+
* Get the Client object
98+
*
99+
* @return return binded gRPC client
100+
*/
101+
const nostd::shared_ptr<OtlpGrpcClient> &GetClient() const noexcept;
102+
#endif
103+
74104
private:
75105
// The configuration options associated with this exporter.
76106
const OtlpGrpcExporterOptions options_;
77107

78108
#ifdef ENABLE_ASYNC_EXPORT
79-
std::shared_ptr<OtlpGrpcClient> client_;
109+
nostd::shared_ptr<OtlpGrpcClient> client_;
110+
nostd::shared_ptr<OtlpGrpcClientReferenceGuard> client_reference_guard_;
80111
#endif
81112

82113
// For testing
83114
friend class OtlpGrpcExporterTestPeer;
84115
friend class OtlpGrpcLogRecordExporterTestPeer;
85116

86117
// Store service stub internally. Useful for testing.
87-
std::unique_ptr<proto::collector::trace::v1::TraceService::StubInterface> trace_service_stub_;
118+
nostd::shared_ptr<proto::collector::trace::v1::TraceService::StubInterface> trace_service_stub_;
88119

89120
/**
90121
* Create an OtlpGrpcExporter using the specified service stub.
91122
* Only tests can call this constructor directly.
92123
* @param stub the service stub to be used for exporting
93124
*/
94125
OtlpGrpcExporter(std::unique_ptr<proto::collector::trace::v1::TraceService::StubInterface> stub);
126+
127+
#ifdef ENABLE_ASYNC_EXPORT
128+
/**
129+
* Create an OtlpGrpcExporter using the specified service stub and gRPC client.
130+
* Only tests can call this constructor directly.
131+
* @param stub the service stub to be used for exporting
132+
* @param client the gRPC client to use
133+
*/
134+
OtlpGrpcExporter(std::unique_ptr<proto::collector::trace::v1::TraceService::StubInterface> stub,
135+
nostd::shared_ptr<OtlpGrpcClient> client);
136+
#endif
137+
95138
std::atomic<bool> is_shutdown_{false};
96139
bool isShutdown() const noexcept;
97140
};

exporters/otlp/include/opentelemetry/exporters/otlp/otlp_grpc_exporter_factory.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <memory>
77

88
#include "opentelemetry/exporters/otlp/otlp_grpc_exporter_options.h"
9+
#include "opentelemetry/nostd/shared_ptr.h"
910
#include "opentelemetry/sdk/trace/exporter.h"
1011

1112
OPENTELEMETRY_BEGIN_NAMESPACE
@@ -14,6 +15,8 @@ namespace exporter
1415
namespace otlp
1516
{
1617

18+
class OtlpGrpcClient;
19+
1720
/**
1821
* Factory class for OtlpGrpcExporter.
1922
*/
@@ -30,6 +33,15 @@ class OPENTELEMETRY_EXPORT OtlpGrpcExporterFactory
3033
*/
3134
static std::unique_ptr<opentelemetry::sdk::trace::SpanExporter> Create(
3235
const OtlpGrpcExporterOptions &options);
36+
37+
#ifdef ENABLE_ASYNC_EXPORT
38+
/**
39+
* Create an OtlpGrpcExporter using the given options and gRPC client.
40+
*/
41+
static std::unique_ptr<opentelemetry::sdk::trace::SpanExporter> Create(
42+
const OtlpGrpcExporterOptions &options,
43+
nostd::shared_ptr<OtlpGrpcClient> client);
44+
#endif
3345
};
3446

3547
} // namespace otlp

exporters/otlp/include/opentelemetry/exporters/otlp/otlp_grpc_log_record_exporter.h

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,18 @@
44
#pragma once
55

66
// clang-format off
7-
87
#include "opentelemetry/exporters/otlp/protobuf_include_prefix.h"
8+
// clang-format on
9+
910
#include "opentelemetry/proto/collector/logs/v1/logs_service.grpc.pb.h"
10-
#include "opentelemetry/exporters/otlp/protobuf_include_suffix.h"
1111

12+
// clang-format off
13+
#include "opentelemetry/exporters/otlp/protobuf_include_suffix.h"
1214
// clang-format on
1315

1416
#include "opentelemetry/exporters/otlp/otlp_environment.h"
1517
#include "opentelemetry/exporters/otlp/otlp_grpc_log_record_exporter_options.h"
18+
#include "opentelemetry/nostd/shared_ptr.h"
1619
#include "opentelemetry/sdk/logs/exporter.h"
1720

1821
#include <atomic>
@@ -23,6 +26,9 @@ namespace exporter
2326
namespace otlp
2427
{
2528

29+
#ifdef ENABLE_ASYNC_EXPORT
30+
class OtlpGrpcClientReferenceGuard;
31+
#endif
2632
class OtlpGrpcClient;
2733

2834
/**
@@ -36,12 +42,25 @@ class OtlpGrpcLogRecordExporter : public opentelemetry::sdk::logs::LogRecordExpo
3642
*/
3743
OtlpGrpcLogRecordExporter();
3844

45+
#ifdef ENABLE_ASYNC_EXPORT
46+
/**
47+
* Create an OtlpGrpcLogRecordExporter using specified OtlpGrpcClient.
48+
*
49+
* @param options options to create exporter
50+
* @param client the gRPC client to use
51+
*/
52+
OtlpGrpcLogRecordExporter(const OtlpGrpcLogRecordExporterOptions &options,
53+
nostd::shared_ptr<OtlpGrpcClient> client);
54+
#endif
55+
3956
/**
4057
* Create an OtlpGrpcLogRecordExporter with user specified options.
4158
* @param options An object containing the user's configuration options.
4259
*/
4360
OtlpGrpcLogRecordExporter(const OtlpGrpcLogRecordExporterOptions &options);
4461

62+
~OtlpGrpcLogRecordExporter() override;
63+
4564
/**
4665
* Creates a recordable that stores the data in protobuf.
4766
* @return a newly initialized Recordable object.
@@ -72,19 +91,29 @@ class OtlpGrpcLogRecordExporter : public opentelemetry::sdk::logs::LogRecordExpo
7291
bool Shutdown(
7392
std::chrono::microseconds timeout = (std::chrono::microseconds::max)()) noexcept override;
7493

94+
#ifdef ENABLE_ASYNC_EXPORT
95+
/**
96+
* Get the Client object
97+
*
98+
* @return return binded gRPC client
99+
*/
100+
const nostd::shared_ptr<OtlpGrpcClient> &GetClient() const noexcept;
101+
#endif
102+
75103
private:
76104
// Configuration options for the exporter
77105
const OtlpGrpcLogRecordExporterOptions options_;
78106

79107
#ifdef ENABLE_ASYNC_EXPORT
80-
std::shared_ptr<OtlpGrpcClient> client_;
108+
nostd::shared_ptr<OtlpGrpcClient> client_;
109+
nostd::shared_ptr<OtlpGrpcClientReferenceGuard> client_reference_guard_;
81110
#endif
82111

83112
// For testing
84113
friend class OtlpGrpcLogRecordExporterTestPeer;
85114

86115
// Store service stub internally. Useful for testing.
87-
std::unique_ptr<proto::collector::logs::v1::LogsService::StubInterface> log_service_stub_;
116+
nostd::shared_ptr<proto::collector::logs::v1::LogsService::StubInterface> log_service_stub_;
88117

89118
/**
90119
* Create an OtlpGrpcLogRecordExporter using the specified service stub.
@@ -93,6 +122,18 @@ class OtlpGrpcLogRecordExporter : public opentelemetry::sdk::logs::LogRecordExpo
93122
*/
94123
OtlpGrpcLogRecordExporter(
95124
std::unique_ptr<proto::collector::logs::v1::LogsService::StubInterface> stub);
125+
#ifdef ENABLE_ASYNC_EXPORT
126+
/**
127+
* Create an OtlpGrpcLogRecordExporter using the specified service stub and gRPC client.
128+
* Only tests can call this constructor directly.
129+
* @param stub the service stub to be used for exporting
130+
* @param client the gRPC client to use
131+
*/
132+
OtlpGrpcLogRecordExporter(
133+
std::unique_ptr<proto::collector::logs::v1::LogsService::StubInterface> stub,
134+
nostd::shared_ptr<OtlpGrpcClient> client);
135+
#endif
136+
96137
std::atomic<bool> is_shutdown_{false};
97138
bool isShutdown() const noexcept;
98139
};

0 commit comments

Comments
 (0)