Skip to content

Commit d76ae18

Browse files
committed
chore: make tracing optional
1 parent 90359ba commit d76ae18

29 files changed

+233
-132
lines changed

.github/workflows/CI.yml

+3
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ jobs:
5151
- name: Check with unstable flag
5252
run: cargo check --features unstable
5353

54+
- name: Check with tracing feature
55+
run: cargo check --features tracing
56+
5457
- name: Run lib tests and doc tests
5558
run: cargo test
5659

Cargo.toml

+6-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ rust-version = "1.63"
2323
# Enables `futures::Stream` implementations for various types.
2424
stream = []
2525

26+
# Enables tracing.
27+
tracing = ["dep:tracing"]
28+
2629
# Enables **unstable** APIs. Any API exposed by this feature has no backwards
2730
# compatibility guarantees. In other words, you should not use this feature for
2831
# anything besides experimentation. Definitely **do not** publish a crate that
@@ -46,12 +49,14 @@ tokio-util = { version = "0.7.1", features = ["codec", "io"] }
4649
tokio = { version = "1", features = ["io-util"] }
4750
bytes = "1"
4851
http = "1"
49-
tracing = { version = "0.1.35", default-features = false, features = ["std"] }
52+
tracing = { version = "0.1.35", default-features = false, features = ["std"], optional = true }
5053
fnv = "1.0.5"
5154
slab = "0.4.2"
5255
indexmap = { version = "2", features = ["std"] }
5356

5457
[dev-dependencies]
58+
# Test
59+
tracing = { version = "0.1.35", default-features = false, features = ["std"] }
5560

5661
# Fuzzing
5762
quickcheck = { version = "1.0.3", default-features = false }

src/client.rs

+12-5
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,10 @@ use crate::codec::{Codec, SendError, UserError};
139139
use crate::ext::Protocol;
140140
use crate::frame::{Headers, Pseudo, Reason, Settings, StreamId};
141141
use crate::proto::{self, Error};
142-
use crate::{FlowControl, PingPong, RecvStream, SendStream};
142+
use crate::{tracing, FlowControl, PingPong, RecvStream, SendStream};
143143

144+
#[cfg(feature = "tracing")]
145+
use ::tracing::Instrument;
144146
use bytes::{Buf, Bytes};
145147
use http::{uri, HeaderMap, Method, Request, Response, Version};
146148
use std::fmt;
@@ -149,7 +151,6 @@ use std::pin::Pin;
149151
use std::task::{Context, Poll};
150152
use std::time::Duration;
151153
use tokio::io::{AsyncRead, AsyncWrite, AsyncWriteExt};
152-
use tracing::Instrument;
153154

154155
/// Initializes new HTTP/2 streams on a connection by sending a request.
155156
///
@@ -1275,10 +1276,15 @@ where
12751276
T: AsyncRead + AsyncWrite + Unpin,
12761277
{
12771278
let builder = Builder::new();
1278-
builder
1279+
1280+
#[cfg(feature = "tracing")]
1281+
return builder
12791282
.handshake(io)
1280-
.instrument(tracing::trace_span!("client_handshake"))
1281-
.await
1283+
.instrument(::tracing::trace_span!("client_handshake"))
1284+
.await;
1285+
1286+
#[cfg(not(feature = "tracing"))]
1287+
return builder.handshake(io).await;
12821288
}
12831289

12841290
// ===== impl Connection =====
@@ -1646,6 +1652,7 @@ impl Peer {
16461652
impl proto::Peer for Peer {
16471653
type Poll = Response<()>;
16481654

1655+
#[cfg(feature = "tracing")]
16491656
const NAME: &'static str = "Client";
16501657

16511658
fn r#dyn() -> proto::DynPeer {

src/codec/framed_read.rs

+23-24
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::frame::{
55
use crate::proto::Error;
66

77
use crate::hpack;
8+
use crate::tracing;
89

910
use futures_core::Stream;
1011

@@ -126,8 +127,7 @@ fn decode_frame(
126127
partial_inout: &mut Option<Partial>,
127128
mut bytes: BytesMut,
128129
) -> Result<Option<Frame>, Error> {
129-
let span = tracing::trace_span!("FramedRead::decode_frame", offset = bytes.len());
130-
let _e = span.enter();
130+
let _span = tracing::trace_span!("FramedRead::decode_frame", offset = bytes.len());
131131

132132
tracing::trace!("decoding frame from {}B", bytes.len());
133133

@@ -159,8 +159,8 @@ fn decode_frame(
159159
// `PROTOCOL_ERROR`.
160160
return Err(Error::library_reset($head.stream_id(), Reason::PROTOCOL_ERROR));
161161
},
162-
Err(e) => {
163-
proto_err!(conn: "failed to load frame; err={:?}", e);
162+
Err(_e) => {
163+
proto_err!(conn: "failed to load frame; err={:?}", _e);
164164
return Err(Error::library_go_away(Reason::PROTOCOL_ERROR));
165165
}
166166
};
@@ -176,8 +176,8 @@ fn decode_frame(
176176
proto_err!(stream: "malformed header block; stream={:?}", id);
177177
return Err(Error::library_reset(id, Reason::PROTOCOL_ERROR));
178178
},
179-
Err(e) => {
180-
proto_err!(conn: "failed HPACK decoding; err={:?}", e);
179+
Err(_e) => {
180+
proto_err!(conn: "failed HPACK decoding; err={:?}", _e);
181181
return Err(Error::library_go_away(Reason::PROTOCOL_ERROR));
182182
}
183183
}
@@ -202,26 +202,26 @@ fn decode_frame(
202202
Kind::Settings => {
203203
let res = frame::Settings::load(head, &bytes[frame::HEADER_LEN..]);
204204

205-
res.map_err(|e| {
206-
proto_err!(conn: "failed to load SETTINGS frame; err={:?}", e);
205+
res.map_err(|_e| {
206+
proto_err!(conn: "failed to load SETTINGS frame; err={:?}", _e);
207207
Error::library_go_away(Reason::PROTOCOL_ERROR)
208208
})?
209209
.into()
210210
}
211211
Kind::Ping => {
212212
let res = frame::Ping::load(head, &bytes[frame::HEADER_LEN..]);
213213

214-
res.map_err(|e| {
215-
proto_err!(conn: "failed to load PING frame; err={:?}", e);
214+
res.map_err(|_e| {
215+
proto_err!(conn: "failed to load PING frame; err={:?}", _e);
216216
Error::library_go_away(Reason::PROTOCOL_ERROR)
217217
})?
218218
.into()
219219
}
220220
Kind::WindowUpdate => {
221221
let res = frame::WindowUpdate::load(head, &bytes[frame::HEADER_LEN..]);
222222

223-
res.map_err(|e| {
224-
proto_err!(conn: "failed to load WINDOW_UPDATE frame; err={:?}", e);
223+
res.map_err(|_e| {
224+
proto_err!(conn: "failed to load WINDOW_UPDATE frame; err={:?}", _e);
225225
Error::library_go_away(Reason::PROTOCOL_ERROR)
226226
})?
227227
.into()
@@ -231,25 +231,25 @@ fn decode_frame(
231231
let res = frame::Data::load(head, bytes.freeze());
232232

233233
// TODO: Should this always be connection level? Probably not...
234-
res.map_err(|e| {
235-
proto_err!(conn: "failed to load DATA frame; err={:?}", e);
234+
res.map_err(|_e| {
235+
proto_err!(conn: "failed to load DATA frame; err={:?}", _e);
236236
Error::library_go_away(Reason::PROTOCOL_ERROR)
237237
})?
238238
.into()
239239
}
240240
Kind::Headers => header_block!(Headers, head, bytes),
241241
Kind::Reset => {
242242
let res = frame::Reset::load(head, &bytes[frame::HEADER_LEN..]);
243-
res.map_err(|e| {
244-
proto_err!(conn: "failed to load RESET frame; err={:?}", e);
243+
res.map_err(|_e| {
244+
proto_err!(conn: "failed to load RESET frame; err={:?}", _e);
245245
Error::library_go_away(Reason::PROTOCOL_ERROR)
246246
})?
247247
.into()
248248
}
249249
Kind::GoAway => {
250250
let res = frame::GoAway::load(&bytes[frame::HEADER_LEN..]);
251-
res.map_err(|e| {
252-
proto_err!(conn: "failed to load GO_AWAY frame; err={:?}", e);
251+
res.map_err(|_e| {
252+
proto_err!(conn: "failed to load GO_AWAY frame; err={:?}", _e);
253253
Error::library_go_away(Reason::PROTOCOL_ERROR)
254254
})?
255255
.into()
@@ -272,8 +272,8 @@ fn decode_frame(
272272
proto_err!(stream: "PRIORITY invalid dependency ID; stream={:?}", id);
273273
return Err(Error::library_reset(id, Reason::PROTOCOL_ERROR));
274274
}
275-
Err(e) => {
276-
proto_err!(conn: "failed to load PRIORITY frame; err={:?};", e);
275+
Err(_e) => {
276+
proto_err!(conn: "failed to load PRIORITY frame; err={:?};", _e);
277277
return Err(Error::library_go_away(Reason::PROTOCOL_ERROR));
278278
}
279279
}
@@ -348,8 +348,8 @@ fn decode_frame(
348348
proto_err!(stream: "malformed CONTINUATION frame; stream={:?}", id);
349349
return Err(Error::library_reset(id, Reason::PROTOCOL_ERROR));
350350
}
351-
Err(e) => {
352-
proto_err!(conn: "failed HPACK decoding; err={:?}", e);
351+
Err(_e) => {
352+
proto_err!(conn: "failed HPACK decoding; err={:?}", _e);
353353
return Err(Error::library_go_away(Reason::PROTOCOL_ERROR));
354354
}
355355
}
@@ -377,8 +377,7 @@ where
377377
type Item = Result<Frame, Error>;
378378

379379
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
380-
let span = tracing::trace_span!("FramedRead::poll_next");
381-
let _e = span.enter();
380+
let _span = tracing::trace_span!("FramedRead::poll_next");
382381
loop {
383382
tracing::trace!("poll");
384383
let bytes = match ready!(Pin::new(&mut self.inner).poll_next(cx)) {

src/codec/framed_write.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::codec::UserError;
22
use crate::codec::UserError::*;
33
use crate::frame::{self, Frame, FrameSize};
4-
use crate::hpack;
4+
use crate::{hpack, tracing};
55

66
use bytes::{Buf, BufMut, BytesMut};
77
use std::pin::Pin;
@@ -128,8 +128,7 @@ where
128128

129129
/// Flush buffered data to the wire
130130
pub fn flush(&mut self, cx: &mut Context) -> Poll<io::Result<()>> {
131-
let span = tracing::trace_span!("FramedWrite::flush");
132-
let _e = span.enter();
131+
let _span = tracing::trace_span!("FramedWrite::flush");
133132

134133
loop {
135134
while !self.encoder.is_empty() {
@@ -207,8 +206,7 @@ where
207206
fn buffer(&mut self, item: Frame<B>) -> Result<(), UserError> {
208207
// Ensure that we have enough capacity to accept the write.
209208
assert!(self.has_capacity());
210-
let span = tracing::trace_span!("FramedWrite::buffer", frame = ?item);
211-
let _e = span.enter();
209+
let _span = tracing::trace_span!("FramedWrite::buffer", frame = ?item);
212210

213211
tracing::debug!(frame = ?item, "send");
214212

src/frame/go_away.rs

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::fmt;
33
use bytes::{BufMut, Bytes};
44

55
use crate::frame::{self, Error, Head, Kind, Reason, StreamId};
6+
use crate::tracing;
67

78
#[derive(Clone, Eq, PartialEq)]
89
pub struct GoAway {

src/frame/headers.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use super::{util, StreamDependency, StreamId};
22
use crate::ext::Protocol;
33
use crate::frame::{Error, Frame, Head, Kind};
44
use crate::hpack::{self, BytesStr};
5+
use crate::tracing;
56

67
use http::header::{self, HeaderName, HeaderValue};
78
use http::{uri, HeaderMap, Method, Request, StatusCode, Uri};

src/frame/ping.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::frame::{Error, Frame, Head, Kind, StreamId};
2+
use crate::tracing;
23
use bytes::BufMut;
34

45
const ACK_FLAG: u8 = 0x1;

src/frame/reset.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::frame::{self, Error, Head, Kind, Reason, StreamId};
2+
use crate::tracing;
23

34
use bytes::BufMut;
45

src/frame/settings.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::fmt;
22

33
use crate::frame::{util, Error, Frame, FrameSize, Head, Kind, StreamId};
4+
use crate::tracing;
45
use bytes::{BufMut, BytesMut};
56

67
#[derive(Clone, Default, Eq, PartialEq)]

src/frame/window_update.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::frame::{self, Error, Head, Kind, StreamId};
2+
use crate::tracing;
23

34
use bytes::BufMut;
45

src/hpack/decoder.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use super::{header::BytesStr, huffman, Header};
2-
use crate::frame;
2+
use crate::{frame, tracing};
33

44
use bytes::{Buf, Bytes, BytesMut};
55
use http::header;
@@ -189,8 +189,7 @@ impl Decoder {
189189
self.last_max_update = size;
190190
}
191191

192-
let span = tracing::trace_span!("hpack::decode");
193-
let _e = span.enter();
192+
let _span = tracing::trace_span!("hpack::decode");
194193

195194
tracing::trace!("decode");
196195

@@ -494,6 +493,7 @@ impl Table {
494493
}
495494
}
496495

496+
#[cfg(any(feature = "tracing", test))]
497497
fn size(&self) -> usize {
498498
self.size
499499
}

src/hpack/encoder.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use super::table::{Index, Table};
22
use super::{huffman, Header};
3+
use crate::tracing;
34

45
use bytes::{BufMut, BytesMut};
56
use http::header::{HeaderName, HeaderValue};
@@ -62,8 +63,7 @@ impl Encoder {
6263
where
6364
I: IntoIterator<Item = Header<Option<HeaderName>>>,
6465
{
65-
let span = tracing::trace_span!("hpack::encode");
66-
let _e = span.enter();
66+
let _span = tracing::trace_span!("hpack::encode");
6767

6868
self.encode_size_updates(dst);
6969

src/lib.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,14 @@
8787
#![allow(clippy::type_complexity, clippy::manual_range_contains)]
8888
#![cfg_attr(test, deny(warnings))]
8989

90+
mod tracing;
91+
9092
macro_rules! proto_err {
9193
(conn: $($msg:tt)+) => {
92-
tracing::debug!("connection error PROTOCOL_ERROR -- {};", format_args!($($msg)+))
94+
crate::tracing::debug!("connection error PROTOCOL_ERROR -- {};", format_args!($($msg)+))
9395
};
9496
(stream: $($msg:tt)+) => {
95-
tracing::debug!("stream error PROTOCOL_ERROR -- {};", format_args!($($msg)+))
97+
crate::tracing::debug!("stream error PROTOCOL_ERROR -- {};", format_args!($($msg)+))
9698
};
9799
}
98100

0 commit comments

Comments
 (0)