tracing-trace: introduce TraceWriter, trace now only exposes the channel

This commit is contained in:
Louis Dureuil 2024-01-31 17:44:54 +01:00
parent 0f327f2821
commit 0e7a411d4d
No known key found for this signature in database
3 changed files with 36 additions and 8 deletions

View File

@ -15,7 +15,7 @@ use crate::entry::{
Entry, Event, MemoryStats, NewCallsite, NewSpan, NewThread, ResourceId, SpanClose, SpanEnter, Entry, Event, MemoryStats, NewCallsite, NewSpan, NewThread, ResourceId, SpanClose, SpanEnter,
SpanExit, SpanId, SpanExit, SpanId,
}; };
use crate::{Error, Trace}; use crate::{Error, Trace, TraceWriter};
/// Layer that measures the time spent in spans. /// Layer that measures the time spent in spans.
pub struct TraceLayer<A: GlobalAlloc + 'static = System> { pub struct TraceLayer<A: GlobalAlloc + 'static = System> {
@ -25,10 +25,10 @@ pub struct TraceLayer<A: GlobalAlloc + 'static = System> {
memory_allocator: Option<&'static StatsAlloc<A>>, memory_allocator: Option<&'static StatsAlloc<A>>,
} }
impl<W: Write> Trace<W> { impl Trace {
pub fn new(writer: W) -> (Self, TraceLayer<System>) { pub fn new() -> (Self, TraceLayer<System>) {
let (sender, receiver) = tokio::sync::mpsc::unbounded_channel(); let (sender, receiver) = tokio::sync::mpsc::unbounded_channel();
let trace = Trace { writer, receiver }; let trace = Trace { receiver };
let layer = TraceLayer { let layer = TraceLayer {
sender, sender,
callsites: Default::default(), callsites: Default::default(),
@ -39,11 +39,10 @@ impl<W: Write> Trace<W> {
} }
pub fn with_stats_alloc<A: GlobalAlloc>( pub fn with_stats_alloc<A: GlobalAlloc>(
writer: W,
stats_alloc: &'static StatsAlloc<A>, stats_alloc: &'static StatsAlloc<A>,
) -> (Self, TraceLayer<A>) { ) -> (Self, TraceLayer<A>) {
let (sender, receiver) = tokio::sync::mpsc::unbounded_channel(); let (sender, receiver) = tokio::sync::mpsc::unbounded_channel();
let trace = Trace { writer, receiver }; let trace = Trace { receiver };
let layer = TraceLayer { let layer = TraceLayer {
sender, sender,
callsites: Default::default(), callsites: Default::default(),
@ -52,6 +51,21 @@ impl<W: Write> Trace<W> {
}; };
(trace, layer) (trace, layer)
} }
}
impl<W: Write> TraceWriter<W> {
pub fn new(writer: W) -> (Self, TraceLayer<System>) {
let (trace, layer) = Trace::new();
(trace.into_writer(writer), layer)
}
pub fn with_stats_alloc<A: GlobalAlloc>(
writer: W,
stats_alloc: &'static StatsAlloc<A>,
) -> (Self, TraceLayer<A>) {
let (trace, layer) = Trace::with_stats_alloc(stats_alloc);
(trace.into_writer(writer), layer)
}
pub async fn receive(&mut self) -> Result<ControlFlow<(), ()>, Error> { pub async fn receive(&mut self) -> Result<ControlFlow<(), ()>, Error> {
let Some(entry) = self.receiver.recv().await else { let Some(entry) = self.receiver.recv().await else {

View File

@ -9,11 +9,25 @@ pub mod processor;
pub use error::Error; pub use error::Error;
pub struct Trace<W: Write> { pub struct TraceWriter<W: Write> {
writer: W, writer: W,
receiver: tokio::sync::mpsc::UnboundedReceiver<Entry>, receiver: tokio::sync::mpsc::UnboundedReceiver<Entry>,
} }
pub struct Trace {
receiver: tokio::sync::mpsc::UnboundedReceiver<Entry>,
}
impl Trace {
pub fn into_receiver(self) -> tokio::sync::mpsc::UnboundedReceiver<Entry> {
self.receiver
}
pub fn into_writer<W: Write>(self, writer: W) -> TraceWriter<W> {
TraceWriter { writer, receiver: self.receiver }
}
}
pub struct TraceReader<R: Read> { pub struct TraceReader<R: Read> {
reader: R, reader: R,
} }

View File

@ -74,7 +74,7 @@ fn on_panic(info: &std::panic::PanicInfo) {
fn main() { fn main() {
let (mut trace, profiling_layer) = let (mut trace, profiling_layer) =
tracing_trace::Trace::new(std::fs::File::create("trace.json").unwrap()); tracing_trace::TraceWriter::new(std::fs::File::create("trace.json").unwrap());
let subscriber = tracing_subscriber::registry() let subscriber = tracing_subscriber::registry()
// any number of other subscriber layers may be added before or // any number of other subscriber layers may be added before or