Debian Codegraph @

third_party/rust/warp/tests/tracing.rs

raw
1 use warp::Filter;
2
3 #[tokio::test]
4 async fn uses_tracing() {
5 // Setup a log subscriber (responsible to print to output)
6 let subscriber = tracing_subscriber::fmt()
7 .with_env_filter("trace")
8 .without_time()
9 .finish();
10
11 // Set the previously created subscriber as the global subscriber
12 tracing::subscriber::set_global_default(subscriber).unwrap();
13 // Redirect normal log messages to the tracing subscriber
14 tracing_log::LogTracer::init().unwrap();
15
16 // Start a span with some metadata (fields)
17 let span = tracing::info_span!("app", domain = "www.example.org");
18 let _guard = span.enter();
19
20 log::info!("logged using log macro");
21
22 let ok = warp::any()
23 .map(|| {
24 tracing::info!("printed for every request");
25 })
26 .untuple_one()
27 .and(warp::path("aa"))
28 .map(|| {
29 tracing::info!("only printed when path '/aa' matches");
30 })
31 .untuple_one()
32 .map(warp::reply)
33 // Here we add the tracing logger which will ensure that all requests has a span with
34 // useful information about the request (method, url, version, remote_addr, etc.)
35 .with(warp::trace::request());
36
37 tracing::info!("logged using tracing macro");
38
39 // Send a request for /
40 let req = warp::test::request();
41 let resp = req.reply(&ok);
42 assert_eq!(resp.await.status(), 404);
43
44 // Send a request for /aa
45 let req = warp::test::request().path("/aa");
46 let resp = req.reply(&ok);
47 assert_eq!(resp.await.status(), 200);
48 }