start adding tracing spans to internals (#478)
				
					
				
			We've adopted `tracing` for diagnostics, but currently, it is just being used as a drop-in replacement for the `log` crate. Ideally, we would want to start emitting more structured diagnostics, using `tracing`'s `Span`s and structured key-value fields. A lot of the logging in `h2` is already written in a style that imitates the formatting of structured key-value logs, but as textual log messages. Migrating the logs to structured `tracing` events therefore is pretty easy to do. I've also started adding spans, mostly in the read path. Finally, I've updated the tests to use `tracing` rather than `env_logger`. The tracing setup happens in a macro, so that a span for each test with the test's name can be generated and entered. This will make the test output easier to read if multiple tests are run concurrently with `--nocapture`. Signed-off-by: Eliza Weisman <eliza@buoyant.io>
This commit is contained in:
		| @@ -8,6 +8,7 @@ pub mod raw; | ||||
| pub mod frames; | ||||
| pub mod mock; | ||||
| pub mod prelude; | ||||
| pub mod trace; | ||||
| pub mod util; | ||||
|  | ||||
| mod client_ext; | ||||
| @@ -24,3 +25,19 @@ pub type Codec<T> = h2::Codec<T, bytes::Bytes>; | ||||
|  | ||||
| // This is the frame type that is sent | ||||
| pub type SendFrame = h2::frame::Frame<bytes::Bytes>; | ||||
|  | ||||
| #[macro_export] | ||||
| macro_rules! trace_init { | ||||
|     () => { | ||||
|         let _guard = $crate::trace::init(); | ||||
|         let span = $crate::prelude::tracing::info_span!( | ||||
|             "test", | ||||
|             "{}", | ||||
|             // get the name of the test thread to generate a unique span for the test | ||||
|             std::thread::current() | ||||
|                 .name() | ||||
|                 .expect("test threads must be named") | ||||
|         ); | ||||
|         let _e = span.enter(); | ||||
|     }; | ||||
| } | ||||
|   | ||||
| @@ -28,7 +28,7 @@ pub use super::assert::assert_frame_eq; | ||||
|  | ||||
| // Re-export useful crates | ||||
| pub use tokio_test::io as mock_io; | ||||
| pub use {bytes, env_logger, futures, http, tokio::io as tokio_io}; | ||||
| pub use {bytes, futures, http, tokio::io as tokio_io, tracing, tracing_subscriber}; | ||||
|  | ||||
| // Re-export primary future types | ||||
| pub use futures::{Future, Sink, Stream}; | ||||
|   | ||||
							
								
								
									
										41
									
								
								tests/h2-support/src/trace.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										41
									
								
								tests/h2-support/src/trace.rs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,41 @@ | ||||
| use std::{io, str}; | ||||
| pub use tracing; | ||||
| pub use tracing_subscriber; | ||||
|  | ||||
| pub fn init() -> tracing::dispatcher::DefaultGuard { | ||||
|     tracing::subscriber::set_default( | ||||
|         tracing_subscriber::fmt() | ||||
|             .with_max_level(tracing::Level::TRACE) | ||||
|             .with_span_events(tracing_subscriber::fmt::format::FmtSpan::CLOSE) | ||||
|             .with_writer(PrintlnWriter { _p: () }) | ||||
|             .finish(), | ||||
|     ) | ||||
| } | ||||
|  | ||||
| struct PrintlnWriter { | ||||
|     _p: (), | ||||
| } | ||||
|  | ||||
| impl tracing_subscriber::fmt::MakeWriter for PrintlnWriter { | ||||
|     type Writer = PrintlnWriter; | ||||
|     fn make_writer(&self) -> Self::Writer { | ||||
|         PrintlnWriter { _p: () } | ||||
|     } | ||||
| } | ||||
|  | ||||
| impl io::Write for PrintlnWriter { | ||||
|     fn write(&mut self, buf: &[u8]) -> io::Result<usize> { | ||||
|         let s = str::from_utf8(buf).map_err(|e| io::Error::new(io::ErrorKind::InvalidInput, e))?; | ||||
|         println!("{}", s); | ||||
|         Ok(s.len()) | ||||
|     } | ||||
|  | ||||
|     fn write_fmt(&mut self, fmt: std::fmt::Arguments<'_>) -> io::Result<()> { | ||||
|         println!("{}", fmt); | ||||
|         Ok(()) | ||||
|     } | ||||
|  | ||||
|     fn flush(&mut self) -> io::Result<()> { | ||||
|         Ok(()) | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user