Skip to content

Commit da3fc76

Browse files
authored
feat(upgrade): introduce tracing as an optional unstable feature (#3326)
* 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. - Introduce several macros that act as wrappers around the `tracing` module's macros to allow for more concise syntax for conditional compilation of `tracing` macro calls. As `tracing` is unstable, the new `trace` module will facilitate transitioning `tracing` to an optional feature, as outlined in #2874 and #3326. - Refactor code to utilize tracing macros from the new `trace` module. - Add a `rustc` configuration flag (`hyper_unstable_tracing`) that must be passed to the compiler when using the `tracing` feature. Throw a compiler error if `tracing` is enabled without the flag. Part of the effort to transtition `tracing` to an unstable dependecy. See #3326 and #2874. - Modify `package.metadata.docs.rs` in `Cargo.toml` to include `tracing` to list of `features` and `--cfg hyper_unstable_tracing` to the list of `rustdoc-args`. - Add unstable section to `lib.rs` detailing unstable features. Add information about `tracing` and `ffi` to unstable features section. - Log errors as a field rather than part of a formatted string. Closes #3319 BREAKING CHANGES: tracing is disabled by default and requires users to opt in to an unstable feature to revert to previous behavior.
1 parent cd9c1a7 commit da3fc76

19 files changed

+178
-43
lines changed

.github/workflows/CI.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ jobs:
144144
uses: taiki-e/install-action@cargo-hack
145145

146146
- name: check --feature-powerset
147-
run: cargo hack check --feature-powerset --depth 2 --skip ffi -Z avoid-dev-deps
147+
run: cargo hack check --feature-powerset --depth 2 --skip ffi,tracing -Z avoid-dev-deps
148148

149149
ffi:
150150
name: Test C API (FFI)

Cargo.toml

+6-3
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ http-body-util = { version = "=0.1.0-rc.3", optional = true }
2929
httparse = "1.8"
3030
h2 = { version = "0.3.9", optional = true }
3131
itoa = "1"
32-
tracing = { version = "0.1", default-features = false, features = ["std"] }
3332
pin-project-lite = "0.2.4"
3433
tokio = { version = "1", features = ["sync"] }
3534

3635
# Optional
3736

3837
httpdate = { version = "1.0", optional = true }
3938
libc = { version = "0.2", optional = true }
39+
tracing = { version = "0.1", default-features = false, features = ["std"], optional = true }
4040
want = { version = "0.3", optional = true }
4141

4242
[dev-dependencies]
@@ -84,12 +84,15 @@ server = ["dep:httpdate"]
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

9093
[package.metadata.docs.rs]
91-
features = ["ffi", "full"]
92-
rustdoc-args = ["--cfg", "docsrs", "--cfg", "hyper_unstable_ffi"]
94+
features = ["ffi", "full", "tracing"]
95+
rustdoc-args = ["--cfg", "docsrs", "--cfg", "hyper_unstable_ffi", "--cfg", "hyper_unstable_tracing"]
9396

9497
[package.metadata.playground]
9598
features = ["full"]

src/body/length.rs

-2
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,6 @@ 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-
use tracing::warn;
54-
5553
if len <= MAX_LEN {
5654
Ok(DecodedLength(len))
5755
} else {

src/client/conn/http1.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,7 @@ where
191191
Err(_canceled) => panic!("dispatch dropped without returning error"),
192192
},
193193
Err(_req) => {
194-
tracing::debug!("connection was not ready");
195-
194+
debug!("connection was not ready");
196195
Err(crate::Error::new_canceled().with("connection was not ready"))
197196
}
198197
}
@@ -219,7 +218,7 @@ where
219218
}))
220219
}
221220
Err(req) => {
222-
tracing::debug!("connection was not ready");
221+
debug!("connection was not ready");
223222
let err = crate::Error::new_canceled().with("connection was not ready");
224223
Either::Right(future::err((err, Some(req))))
225224
}
@@ -478,7 +477,7 @@ impl Builder {
478477
let opts = self.clone();
479478

480479
async move {
481-
tracing::trace!("client handshake HTTP/1");
480+
trace!("client handshake HTTP/1");
482481

483482
let (tx, rx) = dispatch::channel();
484483
let mut conn = proto::Conn::new(io);

src/client/conn/http2.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ where
146146
Err(_canceled) => panic!("dispatch dropped without returning error"),
147147
},
148148
Err(_req) => {
149-
tracing::debug!("connection was not ready");
149+
debug!("connection was not ready");
150150

151151
Err(crate::Error::new_canceled().with("connection was not ready"))
152152
}
@@ -174,7 +174,7 @@ where
174174
}))
175175
}
176176
Err(req) => {
177-
tracing::debug!("connection was not ready");
177+
debug!("connection was not ready");
178178
let err = crate::Error::new_canceled().with("connection was not ready");
179179
Either::Right(future::err((err, Some(req))))
180180
}
@@ -407,7 +407,7 @@ where
407407
let opts = self.clone();
408408

409409
async move {
410-
tracing::trace!("client handshake HTTP/1");
410+
trace!("client handshake HTTP/1");
411411

412412
let (tx, rx) = dispatch::channel();
413413
let h2 = proto::h2::client::handshake(io, rx, &opts.h2_builder, opts.exec, opts.timer)

src/client/dispatch.rs

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use http::{Request, Response};
55
use http_body::Body;
66
use pin_project_lite::pin_project;
77
use tokio::sync::{mpsc, oneshot};
8-
use tracing::trace;
98

109
use crate::{
1110
body::Incoming,

src/lib.rs

+18-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,20 @@
5151
//! - `server`: Enables the HTTP `server`.
5252
//!
5353
//! [feature flags]: https://doc.rust-lang.org/cargo/reference/manifest.html#the-features-section
54-
54+
//!
55+
//! # Unstable Features
56+
//! hyper includes a set of unstable optional features that can be enabled through the use of a
57+
//! feature flag and a [configuration flag].
58+
//!
59+
//! The following is a list of feature flags and their corresponding `RUSTFLAG`:
60+
//! - `ffi`: Enables C API for hyper `hyper_unstable_ffi`.
61+
//! - `tracing`: Enables debug logging with `hyper_unstable_tracing`.
62+
//!
63+
//! Enabling an unstable feature is possible with the following `cargo` command, as of version `1.64.0`:
64+
//! ```notrust
65+
//! RUSTFLAGS="--cfg hyper_unstable_tracing" cargo rustc --features client,http1,http2,tracing --crate-type cdylib
66+
//!```
67+
//! [configuration flag]: https://doc.rust-lang.org/reference/conditional-compilation.html
5568
#[doc(hidden)]
5669
pub use http;
5770

@@ -67,6 +80,10 @@ pub use crate::error::{Error, Result};
6780

6881
#[macro_use]
6982
mod cfg;
83+
84+
#[macro_use]
85+
mod trace;
86+
7087
#[macro_use]
7188
mod common;
7289
pub mod body;

src/proto/h1/conn.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use bytes::{Buf, Bytes};
99
use http::header::{HeaderValue, CONNECTION};
1010
use http::{HeaderMap, Method, Version};
1111
use httparse::ParserConfig;
12-
use tracing::{debug, error, trace};
1312

1413
use super::io::Buffered;
1514
use super::{Decoder, Encode, EncodedBuf, Encoder, Http1Transaction, ParseContext, Wants};
@@ -439,7 +438,7 @@ where
439438

440439
let result = ready!(self.io.poll_read_from_io(cx));
441440
Poll::Ready(result.map_err(|e| {
442-
trace!("force_io_read; io error = {:?}", e);
441+
trace!(error = %e, "force_io_read; io error");
443442
self.state.close();
444443
e
445444
}))
@@ -749,7 +748,9 @@ where
749748

750749
// If still in Reading::Body, just give up
751750
match self.state.reading {
752-
Reading::Init | Reading::KeepAlive => trace!("body drained"),
751+
Reading::Init | Reading::KeepAlive => {
752+
trace!("body drained")
753+
}
753754
_ => self.close_read(),
754755
}
755756
}

src/proto/h1/decode.rs

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use std::io;
44
use std::usize;
55

66
use bytes::Bytes;
7-
use tracing::{debug, trace};
87

98
use crate::common::{task, Poll};
109

src/proto/h1/dispatch.rs

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use std::error::Error as StdError;
33
use crate::rt::{Read, Write};
44
use bytes::{Buf, Bytes};
55
use http::Request;
6-
use tracing::{debug, trace};
76

87
use super::{Http1Transaction, Wants};
98
use crate::body::{Body, DecodedLength, Incoming as IncomingBody};

src/proto/h1/encode.rs

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use std::io::IoSlice;
33

44
use bytes::buf::{Chain, Take};
55
use bytes::Buf;
6-
use tracing::trace;
76

87
use super::io::WriteBuf;
98

src/proto/h1/io.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use std::mem::MaybeUninit;
88

99
use crate::rt::{Read, ReadBuf, Write};
1010
use bytes::{Buf, BufMut, Bytes, BytesMut};
11-
use tracing::{debug, trace};
1211

1312
use super::{Http1Transaction, ParseContext, ParsedMessage};
1413
use crate::common::buf::BufList;
@@ -224,7 +223,7 @@ where
224223
if Pin::new(h1_header_read_timeout_fut).poll(cx).is_ready() {
225224
*parse_ctx.h1_header_read_timeout_running = false;
226225

227-
tracing::warn!("read header from client timeout");
226+
warn!("read header from client timeout");
228227
return Poll::Ready(Err(crate::Error::new_header_timeout()));
229228
}
230229
}

src/proto/h1/role.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use bytes::BytesMut;
99
use http::header::ValueIter;
1010
use http::header::{self, Entry, HeaderName, HeaderValue};
1111
use http::{HeaderMap, Method, StatusCode, Version};
12-
use tracing::{debug, error, trace, trace_span, warn};
1312

1413
use crate::body::DecodedLength;
1514
#[cfg(feature = "server")]
@@ -72,8 +71,7 @@ where
7271
return Ok(None);
7372
}
7473

75-
let span = trace_span!("parse_headers");
76-
let _s = span.enter();
74+
let _entered = trace_span!("parse_headers");
7775

7876
#[cfg(feature = "server")]
7977
if !*ctx.h1_header_read_timeout_running {
@@ -103,8 +101,7 @@ pub(super) fn encode_headers<T>(
103101
where
104102
T: Http1Transaction,
105103
{
106-
let span = trace_span!("encode_headers");
107-
let _s = span.enter();
104+
let _entered = trace_span!("encode_headers");
108105
T::encode(enc, dst)
109106
}
110107

src/proto/h2/client.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ use h2::client::{Builder, Connection, SendRequest};
1212
use h2::SendStream;
1313
use http::{Method, StatusCode};
1414
use pin_project_lite::pin_project;
15-
use tracing::{debug, trace, warn};
1615

1716
use super::ping::{Ponger, Recorder};
1817
use super::{ping, H2Upgraded, PipeToSendStream, SendBuf};
@@ -243,7 +242,9 @@ where
243242
if polled.is_ready() {
244243
*this.is_terminated = true;
245244
}
246-
polled.map_err(|e| debug!("connection error: {}", e))
245+
polled.map_err(|_e| {
246+
debug!(error = %_e, "connection error");
247+
})
247248
}
248249
}
249250

@@ -441,8 +442,8 @@ where
441442

442443
match this.pipe.poll_unpin(cx) {
443444
Poll::Ready(result) => {
444-
if let Err(e) = result {
445-
debug!("client request body error: {}", e);
445+
if let Err(_e) = result {
446+
debug!("client request body error: {}", _e);
446447
}
447448
drop(this.conn_drop_ref.take().expect("Future polled twice"));
448449
drop(this.ping.take().expect("Future polled twice"));

src/proto/h2/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use std::error::Error as StdError;
88
use std::io::{Cursor, IoSlice};
99
use std::mem;
1010
use std::task::Context;
11-
use tracing::{debug, trace, warn};
1211

1312
use crate::body::Body;
1413
use crate::common::{task, Future, Pin, Poll};

src/proto/h2/ping.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ use std::task::{self, Poll};
2626
use std::time::{Duration, Instant};
2727

2828
use h2::{Ping, PingPong};
29-
use tracing::{debug, trace};
3029

3130
use crate::common::time::Time;
3231
use crate::rt::Sleep;
@@ -300,8 +299,8 @@ impl Ponger {
300299
}
301300
}
302301
}
303-
Poll::Ready(Err(e)) => {
304-
debug!("pong error: {}", e);
302+
Poll::Ready(Err(_e)) => {
303+
debug!("pong error: {}", _e);
305304
}
306305
Poll::Pending => {
307306
if let Some(ref mut ka) = self.keep_alive {
@@ -332,8 +331,8 @@ impl Shared {
332331
self.ping_sent_at = Some(Instant::now());
333332
trace!("sent ping");
334333
}
335-
Err(err) => {
336-
debug!("error sending ping: {}", err);
334+
Err(_err) => {
335+
debug!("error sending ping: {}", _err);
337336
}
338337
}
339338
}

src/proto/h2/server.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use h2::server::{Connection, Handshake, SendResponse};
99
use h2::{Reason, RecvStream};
1010
use http::{Method, Request};
1111
use pin_project_lite::pin_project;
12-
use tracing::{debug, trace, warn};
1312

1413
use super::{ping, PipeToSendStream, SendBuf};
1514
use crate::body::{Body, Incoming as IncomingBody};
@@ -508,8 +507,8 @@ where
508507

509508
fn poll(self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> Poll<Self::Output> {
510509
self.poll2(cx).map(|res| {
511-
if let Err(e) = res {
512-
debug!("stream error: {}", e);
510+
if let Err(_e) = res {
511+
debug!("stream error: {}", _e);
513512
}
514513
})
515514
}

0 commit comments

Comments
 (0)