【问题标题】:How to add tracing to a Rust microservice?如何向 Rust 微服务添加跟踪?
【发布时间】:2021-08-29 10:58:58
【问题描述】:

我用 Rust 构建了一个微服务。我接收消息,根据消息请求文档,并使用结果调用 REST api。我使用 warp 构建了 REST api,并使用 reqwest 发送结果。我们使用 jaeger 进行跟踪和“b3”格式。我没有跟踪经验,并且是 Rust 初学者。

问题:我需要在下面添加 warp / reqwest 源以传播跟踪信息并添加我自己的跨度吗?

我的版本端点(为简单起见)如下所示:

pub async fn version() -> Result<impl warp::Reply, Infallible> {
    Ok(warp::reply::with_status(VERSION, http::StatusCode::OK))
}

我假设我必须提取例如这里的traceid/trace信息。

我做的 reqwest 调用如下所示:

pub async fn get_document_content_as_text(
    account_id: &str,
    hash: &str,
) -> Result<String, Box<dyn std::error::Error>> {

    let client = reqwest::Client::builder().build()?;
    let res = client
        .get(url)
        .bearer_auth(TOKEN)
        .send()
        .await?;
    if res.status().is_success() {}
    let text = res.text().await?;
    Ok(text)
}

我假设我必须在此处添加 traceid / 跟踪信息。

【问题讨论】:

    标签: rust microservices jaeger distributed-tracing


    【解决方案1】:

    您需要在 warp 过滤器管道中添加一个跟踪过滤器。

    来自documentation 示例:

    use warp::Filter;
    
    let route = warp::any()
        .map(warp::reply)
        .with(warp::trace(|info| {
            // Create a span using tracing macros
            tracing::info_span!(
                "request",
                method = %info.method(),
                path = %info.path(),
            )
        }));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-09
      • 1970-01-01
      • 2017-08-06
      • 2018-06-04
      • 2016-11-24
      • 1970-01-01
      • 2020-01-13
      • 1970-01-01
      相关资源
      最近更新 更多