use std::any::Any;
use crate::collector::CentralCollector;
use crate::encoder::{self, Encodable, ThreadLocalEncoder};
use crate::primitives::sync::Arc;
use crate::primitives::sync::atomic::AtomicU64;
pub struct FlushContext<'a> {
collector: &'a Arc<CentralCollector>,
drain_epoch: &'a AtomicU64,
}
impl std::fmt::Debug for FlushContext<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("FlushContext")
.field(
"drain_epoch",
&self.drain_epoch.load(std::sync::atomic::Ordering::Relaxed),
)
.finish_non_exhaustive()
}
}
impl<'a> FlushContext<'a> {
pub(crate) fn new(collector: &'a Arc<CentralCollector>, drain_epoch: &'a AtomicU64) -> Self {
Self {
collector,
drain_epoch,
}
}
pub fn record_event(&self, event: &dyn Encodable) {
let _ = encoder::record_encodable_event(event, self.collector, self.drain_epoch);
}
pub fn with_encoder(&self, f: impl FnOnce(&mut ThreadLocalEncoder<'_>)) {
let _ = encoder::with_encoder(f, self.collector, self.drain_epoch);
}
}
pub trait Source: Any + Send {
fn flush(&mut self, ctx: &FlushContext<'_>);
fn name(&self) -> &'static str;
fn on_thread_start(&mut self) -> std::io::Result<()> {
Ok(())
}
fn on_thread_stop(&mut self) {}
fn segment_metadata(&mut self, out: &mut Vec<(String, String)>) {
let _ = out;
}
#[cfg(feature = "pipeline")]
fn segment_processor(&mut self) -> Option<Box<dyn crate::pipeline::SegmentProcessor>> {
None
}
}
#[cfg(feature = "test-util")]
pub fn collect_segment_metadata(sources: &mut [Box<dyn Source>]) -> Vec<(String, String)> {
let mut out = Vec::new();
for source in sources.iter_mut() {
source.segment_metadata(&mut out);
}
out
}