diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index 8009e359..7180c4ae 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -111,6 +111,7 @@ jobs: matrix: os: [ubuntu-latest] toolchain: [stable, beta] + features: [i-implement-a-third-party-backend-and-opt-into-breaking-changes, h3-tracing, 'h3-tracing,i-implement-a-third-party-backend-and-opt-into-breaking-changes'] runs-on: ${{ matrix.os }} steps: - uses: actions/checkout@v3 @@ -125,6 +126,7 @@ jobs: uses: actions-rs/cargo@v1 with: command: test + args: --features ${{ matrix.features }} - name: h3Spec run: ./ci/h3spec.sh if: matrix.toolchain == 'stable' diff --git a/examples/Cargo.toml b/examples/Cargo.toml index 6cdf5feb..9511344d 100644 --- a/examples/Cargo.toml +++ b/examples/Cargo.toml @@ -10,8 +10,8 @@ edition = "2021" anyhow = "1.0" bytes = "1" futures = "0.3" -h3 = { path = "../h3" } -h3-quinn = { path = "../h3-quinn" } +h3 = { path = "../h3", features = ["h3-tracing"] } +h3-quinn = { path = "../h3-quinn", features = ["h3-quinn-tracing"] } h3-webtransport = { path = "../h3-webtransport" } http = "1" quinn = { version = "0.11", default-features = false, features = [ @@ -20,7 +20,11 @@ quinn = { version = "0.11", default-features = false, features = [ "ring", ] } rcgen = { version = "0.13" } -rustls = { version = "0.23", default-features = false, features = ["logging", "ring", "std"] } +rustls = { version = "0.23", default-features = false, features = [ + "logging", + "ring", + "std", +] } rustls-native-certs = "0.7" structopt = "0.3" tokio = { version = "1.27", features = ["full"] } diff --git a/h3-quinn/Cargo.toml b/h3-quinn/Cargo.toml index aade3fdb..26a591b4 100644 --- a/h3-quinn/Cargo.toml +++ b/h3-quinn/Cargo.toml @@ -21,3 +21,7 @@ quinn = { version = "0.11", default-features = false, features = [ tokio-util = { version = "0.7.9" } futures = { version = "0.3.28" } tokio = { version = "1", features = ["io-util"], default-features = false } +tracing = { version = "0.1.40", optional = true } + +[features] +h3-quinn-tracing = ["tracing"] diff --git a/h3-quinn/src/lib.rs b/h3-quinn/src/lib.rs index 7819b383..05441627 100644 --- a/h3-quinn/src/lib.rs +++ b/h3-quinn/src/lib.rs @@ -28,6 +28,9 @@ use h3::{ }; use tokio_util::sync::ReusableBoxFuture; +#[cfg(feature = "h3-quinn-tracing")] +use tracing::instrument; + /// A QUIC connection backed by Quinn /// /// Implements a [`quic::Connection`] backed by a [`quinn::Connection`]. @@ -155,6 +158,7 @@ where type OpenStreams = OpenStreams; type AcceptError = ConnectionError; + #[cfg_attr(feature = "h3-quinn-tracing", instrument(skip_all))] fn poll_accept_bidi( &mut self, cx: &mut task::Context<'_>, @@ -169,6 +173,7 @@ where }))) } + #[cfg_attr(feature = "h3-quinn-tracing", instrument(skip_all))] fn poll_accept_recv( &mut self, cx: &mut task::Context<'_>, @@ -197,6 +202,7 @@ where type BidiStream = BidiStream; type OpenError = ConnectionError; + #[cfg_attr(feature = "h3-quinn-tracing", instrument(skip_all))] fn poll_open_bidi( &mut self, cx: &mut task::Context<'_>, @@ -215,6 +221,7 @@ where })) } + #[cfg_attr(feature = "h3-quinn-tracing", instrument(skip_all))] fn poll_open_send( &mut self, cx: &mut task::Context<'_>, @@ -229,6 +236,7 @@ where Poll::Ready(Ok(Self::SendStream::new(send))) } + #[cfg_attr(feature = "h3-quinn-tracing", instrument(skip_all))] fn close(&mut self, code: h3::error::Code, reason: &[u8]) { self.conn.close( VarInt::from_u64(code.value()).expect("error code VarInt"), @@ -243,6 +251,7 @@ where { type Error = SendDatagramError; + #[cfg_attr(feature = "h3-quinn-tracing", instrument(skip_all))] fn send_datagram(&mut self, data: Datagram) -> Result<(), SendDatagramError> { // TODO investigate static buffer from known max datagram size let mut buf = BytesMut::new(); @@ -259,6 +268,7 @@ impl quic::RecvDatagramExt for Connection { type Error = ConnectionError; #[inline] + #[cfg_attr(feature = "h3-quinn-tracing", instrument(skip_all))] fn poll_accept_datagram( &mut self, cx: &mut task::Context<'_>, @@ -289,6 +299,7 @@ where type BidiStream = BidiStream; type OpenError = ConnectionError; + #[cfg_attr(feature = "h3-quinn-tracing", instrument(skip_all))] fn poll_open_bidi( &mut self, cx: &mut task::Context<'_>, @@ -307,6 +318,7 @@ where })) } + #[cfg_attr(feature = "h3-quinn-tracing", instrument(skip_all))] fn poll_open_send( &mut self, cx: &mut task::Context<'_>, @@ -321,6 +333,7 @@ where Poll::Ready(Ok(Self::SendStream::new(send))) } + #[cfg_attr(feature = "h3-quinn-tracing", instrument(skip_all))] fn close(&mut self, code: h3::error::Code, reason: &[u8]) { self.conn.close( VarInt::from_u64(code.value()).expect("error code VarInt"), @@ -452,6 +465,7 @@ impl quic::RecvStream for RecvStream { type Buf = Bytes; type Error = ReadError; + #[cfg_attr(feature = "h3-quinn-tracing", instrument(skip_all))] fn poll_data( &mut self, cx: &mut task::Context<'_>, @@ -468,6 +482,7 @@ impl quic::RecvStream for RecvStream { Poll::Ready(Ok(chunk?.map(|c| c.bytes))) } + #[cfg_attr(feature = "h3-quinn-tracing", instrument(skip_all))] fn stop_sending(&mut self, error_code: u64) { self.stream .as_mut() @@ -476,6 +491,7 @@ impl quic::RecvStream for RecvStream { .ok(); } + #[cfg_attr(feature = "h3-quinn-tracing", instrument(skip_all))] fn recv_id(&self) -> StreamId { self.stream .as_ref() @@ -573,6 +589,7 @@ where { type Error = SendStreamError; + #[cfg_attr(feature = "h3-quinn-tracing", instrument(skip_all))] fn poll_ready(&mut self, cx: &mut task::Context<'_>) -> Poll> { if let Some(ref mut data) = self.writing { while data.has_remaining() { @@ -598,10 +615,12 @@ where Poll::Ready(Ok(())) } + #[cfg_attr(feature = "h3-quinn-tracing", instrument(skip_all))] fn poll_finish(&mut self, _cx: &mut task::Context<'_>) -> Poll> { Poll::Ready(self.stream.as_mut().unwrap().finish().map_err(|e| e.into())) } + #[cfg_attr(feature = "h3-quinn-tracing", instrument(skip_all))] fn reset(&mut self, reset_code: u64) { let _ = self .stream @@ -610,6 +629,7 @@ where .reset(VarInt::from_u64(reset_code).unwrap_or(VarInt::MAX)); } + #[cfg_attr(feature = "h3-quinn-tracing", instrument(skip_all))] fn send_data>>(&mut self, data: D) -> Result<(), Self::Error> { if self.writing.is_some() { return Err(Self::Error::NotReady); @@ -618,6 +638,7 @@ where Ok(()) } + #[cfg_attr(feature = "h3-quinn-tracing", instrument(skip_all))] fn send_id(&self) -> StreamId { self.stream .as_ref() @@ -633,6 +654,7 @@ impl quic::SendStreamUnframed for SendStream where B: Buf, { + #[cfg_attr(feature = "h3-quinn-tracing", instrument(skip_all))] fn poll_send( &mut self, cx: &mut task::Context<'_>, diff --git a/h3/Cargo.toml b/h3/Cargo.toml index 7475942a..04a94c57 100644 --- a/h3/Cargo.toml +++ b/h3/Cargo.toml @@ -21,6 +21,7 @@ categories = [ [features] i-implement-a-third-party-backend-and-opt-into-breaking-changes = [] +h3-tracing = ["tracing"] [dependencies] bytes = "1" @@ -28,7 +29,7 @@ futures-util = { version = "0.3", default-features = false, features = ["io"] } http = "1" tokio = { version = "1", features = ["sync"] } pin-project-lite = { version = "0.2", default_features = false } -tracing = "0.1.40" +tracing = {version = "0.1.40", optional = true} fastrand = "2.0.1" [dev-dependencies] diff --git a/h3/src/client/connection.rs b/h3/src/client/connection.rs index 7fb591e9..d6bf946b 100644 --- a/h3/src/client/connection.rs +++ b/h3/src/client/connection.rs @@ -9,7 +9,9 @@ use std::{ use bytes::{Buf, BytesMut}; use futures_util::future; use http::request; -use tracing::{info, trace}; + +#[cfg(feature = "h3-tracing")] +use tracing::{info, instrument, trace}; use crate::{ connection::{self, ConnectionInner, ConnectionState, SharedStateRef}, @@ -121,6 +123,7 @@ where B: Buf, { /// Send an HTTP/3 request to the server + #[cfg_attr(feature = "h3-tracing", instrument(skip_all))] pub async fn send_request( &mut self, req: http::Request<()>, @@ -346,17 +349,20 @@ where B: Buf, { /// Initiate a graceful shutdown, accepting `max_push` potentially in-flight server pushes + #[cfg_attr(feature = "h3-tracing", instrument(skip_all))] pub async fn shutdown(&mut self, _max_push: usize) -> Result<(), Error> { // TODO: Calculate remaining pushes once server push is implemented. self.inner.shutdown(&mut self.sent_closing, PushId(0)).await } /// Wait until the connection is closed + #[cfg_attr(feature = "h3-tracing", instrument(skip_all))] pub async fn wait_idle(&mut self) -> Result<(), Error> { future::poll_fn(|cx| self.poll_close(cx)).await } /// Maintain the connection state until it is closed + #[cfg_attr(feature = "h3-tracing", instrument(skip_all))] pub fn poll_close(&mut self, cx: &mut Context<'_>) -> Poll> { while let Poll::Ready(result) = self.inner.poll_control(cx) { match result { @@ -379,7 +385,12 @@ where //= type=TODO //# Once a server has provided new settings, //# clients MUST comply with those values. - Ok(Frame::Settings(_)) => trace!("Got settings"), + Ok(Frame::Settings(_)) => { + #[cfg(feature = "h3-tracing")] + trace!("Got settings"); + () + } + Ok(Frame::Goaway(id)) => { //= https://www.rfc-editor.org/rfc/rfc9114#section-7.2.6 //# The GOAWAY frame is always sent on the control stream. In the @@ -395,6 +406,7 @@ where } self.inner.process_goaway(&mut self.recv_closing, id)?; + #[cfg(feature = "h3-tracing")] info!("Server initiated graceful shutdown, last: StreamId({})", id); } diff --git a/h3/src/client/stream.rs b/h3/src/client/stream.rs index e2b4b8e6..978de81e 100644 --- a/h3/src/client/stream.rs +++ b/h3/src/client/stream.rs @@ -1,6 +1,8 @@ use bytes::Buf; use futures_util::future; use http::{HeaderMap, Response}; +#[cfg(feature = "h3-tracing")] +use tracing::instrument; use crate::{ connection::{self, ConnectionState, SharedStateRef}, @@ -82,6 +84,7 @@ where /// This should be called before trying to receive any data with [`recv_data()`]. /// /// [`recv_data()`]: #method.recv_data + #[cfg_attr(feature = "h3-tracing", instrument(skip_all))] pub async fn recv_response(&mut self) -> Result, Error> { let mut frame = future::poll_fn(|cx| self.inner.stream.poll_next(cx)) .await @@ -141,11 +144,13 @@ where /// Receive some of the request body. // TODO what if called before recv_response ? + #[cfg_attr(feature = "h3-tracing", instrument(skip_all))] pub async fn recv_data(&mut self) -> Result, Error> { self.inner.recv_data().await } /// Receive an optional set of trailers for the response. + #[cfg_attr(feature = "h3-tracing", instrument(skip_all))] pub async fn recv_trailers(&mut self) -> Result, Error> { let res = self.inner.recv_trailers().await; if let Err(ref e) = res { @@ -157,6 +162,7 @@ where } /// Tell the peer to stop sending into the underlying QUIC stream + #[cfg_attr(feature = "h3-tracing", instrument(skip_all))] pub fn stop_sending(&mut self, error_code: crate::error::Code) { // TODO take by value to prevent any further call as this request is cancelled // rename `cancel()` ? @@ -170,6 +176,7 @@ where B: Buf, { /// Send some data on the request body. + #[cfg_attr(feature = "h3-tracing", instrument(skip_all))] pub async fn send_data(&mut self, buf: B) -> Result<(), Error> { self.inner.send_data(buf).await } @@ -179,6 +186,7 @@ where /// Either [`RequestStream::finish`] or /// [`RequestStream::send_trailers`] must be called to finalize a /// request. + #[cfg_attr(feature = "h3-tracing", instrument(skip_all))] pub async fn send_trailers(&mut self, trailers: HeaderMap) -> Result<(), Error> { self.inner.send_trailers(trailers).await } @@ -188,6 +196,7 @@ where /// Either [`RequestStream::finish`] or /// [`RequestStream::send_trailers`] must be called to finalize a /// request. + #[cfg_attr(feature = "h3-tracing", instrument(skip_all))] pub async fn finish(&mut self) -> Result<(), Error> { self.inner.finish().await } diff --git a/h3/src/config.rs b/h3/src/config.rs index ffff248c..1c1271f2 100644 --- a/h3/src/config.rs +++ b/h3/src/config.rs @@ -105,7 +105,10 @@ impl TryFrom for frame::Settings { //# H3_SETTINGS_ERROR. match settings.insert(frame::SettingId::grease(), 0) { Ok(_) => (), - Err(err) => tracing::warn!("Error when adding the grease Setting. Reason {}", err), + Err(_err) => { + #[cfg(feature = "h3-tracing")] + tracing::warn!("Error when adding the grease Setting. Reason {}", _err); + } } } diff --git a/h3/src/connection.rs b/h3/src/connection.rs index 5966eb87..43fa5d6f 100644 --- a/h3/src/connection.rs +++ b/h3/src/connection.rs @@ -9,7 +9,9 @@ use bytes::{Buf, Bytes, BytesMut}; use futures_util::{future, ready}; use http::HeaderMap; use stream::WriteBuf; -use tracing::warn; + +#[cfg(feature = "h3-tracing")] +use tracing::{instrument, warn}; use crate::{ config::{Config, Settings}, @@ -167,6 +169,7 @@ where B: Buf, { /// Sends the settings and initializes the control streams + #[cfg_attr(feature = "h3-tracing", instrument(skip_all))] pub async fn send_control_stream_headers(&mut self) -> Result<(), Error> { #[cfg(test)] if !self.config.send_settings { @@ -176,6 +179,7 @@ where let settings = frame::Settings::try_from(self.config) .map_err(|e| Code::H3_INTERNAL_ERROR.with_cause(e))?; + #[cfg(feature = "h3-tracing")] tracing::debug!("Sending server settings: {:#x?}", settings); //= https://www.rfc-editor.org/rfc/rfc9114#section-3.2 @@ -232,6 +236,7 @@ where } /// Initiates the connection and opens a control stream + #[cfg_attr(feature = "h3-tracing", instrument(skip_all))] pub async fn new(mut conn: C, shared: SharedStateRef, config: Config) -> Result { //= https://www.rfc-editor.org/rfc/rfc9114#section-6.2 //# Endpoints SHOULD create the HTTP control stream as well as the @@ -288,7 +293,7 @@ where } /// Send GOAWAY with specified max_id, iff max_id is smaller than the previous one. - + #[cfg_attr(feature = "h3-tracing", instrument(skip_all))] pub async fn shutdown( &mut self, sent_closing: &mut Option, @@ -317,6 +322,7 @@ where } #[allow(missing_docs)] + #[cfg_attr(feature = "h3-tracing", instrument(skip_all))] pub fn poll_accept_request( &mut self, cx: &mut Context<'_>, @@ -337,6 +343,7 @@ where /// Polls incoming streams /// /// Accepted streams which are not control, decoder, or encoder streams are buffer in `accepted_recv_streams` + #[cfg_attr(feature = "h3-tracing", instrument(skip_all))] pub fn poll_accept_recv(&mut self, cx: &mut Context<'_>) -> Result<(), Error> { if let Some(ref e) = self.shared.read("poll_accept_request").error { return Err(e.clone()); @@ -425,6 +432,7 @@ where } /// Waits for the control stream to be received and reads subsequent frames. + #[cfg_attr(feature = "h3-tracing", instrument(skip_all))] pub fn poll_control(&mut self, cx: &mut Context<'_>) -> Poll, Error>> { if let Some(ref e) = self.shared.read("poll_accept_request").error { return Poll::Ready(Err(e.clone())); @@ -546,6 +554,7 @@ where Poll::Ready(res) } + #[cfg_attr(feature = "h3-tracing", instrument(skip_all))] pub(crate) fn process_goaway( &mut self, recv_closing: &mut Option, @@ -590,6 +599,7 @@ where /// Closes a Connection with code and reason. /// It returns an [`Error`] which can be returned. + #[cfg_attr(feature = "h3-tracing", instrument(skip_all))] pub fn close>(&mut self, code: Code, reason: T) -> Error { self.shared.write("connection close err").error = Some(code.with_reason(reason.as_ref(), crate::error::ErrorLevel::ConnectionError)); @@ -598,6 +608,7 @@ where } // start grease stream and send data + #[cfg_attr(feature = "h3-tracing", instrument(skip_all))] fn poll_grease_stream(&mut self, cx: &mut Context<'_>) -> Poll<()> { if matches!(self.grease_step, GreaseStatus::NotStarted(_)) { self.grease_step = match self.conn.poll_open_send(cx) { @@ -606,7 +617,10 @@ where // could not create grease stream // don't try again self.send_grease_stream_flag = false; + + #[cfg(feature = "h3-tracing")] warn!("grease stream creation failed with"); + return Poll::Ready(()); } Poll::Ready(Ok(stream)) => GreaseStatus::Started(Some(stream)), @@ -625,7 +639,10 @@ where .is_err() { self.send_grease_stream_flag = false; + + #[cfg(feature = "h3-tracing")] warn!("write data on grease stream failed with"); + return Poll::Ready(()); }; } @@ -641,7 +658,10 @@ where // could not write grease frame // don't try again self.send_grease_stream_flag = false; + + #[cfg(feature = "h3-tracing")] warn!("write data on grease stream failed with"); + return Poll::Ready(()); } }; @@ -668,7 +688,10 @@ where // could not finish grease stream // don't try again self.send_grease_stream_flag = false; + + #[cfg(feature = "h3-tracing")] warn!("finish grease stream failed with"); + return Poll::Ready(()); } }; @@ -682,6 +705,7 @@ where } #[allow(missing_docs)] + #[cfg_attr(feature = "h3-tracing", instrument(skip_all))] pub fn accepted_streams_mut(&mut self) -> &mut AcceptedStreams { &mut self.accepted_streams } @@ -725,6 +749,7 @@ where S: quic::RecvStream, { /// Receive some of the request body. + #[cfg_attr(feature = "h3-tracing", instrument(skip_all))] pub fn poll_recv_data( &mut self, cx: &mut Context<'_>, @@ -773,12 +798,15 @@ where .poll_data(cx) .map_err(|e| self.maybe_conn_err(e)) } + /// Receive some of the request body. + #[cfg_attr(feature = "h3-tracing", instrument(skip_all))] pub async fn recv_data(&mut self) -> Result, Error> { future::poll_fn(|cx| self.poll_recv_data(cx)).await } /// Receive trailers + #[cfg_attr(feature = "h3-tracing", instrument(skip_all))] pub async fn recv_trailers(&mut self) -> Result, Error> { let mut trailers = if let Some(encoded) = self.trailers.take() { encoded @@ -847,6 +875,7 @@ where } #[allow(missing_docs)] + #[cfg_attr(feature = "h3-tracing", instrument(skip_all))] pub fn stop_sending(&mut self, err_code: Code) { self.stream.stop_sending(err_code); } @@ -858,6 +887,7 @@ where B: Buf, { /// Send some data on the response body. + #[cfg_attr(feature = "h3-tracing", instrument(skip_all))] pub async fn send_data(&mut self, buf: B) -> Result<(), Error> { let frame = Frame::Data(buf); @@ -868,6 +898,7 @@ where } /// Send a set of trailers to end the request. + #[cfg_attr(feature = "h3-tracing", instrument(skip_all))] pub async fn send_trailers(&mut self, trailers: HeaderMap) -> Result<(), Error> { //= https://www.rfc-editor.org/rfc/rfc9114#section-4.2 //= type=TODO @@ -898,11 +929,13 @@ where } /// Stops a stream with an error code + #[cfg_attr(feature = "h3-tracing", instrument(skip_all))] pub fn stop_stream(&mut self, code: Code) { self.stream.reset(code.into()); } #[allow(missing_docs)] + #[cfg_attr(feature = "h3-tracing", instrument(skip_all))] pub async fn finish(&mut self) -> Result<(), Error> { if self.send_grease_frame { // send a grease frame once per Connection @@ -923,6 +956,7 @@ where S: quic::BidiStream, B: Buf, { + #[cfg_attr(feature = "h3-tracing", instrument(skip_all))] pub(crate) fn split( self, ) -> ( diff --git a/h3/src/frame.rs b/h3/src/frame.rs index a166fa1b..fac41b19 100644 --- a/h3/src/frame.rs +++ b/h3/src/frame.rs @@ -2,6 +2,7 @@ use std::task::{Context, Poll}; use bytes::Buf; +#[cfg(feature = "h3-tracing")] use tracing::trace; use crate::stream::{BufRecvStream, WriteBuf}; @@ -227,8 +228,10 @@ impl FrameDecoder { }; match decoded { - Err(frame::FrameError::UnknownFrame(ty)) => { - trace!("ignore unknown frame type {:#x}", ty); + Err(frame::FrameError::UnknownFrame(_ty)) => { + #[cfg(feature = "h3-tracing")] + trace!("ignore unknown frame type {:#x}", _ty); + src.advance(pos); self.expected = None; continue; diff --git a/h3/src/proto/frame.rs b/h3/src/proto/frame.rs index fcebcd84..20f9d9c2 100644 --- a/h3/src/proto/frame.rs +++ b/h3/src/proto/frame.rs @@ -3,6 +3,7 @@ use std::{ convert::TryInto, fmt::{self, Debug}, }; +#[cfg(feature = "h3-tracing")] use tracing::trace; use crate::webtransport::SessionId; @@ -86,7 +87,9 @@ impl Frame { // // See: https://datatracker.ietf.org/doc/html/draft-ietf-webtrans-http3/#section-4.2 if ty == FrameType::WEBTRANSPORT_BI_STREAM { + #[cfg(feature = "h3-tracing")] tracing::trace!("webtransport frame"); + return Ok(Frame::WebTransportStream(SessionId::decode(buf)?)); } @@ -103,7 +106,10 @@ impl Frame { } let mut payload = buf.take(len as usize); + + #[cfg(feature = "h3-tracing")] trace!("frame ty: {:?}", ty); + let frame = match ty { FrameType::HEADERS => Ok(Frame::Headers(payload.copy_to_bytes(len as usize))), FrameType::SETTINGS => Ok(Frame::Settings(Settings::decode(&mut payload)?)), @@ -122,10 +128,11 @@ impl Frame { } }; - if let Ok(frame) = &frame { + if let Ok(_frame) = &frame { + #[cfg(feature = "h3-tracing")] trace!( "got frame {:?}, len: {}, remaining: {}", - frame, + _frame, len, buf.remaining() ); @@ -536,6 +543,7 @@ impl Settings { //# H3_SETTINGS_ERROR. settings.insert(identifier, value)?; } else { + #[cfg(feature = "h3-tracing")] tracing::debug!("Unsupported setting: {:#x?}", identifier); } } diff --git a/h3/src/qpack/decoder.rs b/h3/src/qpack/decoder.rs index 799002bc..48c1bf7e 100644 --- a/h3/src/qpack/decoder.rs +++ b/h3/src/qpack/decoder.rs @@ -1,6 +1,7 @@ use bytes::{Buf, BufMut}; use std::{convert::TryInto, fmt, io::Cursor, num::TryFromIntError}; +#[cfg(feature = "h3-tracing")] use tracing::trace; use super::{ @@ -118,7 +119,9 @@ impl Decoder { let inserted_on_start = self.table.total_inserted(); while let Some(instruction) = self.parse_instruction(read)? { + #[cfg(feature = "h3-tracing")] trace!("instruction {:?}", instruction); + match instruction { Instruction::Insert(field) => self.table.put(field)?, Instruction::TableSizeUpdate(size) => { diff --git a/h3/src/server/connection.rs b/h3/src/server/connection.rs index 88d2d851..cf7a0941 100644 --- a/h3/src/server/connection.rs +++ b/h3/src/server/connection.rs @@ -37,7 +37,8 @@ use crate::{ use crate::server::request::ResolveRequest; -use tracing::{trace, warn}; +#[cfg(feature = "h3-tracing")] +use tracing::{instrument, trace, warn}; use super::stream::{ReadDatagram, RequestStream}; @@ -89,11 +90,13 @@ where /// Use a custom [`super::builder::Builder`] with [`super::builder::builder()`] to create a connection /// with different settings. /// Provide a Connection which implements [`quic::Connection`]. + #[cfg_attr(feature = "h3-tracing", instrument(skip_all))] pub async fn new(conn: C) -> Result { super::builder::builder().build(conn).await } /// Closes the connection with a code and a reason. + #[cfg_attr(feature = "h3-tracing", instrument(skip_all))] pub fn close>(&mut self, code: Code, reason: T) -> Error { self.inner.close(code, reason) } @@ -109,6 +112,7 @@ where /// It returns a tuple with a [`http::Request`] and an [`RequestStream`]. /// The [`http::Request`] is the received request from the client. /// The [`RequestStream`] can be used to send the response. + #[cfg_attr(feature = "h3-tracing", instrument(skip_all))] pub async fn accept( &mut self, ) -> Result, RequestStream)>, Error> { @@ -154,6 +158,7 @@ where /// This is needed as a bidirectional stream may be read as part of incoming webtransport /// bi-streams. If it turns out that the stream is *not* a `WEBTRANSPORT_STREAM` the request /// may still want to be handled and passed to the user. + #[cfg_attr(feature = "h3-tracing", instrument(skip_all))] pub fn accept_with_frame( &mut self, mut stream: FrameStream, @@ -285,6 +290,7 @@ where /// Initiate a graceful shutdown, accepting `max_request` potentially still in-flight /// /// See [connection shutdown](https://www.rfc-editor.org/rfc/rfc9114.html#connection-shutdown) for more information. + #[cfg_attr(feature = "h3-tracing", instrument(skip_all))] pub async fn shutdown(&mut self, max_requests: usize) -> Result<(), Error> { let max_id = self .last_accepted_stream @@ -298,6 +304,7 @@ where /// /// This could be either a *Request* or a *WebTransportBiStream*, the first frame's type /// decides. + #[cfg_attr(feature = "h3-tracing", instrument(skip_all))] pub fn poll_accept_request( &mut self, cx: &mut Context<'_>, @@ -346,11 +353,13 @@ where } } + #[cfg_attr(feature = "h3-tracing", instrument(skip_all))] pub(crate) fn poll_control(&mut self, cx: &mut Context<'_>) -> Poll> { while (self.poll_next_control(cx)?).is_ready() {} Poll::Pending } + #[cfg_attr(feature = "h3-tracing", instrument(skip_all))] pub(crate) fn poll_next_control( &mut self, cx: &mut Context<'_>, @@ -358,10 +367,15 @@ where let frame = ready!(self.inner.poll_control(cx))?; match &frame { - Frame::Settings(w) => trace!("Got settings > {:?}", w), + Frame::Settings(_setting) => { + #[cfg(feature = "h3-tracing")] + trace!("Got settings > {:?}", _setting); + () + } &Frame::Goaway(id) => self.inner.process_goaway(&mut self.recv_closing, id)?, - f @ Frame::MaxPushId(_) | f @ Frame::CancelPush(_) => { - warn!("Control frame ignored {:?}", f); + _frame @ Frame::MaxPushId(_) | _frame @ Frame::CancelPush(_) => { + #[cfg(feature = "h3-tracing")] + warn!("Control frame ignored {:?}", _frame); //= https://www.rfc-editor.org/rfc/rfc9114#section-7.2.3 //= type=TODO @@ -391,6 +405,7 @@ where Poll::Ready(Ok(frame)) } + #[cfg_attr(feature = "h3-tracing", instrument(skip_all))] fn poll_requests_completion(&mut self, cx: &mut Context<'_>) -> Poll<()> { loop { match self.request_end_recv.poll_recv(cx) { @@ -420,10 +435,13 @@ where B: Buf, { /// Sends a datagram + #[cfg_attr(feature = "h3-tracing", instrument(skip_all))] pub fn send_datagram(&mut self, stream_id: StreamId, data: B) -> Result<(), Error> { self.inner .conn .send_datagram(Datagram::new(stream_id, data))?; + + #[cfg(feature = "h3-tracing")] tracing::info!("Sent datagram"); Ok(()) @@ -436,6 +454,7 @@ where B: Buf, { /// Reads an incoming datagram + #[cfg_attr(feature = "h3-tracing", instrument(skip_all))] pub fn read_datagram(&mut self) -> ReadDatagram { ReadDatagram { conn: self, @@ -449,6 +468,7 @@ where C: quic::Connection, B: Buf, { + #[cfg_attr(feature = "h3-tracing", instrument(skip_all))] fn drop(&mut self) { self.inner.close(Code::H3_NO_ERROR, ""); } diff --git a/h3/src/server/request.rs b/h3/src/server/request.rs index 19c379d8..7d986c86 100644 --- a/h3/src/server/request.rs +++ b/h3/src/server/request.rs @@ -3,6 +3,9 @@ use std::convert::TryFrom; use bytes::Buf; use http::{Request, StatusCode}; +#[cfg(feature = "h3-tracing")] +use tracing::instrument; + use crate::{error::Code, proto::headers::Header, qpack, quic, Error}; use super::stream::RequestStream; @@ -28,6 +31,7 @@ impl> ResolveRequest { } /// Finishes the resolution of the request + #[cfg_attr(feature = "h3-tracing", instrument(skip_all))] pub async fn resolve( mut self, ) -> Result<(Request<()>, RequestStream), Error> { @@ -88,7 +92,10 @@ impl> ResolveRequest { *req.version_mut() = http::Version::HTTP_3; // send the grease frame only once // self.inner.send_grease_frame = false; + + #[cfg(feature = "h3-tracing")] tracing::trace!("replying with: {:?}", req); + Ok((req, self.request_stream)) } } diff --git a/h3/src/server/stream.rs b/h3/src/server/stream.rs index 610b83b6..11a9d0cd 100644 --- a/h3/src/server/stream.rs +++ b/h3/src/server/stream.rs @@ -33,7 +33,8 @@ use crate::{ stream::{self}, }; -use tracing::error; +#[cfg(feature = "h3-tracing")] +use tracing::{error, instrument}; /// Manage request and response transfer for an incoming request /// @@ -62,11 +63,13 @@ where B: Buf, { /// Receive data sent from the client + #[cfg_attr(feature = "h3-tracing", instrument(skip_all))] pub async fn recv_data(&mut self) -> Result, Error> { self.inner.recv_data().await } /// Poll for data sent from the client + #[cfg_attr(feature = "h3-tracing", instrument(skip_all))] pub fn poll_recv_data( &mut self, cx: &mut Context<'_>, @@ -75,11 +78,13 @@ where } /// Receive an optional set of trailers for the request + #[cfg_attr(feature = "h3-tracing", instrument(skip_all))] pub async fn recv_trailers(&mut self) -> Result, Error> { self.inner.recv_trailers().await } /// Tell the peer to stop sending into the underlying QUIC stream + #[cfg_attr(feature = "h3-tracing", instrument(skip_all))] pub fn stop_sending(&mut self, error_code: crate::error::Code) { self.inner.stream.stop_sending(error_code) } @@ -205,10 +210,11 @@ where impl Drop for RequestEnd { fn drop(&mut self) { - if let Err(e) = self.request_end.send(self.stream_id) { + if let Err(_error) = self.request_end.send(self.stream_id) { + #[cfg(feature = "h3-tracing")] error!( "failed to notify connection of request end: {} {}", - self.stream_id, e + self.stream_id, _error ); } } @@ -234,7 +240,9 @@ where type Output = Result>, Error>; fn poll(mut self: std::pin::Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { + #[cfg(feature = "h3-tracing")] tracing::trace!("poll: read_datagram"); + match ready!(self.conn.inner.conn.poll_accept_datagram(cx))? { Some(v) => Poll::Ready(Ok(Some(Datagram::decode(v)?))), None => Poll::Ready(Ok(None)),