-
SummaryI am trying to set up .layer(
TraceLayer::new_for_http().make_span_with(|req: &Request<_>| {
use axum::extract::{MatchedPath, RawPathParams};
let matched_path = req
.extensions()
.get::<MatchedPath>()
.map(MatchedPath::as_str)
.unwrap_or(req.uri().path());
let path_params = dbg!(req.extensions().get::<RawPathParams>()).map(|params| {
params
.into_iter()
.fold(HashMap::<&str, &str>::new(), |mut map, (k, v)| {
map.insert(k, v);
map
})
});
tracing::info_span!(
"http_request",
method = ?req.method(),
matched_path,
?path_params
)
}),
); but for example the following handler async fn path_params(Path(param): Path<String>, raw: RawPathParams) -> String {
raw.into_iter().for_each(|x| debug!("{x:?}"));
dbg!(param)
} results in the following output
Showing that the axum version0.8.1 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
It usually isn't as straightforward to get an extracted parameter as just getting it as an extension. You can look at the implementation to see how You might also want to check |
Beta Was this translation helpful? Give feedback.
Right, in that case I think you need to create the layer with
from_fn
which supports extractors and then I think that you can move the extracted paths to the closure forTraceLayer
.