NVIDIA DeepStream SDK API Reference

9.2 Release
includes/opentelemetry/sdk/trace/batch_span_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/trace/batch_span_processor_options.h"
17 #include "opentelemetry/sdk/trace/batch_span_processor_runtime_options.h"
18 #include "opentelemetry/sdk/trace/exporter.h"
19 #include "opentelemetry/sdk/trace/processor.h"
20 #include "opentelemetry/sdk/trace/recordable.h"
21 #include "opentelemetry/trace/span_context.h"
22 #include "opentelemetry/version.h"
23 
25 namespace sdk
26 {
27 namespace trace
28 {
29 
35 {
36 public:
44  BatchSpanProcessor(std::unique_ptr<SpanExporter> &&exporter,
45  const BatchSpanProcessorOptions &options);
46 
55  BatchSpanProcessor(std::unique_ptr<SpanExporter> &&exporter,
56  const BatchSpanProcessorOptions &options,
57  const BatchSpanProcessorRuntimeOptions &runtime_options);
58 
64  std::unique_ptr<Recordable> MakeRecordable() noexcept override;
65 
74  void OnStart(Recordable &span,
75  const opentelemetry::trace::SpanContext &parent_context) noexcept override;
76 
82  void OnEnd(std::unique_ptr<Recordable> &&span) noexcept override;
83 
89  bool ForceFlush(
90  std::chrono::microseconds timeout = (std::chrono::microseconds::max)()) noexcept override;
91 
100  bool Shutdown(
101  std::chrono::microseconds timeout = (std::chrono::microseconds::max)()) noexcept override;
102 
109  ~BatchSpanProcessor() override;
110 
111 protected:
120  bool InternalShutdown(
121  std::chrono::microseconds timeout = (std::chrono::microseconds::max)()) noexcept;
122 
126  void DoBackgroundWork();
127 
132  virtual void Export();
133 
138  void DrainQueue();
139 
141  {
142  /* Synchronization primitives */
143  std::condition_variable cv, force_flush_cv;
145 
146  /* Important boolean flags to handle the workflow of the processor */
147  std::atomic<bool> is_force_wakeup_background_worker{false};
148  std::atomic<bool> is_shutdown{false};
149  std::atomic<uint64_t> force_flush_pending_sequence{0};
150  std::atomic<uint64_t> force_flush_notified_sequence{0};
151  std::atomic<std::chrono::microseconds::rep> force_flush_timeout_us{0};
152 
153  // Do not use SynchronizationData() = default; here, some versions of GCC&Clang have BUGs
154  // and may not initialize the member correctly. See also
155  // https://stackoverflow.com/questions/53408962/try-to-understand-compiler-error-message-default-member-initializer-required-be
157  };
158 
166  static void NotifyCompletion(uint64_t notify_force_flush,
167  const std::unique_ptr<SpanExporter> &exporter,
168  const std::shared_ptr<SynchronizationData> &synchronization_data);
169 
170  void GetWaitAdjustedTime(std::chrono::microseconds &timeout,
171  std::chrono::time_point<std::chrono::system_clock> &start_time);
172  /* The configured backend exporter */
173  std::unique_ptr<SpanExporter> exporter_;
174 
175  /* Configurable parameters as per the official specs */
176  const size_t max_queue_size_;
177  const std::chrono::milliseconds schedule_delay_millis_;
179 
180  /* The buffer/queue to which the ended spans are added */
181  opentelemetry::sdk::common::CircularBuffer<Recordable> buffer_;
182 
183  std::shared_ptr<SynchronizationData> synchronization_data_;
184 
185  /* The background worker thread */
186  std::shared_ptr<sdk::common::ThreadInstrumentation> worker_thread_instrumentation_;
187  std::thread worker_thread_;
188 };
189 
190 } // namespace trace
191 } // namespace sdk
trace
Definition: includes/opentelemetry/context/context_value.h:18
sdk::trace::BatchSpanProcessor::buffer_
opentelemetry::sdk::common::CircularBuffer< Recordable > buffer_
Definition: includes/opentelemetry/sdk/trace/batch_span_processor.h:181
sdk::trace::BatchSpanProcessor::SynchronizationData::force_flush_cv_m
std::mutex force_flush_cv_m
Definition: includes/opentelemetry/sdk/trace/batch_span_processor.h:144
sdk::trace::BatchSpanProcessor::Shutdown
bool Shutdown(std::chrono::microseconds timeout=(std::chrono::microseconds::max)()) noexcept override
Shuts down the processor and does any cleanup required.
sdk::trace::BatchSpanProcessor::SynchronizationData::cv_m
std::mutex cv_m
Definition: includes/opentelemetry/sdk/trace/batch_span_processor.h:144
sdk::trace::Recordable
Maintains a representation of a span in a format that can be processed by a recorder.
Definition: includes/opentelemetry/sdk/trace/recordable.h:44
sdk::trace::BatchSpanProcessor::Export
virtual void Export()
Exports all ended spans to the configured exporter.
sdk::trace::BatchSpanProcessor::worker_thread_
std::thread worker_thread_
Definition: includes/opentelemetry/sdk/trace/batch_span_processor.h:187
sdk::trace::BatchSpanProcessor::SynchronizationData::cv
std::condition_variable cv
Definition: includes/opentelemetry/sdk/trace/batch_span_processor.h:143
sdk::trace::BatchSpanProcessorOptions
Struct to hold batch SpanProcessor options.
Definition: includes/opentelemetry/sdk/trace/batch_span_processor_options.h:20
sdk::trace::BatchSpanProcessor::DrainQueue
void DrainQueue()
Called when Shutdown() is invoked.
sdk::trace::BatchSpanProcessor::SynchronizationData::shutdown_m
std::mutex shutdown_m
Definition: includes/opentelemetry/sdk/trace/batch_span_processor.h:144
exporter
Definition: includes/opentelemetry/exporters/memory/in_memory_data.h:11
sdk::trace::BatchSpanProcessor::schedule_delay_millis_
const std::chrono::milliseconds schedule_delay_millis_
Definition: includes/opentelemetry/sdk/trace/batch_span_processor.h:177
sdk::trace::BatchSpanProcessorRuntimeOptions
Struct to hold batch SpanProcessor runtime options.
Definition: includes/opentelemetry/sdk/trace/batch_span_processor_runtime_options.h:21
sdk::trace::BatchSpanProcessor::DoBackgroundWork
void DoBackgroundWork()
The background routine performed by the worker thread.
sdk::trace::BatchSpanProcessor::exporter_
std::unique_ptr< SpanExporter > exporter_
Definition: includes/opentelemetry/sdk/trace/batch_span_processor.h:173
sdk::trace::BatchSpanProcessor::max_queue_size_
const size_t max_queue_size_
Definition: includes/opentelemetry/sdk/trace/batch_span_processor.h:176
OPENTELEMETRY_BEGIN_NAMESPACE
#define OPENTELEMETRY_BEGIN_NAMESPACE
Definition: includes/opentelemetry/version.h:21
sdk::trace::BatchSpanProcessor::worker_thread_instrumentation_
std::shared_ptr< sdk::common::ThreadInstrumentation > worker_thread_instrumentation_
Definition: includes/opentelemetry/sdk/trace/batch_span_processor.h:186
sdk::trace::BatchSpanProcessor::ForceFlush
bool ForceFlush(std::chrono::microseconds timeout=(std::chrono::microseconds::max)()) noexcept override
Export all ended spans that have not been exported yet.
max
int max(int a, int b)
Compute the component-wise maximum.
Definition: sources/libs/nvmultiobjecttracker/include/modules/cuDCFv2/helper_math.h:60
sdk::trace::BatchSpanProcessor::SynchronizationData::force_flush_cv
std::condition_variable force_flush_cv
Definition: includes/opentelemetry/sdk/trace/batch_span_processor.h:143
sdk::trace::BatchSpanProcessor::~BatchSpanProcessor
~BatchSpanProcessor() override
Class destructor which invokes the Shutdown() method.
sdk::trace::BatchSpanProcessor::BatchSpanProcessor
BatchSpanProcessor(std::unique_ptr< SpanExporter > &&exporter, const BatchSpanProcessorOptions &options)
Creates a batch span processor by configuring the specified exporter and other parameters as per the ...
sdk::trace::BatchSpanProcessor::SynchronizationData::is_shutdown
std::atomic< bool > is_shutdown
Definition: includes/opentelemetry/sdk/trace/batch_span_processor.h:148
sdk::trace::BatchSpanProcessor::SynchronizationData::SynchronizationData
SynchronizationData()
Definition: includes/opentelemetry/sdk/trace/batch_span_processor.h:156
OPENTELEMETRY_END_NAMESPACE
#define OPENTELEMETRY_END_NAMESPACE
Definition: includes/opentelemetry/version.h:24
sdk::trace::BatchSpanProcessor::OnEnd
void OnEnd(std::unique_ptr< Recordable > &&span) noexcept override
Called when a span ends.
sdk::trace::BatchSpanProcessor::synchronization_data_
std::shared_ptr< SynchronizationData > synchronization_data_
Definition: includes/opentelemetry/sdk/trace/batch_span_processor.h:183
sdk::trace::BatchSpanProcessor::GetWaitAdjustedTime
void GetWaitAdjustedTime(std::chrono::microseconds &timeout, std::chrono::time_point< std::chrono::system_clock > &start_time)
sdk::trace::BatchSpanProcessor::OnStart
void OnStart(Recordable &span, const opentelemetry::trace::SpanContext &parent_context) noexcept override
Called when a span is started.
sdk::trace::BatchSpanProcessor::SynchronizationData::is_force_wakeup_background_worker
std::atomic< bool > is_force_wakeup_background_worker
Definition: includes/opentelemetry/sdk/trace/batch_span_processor.h:147
sdk::trace::BatchSpanProcessor::SynchronizationData::force_flush_pending_sequence
std::atomic< uint64_t > force_flush_pending_sequence
Definition: includes/opentelemetry/sdk/trace/batch_span_processor.h:149
sdk::trace::BatchSpanProcessor::max_export_batch_size_
const size_t max_export_batch_size_
Definition: includes/opentelemetry/sdk/trace/batch_span_processor.h:178
sdk::trace::BatchSpanProcessor::SynchronizationData
Definition: includes/opentelemetry/sdk/trace/batch_span_processor.h:140
sdk::trace::BatchSpanProcessor
This is an implementation of the SpanProcessor which creates batches of finished spans and passes the...
Definition: includes/opentelemetry/sdk/trace/batch_span_processor.h:34
sdk::trace::BatchSpanProcessor::MakeRecordable
std::unique_ptr< Recordable > MakeRecordable() noexcept override
Requests a Recordable(Span) from the configured exporter.
sdk
Definition: includes/opentelemetry/sdk/common/atomic_shared_ptr.h:13
sdk::trace::SpanProcessor
Span processor allow hooks for span start and end method invocations.
Definition: includes/opentelemetry/sdk/trace/processor.h:29
sdk::trace::BatchSpanProcessor::SynchronizationData::force_flush_timeout_us
std::atomic< std::chrono::microseconds::rep > force_flush_timeout_us
Definition: includes/opentelemetry/sdk/trace/batch_span_processor.h:151
sdk::trace::BatchSpanProcessor::NotifyCompletion
static void NotifyCompletion(uint64_t notify_force_flush, const std::unique_ptr< SpanExporter > &exporter, const std::shared_ptr< SynchronizationData > &synchronization_data)
Notify completion of shutdown and force flush.
sdk::trace::BatchSpanProcessor::SynchronizationData::force_flush_notified_sequence
std::atomic< uint64_t > force_flush_notified_sequence
Definition: includes/opentelemetry/sdk/trace/batch_span_processor.h:150
sdk::trace::BatchSpanProcessor::InternalShutdown
bool InternalShutdown(std::chrono::microseconds timeout=(std::chrono::microseconds::max)()) noexcept
Shuts down the processor and does any cleanup required.