use crate::{group, EventGroup, Histogram, SpanGroup, TimingSubscriber};
use std::hash::Hash;
pub struct Builder<S = group::ByName, E = group::ByMessage> {
span_group: S,
event_group: E,
time: quanta::Clock,
}
impl Default for Builder<group::ByName, group::ByMessage> {
fn default() -> Self {
Builder {
span_group: group::ByName,
event_group: group::ByMessage,
time: quanta::Clock::new(),
}
}
}
impl<S, E> Builder<S, E> {
pub fn spans<S2>(self, span_group: S2) -> Builder<S2, E> {
Builder {
span_group,
event_group: self.event_group,
time: self.time,
}
}
pub fn events<E2>(self, event_group: E2) -> Builder<S, E2> {
Builder {
span_group: self.span_group,
event_group,
time: self.time,
}
}
pub fn time(mut self, time: quanta::Clock) -> Builder<S, E> {
self.time = time;
self
}
pub fn build_informed<F>(self, new_histogram: F) -> TimingSubscriber<S, E>
where
S: SpanGroup,
E: EventGroup,
S::Id: Hash + Eq,
E::Id: Hash + Eq,
F: FnMut(&S::Id, &E::Id) -> Histogram<u64> + Send + Sync + 'static,
{
let (tx, rx) = crossbeam::channel::unbounded();
TimingSubscriber {
span_group: self.span_group,
event_group: self.event_group,
time: self.time,
reader: super::ReaderState {
created: rx,
histograms: Default::default(),
}
.into(),
writers: super::WriterState {
last_event: Default::default(),
refcount: Default::default(),
spans: Default::default(),
tls: Default::default(),
idle_recorders: Default::default(),
new_histogram: Box::new(new_histogram),
created: tx,
}
.into(),
}
}
pub fn build<F>(self, mut new_histogram: F) -> TimingSubscriber<S, E>
where
S: SpanGroup,
E: EventGroup,
S::Id: Hash + Eq,
E::Id: Hash + Eq,
F: FnMut() -> Histogram<u64> + Send + Sync + 'static,
{
self.build_informed(move |_: &_, _: &_| (new_histogram)())
}
}