use crate::error::OTelSdkResult;
use crate::logs::SdkLogRecord;
use crate::Resource;
use opentelemetry::logs::Severity;
use opentelemetry::InstrumentationScope;
use std::fmt::Debug;
use std::time;
#[derive(Debug)]
pub struct LogBatch<'a> {
data: LogBatchData<'a>,
}
#[derive(Debug)]
enum LogBatchData<'a> {
SliceOfOwnedData(&'a [Box<(SdkLogRecord, InstrumentationScope)>]), SliceOfBorrowedData(&'a [(&'a SdkLogRecord, &'a InstrumentationScope)]),
}
impl<'a> LogBatch<'a> {
pub fn new(data: &'a [(&'a SdkLogRecord, &'a InstrumentationScope)]) -> LogBatch<'a> {
LogBatch {
data: LogBatchData::SliceOfBorrowedData(data),
}
}
pub(crate) fn new_with_owned_data(
data: &'a [Box<(SdkLogRecord, InstrumentationScope)>],
) -> LogBatch<'a> {
LogBatch {
data: LogBatchData::SliceOfOwnedData(data),
}
}
}
impl LogBatch<'_> {
#[cfg(test)]
pub(crate) fn len(&self) -> usize {
match &self.data {
LogBatchData::SliceOfOwnedData(data) => data.len(),
LogBatchData::SliceOfBorrowedData(data) => data.len(),
}
}
pub fn iter(&self) -> impl Iterator<Item = (&SdkLogRecord, &InstrumentationScope)> {
LogBatchDataIter {
data: &self.data,
index: 0,
}
}
}
struct LogBatchDataIter<'a> {
data: &'a LogBatchData<'a>,
index: usize,
}
impl<'a> Iterator for LogBatchDataIter<'a> {
type Item = (&'a SdkLogRecord, &'a InstrumentationScope);
fn next(&mut self) -> Option<Self::Item> {
match self.data {
LogBatchData::SliceOfOwnedData(data) => {
if self.index < data.len() {
let record = &*data[self.index];
self.index += 1;
Some((&record.0, &record.1))
} else {
None
}
}
LogBatchData::SliceOfBorrowedData(data) => {
if self.index < data.len() {
let record = &data[self.index];
self.index += 1;
Some((record.0, record.1))
} else {
None
}
}
}
}
}
pub trait LogExporter: Send + Sync + Debug {
fn export(
&self,
batch: LogBatch<'_>,
) -> impl std::future::Future<Output = OTelSdkResult> + Send;
fn shutdown_with_timeout(&self, _timeout: time::Duration) -> OTelSdkResult {
Ok(())
}
fn shutdown(&self) -> OTelSdkResult {
self.shutdown_with_timeout(time::Duration::from_secs(5))
}
fn event_enabled(&self, _level: Severity, _target: &str, _name: Option<&str>) -> bool {
true
}
fn set_resource(&mut self, _resource: &Resource) {}
}