Skip to content

Commit 57c5827

Browse files
committed
feat(upgrade): introduce tracing as an optional unstable feature
This change allow users to opt out of tracing via the `tracing` crate by adding tracing as an optional feature. This change is part of the effort, outlined in #2874, to reach hyper 1.0. Closes #3319 BREAKING CHANGES: tracing is disabled by default and requires users to opt in to revert to previous behavior.
1 parent 4281b44 commit 57c5827

14 files changed

Lines changed: 155 additions & 4 deletions

File tree

Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ httpdate = "1.0"
3030
httparse = "1.8"
3131
h2 = { version = "0.3.9", optional = true }
3232
itoa = "1"
33-
tracing = { version = "0.1", default-features = false, features = ["std"] }
3433
pin-project-lite = "0.2.4"
3534
tokio = { version = "1", features = ["sync"] }
3635
want = "0.3"
3736

3837
# Optional
3938

4039
libc = { version = "0.2", optional = true }
40+
tracing = { version = "0.1", default-features = false, features = ["std"], optional = true }
4141

4242
[dev-dependencies]
4343
futures-util = { version = "0.3", default-features = false, features = ["alloc"] }
@@ -84,6 +84,9 @@ server = []
8484
# C-API support (currently unstable (no semver))
8585
ffi = ["dep:libc", "dep:http-body-util"]
8686

87+
# Utilize tracing (currently unstable)
88+
tracing = ["dep:tracing"]
89+
8790
# internal features used in CI
8891
nightly = []
8992

src/body/length.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ impl DecodedLength {
5050
/// Checks the `u64` is within the maximum allowed for content-length.
5151
#[cfg(any(feature = "http1", feature = "http2"))]
5252
pub(crate) fn checked_new(len: u64) -> Result<Self, crate::error::Parse> {
53+
#[cfg(feature = "tracing")]
5354
use tracing::warn;
5455

5556
if len <= MAX_LEN {

src/client/conn/http1.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ where
191191
Err(_canceled) => panic!("dispatch dropped without returning error"),
192192
},
193193
Err(_req) => {
194+
#[cfg(feature = "tracing")]
194195
tracing::debug!("connection was not ready");
195196

196197
Err(crate::Error::new_canceled().with("connection was not ready"))
@@ -219,6 +220,7 @@ where
219220
}))
220221
}
221222
Err(req) => {
223+
#[cfg(feature = "tracing")]
222224
tracing::debug!("connection was not ready");
223225
let err = crate::Error::new_canceled().with("connection was not ready");
224226
Either::Right(future::err((err, Some(req))))
@@ -478,6 +480,7 @@ impl Builder {
478480
let opts = self.clone();
479481

480482
async move {
483+
#[cfg(feature = "tracing")]
481484
tracing::trace!("client handshake HTTP/1");
482485

483486
let (tx, rx) = dispatch::channel();

src/client/conn/http2.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ where
146146
Err(_canceled) => panic!("dispatch dropped without returning error"),
147147
},
148148
Err(_req) => {
149+
#[cfg(feature = "tracing")]
149150
tracing::debug!("connection was not ready");
150151

151152
Err(crate::Error::new_canceled().with("connection was not ready"))
@@ -174,6 +175,7 @@ where
174175
}))
175176
}
176177
Err(req) => {
178+
#[cfg(feature = "tracing")]
177179
tracing::debug!("connection was not ready");
178180
let err = crate::Error::new_canceled().with("connection was not ready");
179181
Either::Right(future::err((err, Some(req))))
@@ -407,6 +409,7 @@ where
407409
let opts = self.clone();
408410

409411
async move {
412+
#[cfg(feature = "tracing")]
410413
tracing::trace!("client handshake HTTP/1");
411414

412415
let (tx, rx) = dispatch::channel();

src/client/dispatch.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use http::{Request, Response};
55
use http_body::Body;
66
use pin_project_lite::pin_project;
77
use tokio::sync::{mpsc, oneshot};
8+
#[cfg(feature = "tracing")]
89
use tracing::trace;
910

1011
use crate::{
@@ -316,6 +317,7 @@ where
316317
return std::task::Poll::Pending;
317318
}
318319
};
320+
#[cfg(feature = "tracing")]
319321
trace!("send_when canceled");
320322
Poll::Ready(())
321323
}

src/proto/h1/conn.rs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use bytes::{Buf, Bytes};
99
use http::header::{HeaderValue, CONNECTION};
1010
use http::{HeaderMap, Method, Version};
1111
use httparse::ParserConfig;
12+
#[cfg(feature = "tracing")]
1213
use tracing::{debug, error, trace};
1314

1415
use super::io::Buffered;
@@ -197,6 +198,7 @@ where
197198
cx: &mut task::Context<'_>,
198199
) -> Poll<Option<crate::Result<(MessageHead<T::Incoming>, DecodedLength, Wants)>>> {
199200
debug_assert!(self.can_read_head());
201+
#[cfg(feature = "tracing")]
200202
trace!("Conn::read_head");
201203

202204
let msg = match ready!(self.io.parse::<T>(
@@ -228,6 +230,7 @@ where
228230
// Note: don't deconstruct `msg` into local variables, it appears
229231
// the optimizer doesn't remove the extra copies.
230232

233+
#[cfg(feature = "tracing")]
231234
debug!("incoming body is {}", msg.decode);
232235

233236
// Prevent accepting HTTP/0.9 responses after the initial one, if any.
@@ -250,6 +253,7 @@ where
250253
};
251254

252255
if msg.decode == DecodedLength::ZERO {
256+
#[cfg(feature = "tracing")]
253257
if msg.expect_continue {
254258
debug!("ignoring expect-continue since body is empty");
255259
}
@@ -277,6 +281,7 @@ where
277281
let was_mid_parse = e.is_parse() || !self.io.read_buf().is_empty();
278282
if was_mid_parse || must_error {
279283
// We check if the buf contains the h2 Preface
284+
#[cfg(feature = "tracing")]
280285
debug!(
281286
"parse error ({}) with {} bytes",
282287
e,
@@ -287,6 +292,7 @@ where
287292
Err(e) => Poll::Ready(Some(Err(e))),
288293
}
289294
} else {
295+
#[cfg(feature = "tracing")]
290296
debug!("read eof");
291297
self.close_write();
292298
Poll::Ready(None)
@@ -304,6 +310,7 @@ where
304310
match ready!(decoder.decode(cx, &mut self.io)) {
305311
Ok(slice) => {
306312
let (reading, chunk) = if decoder.is_eof() {
313+
#[cfg(feature = "tracing")]
307314
debug!("incoming body completed");
308315
(
309316
Reading::KeepAlive,
@@ -314,6 +321,7 @@ where
314321
},
315322
)
316323
} else if slice.is_empty() {
324+
#[cfg(feature = "tracing")]
317325
error!("incoming body unexpectedly ended");
318326
// This should be unreachable, since all 3 decoders
319327
// either set eof=true or return an Err when reading
@@ -325,6 +333,7 @@ where
325333
(reading, Poll::Ready(chunk))
326334
}
327335
Err(e) => {
336+
#[cfg(feature = "tracing")]
328337
debug!("incoming body decode error: {}", e);
329338
(Reading::Closed, Poll::Ready(Some(Err(e))))
330339
}
@@ -333,6 +342,7 @@ where
333342
Reading::Continue(ref decoder) => {
334343
// Write the 100 Continue if not already responded...
335344
if let Writing::Init = self.state.writing {
345+
#[cfg(feature = "tracing")]
336346
trace!("automatically sending 100 Continue");
337347
let cont = b"HTTP/1.1 100 Continue\r\n\r\n";
338348
self.io.headers_buf().extend_from_slice(cont);
@@ -388,6 +398,7 @@ where
388398
debug_assert!(T::is_client());
389399

390400
if !self.io.read_buf().is_empty() {
401+
#[cfg(feature = "tracing")]
391402
debug!("received an unexpected {} bytes", self.io.read_buf().len());
392403
return Poll::Ready(Err(crate::Error::new_unexpected_message()));
393404
}
@@ -396,9 +407,11 @@ where
396407

397408
if num_read == 0 {
398409
let ret = if self.should_error_on_eof() {
410+
#[cfg(feature = "tracing")]
399411
trace!("found unexpected EOF on busy connection: {:?}", self.state);
400412
Poll::Ready(Err(crate::Error::new_incomplete()))
401413
} else {
414+
#[cfg(feature = "tracing")]
402415
trace!("found EOF on idle connection, closing");
403416
Poll::Ready(Ok(()))
404417
};
@@ -408,6 +421,7 @@ where
408421
return ret;
409422
}
410423

424+
#[cfg(feature = "tracing")]
411425
debug!(
412426
"received unexpected {} bytes on an idle connection",
413427
num_read
@@ -426,6 +440,7 @@ where
426440
let num_read = ready!(self.force_io_read(cx)).map_err(crate::Error::new_io)?;
427441

428442
if num_read == 0 {
443+
#[cfg(feature = "tracing")]
429444
trace!("found unexpected EOF on busy connection: {:?}", self.state);
430445
self.state.close_read();
431446
Poll::Ready(Err(crate::Error::new_incomplete()))
@@ -439,6 +454,7 @@ where
439454

440455
let result = ready!(self.io.poll_read_from_io(cx));
441456
Poll::Ready(result.map_err(|e| {
457+
#[cfg(feature = "tracing")]
442458
trace!("force_io_read; io error = {:?}", e);
443459
self.state.close();
444460
e
@@ -468,6 +484,7 @@ where
468484
match self.io.poll_read_from_io(cx) {
469485
Poll::Ready(Ok(n)) => {
470486
if n == 0 {
487+
#[cfg(feature = "tracing")]
471488
trace!("maybe_notify; read eof");
472489
if self.state.is_idle() {
473490
self.state.close();
@@ -478,10 +495,12 @@ where
478495
}
479496
}
480497
Poll::Pending => {
498+
#[cfg(feature = "tracing")]
481499
trace!("maybe_notify; read_from_io blocked");
482500
return;
483501
}
484502
Poll::Ready(Err(e)) => {
503+
#[cfg(feature = "tracing")]
485504
trace!("maybe_notify; read_from_io error: {}", e);
486505
self.state.close();
487506
self.state.error = Some(crate::Error::new_io(e));
@@ -720,17 +739,20 @@ where
720739
pub(crate) fn poll_flush(&mut self, cx: &mut task::Context<'_>) -> Poll<io::Result<()>> {
721740
ready!(Pin::new(&mut self.io).poll_flush(cx))?;
722741
self.try_keep_alive(cx);
742+
#[cfg(feature = "tracing")]
723743
trace!("flushed({}): {:?}", T::LOG, self.state);
724744
Poll::Ready(Ok(()))
725745
}
726746

727747
pub(crate) fn poll_shutdown(&mut self, cx: &mut task::Context<'_>) -> Poll<io::Result<()>> {
728748
match ready!(Pin::new(self.io.io_mut()).poll_shutdown(cx)) {
729749
Ok(()) => {
750+
#[cfg(feature = "tracing")]
730751
trace!("shut down IO complete");
731752
Poll::Ready(Ok(()))
732753
}
733754
Err(e) => {
755+
#[cfg(feature = "tracing")]
734756
debug!("error shutting down IO: {}", e);
735757
Poll::Ready(Err(e))
736758
}
@@ -749,7 +771,10 @@ where
749771

750772
// If still in Reading::Body, just give up
751773
match self.state.reading {
752-
Reading::Init | Reading::KeepAlive => trace!("body drained"),
774+
Reading::Init | Reading::KeepAlive => {
775+
#[cfg(feature = "tracing")]
776+
trace!("body drained")
777+
},
753778
_ => self.close_read(),
754779
}
755780
}
@@ -765,9 +790,11 @@ where
765790
#[cfg(feature = "server")]
766791
pub(crate) fn disable_keep_alive(&mut self) {
767792
if self.state.is_idle() {
793+
#[cfg(feature = "tracing")]
768794
trace!("disable_keep_alive; closing idle connection");
769795
self.state.close();
770796
} else {
797+
#[cfg(feature = "tracing")]
771798
trace!("disable_keep_alive; in-progress connection");
772799
self.state.disable_keep_alive();
773800
}
@@ -782,6 +809,7 @@ where
782809
}
783810

784811
pub(super) fn on_upgrade(&mut self) -> crate::upgrade::OnUpgrade {
812+
#[cfg(feature = "tracing")]
785813
trace!("{}: prepare possible HTTP upgrade", T::LOG);
786814
self.state.prepare_upgrade()
787815
}
@@ -898,6 +926,7 @@ impl fmt::Debug for Writing {
898926
impl std::ops::BitAndAssign<bool> for KA {
899927
fn bitand_assign(&mut self, enabled: bool) {
900928
if !enabled {
929+
#[cfg(feature = "tracing")]
901930
trace!("remote disabling keep-alive");
902931
*self = KA::Disabled;
903932
}
@@ -937,19 +966,22 @@ impl KA {
937966

938967
impl State {
939968
fn close(&mut self) {
969+
#[cfg(feature = "tracing")]
940970
trace!("State::close()");
941971
self.reading = Reading::Closed;
942972
self.writing = Writing::Closed;
943973
self.keep_alive.disable();
944974
}
945975

946976
fn close_read(&mut self) {
977+
#[cfg(feature = "tracing")]
947978
trace!("State::close_read()");
948979
self.reading = Reading::Closed;
949980
self.keep_alive.disable();
950981
}
951982

952983
fn close_write(&mut self) {
984+
#[cfg(feature = "tracing")]
953985
trace!("State::close_write()");
954986
self.writing = Writing::Closed;
955987
self.keep_alive.disable();
@@ -969,6 +1001,7 @@ impl State {
9691001
if let KA::Busy = self.keep_alive.status() {
9701002
self.idle::<T>();
9711003
} else {
1004+
#[cfg(feature = "tracing")]
9721005
trace!(
9731006
"try_keep_alive({}): could keep-alive, but status = {:?}",
9741007
T::LOG,

0 commit comments

Comments
 (0)