NVIDIA DeepStream SDK API Reference

9.2 Release
9.2/includes/opentelemetry/sdk/logs/batch_log_record_processor.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 <stddef.h>
7 #include <atomic>
8 #include <chrono>
9 #include <condition_variable>
10 #include <cstdint>
11 #include <memory>
12 #include <mutex>
13 #include <thread>
14 
15 #include "opentelemetry/sdk/common/circular_buffer.h"
16 #include "opentelemetry/sdk/logs/batch_log_record_processor_options.h"
17 #include "opentelemetry/sdk/logs/batch_log_record_processor_runtime_options.h"
18 #include "opentelemetry/sdk/logs/exporter.h"
19 #include "opentelemetry/sdk/logs/processor.h"
20 #include "opentelemetry/sdk/logs/recordable.h"
21 #include "opentelemetry/version.h"
22 
24 namespace sdk
25 {
26 
27 namespace logs
28 {
29 
34 class BatchLogRecordProcessor : public LogRecordProcessor
35 {
36 public:
48  explicit BatchLogRecordProcessor(
49  std::unique_ptr<LogRecordExporter> &&exporter,
50  const size_t max_queue_size = 2048,
51  const std::chrono::milliseconds scheduled_delay_millis = std::chrono::milliseconds(5000),
52  const size_t max_export_batch_size = 512);
53 
61  explicit BatchLogRecordProcessor(std::unique_ptr<LogRecordExporter> &&exporter,
62  const BatchLogRecordProcessorOptions &options);
63 
72  explicit BatchLogRecordProcessor(std::unique_ptr<LogRecordExporter> &&exporter,
73  const BatchLogRecordProcessorOptions &options,
74  const BatchLogRecordProcessorRuntimeOptions &runtime_options);
75 
77  std::unique_ptr<Recordable> MakeRecordable() noexcept override;
78 
84  void OnEmit(std::unique_ptr<Recordable> &&record) noexcept override;
85 
91  bool ForceFlush(
92  std::chrono::microseconds timeout = (std::chrono::microseconds::max)()) noexcept override;
93 
102  bool Shutdown(
103  std::chrono::microseconds timeout = (std::chrono::microseconds::max)()) noexcept override;
104 
108  ~BatchLogRecordProcessor() override;
109 
110 protected:
115  bool InternalShutdown(
116  std::chrono::microseconds timeout = (std::chrono::microseconds::max)()) noexcept;
117 
121  void DoBackgroundWork();
122 
127  virtual void Export();
128 
133  void DrainQueue();
134 
135  struct SynchronizationData
136  {
137  /* Synchronization primitives */
138  std::condition_variable cv, force_flush_cv;
139  std::mutex cv_m, force_flush_cv_m, shutdown_m;
140 
141  /* Important boolean flags to handle the workflow of the processor */
142  std::atomic<bool> is_force_wakeup_background_worker{false};
143  std::atomic<bool> is_shutdown{false};
144  std::atomic<uint64_t> force_flush_pending_sequence{0};
145  std::atomic<uint64_t> force_flush_notified_sequence{0};
146  std::atomic<std::chrono::microseconds::rep> force_flush_timeout_us{0};
147 
148  // Do not use SynchronizationData() = default; here, some versions of GCC&Clang have BUGs
149  // and may not initialize the member correctly. See also
150  // https://stackoverflow.com/questions/53408962/try-to-understand-compiler-error-message-default-member-initializer-required-be
152  };
153 
161  static void NotifyCompletion(uint64_t notify_force_flush,
162  const std::unique_ptr<LogRecordExporter> &exporter,
163  const std::shared_ptr<SynchronizationData> &synchronization_data);
164 
165  void GetWaitAdjustedTime(std::chrono::microseconds &timeout,
166  std::chrono::time_point<std::chrono::system_clock> &start_time);
167 
168  /* The configured backend log exporter */
169  std::unique_ptr<LogRecordExporter> exporter_;
170 
171  /* Configurable parameters as per the official *trace* specs */
172  const size_t max_queue_size_;
173  const std::chrono::milliseconds scheduled_delay_millis_;
174  const size_t max_export_batch_size_;
175  /* The buffer/queue to which the ended logs are added */
176  opentelemetry::sdk::common::CircularBuffer<Recordable> buffer_;
177 
178  std::shared_ptr<SynchronizationData> synchronization_data_;
179 
180  /* The background worker thread */
181  std::shared_ptr<sdk::common::ThreadInstrumentation> worker_thread_instrumentation_;
182  std::thread worker_thread_;
183 };
184 
185 } // namespace logs
186 } // namespace sdk
nostd::unique_ptr
std::unique_ptr< _Types... > unique_ptr
Definition: includes/opentelemetry/std/unique_ptr.h:19
sdk::logs::BatchLogRecordProcessor::DrainQueue
void DrainQueue()
Called when Shutdown() is invoked.
sdk::logs::BatchLogRecordProcessor::Shutdown
bool Shutdown(std::chrono::microseconds timeout=(std::chrono::microseconds::max)()) noexcept override
Shuts down the processor and does any cleanup required.
sdk::logs::BatchLogRecordProcessor::max_queue_size_
const size_t max_queue_size_
Definition: includes/opentelemetry/sdk/logs/batch_log_record_processor.h:172
logs
Definition: includes/opentelemetry/logs/event_id.h:13
exporter
Definition: includes/opentelemetry/exporters/memory/in_memory_data.h:11
sdk::logs::BatchLogRecordProcessor::synchronization_data_
std::shared_ptr< SynchronizationData > synchronization_data_
Definition: includes/opentelemetry/sdk/logs/batch_log_record_processor.h:178
sdk::logs::BatchLogRecordProcessor::InternalShutdown
bool InternalShutdown(std::chrono::microseconds timeout=(std::chrono::microseconds::max)()) noexcept
Shuts down the processor and does any cleanup required.
sdk::logs::BatchLogRecordProcessor::exporter_
std::unique_ptr< LogRecordExporter > exporter_
Definition: includes/opentelemetry/sdk/logs/batch_log_record_processor.h:169
sdk::logs::BatchLogRecordProcessor::max_export_batch_size_
const size_t max_export_batch_size_
Definition: includes/opentelemetry/sdk/logs/batch_log_record_processor.h:174
OPENTELEMETRY_BEGIN_NAMESPACE
#define OPENTELEMETRY_BEGIN_NAMESPACE
Definition: includes/opentelemetry/version.h:21
sdk::logs::BatchLogRecordProcessor::OnEmit
void OnEmit(std::unique_ptr< Recordable > &&record) noexcept override
Called when the Logger's log method creates a log record.
sdk::logs::BatchLogRecordProcessor::Export
virtual void Export()
Exports all logs to the configured exporter.
sdk::logs::BatchLogRecordProcessor::worker_thread_
std::thread worker_thread_
Definition: includes/opentelemetry/sdk/logs/batch_log_record_processor.h:182
sdk::logs::BatchLogRecordProcessor::worker_thread_instrumentation_
std::shared_ptr< sdk::common::ThreadInstrumentation > worker_thread_instrumentation_
Definition: includes/opentelemetry/sdk/logs/batch_log_record_processor.h:181
max
int max(int a, int b)
Compute the component-wise maximum.
Definition: sources/libs/nvmultiobjecttracker/include/modules/cuDCFv2/helper_math.h:60
OPENTELEMETRY_END_NAMESPACE
#define OPENTELEMETRY_END_NAMESPACE
Definition: includes/opentelemetry/version.h:24
sdk::logs::BatchLogRecordProcessor::SynchronizationData::SynchronizationData
SynchronizationData()
Definition: 9.2/includes/opentelemetry/sdk/logs/batch_log_record_processor.h:151
sdk::logs::BatchLogRecordProcessor::GetWaitAdjustedTime
void GetWaitAdjustedTime(std::chrono::microseconds &timeout, std::chrono::time_point< std::chrono::system_clock > &start_time)
sdk::logs::BatchLogRecordProcessor::BatchLogRecordProcessor
BatchLogRecordProcessor(std::unique_ptr< LogRecordExporter > &&exporter, const size_t max_queue_size=2048, const std::chrono::milliseconds scheduled_delay_millis=std::chrono::milliseconds(5000), const size_t max_export_batch_size=512)
Creates a batch log processor by configuring the specified exporter and other parameters as per the o...
sdk::logs::BatchLogRecordProcessor::DoBackgroundWork
void DoBackgroundWork()
The background routine performed by the worker thread.
sdk::logs::BatchLogRecordProcessor::buffer_
opentelemetry::sdk::common::CircularBuffer< Recordable > buffer_
Definition: includes/opentelemetry/sdk/logs/batch_log_record_processor.h:176
std
Extensions to the C++ standard library.
Definition: includes/opentelemetry/nostd/string_view.h:212
sdk::logs::BatchLogRecordProcessor::NotifyCompletion
static void NotifyCompletion(uint64_t notify_force_flush, const std::unique_ptr< LogRecordExporter > &exporter, const std::shared_ptr< SynchronizationData > &synchronization_data)
Notify completion of shutdown and force flush.
sdk::logs::BatchLogRecordProcessor::scheduled_delay_millis_
const std::chrono::milliseconds scheduled_delay_millis_
Definition: includes/opentelemetry/sdk/logs/batch_log_record_processor.h:173
sdk
Definition: includes/opentelemetry/sdk/common/atomic_shared_ptr.h:13
sdk::logs::BatchLogRecordProcessor::ForceFlush
bool ForceFlush(std::chrono::microseconds timeout=(std::chrono::microseconds::max)()) noexcept override
Export all log records that have not been exported yet.
sdk::logs::BatchLogRecordProcessor::MakeRecordable
std::unique_ptr< Recordable > MakeRecordable() noexcept override
Makes a new recordable.