reqwest-middleware/reqwest-tracing
Alex Wakefield 3457bf5702
Add `OtelPathNames` for span names (#89)
* feat: add `OtelPathNames` for span names

If this extension is provided span names will be `<method> <path name>`.
These path names will include parameter names rather than IDs or other elements that would increase the cardinality.

* doc: update changelog

* refactor: update `known_paths` to return error

* refactor: return `anyhow::Error` instead

This is as `reqwest_middleware::Error` is more focused on handling a
request.
2023-04-26 15:28:32 +01:00
..
src Add `OtelPathNames` for span names (#89) 2023-04-26 15:28:32 +01:00
CHANGELOG.md bump all versions (#71) 2022-11-15 10:31:34 +00:00
Cargo.toml Add `OtelPathNames` for span names (#89) 2023-04-26 15:28:32 +01:00
README.md feat: wasm32-unknown-unknown support (#79) 2023-03-09 11:33:13 +00:00

README.md

reqwest-tracing

Opentracing middleware implementation for reqwest-middleware.

Crates.io Docs.rs CI Coverage Status

Overview

Attach TracingMiddleware to your client to automatically trace HTTP requests:

# Cargo.toml
# ...
[dependencies]
opentelemetry = "0.18"
reqwest = "0.11"
reqwest-middleware = "0.1.1"
reqwest-retry = "0.1.1"
reqwest-tracing = { version = "0.3.1", features = ["opentelemetry_0_18"] }
tokio = { version = "1.12.0", features = ["macros", "rt-multi-thread"] }
tracing = "0.1"
tracing-opentelemetry = "0.18"
tracing-subscriber = "0.3"
task-local-extensions = "0.1.4"
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 task_local_extensions::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: ...

See the 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:

[dependencies]
# ...
reqwest-tracing = { version = "0.3.1", features = ["opentelemetry_0_18"] }

Available opentelemetry features are opentelemetry_0_18, opentelemetry_0_17, opentelemetry_0_16, opentelemetry_0_15, opentelemetry_0_14 and opentelemetry_0_13.

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.
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.