|
| 1 | +//! # OpenTelemetry Log Processor Interface |
| 2 | +//! |
| 3 | +//! The `LogProcessor` interface provides hooks for log record processing and |
| 4 | +//! exporting. Log processors receive `LogRecord`s emitted by the SDK's |
| 5 | +//! `Logger` and determine how these records are handled. |
| 6 | +//! |
| 7 | +//! Built-in log processors are responsible for converting logs to exportable |
| 8 | +//! representations and passing them to configured exporters. They can be |
| 9 | +//! registered directly with a `LoggerProvider`. |
| 10 | +//! |
| 11 | +//! ## Types of Log Processors |
| 12 | +//! |
| 13 | +//! - **SimpleLogProcessor**: Forwards log records to the exporter immediately |
| 14 | +//! after they are emitted. This processor is **synchronous** and is designed |
| 15 | +//! for debugging or testing purposes. It is **not suitable for production** |
| 16 | +//! environments due to its lack of batching, performance optimizations, or support |
| 17 | +//! for high-throughput scenarios. |
| 18 | +//! |
| 19 | +//! - **BatchLogProcessor**: Buffers log records and sends them to the exporter |
| 20 | +//! in batches. This processor is designed for **production use** in high-throughput |
| 21 | +//! applications and reduces the overhead of frequent exports by using a background |
| 22 | +//! thread for batch processing. |
| 23 | +//! |
| 24 | +//! ## Diagram |
| 25 | +//! |
| 26 | +//! ```ascii |
| 27 | +//! +-----+---------------+ +-----------------------+ +-------------------+ |
| 28 | +//! | | | | | | | |
| 29 | +//! | SDK | Logger.emit() +---> (Simple)LogProcessor +---> LogExporter | |
| 30 | +//! | | | | (Batch)LogProcessor +---> (OTLPExporter) | |
| 31 | +//! +-----+---------------+ +-----------------------+ +-------------------+ |
| 32 | +//! ``` |
| 33 | +
|
1 | 34 | use crate::{ |
2 | 35 | export::logs::{ExportResult, LogBatch, LogExporter}, |
3 | 36 | logs::{LogError, LogRecord, LogResult}, |
@@ -70,10 +103,24 @@ pub trait LogProcessor: Send + Sync + Debug { |
70 | 103 | fn set_resource(&self, _resource: &Resource) {} |
71 | 104 | } |
72 | 105 |
|
73 | | -/// A [LogProcessor] that passes logs to the configured `LogExporter`, as soon |
74 | | -/// as they are emitted, without any batching. This is typically useful for |
75 | | -/// debugging and testing. For scenarios requiring higher |
76 | | -/// performance/throughput, consider using [BatchLogProcessor]. |
| 106 | +/// A [`LogProcessor`] designed for testing and debugging purpose, that immediately |
| 107 | +/// exports log records as they are emitted. |
| 108 | +/// ## Example |
| 109 | +/// |
| 110 | +/// ### Using a SimpleLogProcessor |
| 111 | +/// |
| 112 | +/// ```rust |
| 113 | +/// use opentelemetry_sdk::logs::{SimpleLogProcessor, LoggerProvider}; |
| 114 | +/// use opentelemetry::global; |
| 115 | +/// use opentelemetry_sdk::export::logs::LogExporter; |
| 116 | +/// use opentelemetry_sdk::testing::logs::InMemoryLogExporter; |
| 117 | +/// |
| 118 | +/// let exporter = InMemoryLogExporter::default(); // Replace with an actual exporter |
| 119 | +/// let provider = LoggerProvider::builder() |
| 120 | +/// .with_simple_exporter(exporter) |
| 121 | +/// .build(); |
| 122 | +/// |
| 123 | +/// ``` |
77 | 124 | #[derive(Debug)] |
78 | 125 | pub struct SimpleLogProcessor<T: LogExporter> { |
79 | 126 | exporter: Mutex<T>, |
@@ -165,8 +212,42 @@ enum BatchMessage { |
165 | 212 | SetResource(Arc<Resource>), |
166 | 213 | } |
167 | 214 |
|
168 | | -/// A [`LogProcessor`] that buffers log records and reports |
169 | | -/// them at a pre-configured interval from a dedicated background thread. |
| 215 | +/// The `BatchLogProcessor` collects finished logs in a buffer and exports them |
| 216 | +/// in batches to the configured `LogExporter`. This processor is ideal for |
| 217 | +/// high-throughput environments, as it minimizes the overhead of exporting logs |
| 218 | +/// individually. It uses a **dedicated background thread** to manage and export logs |
| 219 | +/// asynchronously, ensuring that the application's main execution flow is not blocked. |
| 220 | +/// |
| 221 | +/// - This processor supports the following configurations: |
| 222 | +/// - **Queue size**: Maximum number of log records that can be buffered. |
| 223 | +/// - **Batch size**: Maximum number of log records to include in a single export. |
| 224 | +/// - **Export timeout**: Maximum duration allowed for an export operation. |
| 225 | +/// - **Scheduled delay**: Frequency at which the batch is exported. |
| 226 | +/// |
| 227 | +/// ### Using a BatchLogProcessor: |
| 228 | +/// |
| 229 | +/// ```rust |
| 230 | +/// use opentelemetry_sdk::logs::{BatchLogProcessor, BatchConfigBuilder, LoggerProvider}; |
| 231 | +/// use opentelemetry::global; |
| 232 | +/// use std::time::Duration; |
| 233 | +/// use opentelemetry_sdk::testing::logs::InMemoryLogExporter; |
| 234 | +/// |
| 235 | +/// let exporter = InMemoryLogExporter::default(); // Replace with an actual exporter |
| 236 | +/// let processor = BatchLogProcessor::builder(exporter) |
| 237 | +/// .with_batch_config( |
| 238 | +/// BatchConfigBuilder::default() |
| 239 | +/// .with_max_queue_size(2048) |
| 240 | +/// .with_max_export_batch_size(512) |
| 241 | +/// .with_scheduled_delay(Duration::from_secs(5)) |
| 242 | +/// .with_max_export_timeout(Duration::from_secs(30)) |
| 243 | +/// .build(), |
| 244 | +/// ) |
| 245 | +/// .build(); |
| 246 | +/// |
| 247 | +/// let provider = LoggerProvider::builder() |
| 248 | +/// .with_log_processor(processor) |
| 249 | +/// .build(); |
| 250 | +/// |
170 | 251 | pub struct BatchLogProcessor { |
171 | 252 | message_sender: SyncSender<BatchMessage>, |
172 | 253 | handle: Mutex<Option<thread::JoinHandle<()>>>, |
|
0 commit comments