Skip to content

Instantly share code, notes, and snippets.

@Geal
Created December 2, 2021 18:16
Show Gist options
  • Save Geal/68bb703d2c307e64bc415c60b6e75107 to your computer and use it in GitHub Desktop.
Save Geal/68bb703d2c307e64bc415c60b6e75107 to your computer and use it in GitHub Desktop.
[package]
name = "warp-tracing"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
futures-util = "0.3.18"
tokio = { version = "1.14.0", features = ["full"] }
tracing = "0.1.29"
tracing-subscriber = { version = "0.3.3", features = ["env-filter"] }
warp = "0.3.2"
#![deny(warnings)]
use std::convert::Infallible;
use futures_util::StreamExt;
use tracing_subscriber::fmt::format::FmtSpan;
use warp::{
hyper::{Body, Response},
Filter, Reply,
};
#[tokio::main]
async fn main() {
let filter = std::env::var("RUST_LOG").unwrap_or_else(|_| "tracing=info,warp=debug".to_owned());
tracing_subscriber::fmt()
.with_env_filter(filter)
.with_span_events(FmtSpan::CLOSE)
.init();
let hello = warp::path("hello")
.and(warp::get())
.map(|| {
tracing::info!("saying hello...");
"Hello, World!"
})
.with(warp::trace::named("hello"));
let goodbye = warp::path("goodbye")
.and(warp::get())
.and_then(|| async {
println!("in goodbye");
let chunks: Vec<Result<_, std::io::Error>> = vec![Ok("hello"), Ok(" "), Ok("world")];
let stream = futures_util::stream::iter(chunks).map(|chunk| {
println!("sending chunk");
chunk
});
let body = Body::wrap_stream(stream);
println!("will return body");
Ok::<Box<dyn Reply>, Infallible>(Box::new(Response::new(body)))
})
.with(warp::trace::named("goodbye"));
let routes = hello.or(goodbye).with(warp::trace::request());
warp::serve(routes).run(([127, 0, 0, 1], 3030)).await;
}
Running `target/debug/warp-tracing`
2021-12-02T18:13:05.969237Z INFO Server::run{addr=127.0.0.1:3030}: warp::server: listening on http://127.0.0.1:3030
2021-12-02T18:13:22.131648Z DEBUG request{method=GET path=/goodbye version=HTTP/1.1 remote.addr=127.0.0.1:43980}: warp::filters::trace: received request
2021-12-02T18:13:22.131692Z INFO request{method=GET path=/goodbye version=HTTP/1.1 remote.addr=127.0.0.1:43980}: warp::filters::trace: processing request
2021-12-02T18:13:22.131725Z INFO request{method=GET path=/goodbye version=HTTP/1.1 remote.addr=127.0.0.1:43980}:context{hello}: warp::filters::trace: processing request
2021-12-02T18:13:22.131810Z WARN request{method=GET path=/goodbye version=HTTP/1.1 remote.addr=127.0.0.1:43980}:context{hello}: warp::filters::trace: unable to serve request (client error) status=404 error=Rejection(NotFound)
2021-12-02T18:13:22.131862Z INFO request{method=GET path=/goodbye version=HTTP/1.1 remote.addr=127.0.0.1:43980}:context{goodbye}: warp::filters::trace: processing request
2021-12-02T18:13:22.131892Z DEBUG request{method=GET path=/goodbye version=HTTP/1.1 remote.addr=127.0.0.1:43980}:context{hello}: warp::filters::trace: close time.busy=73.6µs time.idle=98.6µs
in goodbye
will return body
2021-12-02T18:13:22.131940Z INFO request{method=GET path=/goodbye version=HTTP/1.1 remote.addr=127.0.0.1:43980}:context{goodbye}: warp::filters::trace: finished processing with success status=200
2021-12-02T18:13:22.131979Z DEBUG request{method=GET path=/goodbye version=HTTP/1.1 remote.addr=127.0.0.1:43980}:context{goodbye}: warp::filters::trace: close time.busy=85.0µs time.idle=43.1µs
2021-12-02T18:13:22.132004Z INFO request{method=GET path=/goodbye version=HTTP/1.1 remote.addr=127.0.0.1:43980}: warp::filters::trace: finished processing with success status=200
2021-12-02T18:13:22.132026Z INFO request{method=GET path=/goodbye version=HTTP/1.1 remote.addr=127.0.0.1:43980}: warp::filters::trace: close time.busy=301µs time.idle=109µs
sending chunk
sending chunk
sending chunk
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment