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:
@@ -61,6 +61,8 @@ impl<T> FramedRead<T> {
|
||||
|
||||
fn decode_frame(&mut self, mut bytes: BytesMut) -> Result<Option<Frame>, RecvError> {
|
||||
use self::RecvError::*;
|
||||
let span = tracing::trace_span!("FramedRead::decode_frame", offset = bytes.len());
|
||||
let _e = span.enter();
|
||||
|
||||
tracing::trace!("decoding frame from {}B", bytes.len());
|
||||
|
||||
@@ -74,7 +76,7 @@ impl<T> FramedRead<T> {
|
||||
|
||||
let kind = head.kind();
|
||||
|
||||
tracing::trace!(" -> kind={:?}", kind);
|
||||
tracing::trace!(frame.kind = ?kind);
|
||||
|
||||
macro_rules! header_block {
|
||||
($frame:ident, $head:ident, $bytes:ident) => ({
|
||||
@@ -338,6 +340,8 @@ where
|
||||
type Item = Result<Frame, RecvError>;
|
||||
|
||||
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
|
||||
let span = tracing::trace_span!("FramedRead::poll_next");
|
||||
let _e = span.enter();
|
||||
loop {
|
||||
tracing::trace!("poll");
|
||||
let bytes = match ready!(Pin::new(&mut self.inner).poll_next(cx)) {
|
||||
@@ -346,9 +350,9 @@ where
|
||||
None => return Poll::Ready(None),
|
||||
};
|
||||
|
||||
tracing::trace!("poll; bytes={}B", bytes.len());
|
||||
tracing::trace!(read.bytes = bytes.len());
|
||||
if let Some(frame) = self.decode_frame(bytes)? {
|
||||
tracing::debug!("received; frame={:?}", frame);
|
||||
tracing::debug!(?frame, "received");
|
||||
return Poll::Ready(Some(Ok(frame)));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -105,8 +105,10 @@ where
|
||||
pub fn buffer(&mut self, item: Frame<B>) -> Result<(), UserError> {
|
||||
// Ensure that we have enough capacity to accept the write.
|
||||
assert!(self.has_capacity());
|
||||
let span = tracing::trace_span!("FramedWrite::buffer", frame = ?item);
|
||||
let _e = span.enter();
|
||||
|
||||
tracing::debug!("send; frame={:?}", item);
|
||||
tracing::debug!(frame = ?item, "send");
|
||||
|
||||
match item {
|
||||
Frame::Data(mut v) => {
|
||||
@@ -150,19 +152,19 @@ where
|
||||
}
|
||||
Frame::Settings(v) => {
|
||||
v.encode(self.buf.get_mut());
|
||||
tracing::trace!("encoded settings; rem={:?}", self.buf.remaining());
|
||||
tracing::trace!(rem = self.buf.remaining(), "encoded settings");
|
||||
}
|
||||
Frame::GoAway(v) => {
|
||||
v.encode(self.buf.get_mut());
|
||||
tracing::trace!("encoded go_away; rem={:?}", self.buf.remaining());
|
||||
tracing::trace!(rem = self.buf.remaining(), "encoded go_away");
|
||||
}
|
||||
Frame::Ping(v) => {
|
||||
v.encode(self.buf.get_mut());
|
||||
tracing::trace!("encoded ping; rem={:?}", self.buf.remaining());
|
||||
tracing::trace!(rem = self.buf.remaining(), "encoded ping");
|
||||
}
|
||||
Frame::WindowUpdate(v) => {
|
||||
v.encode(self.buf.get_mut());
|
||||
tracing::trace!("encoded window_update; rem={:?}", self.buf.remaining());
|
||||
tracing::trace!(rem = self.buf.remaining(), "encoded window_update");
|
||||
}
|
||||
|
||||
Frame::Priority(_) => {
|
||||
@@ -174,7 +176,7 @@ where
|
||||
}
|
||||
Frame::Reset(v) => {
|
||||
v.encode(self.buf.get_mut());
|
||||
tracing::trace!("encoded reset; rem={:?}", self.buf.remaining());
|
||||
tracing::trace!(rem = self.buf.remaining(), "encoded reset");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -183,18 +185,19 @@ where
|
||||
|
||||
/// Flush buffered data to the wire
|
||||
pub fn flush(&mut self, cx: &mut Context) -> Poll<io::Result<()>> {
|
||||
tracing::trace!("flush");
|
||||
let span = tracing::trace_span!("FramedWrite::flush");
|
||||
let _e = span.enter();
|
||||
|
||||
loop {
|
||||
while !self.is_empty() {
|
||||
match self.next {
|
||||
Some(Next::Data(ref mut frame)) => {
|
||||
tracing::trace!(" -> queued data frame");
|
||||
tracing::trace!(queued_data_frame = true);
|
||||
let mut buf = (&mut self.buf).chain(frame.payload_mut());
|
||||
ready!(Pin::new(&mut self.inner).poll_write_buf(cx, &mut buf))?;
|
||||
}
|
||||
_ => {
|
||||
tracing::trace!(" -> not a queued data frame");
|
||||
tracing::trace!(queued_data_frame = false);
|
||||
ready!(Pin::new(&mut self.inner).poll_write_buf(cx, &mut self.buf))?;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user