rust - Rust axum / hyper 请求局部变量
问题描述
有没有办法将变量绑定到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 传递到外部任务队列中。有什么建议么?
解决方案
在rustlang 论坛的解决方案之后,我将它变成了一个小板条箱
推荐阅读
- r - R 脚本在本地机器上成功运行,而不是在 EC2 实例上
- python - Python - 在 CSV 或 XLS 中匹配的正则表达式,并返回匹配的位置
- java - Android Studio androidx.coordinatorlayout error and how to set up 3Dot Menu?
- string - Oracle SQL:我应该如何识别字符串的长度?
- flash - Adobe Air 错误 #3001:文件或目录访问被拒绝
- python - Python中的偏相关
- docker - 如何在离线环境中同时使用 swagger-editor 和 swagger-codegen docker 镜像
- r - geom_dot 为不同的组提供不同的点大小并将一个放在前面
- r - R - 从现有列值创建和命名数据框
- android - 用 constrainLayout 替换滚动视图中的加权线性布局