Skip to content

Commit

Permalink
fix: otel tracing
Browse files Browse the repository at this point in the history
  • Loading branch information
fundon committed Sep 27, 2024
1 parent 002f031 commit 3400440
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 19 deletions.
4 changes: 2 additions & 2 deletions examples/otel/tracing/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
43 changes: 26 additions & 17 deletions viz-core/src/middleware/otel/tracing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand All @@ -26,25 +26,28 @@ use crate::{
/// `OpenTelemetry` tracing config.
#[derive(Debug)]
pub struct Config<T> {
tracer: Arc<T>,
tracer: T,
name: Option<String>,
}

impl<T> Config<T> {
/// Creats new `OpenTelemetry` tracing config.
pub fn new(t: T) -> Self {
Self {
tracer: Arc::new(t),
}
pub fn new(t: T, name: Option<String>) -> Self {
Self { tracer: t, name }
}
}

impl<H, T> Transform<H> for Config<T> {
impl<H, T> Transform<H> for Config<T>
where
T: Clone,
{
type Output = TracingMiddleware<H, T>;

fn transform(&self, h: H) -> Self::Output {
TracingMiddleware {
h,
tracer: self.tracer.clone(),
name: self.name.to_owned().unwrap_or("tracing".to_string()),
}
}
}
Expand All @@ -53,16 +56,18 @@ impl<H, T> Transform<H> for Config<T> {
#[derive(Debug, Clone)]
pub struct TracingMiddleware<H, T> {
h: H,
tracer: Arc<T>,
tracer: T,
name: String,
}

#[crate::async_trait]
impl<H, O, T> Handler<Request> for TracingMiddleware<H, T>
where
H: Handler<Request, Output = Result<O>>,
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,
<T::Tracer as Tracer>::Span: Span + Send + Sync + 'static,
{
type Output = Result<Response>;

Expand All @@ -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![]);

Expand Down

0 comments on commit 3400440

Please sign in to comment.