Skip to content

Commit

Permalink
fix(trace): change span macros to ensure that Span is not dropped e…
Browse files Browse the repository at this point in the history
…arly

Span macros in the `trace` module drop the `Span` early, making them
useless. The fix changes their implementation such that they return the
RAII guard, so the span is consumed and the guard is not dropped.
  • Loading branch information
RamziA961 committed Sep 30, 2023
1 parent 033d715 commit 8ebbd49
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 115 deletions.
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ pub use crate::error::{Error, Result};
mod cfg;

#[macro_use]
#[cfg_attr(docsrs, doc(all(feature = "tracing"), hyper_unstable_tracing))]
mod trace;

#[macro_use]
Expand Down
4 changes: 2 additions & 2 deletions src/proto/h1/role.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ where
return Ok(None);
}

trace_span!("parse_headers");
let _entered = trace_span!("parse_headers");

#[cfg(feature = "server")]
if !*ctx.h1_header_read_timeout_running {
Expand Down Expand Up @@ -101,7 +101,7 @@ pub(super) fn encode_headers<T>(
where
T: Http1Transaction,
{
trace_span!("encode_headers");
let _entered = trace_span!("encode_headers");
T::encode(enc, dst)
}

Expand Down
157 changes: 45 additions & 112 deletions src/trace.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// For completeness, wrappers around all of tracing's public macros are provided, even if they are
// not used at the present time.
// For completeness, wrappers around all of tracing's public logging and span macros are provided,
// even if they are not used at the present time.
#![allow(unused_macros)]

//! Internal Tracing macro module
Expand Down Expand Up @@ -29,184 +29,117 @@
//! RUSTFLAGS="--cfg hyper_unstable_tracing" cargo rustc --features client,http1,http2,tracing --crate-type cdylib
//! ```
#[cfg(not(hyper_unstable_tracing))]
compile_error!(
"\
The `tracing` feature is unstable, and requires the \
`RUSTFLAGS='--cfg hyper_unstable_tracing'` environment variable to be set.\
"
);

macro_rules! debug {
($($arg:tt)+) => {
#[cfg(feature = "tracing")]
{
#[cfg(not(hyper_unstable_tracing))]
compile_error!(
"\
The `tracing` feature is unstable, and requires the \
`RUSTFLAGS='--cfg hyper_unstable_tracing'` environment variable to be set.\
"
);
tracing::debug!($($arg)+);
}
tracing::debug!($($arg)+);
}
}

macro_rules! debug_span {
($($arg:tt)*) => {
#[cfg(feature = "tracing")]
{
#[cfg(not(hyper_unstable_tracing))]
compile_error!(
"\
The `tracing` feature is unstable, and requires the \
`RUSTFLAGS='--cfg hyper_unstable_tracing'` environment variable to be set.\
"
);
let span = tracing::debug_span!($($arg)+);
let _ = span.enter();
#[cfg(feature = "tracing")]
{
let _span = tracing::debug_span!($($arg)+);
_span.entered();
}
}
}
}

macro_rules! error {
($($arg:tt)*) => {
#[cfg(feature = "tracing")]
{
#[cfg(not(hyper_unstable_tracing))]
compile_error!(
"\
The `tracing` feature is unstable, and requires the \
`RUSTFLAGS='--cfg hyper_unstable_tracing'` environment variable to be set.\
"
);
tracing::error!($($arg)+);
}
tracing::error!($($arg)+);
}
}

macro_rules! error_span {
($($arg:tt)*) => {
#[cfg(feature = "tracing")]
{
#[cfg(not(hyper_unstable_tracing))]
compile_error!(
"\
The `tracing` feature is unstable, and requires the \
`RUSTFLAGS='--cfg hyper_unstable_tracing'` environment variable to be set.\
"
);
let span = tracing::error_span!($($arg)+);
let _ = span.enter();
#[cfg(feature = "tracing")]
{
let _span = tracing::error_span!($($arg)+);
_span.entered();
}
}
}
}

macro_rules! info {
($($arg:tt)*) => {
#[cfg(feature = "tracing")]
{
#[cfg(not(hyper_unstable_tracing))]
compile_error!(
"\
The `tracing` feature is unstable, and requires the \
`RUSTFLAGS='--cfg hyper_unstable_tracing'` environment variable to be set.\
"
);
tracing::info!($($arg)+);
}
tracing::info!($($arg)+);
}
}

macro_rules! info_span {
($($arg:tt)*) => {
#[cfg(feature = "tracing")]
{
#[cfg(not(hyper_unstable_tracing))]
compile_error!(
"\
The `tracing` feature is unstable, and requires the \
`RUSTFLAGS='--cfg hyper_unstable_tracing'` environment variable to be set.\
"
);
let span = tracing::info_span!($($arg)+);
let _ = span.enter();
#[cfg(feature = "tracing")]
{
let _span = tracing::info_span!($($arg)+);
_span.entered();
}
}
}
}

macro_rules! trace {
($($arg:tt)*) => {
#[cfg(feature = "tracing")]
{
#[cfg(not(hyper_unstable_tracing))]
compile_error!(
"\
The `tracing` feature is unstable, and requires the \
`RUSTFLAGS='--cfg hyper_unstable_tracing'` environment variable to be set.\
"
);
tracing::trace!($($arg)+);
}
tracing::trace!($($arg)+);
}
}

macro_rules! trace_span {
($($arg:tt)*) => {
#[cfg(feature = "tracing")]
{
#[cfg(not(hyper_unstable_tracing))]
compile_error!(
"\
The `tracing` feature is unstable, and requires the \
`RUSTFLAGS='--cfg hyper_unstable_tracing'` environment variable to be set.\
"
);
let span = tracing::trace_span!($($arg)+);
let _ = span.enter();
#[cfg(feature = "tracing")]
{
let _span = tracing::trace_span!($($arg)+);
_span.entered();
}
}
}
}

macro_rules! span {
($($arg:tt)*) => {
#[cfg(feature = "tracing")]
{
#[cfg(not(hyper_unstable_tracing))]
compile_error!(
"\
The `tracing` feature is unstable, and requires the \
`RUSTFLAGS='--cfg hyper_unstable_tracing'` environment variable to be set.\
"
);
let span = tracing::span!($($arg)+);
let _ = span.enter();
#[cfg(feature = "tracing")]
{
let _span = tracing::span!($($arg)+);
_span.entered();
}
}
}
}

macro_rules! warn {
($($arg:tt)*) => {
#[cfg(feature = "tracing")]
{
#[cfg(not(hyper_unstable_tracing))]
compile_error!(
"\
The `tracing` feature is unstable, and requires the \
`RUSTFLAGS='--cfg hyper_unstable_tracing'` environment variable to be set.\
"
);
tracing::warn!($($arg)+);
}
tracing::warn!($($arg)+);
}
}

macro_rules! warn_span {
($($arg:tt)*) => {
#[cfg(feature = "tracing")]
{
#[cfg(not(hyper_unstable_tracing))]
compile_error!(
"\
The `tracing` feature is unstable, and requires the \
`RUSTFLAGS='--cfg hyper_unstable_tracing'` environment variable to be set.\
"
);
let span = tracing::warn_span!($($arg)+);
let _ = span.enter();
#[cfg(feature = "tracing")]
{
let _span = tracing::warn_span!($($arg)+);
_span.entered();
}
}
}
}

0 comments on commit 8ebbd49

Please sign in to comment.