Compare commits
9 Commits
Author | SHA1 | Date |
---|---|---|
Michael Pfaff | 84c3cada2c | |
Michael Pfaff | cec213adda | |
Michael Pfaff | 25b876ec0c | |
Michael Pfaff | 68e356811a | |
Michael Pfaff | c269c55cab | |
Michael Pfaff | 918577455e | |
Michael Pfaff | 0db36d48cc | |
Michael Pfaff | 9e8d5ef5e9 | |
Michael Pfaff | d517d3d03c |
16
Cargo.toml
16
Cargo.toml
|
@ -1,15 +1,13 @@
|
|||
[package]
|
||||
name = "tide_tracing"
|
||||
version = "0.4.1"
|
||||
version = "0.6.3"
|
||||
authors = ["Michael Pfaff <michael@pfaff.dev>"]
|
||||
edition = "2018"
|
||||
edition = "2021"
|
||||
license = "MIT OR Apache-2.0"
|
||||
|
||||
# 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", tag = "v0.1.1" }
|
||||
tide = { version = "0.16" }
|
||||
tracing = { version = "0.1" }
|
||||
async-trait = { version = "0.1" }
|
||||
chrono = { version = "0.4" }
|
||||
nrid = { git = "https://git.pfaff.dev/michael/nrid.rs.git", tag = "v0.2.0", default-features = false }
|
||||
tide = { version = "0.16", default-features = false }
|
||||
tracing = "0.1"
|
||||
async-trait = "0.1"
|
||||
num_enum = "0.5"
|
||||
|
|
247
src/lib.rs
247
src/lib.rs
|
@ -1,27 +1,161 @@
|
|||
#![feature(const_trait_impl)]
|
||||
|
||||
#[macro_use]
|
||||
extern crate tracing;
|
||||
|
||||
use chrono::Utc;
|
||||
use nrid::Nrid;
|
||||
use tide::Middleware;
|
||||
use tide::Next;
|
||||
use tide::Request;
|
||||
use tracing::Instrument;
|
||||
use tracing::Level;
|
||||
|
||||
/// A traceable component of a [`Request`].
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, num_enum::FromPrimitive)]
|
||||
#[repr(u8)]
|
||||
pub enum ReqComp {
|
||||
#[num_enum(default)]
|
||||
__Default = 0b00000000,
|
||||
Method = 0b00000001,
|
||||
Path = 0b00000010,
|
||||
IP = 0b00000100,
|
||||
Query = 0b00001000,
|
||||
}
|
||||
|
||||
/// A selection of traceable components of a [`Request`].
|
||||
#[derive(Debug, Clone, Copy, Eq, Hash)]
|
||||
#[repr(transparent)]
|
||||
pub struct ReqComps(u8);
|
||||
|
||||
impl ReqComps {
|
||||
#[inline]
|
||||
pub const fn contains(self, mask: ReqComp) -> bool {
|
||||
(self & mask).0 == mask as u8
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub const fn contains_all(self, mask: ReqComps) -> bool {
|
||||
self & mask == mask
|
||||
}
|
||||
}
|
||||
|
||||
impl const Default for ReqComps {
|
||||
#[inline]
|
||||
fn default() -> Self {
|
||||
Self(0)
|
||||
}
|
||||
}
|
||||
|
||||
// unfortunately, the automatic Into impl will not be const.
|
||||
impl const From<ReqComp> for ReqComps {
|
||||
#[inline]
|
||||
fn from(comp: ReqComp) -> Self {
|
||||
Self(comp as u8)
|
||||
}
|
||||
}
|
||||
|
||||
impl const core::cmp::PartialEq for ReqComps {
|
||||
#[inline]
|
||||
fn eq(&self, comp: &Self) -> bool {
|
||||
self.0 == comp.0
|
||||
}
|
||||
}
|
||||
|
||||
impl const core::ops::BitOr for ReqComp {
|
||||
type Output = ReqComps;
|
||||
|
||||
#[inline]
|
||||
fn bitor(self, rhs: Self) -> Self::Output {
|
||||
ReqComps((self as u8) | (rhs as u8))
|
||||
}
|
||||
}
|
||||
|
||||
impl const core::ops::BitOr for ReqComps {
|
||||
type Output = ReqComps;
|
||||
|
||||
#[inline]
|
||||
fn bitor(self, rhs: Self) -> Self::Output {
|
||||
ReqComps((self.0 as u8) | (rhs.0 as u8))
|
||||
}
|
||||
}
|
||||
|
||||
impl const core::ops::BitOr<ReqComp> for ReqComps {
|
||||
type Output = ReqComps;
|
||||
|
||||
#[inline]
|
||||
fn bitor(self, rhs: ReqComp) -> Self::Output {
|
||||
ReqComps((self.0 as u8) | (rhs as u8))
|
||||
}
|
||||
}
|
||||
|
||||
// TODO make `const` when is "stable" #57349 <https://github.com/rust-lang/rust/issues/57349>
|
||||
impl core::ops::BitOrAssign for ReqComps {
|
||||
#[inline]
|
||||
fn bitor_assign(&mut self, rhs: ReqComps) {
|
||||
self.0 = (self.0 as u8) | (rhs.0 as u8)
|
||||
}
|
||||
}
|
||||
|
||||
// TODO make `const` when is "stable" #57349 <https://github.com/rust-lang/rust/issues/57349>
|
||||
impl core::ops::BitOrAssign<ReqComp> for ReqComps {
|
||||
#[inline]
|
||||
fn bitor_assign(&mut self, rhs: ReqComp) {
|
||||
self.0 = (self.0 as u8) | (rhs as u8)
|
||||
}
|
||||
}
|
||||
|
||||
impl const core::ops::BitAnd for ReqComp {
|
||||
type Output = ReqComps;
|
||||
|
||||
#[inline]
|
||||
fn bitand(self, rhs: Self) -> Self::Output {
|
||||
ReqComps((self as u8) & (rhs as u8))
|
||||
}
|
||||
}
|
||||
|
||||
impl const core::ops::BitAnd for ReqComps {
|
||||
type Output = ReqComps;
|
||||
|
||||
#[inline]
|
||||
fn bitand(self, rhs: Self) -> Self::Output {
|
||||
ReqComps((self.0 as u8) & (rhs.0 as u8))
|
||||
}
|
||||
}
|
||||
|
||||
impl const core::ops::BitAnd<ReqComp> for ReqComps {
|
||||
type Output = ReqComps;
|
||||
|
||||
#[inline]
|
||||
fn bitand(self, rhs: ReqComp) -> Self::Output {
|
||||
ReqComps((self.0 as u8) & (rhs as u8))
|
||||
}
|
||||
}
|
||||
|
||||
// TODO make `const` when is "stable" #57349 <https://github.com/rust-lang/rust/issues/57349>
|
||||
impl core::ops::BitAndAssign for ReqComps {
|
||||
#[inline]
|
||||
fn bitand_assign(&mut self, rhs: ReqComps) {
|
||||
self.0 = (self.0 as u8) & (rhs.0 as u8)
|
||||
}
|
||||
}
|
||||
|
||||
// TODO make `const` when is "stable" #57349 <https://github.com/rust-lang/rust/issues/57349>
|
||||
impl core::ops::BitAndAssign<ReqComp> for ReqComps {
|
||||
#[inline]
|
||||
fn bitand_assign(&mut self, rhs: ReqComp) {
|
||||
self.0 = (self.0 as u8) & (rhs as u8)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct LogMiddleware {
|
||||
pub include_ip: bool,
|
||||
pub include_query: bool,
|
||||
pub multiline: bool,
|
||||
pub components: ReqComps,
|
||||
}
|
||||
|
||||
impl Default for LogMiddleware {
|
||||
impl const Default for LogMiddleware {
|
||||
#[inline]
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
include_ip: false,
|
||||
include_query: false,
|
||||
multiline: false,
|
||||
components: ReqComp::Method | ReqComp::Path,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -32,53 +166,40 @@ impl LogMiddleware {
|
|||
req: Request<State>,
|
||||
next: Next<'a, State>,
|
||||
) -> tide::Result {
|
||||
let start = Utc::now();
|
||||
let id = Nrid::now();
|
||||
let uri = req.url().clone();
|
||||
let ip = req.remote().unwrap_or("unknown").to_string();
|
||||
let span = span!(Level::ERROR, "Request", id = %id);
|
||||
let res = async {
|
||||
let path = uri.path();
|
||||
let method = req.method();
|
||||
let query = uri.query().unwrap_or("");
|
||||
let received = start.to_rfc3339_opts(chrono::SecondsFormat::AutoSi, false);
|
||||
|
||||
if self.multiline {
|
||||
debug!(path = path);
|
||||
|
||||
debug!(method = %method);
|
||||
|
||||
if self.include_ip {
|
||||
debug!(client_ip = %ip);
|
||||
}
|
||||
|
||||
debug!(received = %received);
|
||||
|
||||
if self.include_query {
|
||||
debug!(query = query);
|
||||
}
|
||||
} else {
|
||||
debug!(path = path, method = %method, client_ip = %ip, received = %received, query = query);
|
||||
}
|
||||
|
||||
// debug!(method = %req.method(), query = uri.query().unwrap_or(""), client_ip = %ip, received = %start, "Request recieved");
|
||||
use tracing::field::Empty;
|
||||
let span = debug_span!("Request", id = %id, "http.method" = Empty, "http.path" = Empty, "conn.ip" = Empty, "http.query" = Empty);
|
||||
let url = req.url();
|
||||
if self.components.contains(ReqComp::Method) {
|
||||
span.record("http.method", &tracing::field::display(req.method()));
|
||||
}
|
||||
if self.components.contains(ReqComp::Path) {
|
||||
span.record("http.path", &url.path());
|
||||
}
|
||||
if self.components.contains(ReqComp::IP) {
|
||||
span.record(
|
||||
"conn.ip",
|
||||
&tracing::field::display(req.remote().unwrap_or("unknown").to_string()),
|
||||
);
|
||||
}
|
||||
if self.components.contains(ReqComp::Query) {
|
||||
span.record("http.query", &url.query().unwrap_or(""));
|
||||
}
|
||||
async move {
|
||||
let res = next.run(req).await;
|
||||
let end = Utc::now();
|
||||
let span = span!(Level::ERROR, "Response");
|
||||
async {
|
||||
let status = res.status();
|
||||
let duration = end.signed_duration_since(start).to_std().unwrap();
|
||||
|
||||
if self.multiline {
|
||||
debug!(status = %status);
|
||||
debug!(duration = ?duration);
|
||||
} else {
|
||||
debug!(status = %status, duration = ?duration);
|
||||
let status = res.status();
|
||||
match res.error() {
|
||||
Some(e) => {
|
||||
error!(status = %status, "Response with internal error: {e:?}");
|
||||
}
|
||||
}.instrument(span).await;
|
||||
res
|
||||
}.instrument(span).await;
|
||||
Ok(res)
|
||||
None => {
|
||||
debug!(status = %status, "Response");
|
||||
}
|
||||
}
|
||||
Ok(res)
|
||||
}
|
||||
.instrument(span)
|
||||
.await
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -88,3 +209,23 @@ impl<State: Clone + Send + Sync + 'static> Middleware<State> for LogMiddleware {
|
|||
self.log(req, next).await
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_mask() {
|
||||
const FIELDS: &'static [ReqComp] = &[ReqComp::Path, ReqComp::IP];
|
||||
|
||||
let mut comps = ReqComps::default();
|
||||
|
||||
for field in FIELDS {
|
||||
comps |= *field;
|
||||
}
|
||||
|
||||
assert!(comps.contains(ReqComp::Path));
|
||||
assert!(comps.contains(ReqComp::IP));
|
||||
assert!(!comps.contains(ReqComp::Query));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue