fix: query logging
This commit is contained in:
parent
5f4d694d07
commit
0b0561406d
@ -1,4 +1,4 @@
|
|||||||
use std::{collections::HashMap, fmt::Display};
|
use std::{borrow::Cow, collections::HashMap, fmt::Display};
|
||||||
|
|
||||||
use super::AppError;
|
use super::AppError;
|
||||||
use async_tungstenite::{
|
use async_tungstenite::{
|
||||||
@ -52,7 +52,7 @@ fn extract_password_from_basic_auth(auth: &str) -> Result<String, AppError> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
struct QueryDisplay<'a, 'b> {
|
struct QueryDisplay<'a, 'b> {
|
||||||
map: HashMap<&'a str, &'b str>,
|
map: HashMap<Cow<'a, str>, Cow<'b, str>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, 'b> Display for QueryDisplay<'a, 'b> {
|
impl<'a, 'b> Display for QueryDisplay<'a, 'b> {
|
||||||
@ -60,7 +60,7 @@ impl<'a, 'b> Display for QueryDisplay<'a, 'b> {
|
|||||||
let length = self.map.len();
|
let length = self.map.len();
|
||||||
for (index, (k, v)) in self.map.iter().enumerate() {
|
for (index, (k, v)) in self.map.iter().enumerate() {
|
||||||
write!(f, "{k}={v}")?;
|
write!(f, "{k}={v}")?;
|
||||||
if index != length {
|
if index < length - 1 {
|
||||||
write!(f, "&")?;
|
write!(f, "&")?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -71,12 +71,11 @@ impl<'a, 'b> Display for QueryDisplay<'a, 'b> {
|
|||||||
fn make_span_trace<B>(req: &Request<B>) -> Span {
|
fn make_span_trace<B>(req: &Request<B>) -> Span {
|
||||||
let query = req.uri().query();
|
let query = req.uri().query();
|
||||||
let mut query_map = query
|
let mut query_map = query
|
||||||
.and_then(|v| serde_qs::from_str::<HashMap<&str, &str>>(v).ok())
|
.and_then(|v| serde_qs::from_str::<HashMap<Cow<str>, Cow<str>>>(v).ok())
|
||||||
.unwrap_or_else(HashMap::new);
|
.unwrap_or_else(HashMap::new);
|
||||||
if query_map.contains_key("token") {
|
if query_map.contains_key("token") {
|
||||||
query_map.insert("token", "<redacted>");
|
query_map.insert(Cow::Borrowed("token"), Cow::Borrowed("<redacted>"));
|
||||||
}
|
}
|
||||||
let query_display = QueryDisplay { map: query_map };
|
|
||||||
|
|
||||||
let request_id = req
|
let request_id = req
|
||||||
.headers()
|
.headers()
|
||||||
@ -84,14 +83,25 @@ fn make_span_trace<B>(req: &Request<B>) -> Span {
|
|||||||
.and_then(|v| v.to_str().ok())
|
.and_then(|v| v.to_str().ok())
|
||||||
.unwrap_or("no id set");
|
.unwrap_or("no id set");
|
||||||
|
|
||||||
tracing::debug_span!(
|
if query_map.is_empty() {
|
||||||
"request",
|
tracing::debug_span!(
|
||||||
method = %req.method(),
|
"request",
|
||||||
path = %req.uri().path(),
|
method = %req.method(),
|
||||||
query = %query_display,
|
path = %req.uri().path(),
|
||||||
version = ?req.version(),
|
version = ?req.version(),
|
||||||
id = %request_id,
|
id = %request_id,
|
||||||
)
|
)
|
||||||
|
} else {
|
||||||
|
let query_display = QueryDisplay { map: query_map };
|
||||||
|
tracing::debug_span!(
|
||||||
|
"request",
|
||||||
|
method = %req.method(),
|
||||||
|
path = %req.uri().path(),
|
||||||
|
query = %query_display,
|
||||||
|
version = ?req.version(),
|
||||||
|
id = %request_id,
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) async fn handler(state: AppState) -> Result<(Router, Router), AppError> {
|
pub(super) async fn handler(state: AppState) -> Result<(Router, Router), AppError> {
|
||||||
|
Loading…
Reference in New Issue
Block a user