|
| 1 | +use crate::export::logs::{LogData, LogExporter}; |
| 2 | +use async_trait::async_trait; |
| 3 | +use opentelemetry::logs::{LogError, LogResult}; |
| 4 | +use std::sync::{Arc, Mutex}; |
| 5 | + |
| 6 | +/// An in-memory logs exporter that stores logs data in memory.. |
| 7 | +/// |
| 8 | +/// This exporter is useful for testing and debugging purposes. |
| 9 | +/// It stores logs in a `Vec<LogData>`. Logs can be retrieved using |
| 10 | +/// `get_emitted_logs` method. |
| 11 | +/// |
| 12 | +/// # Example |
| 13 | +/// ```no_run |
| 14 | +///# use log::{error, info, Level, warn}; |
| 15 | +///# use opentelemetry_appender_log::OpenTelemetryLogBridge; |
| 16 | +///# use opentelemetry_sdk::logs::{BatchLogProcessor, LoggerProvider}; |
| 17 | +///# use opentelemetry_sdk::runtime; |
| 18 | +///# use opentelemetry_sdk::testing::logs::InMemoryLogsExporter; |
| 19 | +/// |
| 20 | +///# #[tokio::main] |
| 21 | +///# async fn main() { |
| 22 | +/// //Create an InMemoryLogsExporter |
| 23 | +/// let exporter: InMemoryLogsExporter = InMemoryLogsExporter::default(); |
| 24 | +/// //Create a LoggerProvider and register the exporter |
| 25 | +/// let logger_provider = LoggerProvider::builder() |
| 26 | +/// .with_log_processor(BatchLogProcessor::builder(exporter.clone(), runtime::Tokio).build()) |
| 27 | +/// .build(); |
| 28 | +
|
| 29 | +/// // Setup Log Appender for the log crate. |
| 30 | +/// let otel_log_appender = OpenTelemetryLogBridge::new(&logger_provider); |
| 31 | +/// log::set_boxed_logger(Box::new(otel_log_appender)).unwrap(); |
| 32 | +/// log::set_max_level(Level::Info.to_level_filter()); |
| 33 | +
|
| 34 | +/// // Emit logs using macros from the log crate. |
| 35 | +/// error!("hello from {}. My price is {}", "apple", 2.99); |
| 36 | +/// warn!("warn!"); |
| 37 | +/// info!("test log!"); |
| 38 | +
|
| 39 | +/// logger_provider.force_flush(); |
| 40 | +
|
| 41 | +/// let emitted_logs = exporter.get_emitted_logs().unwrap(); |
| 42 | +/// for log in emitted_logs { |
| 43 | +/// println!("{:?}", log); |
| 44 | +/// } |
| 45 | +///# } |
| 46 | +/// ``` |
| 47 | +/// |
| 48 | +#[derive(Clone, Debug)] |
| 49 | +pub struct InMemoryLogsExporter { |
| 50 | + logs: Arc<Mutex<Vec<LogData>>>, |
| 51 | +} |
| 52 | + |
| 53 | +impl Default for InMemoryLogsExporter { |
| 54 | + fn default() -> Self { |
| 55 | + InMemoryLogsExporterBuilder::new().build() |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +///Builder for ['InMemoryLogsExporter']. |
| 60 | +/// # Example |
| 61 | +/// |
| 62 | +/// ```no_run |
| 63 | +///# use opentelemetry_sdk::testing::logs::{InMemoryLogsExporter, InMemoryLogsExporterBuilder}; |
| 64 | +///# use log::{error, info, Level, warn}; |
| 65 | +///# use opentelemetry_appender_log::OpenTelemetryLogBridge; |
| 66 | +///# use opentelemetry_sdk::logs::{BatchLogProcessor, LoggerProvider}; |
| 67 | +///# use opentelemetry_sdk::runtime; |
| 68 | +/// |
| 69 | +///# #[tokio::main] |
| 70 | +///# async fn main() { |
| 71 | +/// //Create an InMemoryLogsExporter |
| 72 | +/// let exporter: InMemoryLogsExporter = InMemoryLogsExporterBuilder::default().build(); |
| 73 | +/// //Create a LoggerProvider and register the exporter |
| 74 | +/// let logger_provider = LoggerProvider::builder() |
| 75 | +/// .with_log_processor(BatchLogProcessor::builder(exporter.clone(), runtime::Tokio).build()) |
| 76 | +/// .build(); |
| 77 | +/// // Setup Log Appender for the log crate. |
| 78 | +/// let otel_log_appender = OpenTelemetryLogBridge::new(&logger_provider); |
| 79 | +/// log::set_boxed_logger(Box::new(otel_log_appender)).unwrap(); |
| 80 | +/// log::set_max_level(Level::Info.to_level_filter()); |
| 81 | +/// // Emit logs using macros from the log crate. |
| 82 | +/// error!("hello from {}. My price is {}", "apple", 2.99); |
| 83 | +/// warn!("warn!"); |
| 84 | +/// info!("test log!"); |
| 85 | +/// logger_provider.force_flush(); |
| 86 | +/// let emitted_logs = exporter.get_emitted_logs().unwrap(); |
| 87 | +/// for log in emitted_logs { |
| 88 | +/// println!("{:?}", log); |
| 89 | +/// } |
| 90 | +///# } |
| 91 | +/// |
| 92 | +/// ``` |
| 93 | +/// |
| 94 | +#[derive(Debug, Clone)] |
| 95 | +pub struct InMemoryLogsExporterBuilder {} |
| 96 | + |
| 97 | +impl Default for InMemoryLogsExporterBuilder { |
| 98 | + fn default() -> Self { |
| 99 | + Self::new() |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +impl InMemoryLogsExporterBuilder { |
| 104 | + /// Creates a new instance of `InMemoryLogsExporter`. |
| 105 | + /// |
| 106 | + pub fn new() -> Self { |
| 107 | + Self {} |
| 108 | + } |
| 109 | + |
| 110 | + /// Creates a new instance of `InMemoryLogsExporter`. |
| 111 | + /// |
| 112 | + pub fn build(&self) -> InMemoryLogsExporter { |
| 113 | + InMemoryLogsExporter { |
| 114 | + logs: Arc::new(Mutex::new(Vec::new())), |
| 115 | + } |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +impl InMemoryLogsExporter { |
| 120 | + /// Returns the logs emitted via Logger as a vector of `LogData`. |
| 121 | + /// |
| 122 | + /// # Example |
| 123 | + /// |
| 124 | + /// ``` |
| 125 | + /// use opentelemetry_sdk::testing::logs::{InMemoryLogsExporter, InMemoryLogsExporterBuilder}; |
| 126 | + /// |
| 127 | + /// let exporter = InMemoryLogsExporterBuilder::default().build(); |
| 128 | + /// let emitted_logs = exporter.get_emitted_logs().unwrap(); |
| 129 | + /// ``` |
| 130 | + /// |
| 131 | + pub fn get_emitted_logs(&self) -> LogResult<Vec<LogData>> { |
| 132 | + self.logs |
| 133 | + .lock() |
| 134 | + .map(|logs_guard| logs_guard.iter().cloned().collect()) |
| 135 | + .map_err(LogError::from) |
| 136 | + } |
| 137 | + |
| 138 | + /// Clears the internal (in-memory) storage of logs. |
| 139 | + /// |
| 140 | + /// # Example |
| 141 | + /// |
| 142 | + /// ``` |
| 143 | + /// use opentelemetry_sdk::testing::logs::{InMemoryLogsExporter, InMemoryLogsExporterBuilder}; |
| 144 | + /// |
| 145 | + /// let exporter = InMemoryLogsExporterBuilder::default().build(); |
| 146 | + /// exporter.reset(); |
| 147 | + /// ``` |
| 148 | + /// |
| 149 | + pub fn reset(&self) { |
| 150 | + let _ = self |
| 151 | + .logs |
| 152 | + .lock() |
| 153 | + .map(|mut logs_guard| logs_guard.clear()) |
| 154 | + .map_err(LogError::from); |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +#[async_trait] |
| 159 | +impl LogExporter for InMemoryLogsExporter { |
| 160 | + async fn export(&mut self, batch: Vec<LogData>) -> LogResult<()> { |
| 161 | + self.logs |
| 162 | + .lock() |
| 163 | + .map(|mut logs_guard| logs_guard.append(&mut batch.clone())) |
| 164 | + .map_err(LogError::from) |
| 165 | + } |
| 166 | + fn shutdown(&mut self) { |
| 167 | + self.reset(); |
| 168 | + } |
| 169 | +} |
0 commit comments