try logging the full URI instead of just the path for tracing_span

Signed-off-by: strawberry <strawberry@puppygock.gay>
This commit is contained in:
strawberry 2024-04-23 22:33:06 -04:00 committed by June
parent 6874ac0015
commit 418ec87cfd

View file

@ -38,11 +38,12 @@ use tokio::{
use tower::ServiceBuilder;
use tower_http::{
catch_panic::CatchPanicLayer,
classify::ServerErrorsFailureClass,
cors::{self, CorsLayer},
trace::{DefaultOnFailure, TraceLayer},
trace::TraceLayer,
ServiceBuilderExt as _,
};
use tracing::{debug, error, info, warn, Level};
use tracing::{debug, error, info, warn, Span};
use tracing_subscriber::{prelude::*, reload, EnvFilter, Registry};
use utils::error::{Error, Result};
@ -303,7 +304,9 @@ async fn build(server: &Server) -> io::Result<axum::routing::IntoMakeService<Rou
.layer(
TraceLayer::new_for_http()
.make_span_with(tracing_span::<_>)
.on_failure(DefaultOnFailure::new().level(Level::INFO)),
.on_failure(|error: ServerErrorsFailureClass, latency: Duration, _span: &Span| {
debug_info!("response failed: {error:?}, latency: {latency:?}");
}),
)
.layer(axum::middleware::from_fn(request_handler))
.layer(cors_layer(server))
@ -424,14 +427,16 @@ fn compression_layer(server: &Server) -> tower_http::compression::CompressionLay
compression_layer
}
fn tracing_span<T>(request: &http::Request<T>) -> tracing::Span {
let path = if let Some(path) = request.extensions().get::<MatchedPath>() {
path.as_str()
} else {
request.uri().path()
};
fn tracing_span<T>(request: &http::Request<T>) -> Span {
let path = request.extensions().get::<MatchedPath>();
let uri = &request.uri().to_string();
tracing::info_span!("handle", %path)
if let Some(path) = path {
let path = path.as_str();
tracing::info_span!("handle", %path, %uri)
} else {
tracing::info_span!("handle", %uri)
}
}
/// Non-async initializations