有没有办法将变量绑定到Axum请求?
特别是,我尝试将请求id添加到每个跟踪事件中。我可以用tower::trace中间件这样做:
#[derive(Clone)]
pub struct RequestSpan;
impl<B> tower_http::trace::MakeSpan<B> for RequestSpan {
fn make_span(&mut self, request: &http::Request<B>) -> tracing::Span {
tracing::error_span!(
"rq",
id = %ulid::Ulid::new().to_string(),
method = %request.method(),
uri = %request.uri(),
version = ?request.version(),
)
}
}
...
let middleware_stack = tower::ServiceBuilder::new()
.layer(TraceLayer::new_for_http().make_span_with(RequestSpan))它在服务器范围内工作,但我还需要将请求id传递到外部任务队列中。有什么建议吗?
发布于 2021-09-29 07:42:05
按照rustlang forum的解决方案,我已经把它变成了一个little crate
https://stackoverflow.com/questions/69328888
复制相似问题