23 个版本
0.5.3 | 2024 年 8 月 9 日 |
---|---|
0.5.2 | 2024 年 7 月 15 日 |
0.5.1 | 2024 年 6 月 28 日 |
0.4.8 | 2024 年 3 月 15 日 |
0.2.0 | 2021 年 11 月 30 日 |
#133 在 HTTP 客户端
每月 246,085 次下载
用于 74 个 Crates(46 个直接使用)
95KB
1K SLoC
reqwest-tracing
Opentracing 中间件实现,用于 reqwest-middleware
。
概述
将 TracingMiddleware
附加到您的客户端,以自动跟踪 HTTP 请求
# Cargo.toml
# ...
[dependencies]
opentelemetry = "0.22"
reqwest = { version = "0.12", features = ["rustls-tls"] }
reqwest-middleware = "0.3"
reqwest-retry = "0.5"
reqwest-tracing = { version = "0.5", features = ["opentelemetry_0_22"] }
tokio = { version = "1.12.0", features = ["macros", "rt-multi-thread"] }
tracing = "0.1"
tracing-opentelemetry = "0.23"
tracing-subscriber = "0.3"
use reqwest_tracing::{default_on_request_end, reqwest_otel_span, ReqwestOtelSpanBackend, TracingMiddleware};
use opentelemetry::sdk::export::trace::stdout;
use reqwest::{Request, Response};
use reqwest_middleware::{ClientBuilder, Result};
use std::time::Instant;
use http::Extensions;
use tracing::Span;
use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::Registry;
pub struct TimeTrace;
impl ReqwestOtelSpanBackend for TimeTrace {
fn on_request_start(req: &Request, extension: &mut Extensions) -> Span {
extension.insert(Instant::now());
reqwest_otel_span!(name="example-request", req, time_elapsed = tracing::field::Empty)
}
fn on_request_end(span: &Span, outcome: &Result<Response>, extension: &mut Extensions) {
let time_elapsed = extension.get::<Instant>().unwrap().elapsed().as_millis() as i64;
default_on_request_end(span, outcome);
span.record("time_elapsed", &time_elapsed);
}
}
#[tokio::main]
async fn main() {
let tracer = stdout::new_pipeline().install_simple();
let telemetry = tracing_opentelemetry::layer().with_tracer(tracer);
let subscriber = Registry::default().with(telemetry);
tracing::subscriber::set_global_default(subscriber).unwrap();
run().await;
}
async fn run() {
let client = ClientBuilder::new(reqwest::Client::new())
.with(TracingMiddleware::<TimeTrace>::new())
.build();
client.get("https://truelayer.com").send().await.unwrap();
}
$ cargo run
SpanData { span_context: SpanContext { trace_id: ...
有关如何设置跟踪订阅者以使用范围的信息,请参阅 tracing
crate。
如何安装
将 reqwest-tracing
添加到您的依赖项中。可选地通过启用 opentelemetry 版本功能启用 opentelemetry 集成
[dependencies]
# ...
reqwest-tracing = { version = "0.5.0", features = ["opentelemetry_0_22"] }
可用的 opentelemetry 功能有 opentelemetry_0_22
,opentelemetry_0_21
,和 opentelemetry_0_20
。
许可证
根据您的选择,受Apache License,版本 2.0或MIT 许可证约束。除非您明确声明,否则根据 Apache-2.0 许可证定义,您有意提交的任何贡献,均应按上述方式双重许可,而无需任何其他条款或条件。
依赖项
~4–16MB
~225K SLoC