Initial commit

This commit is contained in:
Michael Pfaff 2021-01-18 22:39:08 -05:00
commit 53eac89cae
Signed by: michael
GPG Key ID: E53B118B12B5C7F9
5 changed files with 1707 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

1660
Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

12
Cargo.toml Normal file
View File

@ -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"

3
README.md Normal file
View File

@ -0,0 +1,3 @@
# Tide Tracing
Simple middleware for Tide to log requests with Tracing

31
src/lib.rs Normal file
View File

@ -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)
})
}