NVIDIA DeepStream SDK API Reference

9.2 Release
includes/opentelemetry/exporters/otlp/otlp_http_client.h
Go to the documentation of this file.
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3 
4 #pragma once
5 
6 #include <atomic>
7 #include <chrono>
8 #include <condition_variable>
9 #include <cstddef>
10 #include <functional>
11 #include <list>
12 #include <memory>
13 #include <mutex>
14 #include <string>
15 #include <unordered_map>
16 
17 #include "opentelemetry/exporters/otlp/otlp_environment.h"
18 #include "opentelemetry/exporters/otlp/otlp_http.h"
19 #include "opentelemetry/ext/http/client/http_client.h"
20 #include "opentelemetry/nostd/string_view.h"
21 #include "opentelemetry/nostd/variant.h"
22 #include "opentelemetry/sdk/common/exporter_utils.h"
23 #include "opentelemetry/sdk/common/thread_instrumentation.h"
24 #include "opentelemetry/version.h"
25 
26 // forward declare google::protobuf::Message
27 namespace google
28 {
29 namespace protobuf
30 {
31 class Message;
32 }
33 } // namespace google
34 
36 namespace exporter
37 {
38 namespace otlp
39 {
40 // The default URL path to post metric data.
41 constexpr char kDefaultMetricsPath[] = "/v1/metrics";
42 // The HTTP header "Content-Type"
43 constexpr char kHttpJsonContentType[] = "application/json";
44 constexpr char kHttpBinaryContentType[] = "application/x-protobuf";
45 
50 {
51  std::string url;
52 
55 
56  // By default, post binary data
58 
59  // If convert bytes into hex. By default, we will convert all bytes but id into base64
60  // This option is ignored if content_type is not kJson
62 
63  // By default, do not compress data
64  std::string compression = "none";
65 
66  // If using the json name of protobuf field to set the key of json. By default, we will use the
67  // field name just like proto files.
68  bool use_json_name = false;
69 
70  // Whether to print the status of the HTTP client in the console
71  bool console_debug = false;
72 
73  std::chrono::system_clock::duration timeout;
74 
75  // Additional HTTP headers
77 
78  // Retry policy for select failure codes
80 
81  // Concurrent requests
82  std::size_t max_concurrent_requests = 64;
83 
84  // Requests per connections
85  std::size_t max_requests_per_connection = 8;
86 
87  // User agent
88  std::string user_agent;
89 
90  std::shared_ptr<sdk::common::ThreadInstrumentation> thread_instrumentation =
91  std::shared_ptr<sdk::common::ThreadInstrumentation>(nullptr);
92 
94  nostd::string_view input_url,
95  bool input_ssl_insecure_skip_verify,
96  nostd::string_view input_ssl_ca_cert_path,
97  nostd::string_view input_ssl_ca_cert_string,
98  nostd::string_view input_ssl_client_key_path,
99  nostd::string_view input_ssl_client_key_string,
100  nostd::string_view input_ssl_client_cert_path,
101  nostd::string_view input_ssl_client_cert_string,
102  nostd::string_view input_ssl_min_tls,
103  nostd::string_view input_ssl_max_tls,
104  nostd::string_view input_ssl_cipher,
105  nostd::string_view input_ssl_cipher_suite,
106  HttpRequestContentType input_content_type,
107  JsonBytesMappingKind input_json_bytes_mapping,
108  nostd::string_view input_compression,
109  bool input_use_json_name,
110  bool input_console_debug,
111  std::chrono::system_clock::duration input_timeout,
112  const OtlpHeaders &input_http_headers,
113  std::uint32_t input_retry_policy_max_attempts,
114  std::chrono::duration<float> input_retry_policy_initial_backoff,
115  std::chrono::duration<float> input_retry_policy_max_backoff,
116  float input_retry_policy_backoff_multiplier,
117  const std::shared_ptr<sdk::common::ThreadInstrumentation> &input_thread_instrumentation,
118  std::size_t input_concurrent_sessions = 64,
119  std::size_t input_max_requests_per_connection = 8,
120  nostd::string_view input_user_agent = GetOtlpDefaultUserAgent())
121  : url(input_url),
122  ssl_options(input_url,
123  input_ssl_insecure_skip_verify,
124  input_ssl_ca_cert_path,
125  input_ssl_ca_cert_string,
126  input_ssl_client_key_path,
127  input_ssl_client_key_string,
128  input_ssl_client_cert_path,
129  input_ssl_client_cert_string,
130  input_ssl_min_tls,
131  input_ssl_max_tls,
132  input_ssl_cipher,
133  input_ssl_cipher_suite),
134  content_type(input_content_type),
135  json_bytes_mapping(input_json_bytes_mapping),
136  compression(input_compression),
137  use_json_name(input_use_json_name),
138  console_debug(input_console_debug),
139  timeout(input_timeout),
140  http_headers(input_http_headers),
141  retry_policy{input_retry_policy_max_attempts, input_retry_policy_initial_backoff,
142  input_retry_policy_max_backoff, input_retry_policy_backoff_multiplier},
143  max_concurrent_requests(input_concurrent_sessions),
144  max_requests_per_connection(input_max_requests_per_connection),
145  user_agent(input_user_agent),
146  thread_instrumentation(input_thread_instrumentation)
147  {}
148 };
149 
154 {
155 public:
159  explicit OtlpHttpClient(OtlpHttpClientOptions &&options);
160 
161  ~OtlpHttpClient();
162  OtlpHttpClient(const OtlpHttpClient &) = delete;
163  OtlpHttpClient &operator=(const OtlpHttpClient &) = delete;
164  OtlpHttpClient(OtlpHttpClient &&) = delete;
166 
173  sdk::common::ExportResult Export(const google::protobuf::Message &message) noexcept;
174 
183  const google::protobuf::Message &message,
184  std::function<bool(opentelemetry::sdk::common::ExportResult)> &&result_callback) noexcept;
185 
195  const google::protobuf::Message &message,
196  std::function<bool(opentelemetry::sdk::common::ExportResult)> &&result_callback,
197  std::size_t max_running_requests) noexcept;
198 
202  bool ForceFlush(std::chrono::microseconds timeout = (std::chrono::microseconds::max)()) noexcept;
203 
210  bool Shutdown(std::chrono::microseconds timeout = std::chrono::microseconds(0)) noexcept;
211 
217  void ReleaseSession(const opentelemetry::ext::http::client::Session &session) noexcept;
218 
223  inline const OtlpHttpClientOptions &GetOptions() const noexcept { return options_; }
224 
229  bool IsShutdown() const noexcept;
230 
231 private:
232  struct HttpSessionData
233  {
234  std::shared_ptr<opentelemetry::ext::http::client::Session> session;
235  std::shared_ptr<opentelemetry::ext::http::client::EventHandler> event_handle;
236 
237  HttpSessionData() = default;
238 
239  explicit HttpSessionData(
240  std::shared_ptr<opentelemetry::ext::http::client::Session> &&input_session,
241  std::shared_ptr<opentelemetry::ext::http::client::EventHandler> &&input_handle)
242  : session(std::move(input_session)), event_handle(std::move(input_handle))
243  {}
244  };
245 
252  const google::protobuf::Message &message,
253  std::function<bool(opentelemetry::sdk::common::ExportResult)> &&result_callback) noexcept;
254 
259  void addSession(HttpSessionData &&session_data) noexcept;
260 
267  bool cleanupGCSessions() noexcept;
268 
269  // For testing
273 
281  std::shared_ptr<ext::http::client::HttpClient> http_client);
282 
283  // Stores if this HTTP client had its Shutdown() method called
284  std::atomic<bool> is_shutdown_;
285 
286  // The configuration options associated with this HTTP client.
287  const OtlpHttpClientOptions options_;
288 
289  // Object that stores the HTTP sessions that have been created
290  std::shared_ptr<ext::http::client::HttpClient> http_client_;
291 
292  // Cached parsed URI
293  std::string http_uri_;
294 
295  // Running sessions and event handles
296  std::unordered_map<const opentelemetry::ext::http::client::Session *, HttpSessionData>
297  running_sessions_;
298  // Sessions and event handles that are waiting to be deleted
299  std::list<HttpSessionData> gc_sessions_;
300  // Lock for running_sessions_, gc_sessions_ and http_client_
301  std::recursive_mutex session_manager_lock_;
302  // Condition variable and mutex to control the concurrency count of running sessions
303  std::mutex session_waker_lock_;
304  std::condition_variable session_waker_;
305  std::atomic<size_t> start_session_counter_;
306  std::atomic<size_t> finished_session_counter_;
307 };
308 } // namespace otlp
309 } // namespace exporter
sdk::common::ExportResult
ExportResult
ExportResult is returned as result of exporting a batch of Records.
Definition: includes/opentelemetry/sdk/common/exporter_utils.h:16
exporter::otlp::OtlpHttpClient::operator=
OtlpHttpClient & operator=(const OtlpHttpClient &)=delete
exporter::otlp::OtlpHttpClientOptions::content_type
HttpRequestContentType content_type
Definition: includes/opentelemetry/exporters/otlp/otlp_http_client.h:57
ext
Definition: includes/opentelemetry/ext/http/client/http_client.h:63
exporter::otlp::OtlpHttpClient
The OTLP HTTP client exports span data in OpenTelemetry Protocol (OTLP) format.
Definition: includes/opentelemetry/exporters/otlp/otlp_http_client.h:153
exporter::otlp::JsonBytesMappingKind
JsonBytesMappingKind
Definition: includes/opentelemetry/exporters/otlp/otlp_http.h:15
absl::OTABSL_OPTION_NAMESPACE_NAME::variant
Definition: includes/opentelemetry/nostd/variant.h:61
exporter::otlp::OtlpHttpClient::ForceFlush
bool ForceFlush(std::chrono::microseconds timeout=(std::chrono::microseconds::max)()) noexcept
Force flush the HTTP client.
ext::http::client::HttpSslOptions
Definition: includes/opentelemetry/ext/http/client/http_client.h:121
exporter::otlp::OtlpHttpClientOptions::url
std::string url
Definition: includes/opentelemetry/exporters/otlp/otlp_http_client.h:51
nostd::shared_ptr
std::shared_ptr< _Types... > shared_ptr
Definition: includes/opentelemetry/std/shared_ptr.h:19
sdk::configuration::OtlpHttpEncoding::protobuf
@ protobuf
exporter::otlp::kHttpJsonContentType
constexpr char kHttpJsonContentType[]
Definition: includes/opentelemetry/exporters/otlp/otlp_http_client.h:43
exporter
Definition: includes/opentelemetry/exporters/memory/in_memory_data.h:11
exporter::otlp::OtlpHttpClientOptions::retry_policy
ext::http::client::RetryPolicy retry_policy
Definition: includes/opentelemetry/exporters/otlp/otlp_http_client.h:79
exporter::otlp::OtlpHttpClient::OtlpHttpLogRecordExporterTestPeer
friend class OtlpHttpLogRecordExporterTestPeer
Definition: includes/opentelemetry/exporters/otlp/otlp_http_client.h:271
OPENTELEMETRY_BEGIN_NAMESPACE
#define OPENTELEMETRY_BEGIN_NAMESPACE
Definition: includes/opentelemetry/version.h:21
exporter::otlp::HttpRequestContentType::kBinary
@ kBinary
exporter::otlp::OtlpHeaders
std::multimap< std::string, std::string, cmp_ic > OtlpHeaders
Definition: includes/opentelemetry/exporters/otlp/otlp_environment.h:139
ext::http::client::RetryPolicy
Definition: includes/opentelemetry/ext/http/client/http_client.h:231
exporter::otlp::OtlpHttpClient::GetOptions
const OtlpHttpClientOptions & GetOptions() const noexcept
Get options of current OTLP http client.
Definition: includes/opentelemetry/exporters/otlp/otlp_http_client.h:223
exporter::otlp::OtlpHttpClient::IsShutdown
bool IsShutdown() const noexcept
Get if this OTLP http client is shutdown.
exporter::otlp::OtlpHttpClientOptions::timeout
std::chrono::system_clock::duration timeout
Definition: includes/opentelemetry/exporters/otlp/otlp_http_client.h:73
exporter::otlp::OtlpHttpClientOptions::json_bytes_mapping
JsonBytesMappingKind json_bytes_mapping
Definition: includes/opentelemetry/exporters/otlp/otlp_http_client.h:61
max
int max(int a, int b)
Compute the component-wise maximum.
Definition: sources/libs/nvmultiobjecttracker/include/modules/cuDCFv2/helper_math.h:60
exporter::otlp::OtlpHttpClient::~OtlpHttpClient
~OtlpHttpClient()
exporter::otlp::OtlpHttpClientOptions::max_requests_per_connection
std::size_t max_requests_per_connection
Definition: includes/opentelemetry/exporters/otlp/otlp_http_client.h:85
exporter::otlp::OtlpHttpClientOptions
Struct to hold OTLP HTTP client options.
Definition: includes/opentelemetry/exporters/otlp/otlp_http_client.h:49
OPENTELEMETRY_END_NAMESPACE
#define OPENTELEMETRY_END_NAMESPACE
Definition: includes/opentelemetry/version.h:24
exporter::otlp::OtlpHttpClientOptions::user_agent
std::string user_agent
Definition: includes/opentelemetry/exporters/otlp/otlp_http_client.h:88
exporter::otlp::OtlpHttpClient::OtlpHttpClient
OtlpHttpClient(OtlpHttpClientOptions &&options)
Create an OtlpHttpClient using the given options.
exporter::otlp::OtlpHttpClientOptions::console_debug
bool console_debug
Definition: includes/opentelemetry/exporters/otlp/otlp_http_client.h:71
absl::move
constexpr absl::OTABSL_OPTION_NAMESPACE_NAME::remove_reference_t< T > && move(T &&t) noexcept
Definition: includes/opentelemetry/nostd/internal/absl/utility/utility.h:199
exporter::otlp::GetOtlpDefaultUserAgent
std::string GetOtlpDefaultUserAgent()
Definition: includes/opentelemetry/exporters/otlp/otlp_environment.h:21
exporter::otlp::OtlpHttpClientOptions::OtlpHttpClientOptions
OtlpHttpClientOptions(nostd::string_view input_url, bool input_ssl_insecure_skip_verify, nostd::string_view input_ssl_ca_cert_path, nostd::string_view input_ssl_ca_cert_string, nostd::string_view input_ssl_client_key_path, nostd::string_view input_ssl_client_key_string, nostd::string_view input_ssl_client_cert_path, nostd::string_view input_ssl_client_cert_string, nostd::string_view input_ssl_min_tls, nostd::string_view input_ssl_max_tls, nostd::string_view input_ssl_cipher, nostd::string_view input_ssl_cipher_suite, HttpRequestContentType input_content_type, JsonBytesMappingKind input_json_bytes_mapping, nostd::string_view input_compression, bool input_use_json_name, bool input_console_debug, std::chrono::system_clock::duration input_timeout, const OtlpHeaders &input_http_headers, std::uint32_t input_retry_policy_max_attempts, std::chrono::duration< float > input_retry_policy_initial_backoff, std::chrono::duration< float > input_retry_policy_max_backoff, float input_retry_policy_backoff_multiplier, const std::shared_ptr< sdk::common::ThreadInstrumentation > &input_thread_instrumentation, std::size_t input_concurrent_sessions=64, std::size_t input_max_requests_per_connection=8, nostd::string_view input_user_agent=GetOtlpDefaultUserAgent())
Definition: includes/opentelemetry/exporters/otlp/otlp_http_client.h:93
std
Extensions to the C++ standard library.
Definition: includes/opentelemetry/nostd/string_view.h:212
exporter::otlp::OtlpHttpClient::OtlpHttpExporterTestPeer
friend class OtlpHttpExporterTestPeer
Definition: includes/opentelemetry/exporters/otlp/otlp_http_client.h:270
exporter::otlp::OtlpHttpClientOptions::compression
std::string compression
Definition: includes/opentelemetry/exporters/otlp/otlp_http_client.h:64
exporter::otlp::HttpRequestContentType
HttpRequestContentType
Definition: includes/opentelemetry/exporters/otlp/otlp_http.h:22
exporter::otlp::OtlpHttpClient::ReleaseSession
void ReleaseSession(const opentelemetry::ext::http::client::Session &session) noexcept
Release the lifetime of specify session.
exporter::otlp::OtlpHttpClientOptions::thread_instrumentation
std::shared_ptr< sdk::common::ThreadInstrumentation > thread_instrumentation
Definition: includes/opentelemetry/exporters/otlp/otlp_http_client.h:90
exporter::otlp::OtlpHttpClient::OtlpHttpMetricExporterTestPeer
friend class OtlpHttpMetricExporterTestPeer
Definition: includes/opentelemetry/exporters/otlp/otlp_http_client.h:272
exporter::otlp::OtlpHttpClientOptions::max_concurrent_requests
std::size_t max_concurrent_requests
Definition: includes/opentelemetry/exporters/otlp/otlp_http_client.h:82
exporter::otlp::kHttpBinaryContentType
constexpr char kHttpBinaryContentType[]
Definition: includes/opentelemetry/exporters/otlp/otlp_http_client.h:44
exporter::otlp::OtlpHttpClientOptions::ssl_options
ext::http::client::HttpSslOptions ssl_options
SSL options.
Definition: includes/opentelemetry/exporters/otlp/otlp_http_client.h:54
exporter::otlp::OtlpHttpClient::Shutdown
bool Shutdown(std::chrono::microseconds timeout=std::chrono::microseconds(0)) noexcept
Shut down the HTTP client.
opentelemetry
Definition: includes/opentelemetry/exporters/otlp/otlp_grpc_client.h:38
exporter::otlp::JsonBytesMappingKind::kHexId
@ kHexId
exporter::otlp::OtlpHttpClientOptions::use_json_name
bool use_json_name
Definition: includes/opentelemetry/exporters/otlp/otlp_http_client.h:68
exporter::otlp::OtlpHttpClient::Export
sdk::common::ExportResult Export(const google::protobuf::Message &message) noexcept
Sync export.
exporter::otlp::OtlpHttpClientOptions::http_headers
OtlpHeaders http_headers
Definition: includes/opentelemetry/exporters/otlp/otlp_http_client.h:76
google
Definition: includes/opentelemetry/exporters/otlp/otlp_grpc_client.h:30
exporter::otlp::kDefaultMetricsPath
constexpr char kDefaultMetricsPath[]
Definition: includes/opentelemetry/exporters/otlp/otlp_http_client.h:41
nostd::string_view
Back port of std::string_view to work with pre-cpp-17 compilers.
Definition: includes/opentelemetry/nostd/string_view.h:37