From a38d802fa7dcc4abf9ffed39ee504b31f99e51a6 Mon Sep 17 00:00:00 2001 From: Rajasekharan Vengalil Date: Tue, 14 Jan 2020 16:55:42 -0800 Subject: [PATCH] Address comments --- actix-tracing/src/lib.rs | 66 +++++++++++----------------------------- 1 file changed, 17 insertions(+), 49 deletions(-) diff --git a/actix-tracing/src/lib.rs b/actix-tracing/src/lib.rs index bcda4537..d3bfaf0c 100644 --- a/actix-tracing/src/lib.rs +++ b/actix-tracing/src/lib.rs @@ -8,56 +8,24 @@ use actix_service::{apply, IntoServiceFactory, Service, ServiceFactory, Transfor use futures_util::future::{ok, Either, Ready}; use tracing_futures::{Instrument, Instrumented}; -/// Trait representing a type that is able to produce spans for a given value. -/// -/// The generic argument `T` is typically the request type of a `Service` -/// implementation. -pub trait MakeSpan { - /// Creates a new span for the given request value. - /// - /// By default, this function returns `None`. - fn span_for(&self, _req: &T) -> Option { - None - } -} - -impl MakeSpan for F -where - F: Fn(&T) -> Option, -{ - fn span_for(&self, req: &T) -> Option { - (self)(req) - } -} - -impl MakeSpan for tracing::Span { - fn span_for(&self, _: &T) -> Option { - Some(self.clone()) - } -} - /// A `Service` implementation that automatically enters/exits tracing spans /// for the wrapped inner service. #[derive(Clone)] -pub struct TracingService { +pub struct TracingService { inner: S, - make_span: T, + make_span: F, } -impl TracingService -where - S: Service, - T: MakeSpan, -{ - pub fn new(inner: S, make_span: T) -> Self { +impl TracingService { + pub fn new(inner: S, make_span: F) -> Self { TracingService { inner, make_span } } } -impl Service for TracingService +impl Service for TracingService where S: Service, - T: MakeSpan, + F: Fn(&S::Request) -> Option, { type Request = S::Request; type Response = S::Response; @@ -69,7 +37,7 @@ where } fn call(&mut self, req: Self::Request) -> Self::Future { - let span = self.make_span.span_for(&req); + let span = (self.make_span)(&req); let _enter = span.as_ref().map(|s| s.enter()); let fut = self.inner.call(req); @@ -89,13 +57,13 @@ where /// A `Transform` implementation that wraps services with a [`TracingService`]. /// /// [`TracingService`]: struct.TracingService.html -pub struct TracingTransform { - make_span: T, +pub struct TracingTransform { + make_span: F, _p: PhantomData, } -impl TracingTransform { - pub fn new(make_span: T) -> Self { +impl TracingTransform { + pub fn new(make_span: F) -> Self { TracingTransform { make_span, _p: PhantomData, @@ -103,7 +71,7 @@ impl TracingTransform { } } -impl Transform for TracingTransform +impl Transform for TracingTransform where S: Service, U: ServiceFactory< @@ -112,13 +80,13 @@ where Error = S::Error, Service = S, >, - T: MakeSpan + Clone, + F: Fn(&S::Request) -> Option + Clone, { type Request = S::Request; type Response = S::Response; type Error = S::Error; + type Transform = TracingService; type InitError = U::InitError; - type Transform = TracingService; type Future = Ready>; fn new_transform(&self, service: S) -> Self::Future { @@ -144,9 +112,9 @@ where /// ``` /// /// [`MakeSpan`]: trait.MakeSpan.html -pub fn trace( +pub fn trace( service_factory: U, - make_span: T, + make_span: F, ) -> impl ServiceFactory< Config = S::Config, Request = S::Request, @@ -156,7 +124,7 @@ pub fn trace( > where S: ServiceFactory, - T: MakeSpan + Clone, + F: Fn(&S::Request) -> Option + Clone, U: IntoServiceFactory, { apply(