Expand description
Opentracing middleware implementation for reqwest_middleware
.
Attach TracingMiddleware
to your client to automatically trace HTTP requests.
The simplest possible usage:
use reqwest_middleware::{ClientBuilder};
use reqwest_tracing::TracingMiddleware;
let reqwest_client = reqwest::Client::builder().build().unwrap();
let client = ClientBuilder::new(reqwest_client)
// Insert the tracing middleware
.with(TracingMiddleware::default())
.build();
let resp = client.get("https://truelayer.com").send().await.unwrap();
To customise the span names use OtelName
.
use reqwest_middleware::{ClientBuilder, Extension};
use reqwest_tracing::{
TracingMiddleware, OtelName
};
let reqwest_client = reqwest::Client::builder().build().unwrap();
let client = ClientBuilder::new(reqwest_client)
// Inserts the extension before the request is started
.with_init(Extension(OtelName("my-client".into())))
// Makes use of that extension to specify the otel name
.with(TracingMiddleware::default())
.build();
let resp = client.get("https://truelayer.com").send().await.unwrap();
// Or specify it on the individual request (will take priority)
let resp = client.post("https://api.truelayer.com/payment")
.with_extension(OtelName("POST /payment".into()))
.send()
.await
.unwrap();
In this example we define a custom span builder to calculate the request time elapsed and we register the TracingMiddleware
.
Note that Opentelemetry tracks start and stop already, there is no need to have a custom builder like this.
use reqwest_middleware::Result;
use http::Extensions;
use reqwest::{Request, Response};
use reqwest_middleware::ClientBuilder;
use reqwest_tracing::{
default_on_request_end, reqwest_otel_span, ReqwestOtelSpanBackend, TracingMiddleware
};
use tracing::Span;
use std::time::{Duration, Instant};
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);
}
}
let http = ClientBuilder::new(reqwest::Client::new())
.with(TracingMiddleware::<TimeTrace>::new())
.build();
Macros§
- reqwest_
otel_ span reqwest_otel_span!
creates a newtracing::Span
. It empowers you to add custom properties to the span on top of the default properties provided by the macro
Structs§
- Default
Span Backend - The default
ReqwestOtelSpanBackend
forTracingMiddleware
. Note that it doesn’t include theurl.full
field in spans, you can useSpanBackendWithUrl
to add it. - Disable
Otel Propagation DisableOtelPropagation
disables opentelemetry header propagation, while still tracing the HTTP request.- Otel
Name OtelName
allows customisation of the name of the spans created byDefaultSpanBackend
andSpanBackendWithUrl
.- Otel
Path Names OtelPathNames
allows including templated paths in the spans created byDefaultSpanBackend
andSpanBackendWithUrl
.- Span
Backend With Url - Similar to
DefaultSpanBackend
but also adds theurl.full
attribute to request spans. - Tracing
Middleware - Middleware for tracing requests using the current Opentelemetry Context.
Constants§
- ERROR_
CAUSE_ CHAIN - The
error.cause_chain
field added to the span byreqwest_otel_span
- ERROR_
MESSAGE - The
error.message
field added to the span byreqwest_otel_span
- HTTP_
REQUEST_ METHOD - The
http.request.method
field added to the span byreqwest_otel_span
- HTTP_
RESPONSE_ STATUS_ CODE - The
http.response.status_code
field added to the span byreqwest_otel_span
- OTEL_
KIND - The
otel.kind
field added to the span byreqwest_otel_span
- OTEL_
NAME - The
otel.name
field added to the span byreqwest_otel_span
- OTEL_
STATUS_ CODE - The
otel.status_code
field added to the span byreqwest_otel_span
- SERVER_
ADDRESS - The
server.address
field added to the span byreqwest_otel_span
- SERVER_
PORT - The
server.port
field added to the span byreqwest_otel_span
- URL_
FULL - The
url.full
field added to the span byreqwest_otel_span
- URL_
SCHEME - The
url.scheme
field added to the span byreqwest_otel_span
- USER_
AGENT_ ORIGINAL - The
user_agent.original
field added to the span byreqwest_otel_span
Traits§
- Reqwest
Otel Span Backend ReqwestOtelSpanBackend
allows you to customise the span attached byTracingMiddleware
to incoming requests.
Functions§
- default_
on_ request_ end - Populates default success/failure fields for a given
reqwest_otel_span!
span. - default_
on_ request_ failure - Populates default failure fields for a given
reqwest_otel_span!
span. - default_
on_ request_ success - Populates default success fields for a given
reqwest_otel_span!
span. - default_
span_ name - Determine the name of the span that should be associated with this request.