feat: add tracing
CI / Rust CI (push) Successful in 4m41s

This commit is contained in:
2026-04-25 19:55:58 +02:00
parent 57a3d04625
commit 4596dfe28b
5 changed files with 110 additions and 16 deletions
Generated
+88 -3
View File
@@ -194,6 +194,8 @@ dependencies = [
"tokio",
"tokio-stream",
"tower-http",
"tracing",
"tracing-subscriber",
"utoipa",
"uuid",
"wiremock",
@@ -943,6 +945,15 @@ version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "112b39cec0b298b6c1999fee3e31427f74f676e4cb9879ed1a121b43661a4154"
[[package]]
name = "matchers"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d1525a2a28c7f4fa0fc98bb91ae755d1e2d1505079e05539e35bc876b5d65ae9"
dependencies = [
"regex-automata",
]
[[package]]
name = "matchit"
version = "0.8.4"
@@ -972,6 +983,15 @@ dependencies = [
"windows-sys 0.61.2",
]
[[package]]
name = "nu-ansi-term"
version = "0.50.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7957b9740744892f114936ab4a57b3f487491bbeafaf8083688b16841a4240e5"
dependencies = [
"windows-sys 0.61.2",
]
[[package]]
name = "num-bigint"
version = "0.4.6"
@@ -1538,6 +1558,15 @@ dependencies = [
"serde",
]
[[package]]
name = "sharded-slab"
version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f40ca3c46823713e0d4209592e8d6e826aa57e928f09752619fc696c499637f6"
dependencies = [
"lazy_static",
]
[[package]]
name = "shlex"
version = "1.3.0"
@@ -1701,6 +1730,15 @@ dependencies = [
"syn",
]
[[package]]
name = "thread_local"
version = "1.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f60246a4944f24f6e018aa17cdeffb7818b76356965d03b07d6a9886e8962185"
dependencies = [
"cfg-if",
]
[[package]]
name = "time"
version = "0.3.47"
@@ -1867,16 +1905,27 @@ checksum = "8df9b6e13f2d32c91b9bd719c00d1958837bc7dec474d94952798cc8e69eeec3"
[[package]]
name = "tracing"
version = "0.1.37"
version = "0.1.44"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ce8c33a8d48bd45d624a6e523445fd21ec13d3653cd51f681abf67418f54eb8"
checksum = "63e71662fa4b2a2c3a26f570f037eb95bb1f85397f3cd8076caed2f026a6d100"
dependencies = [
"cfg-if",
"log",
"pin-project-lite",
"tracing-attributes",
"tracing-core",
]
[[package]]
name = "tracing-attributes"
version = "0.1.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7490cfa5ec963746568740651ac6781f701c9c5ea257c58e057f3ba8cf69e8da"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "tracing-core"
version = "0.1.36"
@@ -1884,6 +1933,36 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "db97caf9d906fbde555dd62fa95ddba9eecfd14cb388e4f491a66d74cd5fb79a"
dependencies = [
"once_cell",
"valuable",
]
[[package]]
name = "tracing-log"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ee855f1f400bd0e5c02d150ae5de3840039a3f54b025156404e34c23c03f47c3"
dependencies = [
"log",
"once_cell",
"tracing-core",
]
[[package]]
name = "tracing-subscriber"
version = "0.3.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cb7f578e5945fb242538965c2d0b04418d38ec25c79d160cd279bf0731c8d319"
dependencies = [
"matchers",
"nu-ansi-term",
"once_cell",
"regex-automata",
"sharded-slab",
"smallvec",
"thread_local",
"tracing",
"tracing-core",
"tracing-log",
]
[[package]]
@@ -1970,6 +2049,12 @@ dependencies = [
"wasm-bindgen",
]
[[package]]
name = "valuable"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ba73ea9cf16a25df0c8caa16c51acb937d5712a8429db78a3ee29d5dcacd3a65"
[[package]]
name = "walkdir"
version = "2.5.0"
+2
View File
@@ -23,3 +23,5 @@ futures = "0.3"
chrono = { version = "0.4.44", features = ["serde"] }
uuid = { version = "1.23.1", features = ["v4", "serde"] }
tower-http = { version = "0.6.8", features = ["cors"] }
tracing = "0.1.44"
tracing-subscriber = { version = "0.3", features = ["env-filter"]}
+9
View File
@@ -16,11 +16,20 @@ use std::env;
use std::net::SocketAddr;
use std::sync::Arc;
use tower_http::cors::CorsLayer;
use tracing_subscriber::{EnvFilter, fmt};
static OLLAMA_URL: Lazy<String> = Lazy::new(|| env::var("OLLAMA_URL").expect("OLLAMA_URL not set"));
pub fn init_tracing() {
let filter = env::var("RUST_LOG").unwrap_or_else(|_| "info".to_string());
fmt().with_env_filter(EnvFilter::new(filter)).init();
}
#[tokio::main]
async fn main() {
init_tracing();
#[cfg(debug_assertions)]
{
dotenvy::dotenv().ok();
+2 -3
View File
@@ -10,7 +10,7 @@ pub struct Claims {
pub preferred_username: Option<String>,
pub exp: usize,
pub iss: String,
pub aud: Option<String>,
pub aud: Option<Vec<String>>,
pub realm_access: Option<RealmAccess>,
}
@@ -47,9 +47,8 @@ pub fn validate_token(token: &str, jwks: &Value) -> Result<Claims, String> {
validation.set_issuer(&[ISSUER.as_str()]);
// Optional but recommended:
validation.validate_exp = true;
validation.validate_aud = false; // depends on your Keycloak config
validation.validate_aud = false;
// 5. Decode & verify
let token_data = decode::<Claims>(token, &decoding_key, &validation)
+8 -9
View File
@@ -6,18 +6,15 @@ use crate::middlewares::auth::{
};
pub async fn auth_middleware(mut request: Request, next: Next) -> Result<Response, StatusCode> {
#[cfg(debug_assertions)]
println!("Middleware hit");
tracing::debug!("Middleware hit");
let headers = request.headers();
#[cfg(debug_assertions)]
println!("Headers extracted");
tracing::debug!("Headers extracted");
let auth_header = headers.get("authorization").and_then(|v| v.to_str().ok());
#[cfg(debug_assertions)]
println!("Auth header: {:?}", auth_header);
tracing::debug!("Auth header: {:?}", auth_header);
let auth_header = auth_header.ok_or(StatusCode::UNAUTHORIZED)?;
@@ -29,8 +26,7 @@ pub async fn auth_middleware(mut request: Request, next: Next) -> Result<Respons
match validate_token(token, &jwks) {
Ok(claims) => {
#[cfg(debug_assertions)]
println!("Token valid");
tracing::debug!("Token valid");
request.extensions_mut().insert(claims);
@@ -44,7 +40,10 @@ pub async fn auth_middleware(mut request: Request, next: Next) -> Result<Respons
request.extensions_mut().insert(claims);
Ok(next.run(request).await)
}
Err(_) => Err(StatusCode::UNAUTHORIZED),
Err(e) => {
eprintln!("JWT validation failed: {:?}", e);
Err(StatusCode::UNAUTHORIZED)
}
}
}
}