Skip to content

Commit 4e1e2d1

Browse files
committed
chore: make tracing optional
1 parent b9d5397 commit 4e1e2d1

29 files changed

+233
-132
lines changed

.github/workflows/CI.yml

Lines changed: 3 additions & 0 deletions
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

Lines changed: 6 additions & 1 deletion
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

Lines changed: 12 additions & 5 deletions
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 =====
@@ -1652,6 +1658,7 @@ impl Peer {
16521658
impl proto::Peer for Peer {
16531659
type Poll = Response<()>;
16541660

1661+
#[cfg(feature = "tracing")]
16551662
const NAME: &'static str = "Client";
16561663

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

src/codec/framed_read.rs

Lines changed: 23 additions & 24 deletions
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

@@ -158,8 +158,8 @@ fn decode_frame(
158158
// `PROTOCOL_ERROR`.
159159
return Err(Error::library_reset($head.stream_id(), Reason::PROTOCOL_ERROR));
160160
},
161-
Err(e) => {
162-
proto_err!(conn: "failed to load frame; err={:?}", e);
161+
Err(_e) => {
162+
proto_err!(conn: "failed to load frame; err={:?}", _e);
163163
return Err(Error::library_go_away(Reason::PROTOCOL_ERROR));
164164
}
165165
};
@@ -175,8 +175,8 @@ fn decode_frame(
175175
proto_err!(stream: "malformed header block; stream={:?}", id);
176176
return Err(Error::library_reset(id, Reason::PROTOCOL_ERROR));
177177
},
178-
Err(e) => {
179-
proto_err!(conn: "failed HPACK decoding; err={:?}", e);
178+
Err(_e) => {
179+
proto_err!(conn: "failed HPACK decoding; err={:?}", _e);
180180
return Err(Error::library_go_away(Reason::PROTOCOL_ERROR));
181181
}
182182
}
@@ -201,26 +201,26 @@ fn decode_frame(
201201
Kind::Settings => {
202202
let res = frame::Settings::load(head, &bytes[frame::HEADER_LEN..]);
203203

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

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

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

232232
// TODO: Should this always be connection level? Probably not...
233-
res.map_err(|e| {
234-
proto_err!(conn: "failed to load DATA frame; err={:?}", e);
233+
res.map_err(|_e| {
234+
proto_err!(conn: "failed to load DATA frame; err={:?}", _e);
235235
Error::library_go_away(Reason::PROTOCOL_ERROR)
236236
})?
237237
.into()
238238
}
239239
Kind::Headers => header_block!(Headers, head, bytes),
240240
Kind::Reset => {
241241
let res = frame::Reset::load(head, &bytes[frame::HEADER_LEN..]);
242-
res.map_err(|e| {
243-
proto_err!(conn: "failed to load RESET frame; err={:?}", e);
242+
res.map_err(|_e| {
243+
proto_err!(conn: "failed to load RESET frame; err={:?}", _e);
244244
Error::library_go_away(Reason::PROTOCOL_ERROR)
245245
})?
246246
.into()
247247
}
248248
Kind::GoAway => {
249249
let res = frame::GoAway::load(&bytes[frame::HEADER_LEN..]);
250-
res.map_err(|e| {
251-
proto_err!(conn: "failed to load GO_AWAY frame; err={:?}", e);
250+
res.map_err(|_e| {
251+
proto_err!(conn: "failed to load GO_AWAY frame; err={:?}", _e);
252252
Error::library_go_away(Reason::PROTOCOL_ERROR)
253253
})?
254254
.into()
@@ -271,8 +271,8 @@ fn decode_frame(
271271
proto_err!(stream: "PRIORITY invalid dependency ID; stream={:?}", id);
272272
return Err(Error::library_reset(id, Reason::PROTOCOL_ERROR));
273273
}
274-
Err(e) => {
275-
proto_err!(conn: "failed to load PRIORITY frame; err={:?};", e);
274+
Err(_e) => {
275+
proto_err!(conn: "failed to load PRIORITY frame; err={:?};", _e);
276276
return Err(Error::library_go_away(Reason::PROTOCOL_ERROR));
277277
}
278278
}
@@ -347,8 +347,8 @@ fn decode_frame(
347347
proto_err!(stream: "malformed CONTINUATION frame; stream={:?}", id);
348348
return Err(Error::library_reset(id, Reason::PROTOCOL_ERROR));
349349
}
350-
Err(e) => {
351-
proto_err!(conn: "failed HPACK decoding; err={:?}", e);
350+
Err(_e) => {
351+
proto_err!(conn: "failed HPACK decoding; err={:?}", _e);
352352
return Err(Error::library_go_away(Reason::PROTOCOL_ERROR));
353353
}
354354
}
@@ -376,8 +376,7 @@ where
376376
type Item = Result<Frame, Error>;
377377

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

src/codec/framed_write.rs

Lines changed: 3 additions & 5 deletions
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;
@@ -130,8 +130,7 @@ where
130130

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

136135
loop {
137136
while !self.encoder.is_empty() {
@@ -212,8 +211,7 @@ where
212211
fn buffer(&mut self, item: Frame<B>) -> Result<(), UserError> {
213212
// Ensure that we have enough capacity to accept the write.
214213
assert!(self.has_capacity());
215-
let span = tracing::trace_span!("FramedWrite::buffer", frame = ?item);
216-
let _e = span.enter();
214+
let _span = tracing::trace_span!("FramedWrite::buffer", frame = ?item);
217215

218216
tracing::debug!(frame = ?item, "send");
219217

src/frame/go_away.rs

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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)]

0 commit comments

Comments
 (0)