Initial commit
This commit is contained in:
commit
53eac89cae
|
@ -0,0 +1 @@
|
|||
/target
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,12 @@
|
|||
[package]
|
||||
name = "tide_tracing"
|
||||
version = "0.1.0"
|
||||
authors = ["Michael Pfaff <michael@pfaff.dev>"]
|
||||
edition = "2018"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
nrid = { git = "https://git.pfaff.dev/michael/nrid.rs.git" }
|
||||
tide = "0.15.0"
|
||||
tracing = "0.1.15"
|
|
@ -0,0 +1,3 @@
|
|||
# Tide Tracing
|
||||
|
||||
Simple middleware for Tide to log requests with Tracing
|
|
@ -0,0 +1,31 @@
|
|||
#[macro_use]
|
||||
extern crate tracing;
|
||||
|
||||
use core::future::Future;
|
||||
use core::pin::Pin;
|
||||
use nrid::Nrid;
|
||||
use std::time::Instant;
|
||||
use tide::Next;
|
||||
use tide::Request;
|
||||
use tracing::Level;
|
||||
|
||||
/// Middleware for the [`req_access_token!`] macro.
|
||||
pub fn log_middleware<'a, State: 'static + Clone + Send + Sync>(
|
||||
req: Request<State>,
|
||||
next: Next<'a, State>,
|
||||
) -> Pin<Box<dyn Future<Output = tide::Result> + Send + 'a>> {
|
||||
Box::pin(async {
|
||||
let now = Instant::now();
|
||||
let id = Nrid::now();
|
||||
let uri = req.url();
|
||||
// doesn't take forwarding into account and includes useless socket port information
|
||||
// let ip = req.peer_addr().unwrap_or("unknown").to_string();
|
||||
let ip = req.remote().unwrap_or("unknown").to_string();
|
||||
let span = span!(Level::ERROR, "Request", id = %id);
|
||||
let _enter = span.enter();
|
||||
debug!(req.method = %req.method(), req.uri.path = uri.path(), req.uri.query = uri.query().unwrap_or(""), client_ip = %ip, received = ?now);
|
||||
let res = next.run(req).await;
|
||||
debug!(status = %res.status(), elapsed = ?now.elapsed(), "Response");
|
||||
Ok(res)
|
||||
})
|
||||
}
|
Loading…
Reference in New Issue