首页 > 解决方案 > 如何向 Rust 微服务添加跟踪?

问题描述

我用 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 / 跟踪信息。

标签: rustmicroservicesjaegerdistributed-tracing

解决方案


您需要将跟踪过滤器添加到扭曲过滤器管道中。

文档示例中:

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(),
        )
    }));

推荐阅读