tracing improvements

This commit is contained in:
azdle 2025-07-13 14:45:24 -05:00
parent 6551b0ed90
commit f9eec3be3a
4 changed files with 38 additions and 3 deletions

18
Cargo.lock generated
View file

@ -2866,6 +2866,22 @@ dependencies = [
"tracing", "tracing",
] ]
[[package]]
name = "tower-http"
version = "0.6.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "adc82fd73de2a9722ac5da747f12383d2bfdb93591ee6c58486e0097890f05f2"
dependencies = [
"bitflags",
"bytes",
"http",
"http-body",
"pin-project-lite",
"tower-layer",
"tower-service",
"tracing",
]
[[package]] [[package]]
name = "tower-layer" name = "tower-layer"
version = "0.3.3" version = "0.3.3"
@ -3583,6 +3599,8 @@ dependencies = [
"tokio", "tokio",
"tokio-stream", "tokio-stream",
"tokio-util", "tokio-util",
"tower",
"tower-http",
"tracing", "tracing",
"tracing-subscriber", "tracing-subscriber",
"uuid", "uuid",

View file

@ -23,8 +23,10 @@ thiserror = "2.0"
tokio = { version = "1.28.2", features = ["full"] } tokio = { version = "1.28.2", features = ["full"] }
tokio-stream = "0.1" tokio-stream = "0.1"
tokio-util = "0.7.15" tokio-util = "0.7.15"
tower = "0.5.2"
tower-http = { version = "0.6.6", features = ["trace"] }
tracing = "0.1.37" tracing = "0.1.37"
tracing-subscriber = { version = "0.3", features =["env-filter"] } tracing-subscriber = { version = "0.3", features = ["env-filter", "fmt"] }
uuid = { version = "1.16.0", features = ["v4"] } uuid = { version = "1.16.0", features = ["v4"] }
[dev-dependencies] [dev-dependencies]

View file

@ -12,6 +12,7 @@ use std::pin::pin;
use std::pin::Pin; use std::pin::Pin;
use std::sync::Arc; use std::sync::Arc;
use tokio::signal; use tokio::signal;
use tower_http::trace::TraceLayer;
use tracing::info; use tracing::info;
use crate::Conf; use crate::Conf;
@ -52,7 +53,9 @@ impl ZeroToAxum {
db, db,
}; };
let app = routes::build().with_state(app_state); let app = routes::build()
.with_state(app_state)
.layer(TraceLayer::new_for_http());
let listener = tokio::net::TcpListener::bind(&conf.listen).await.unwrap(); let listener = tokio::net::TcpListener::bind(&conf.listen).await.unwrap();
let bound_addr = listener.local_addr().unwrap(); let bound_addr = listener.local_addr().unwrap();

View file

@ -1,3 +1,5 @@
use std::fmt;
use axum::{http::StatusCode, response::IntoResponse, routing::post, Form, Router}; use axum::{http::StatusCode, response::IntoResponse, routing::post, Form, Router};
use axum_extra::extract::cookie::{Cookie, PrivateCookieJar}; use axum_extra::extract::cookie::{Cookie, PrivateCookieJar};
use serde::Deserialize; use serde::Deserialize;
@ -17,11 +19,21 @@ pub struct LoginForm {
password: String, password: String,
} }
impl fmt::Debug for LoginForm {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("LoginForm")
.field("username", &self.username)
.field("password", &"REDACTED")
.finish()
}
}
#[tracing::instrument]
pub async fn login( pub async fn login(
jar: PrivateCookieJar, jar: PrivateCookieJar,
Form(form): Form<LoginForm>, Form(form): Form<LoginForm>,
) -> Result<PrivateCookieJar, LoginError> { ) -> Result<PrivateCookieJar, LoginError> {
info!(form.username, form.password, "login attempt"); info!("login attempt");
if form.username != "admin" { if form.username != "admin" {
return Err(LoginError::UnknownUser); return Err(LoginError::UnknownUser);