From df4990d62ec7c1cea06661f68edf287a193a2e90 Mon Sep 17 00:00:00 2001 From: Conrad Ludgate Date: Tue, 15 Nov 2022 17:07:36 +0000 Subject: [PATCH] fix last tracing test --- reqwest-tracing/src/lib.rs | 13 ++++++------ reqwest-tracing/src/middleware.rs | 17 ++++++++------- .../src/reqwest_otel_span_builder.rs | 21 +++++++++++-------- .../src/reqwest_otel_span_macro.rs | 6 +++--- 4 files changed, 32 insertions(+), 25 deletions(-) diff --git a/reqwest-tracing/src/lib.rs b/reqwest-tracing/src/lib.rs index b7bee26..1fd8b0d 100644 --- a/reqwest-tracing/src/lib.rs +++ b/reqwest-tracing/src/lib.rs @@ -62,16 +62,17 @@ //! use tracing::Span; //! use std::time::{Duration, Instant}; //! -//! pub struct TimeTrace; +//! pub struct TimeTrace(Instant); //! //! 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_start(req: &Request, _extension: &mut Extensions) -> (Self, Span) { +//! let now = Self(Instant::now()); +//! let span = reqwest_otel_span!(name="example-request", req, time_elapsed = tracing::field::Empty); +//! (now, span) //! } //! -//! fn on_request_end(span: &Span, outcome: &Result, extension: &mut Extensions) { -//! let time_elapsed = extension.get::().unwrap().elapsed().as_millis() as i64; +//! fn on_request_end(self, span: &Span, outcome: &Result) { +//! let time_elapsed = self.0.elapsed().as_millis() as i64; //! default_on_request_end(span, outcome); //! span.record("time_elapsed", &time_elapsed); //! } diff --git a/reqwest-tracing/src/middleware.rs b/reqwest-tracing/src/middleware.rs index ea69797..762bd32 100644 --- a/reqwest-tracing/src/middleware.rs +++ b/reqwest-tracing/src/middleware.rs @@ -47,14 +47,14 @@ where fn layer(&self, inner: Svc) -> Self::Service { TracingMiddlewareService { service: inner, - layer: *self, + _layer: *self, } } } /// Middleware Service for tracing requests using the current Opentelemetry Context. pub struct TracingMiddlewareService { - layer: TracingMiddleware, + _layer: TracingMiddleware, service: Svc, } @@ -77,7 +77,7 @@ where request, mut extensions, } = req; - let request_span = ReqwestOtelSpan::on_request_start(&request, &mut extensions); + let (backend, span) = ReqwestOtelSpan::on_request_start(&request, &mut extensions); // Adds tracing headers to the given request to propagate the OpenTelemetry context to downstream revivers of the request. // Spans added by downstream consumers will be part of the same trace. #[cfg(any( @@ -96,8 +96,8 @@ where }); TracingMiddlewareFuture { - layer: self.layer, - span: request_span, + span, + backend: Some(backend), future, } } @@ -105,8 +105,8 @@ where pin_project!( pub struct TracingMiddlewareFuture { - layer: TracingMiddleware, span: Span, + backend: Option, #[pin] future: F, } @@ -123,7 +123,10 @@ impl>> Fut let _guard = this.span.enter(); ready!(this.future.poll(cx)) }; - S::on_request_end(this.span, &outcome); + this.backend + .take() + .expect("poll should not be called after completion") + .on_request_end(this.span, &outcome); Poll::Ready(outcome) } } diff --git a/reqwest-tracing/src/reqwest_otel_span_builder.rs b/reqwest-tracing/src/reqwest_otel_span_builder.rs index 934ab5c..91eadab 100644 --- a/reqwest-tracing/src/reqwest_otel_span_builder.rs +++ b/reqwest-tracing/src/reqwest_otel_span_builder.rs @@ -39,12 +39,12 @@ pub const HTTP_USER_AGENT: &str = "http.user_agent"; /// Check out [`reqwest_otel_span`] documentation for examples. /// /// [`TracingMiddleware`]: crate::middleware::TracingMiddleware. -pub trait ReqwestOtelSpanBackend { +pub trait ReqwestOtelSpanBackend: Sized { /// Initalized a new span before the request is executed. - fn on_request_start(req: &Request, extension: &mut Extensions) -> Span; + fn on_request_start(req: &Request, extension: &mut Extensions) -> (Self, Span); /// Runs after the request call has executed. - fn on_request_end(span: &Span, outcome: &Result); + fn on_request_end(self, span: &Span, outcome: &Result); } /// Populates default success/failure fields for a given [`reqwest_otel_span!`] span. @@ -95,15 +95,15 @@ pub fn default_on_request_failure(span: &Span, e: &Error) { pub struct DefaultSpanBackend; impl ReqwestOtelSpanBackend for DefaultSpanBackend { - fn on_request_start(req: &Request, ext: &mut Extensions) -> Span { + fn on_request_start(req: &Request, ext: &mut Extensions) -> (DefaultSpanBackend, Span) { let name = ext .get::() .map(|on| on.0.as_ref()) .unwrap_or("reqwest-http-client"); - reqwest_otel_span!(name = name, req) + (Self, reqwest_otel_span!(name = name, req)) } - fn on_request_end(span: &Span, outcome: &Result) { + fn on_request_end(self, span: &Span, outcome: &Result) { default_on_request_end(span, outcome) } } @@ -119,16 +119,19 @@ fn get_header_value(key: &str, headers: &HeaderMap) -> String { pub struct SpanBackendWithUrl; impl ReqwestOtelSpanBackend for SpanBackendWithUrl { - fn on_request_start(req: &Request, ext: &mut Extensions) -> Span { + fn on_request_start(req: &Request, ext: &mut Extensions) -> (Self, Span) { let name = ext .get::() .map(|on| on.0.as_ref()) .unwrap_or("reqwest-http-client"); - reqwest_otel_span!(name = name, req, http.url = %remove_credentials(req.url())) + ( + Self, + reqwest_otel_span!(name = name, req, http.url = %remove_credentials(req.url())), + ) } - fn on_request_end(span: &Span, outcome: &Result) { + fn on_request_end(self, span: &Span, outcome: &Result) { default_on_request_end(span, outcome) } } diff --git a/reqwest-tracing/src/reqwest_otel_span_macro.rs b/reqwest-tracing/src/reqwest_otel_span_macro.rs index e3312dd..0ce472a 100644 --- a/reqwest-tracing/src/reqwest_otel_span_macro.rs +++ b/reqwest-tracing/src/reqwest_otel_span_macro.rs @@ -41,11 +41,11 @@ /// pub struct CustomReqwestOtelSpanBackend; /// /// impl ReqwestOtelSpanBackend for CustomReqwestOtelSpanBackend { -/// fn on_request_start(req: &Request, _extension: &mut Extensions) -> Span { -/// reqwest_otel_span!(name = "reqwest-http-request", req) +/// fn on_request_start(req: &Request, _extension: &mut Extensions) -> (Self, Span) { +/// (Self, reqwest_otel_span!(name = "reqwest-http-request", req)) /// } /// -/// fn on_request_end(span: &Span, outcome: &Result) { +/// fn on_request_end(self, span: &Span, outcome: &Result) { /// default_on_request_end(span, outcome) /// } /// }