2021-08-10 16:07:08 +00:00
|
|
|
# reqwest-tracing
|
|
|
|
|
|
|
|
Opentracing middleware implementation for
|
|
|
|
[`reqwest-middleware`](https://crates.io/crates/reqwest-middleware).
|
|
|
|
|
|
|
|
[![Crates.io](https://img.shields.io/crates/v/reqwest-tracing.svg)](https://crates.io/crates/reqwest-tracing)
|
|
|
|
[![Docs.rs](https://docs.rs/reqwest-tracing/badge.svg)](https://docs.rs/reqwest-tracing)
|
|
|
|
[![CI](https://github.com/TrueLayer/reqwest-middleware/workflows/CI/badge.svg)](https://github.com/TrueLayer/reqwest-middleware/actions)
|
|
|
|
[![Coverage Status](https://coveralls.io/repos/github/TrueLayer/reqwest-middleware/badge.svg?branch=main&t=UWgSpm)](https://coveralls.io/github/TrueLayer/reqwest-middleware?branch=main)
|
|
|
|
|
|
|
|
## Overview
|
|
|
|
|
|
|
|
Attach `TracingMiddleware` to your client to automatically trace HTTP requests:
|
|
|
|
|
2021-09-28 18:26:03 +00:00
|
|
|
```toml
|
|
|
|
# Cargo.toml
|
|
|
|
# ...
|
|
|
|
[dependencies]
|
2022-09-20 13:07:32 +00:00
|
|
|
opentelemetry = "0.18"
|
2021-09-28 18:26:03 +00:00
|
|
|
reqwest = "0.11"
|
|
|
|
reqwest-middleware = "0.1.1"
|
|
|
|
reqwest-retry = "0.1.1"
|
2022-09-20 13:07:32 +00:00
|
|
|
reqwest-tracing = { version = "0.3.1", features = ["opentelemetry_0_18"] }
|
2021-09-28 18:26:03 +00:00
|
|
|
tokio = { version = "1.12.0", features = ["macros", "rt-multi-thread"] }
|
|
|
|
tracing = "0.1"
|
2022-09-20 13:07:32 +00:00
|
|
|
tracing-opentelemetry = "0.18"
|
2022-04-21 17:38:15 +00:00
|
|
|
tracing-subscriber = "0.3"
|
2022-10-05 13:56:46 +00:00
|
|
|
task-local-extensions = "0.1.0"
|
2021-09-28 18:26:03 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
```rust,skip
|
2022-06-30 13:54:54 +00:00
|
|
|
use reqwest_tracing::{default_on_request_end, reqwest_otel_span, ReqwestOtelSpanBackend, TracingMiddleware};
|
2021-09-28 18:26:03 +00:00
|
|
|
use opentelemetry::sdk::export::trace::stdout;
|
2022-06-30 13:54:54 +00:00
|
|
|
use reqwest::{Request, Response};
|
|
|
|
use reqwest_middleware::{ClientBuilder, Result};
|
|
|
|
use std::time::Instant;
|
|
|
|
use task_local_extensions::Extensions;
|
|
|
|
use tracing::Span;
|
2021-08-10 16:07:08 +00:00
|
|
|
use tracing_subscriber::layer::SubscriberExt;
|
|
|
|
use tracing_subscriber::Registry;
|
|
|
|
|
2022-06-30 13:54:54 +00:00
|
|
|
pub struct TimeTrace;
|
|
|
|
|
|
|
|
impl ReqwestOtelSpanBackend for TimeTrace {
|
|
|
|
fn on_request_start(req: &Request, extension: &mut Extensions) -> Span {
|
|
|
|
extension.insert(Instant::now());
|
2022-11-23 07:39:54 +00:00
|
|
|
reqwest_otel_span!(name="example-request", req, time_elapsed = tracing::field::Empty)
|
2022-06-30 13:54:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-10 16:07:08 +00:00
|
|
|
#[tokio::main]
|
|
|
|
async fn main() {
|
2022-06-30 13:54:54 +00:00
|
|
|
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();
|
2021-08-10 16:07:08 +00:00
|
|
|
|
2022-06-30 13:54:54 +00:00
|
|
|
run().await;
|
2021-08-10 16:07:08 +00:00
|
|
|
}
|
|
|
|
|
2021-09-28 18:26:03 +00:00
|
|
|
async fn run() {
|
2022-06-30 13:54:54 +00:00
|
|
|
let client = ClientBuilder::new(reqwest::Client::new())
|
|
|
|
.with(TracingMiddleware::<TimeTrace>::new())
|
|
|
|
.build();
|
2021-08-10 16:07:08 +00:00
|
|
|
|
2022-06-30 13:54:54 +00:00
|
|
|
client.get("https://truelayer.com").send().await.unwrap();
|
2021-08-10 16:07:08 +00:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2021-09-28 18:26:03 +00:00
|
|
|
```terminal
|
|
|
|
$ cargo run
|
|
|
|
SpanData { span_context: SpanContext { trace_id: ...
|
|
|
|
```
|
|
|
|
|
2021-08-10 16:07:08 +00:00
|
|
|
See the [`tracing`](https://crates.io/crates/tracing) crate for more information on how to set up a
|
|
|
|
tracing subscriber to make use of the spans.
|
|
|
|
|
|
|
|
## How to install
|
|
|
|
|
|
|
|
Add `reqwest-tracing` to your dependencies. Optionally enable opentelemetry integration by enabling
|
|
|
|
an opentelemetry version feature:
|
|
|
|
|
|
|
|
```toml
|
|
|
|
[dependencies]
|
|
|
|
# ...
|
2022-09-20 13:07:32 +00:00
|
|
|
reqwest-tracing = { version = "0.3.1", features = ["opentelemetry_0_18"] }
|
2021-08-10 16:07:08 +00:00
|
|
|
```
|
|
|
|
|
2022-09-20 13:07:32 +00:00
|
|
|
Available opentelemetry features are `opentelemetry_0_18`, `opentelemetry_0_17`, `opentelemetry_0_16`, `opentelemetry_0_15`, `opentelemetry_0_14` and
|
2021-08-10 16:07:08 +00:00
|
|
|
`opentelemetry_0_13`.
|
|
|
|
|
|
|
|
#### License
|
|
|
|
|
|
|
|
<sup>
|
|
|
|
Licensed under either of <a href="LICENSE-APACHE">Apache License, Version
|
|
|
|
2.0</a> or <a href="LICENSE-MIT">MIT license</a> at your option.
|
|
|
|
</sup>
|
|
|
|
|
|
|
|
<br>
|
|
|
|
|
|
|
|
<sub>
|
|
|
|
Unless you explicitly state otherwise, any contribution intentionally submitted
|
|
|
|
for inclusion in the work by you, as defined in the Apache-2.0 license, shall be
|
|
|
|
dual licensed as above, without any additional terms or conditions.
|
|
|
|
</sub>
|