Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ tokio = { version = "1", features = ["macros", "test-util", "signal", "net", "io
tokio-test = "0.4"
tower-test = "0.4"
pretty_env_logger = "0.5"
tracing-subscriber = { version = "0.3", default-features = false, features = ["registry", "std"] }
tracing-core = "0.1.29"
tracing-subscriber = { version = "0.3", default-features = false, features = ["fmt", "registry", "std"] }

[target.'cfg(any(target_os = "linux", target_os = "macos"))'.dev-dependencies]
pnet_datalink = "0.35.0"
Expand Down
62 changes: 62 additions & 0 deletions examples/client_tracing.rs

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see the pr description for logs from this example.

Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use std::env;

use http_body_util::{BodyExt, Empty};
use hyper::Request;
use hyper_util::client::legacy::{Client, connect::HttpConnector};
use tracing::Instrument;

#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let _tracing = tracing_subscriber::fmt()
.with_max_level(tracing::Level::TRACE)
.init();
tracing::info!("tracing subscriber initialized");

let url = match env::args().nth(1) {
Some(url) => url,
None => {
tracing::error!("Usage: client <url>");
return Ok(());
}
};

// HTTPS requires picking a TLS implementation, so give a better
// warning if the user tries to request an 'https' URL.
let url = url
.parse::<hyper::Uri>()
.inspect_err(|err| tracing::error!(%err, "failed to parse url"))?;
if url.scheme_str() != Some("http") {
tracing::error!("This example only works with 'http' URLs.");
return Ok(());
}
tracing::info!(%url, "parsed URL");

let executor = {
// Propagate spans to spawned tasks.
use hyper_util::rt::{CurrentSpanExecutor, TokioExecutor};
let tokio = TokioExecutor::new();
CurrentSpanExecutor::new(tokio)
};

let client = Client::builder(executor).build(HttpConnector::new());

let req = Request::builder()
.uri(url.clone())
.body(Empty::<bytes::Bytes>::new())?;

let span = tracing::info_span!("sending request", %url);
let resp = client.request(req).instrument(span).await?;

let (resp, body) = resp.into_parts();
let body = body.collect().await.unwrap().to_bytes().to_vec();
let body = String::from_utf8(body).unwrap();

tracing::info!(
?resp.version,
%resp.status,
resp.body = %body,
"received response",
);

Ok(())
}
5 changes: 5 additions & 0 deletions src/rt/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
//! Runtime utilities

#[cfg(feature = "tracing")]
pub mod tracing;
#[cfg(feature = "tracing")]
pub use self::tracing::{CurrentSpanExecutor, MkSpanExecutor, WithSpanExecutor};

#[cfg(feature = "client-legacy")]
mod io;
#[cfg(feature = "client-legacy")]
Expand Down
8 changes: 7 additions & 1 deletion src/rt/tokio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,13 @@ mod with_tokio_io;
/// Future executor that utilises `tokio` threads.
///
/// Spawned futures do not inherit the current tracing span, even when the
/// `tracing` feature is enabled.
/// `tracing` feature is enabled. To propagate spans, wrap this executor in
/// one of the components from [`rt::tracing`](crate::rt::tracing), such as
/// [`CurrentSpanExecutor`](crate::rt::CurrentSpanExecutor) (available with
Comment thread
cratelyn marked this conversation as resolved.
/// the `tracing` feature).
///
/// See the module-level documentation of [`rt::tracing`](crate::rt::tracing)
/// for more information about propagating [`tracing`] spans to spawned tasks.
Comment on lines +79 to +80

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i've added a link to the module-level documentation where there is more information about choosing a particular executor. we still mention CurrentSpanExecutor to provide a likely workable default for most users.

///
/// The temporary `rt-tracing-exec-force` feature restores propagation of the
/// current span for libraries that do not allow customizing their executor.
Expand Down
Loading
Loading