It is not uncommon that users who are new to tracing look at the examples in the `master` branch of the repository and find that they don't compile. This is because they are examples which compile with the code from the master branch, which is for the as yet unreleased tracing 0.2.0 ecosystem. Users should instead go to the `v0.1.x` branch to find examples compatible with the crates published on crates.io. This change adds a doc-comment to the beginning of every example file informing the user of this fact and suggesting that they check out the `v0.1.x` branch instead.
47 lines
1.3 KiB
Rust
47 lines
1.3 KiB
Rust
//! NOTE: This is pre-release documentation for the upcoming tracing 0.2.0 ecosystem. For the
|
|
//! release examples, please see the `v0.1.x` branch instead.
|
|
use http::{Method, Request, Uri};
|
|
use hyper::{client::Client, Body};
|
|
use std::time::Duration;
|
|
use tower::{Service, ServiceBuilder};
|
|
use tracing::info;
|
|
use tracing_tower::request_span;
|
|
|
|
type Err = Box<dyn std::error::Error + Send + Sync + 'static>;
|
|
|
|
fn req_span<A>(req: &Request<A>) -> tracing::Span {
|
|
let span = tracing::info_span!(
|
|
"request",
|
|
req.method = ?req.method(),
|
|
req.uri = ?req.uri(),
|
|
req.version = ?req.version(),
|
|
headers = ?req.headers()
|
|
);
|
|
tracing::info!(parent: &span, "sending request");
|
|
span
|
|
}
|
|
|
|
#[tokio::main]
|
|
async fn main() -> Result<(), Err> {
|
|
tracing_subscriber::fmt()
|
|
.with_env_filter("tower=trace")
|
|
.try_init()?;
|
|
|
|
let mut svc = ServiceBuilder::new()
|
|
.timeout(Duration::from_millis(250))
|
|
.layer(request_span::layer(req_span))
|
|
.service(Client::new());
|
|
|
|
let uri = Uri::from_static("http://httpbin.org");
|
|
|
|
let req = Request::builder()
|
|
.method(Method::GET)
|
|
.uri(uri)
|
|
.body(Body::empty())
|
|
.expect("Unable to build request; this is a bug.");
|
|
|
|
let res = svc.call(req).await?;
|
|
info!(message = "got a response", res.headers = ?res.headers());
|
|
|
|
Ok(())
|
|
}
|