diff --git a/examples/otel/tracing/src/main.rs b/examples/otel/tracing/src/main.rs index c1e28518..f15e3b25 100644 --- a/examples/otel/tracing/src/main.rs +++ b/examples/otel/tracing/src/main.rs @@ -4,13 +4,13 @@ use opentelemetry::global; use opentelemetry_sdk::{ runtime::TokioCurrentThread, - {propagation::TraceContextPropagator, trace::Tracer}, + {propagation::TraceContextPropagator, trace::TracerProvider}, }; use std::net::SocketAddr; use tokio::net::TcpListener; use viz::{middleware::otel, serve, Request, Result, Router}; -fn init_tracer() -> Tracer { +fn init_tracer() -> TracerProvider { global::set_text_map_propagator(TraceContextPropagator::new()); opentelemetry_otlp::new_pipeline() .tracing() diff --git a/viz-core/src/middleware/otel/tracing.rs b/viz-core/src/middleware/otel/tracing.rs index 2d9b36a7..d53f38c7 100644 --- a/viz-core/src/middleware/otel/tracing.rs +++ b/viz-core/src/middleware/otel/tracing.rs @@ -2,13 +2,13 @@ //! //! [`OpenTelemetry`]: https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/http.md -use std::sync::Arc; - use http::{uri::Scheme, HeaderValue}; use opentelemetry::{ global, propagation::Extractor, - trace::{FutureExt as OtelFutureExt, Span, SpanKind, Status, TraceContextExt, Tracer}, + trace::{ + FutureExt as OtelFutureExt, Span, SpanKind, Status, TraceContextExt, Tracer, TracerProvider, + }, Context, KeyValue, }; use opentelemetry_semantic_conventions::trace::{ @@ -26,25 +26,28 @@ use crate::{ /// `OpenTelemetry` tracing config. #[derive(Debug)] pub struct Config { - tracer: Arc, + tracer: T, + name: Option, } impl Config { /// Creats new `OpenTelemetry` tracing config. - pub fn new(t: T) -> Self { - Self { - tracer: Arc::new(t), - } + pub fn new(t: T, name: Option) -> Self { + Self { tracer: t, name } } } -impl Transform for Config { +impl Transform for Config +where + T: Clone, +{ type Output = TracingMiddleware; fn transform(&self, h: H) -> Self::Output { TracingMiddleware { h, tracer: self.tracer.clone(), + name: self.name.to_owned().unwrap_or("tracing".to_string()), } } } @@ -53,7 +56,8 @@ impl Transform for Config { #[derive(Debug, Clone)] pub struct TracingMiddleware { h: H, - tracer: Arc, + tracer: T, + name: String, } #[crate::async_trait] @@ -61,8 +65,9 @@ impl Handler for TracingMiddleware where H: Handler>, O: IntoResponse, - T: Tracer + Send + Sync + Clone + 'static, - T::Span: Send + Sync + 'static, + T: TracerProvider + Send + Sync + Clone + 'static, + T::Tracer: Tracer + Send + Sync + 'static, + ::Span: Span + Send + Sync + 'static, { type Output = Result; @@ -73,13 +78,17 @@ where let http_route = &req.route_info().pattern; let attributes = build_attributes(&req, http_route.as_str()); - - let mut span = self + let tracer = self .tracer - .span_builder(format!("{} {}", req.method(), http_route)) - .with_kind(SpanKind::Server) + .tracer_builder(self.name.to_owned()) .with_attributes(attributes) - .start_with_context(&*self.tracer, &parent_context); + .build(); + let mut span = tracer.build_with_context( + tracer + .span_builder(format!("{} {}", req.method(), http_route)) + .with_kind(SpanKind::Server), + &parent_context, + ); span.add_event("request.started".to_string(), vec![]);