gitdataai/libs/observability/src/tracing_middleware.rs
ZhenYi b4024aa690 feat(observability): Phase 6 OTLP tracing + Prometheus /metrics endpoint
- Add HTTP OTLP exporter (opentelemetry-otlp 0.31) via SdkTracerProvider +
  BatchSpanProcessor + tracing_opentelemetry layer
- Add Prometheus /metrics handler via metrics-exporter-prometheus 0.13
- Replace slog with tracing throughout: HttpMetrics, TracingSpanMiddleware
- Replace .init() with .try_init() to allow OTLP layer registration after
  init_tracing_subscriber()
- otlp.rs: SpanExporter::builder().with_http().with_endpoint(),
  Resource::builder().with_service_name(), .with_attribute(KeyValue::new(...))
- prometheus_exporter.rs: install_recorder(), prometheus_handler(),
  spawn_http_metrics_poller()
2026-04-21 22:28:15 +08:00

84 lines
2.5 KiB
Rust

//! Actix-web middleware that creates a `tracing::Span` for each HTTP request.
//!
//! The span is set as the current context so all downstream async code is
//! automatically instrumented. When the `tracing_opentelemetry` layer is
//! active, spans are exported via OTLP.
use actix_web::dev::{Service, ServiceRequest, ServiceResponse, Transform};
use std::sync::Arc;
use std::task::{Context, Poll};
/// Actix-web middleware that creates a tracing span per request.
pub struct TracingSpanMiddleware;
impl TracingSpanMiddleware {
pub fn new() -> Self {
Self
}
}
impl Default for TracingSpanMiddleware {
fn default() -> Self {
Self::new()
}
}
impl<S, B> Transform<S, ServiceRequest> for TracingSpanMiddleware
where
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = actix_web::Error> + 'static,
S::Future: 'static,
B: 'static,
{
type Response = ServiceResponse<B>;
type Error = actix_web::Error;
type Transform = TracingSpanMiddlewareService<S>;
type InitError = ();
type Future = std::future::Ready<Result<Self::Transform, Self::InitError>>;
fn new_transform(&self, service: S) -> Self::Future {
std::future::ready(Ok(TracingSpanMiddlewareService {
service: Arc::new(service),
}))
}
}
pub struct TracingSpanMiddlewareService<S> {
service: Arc<S>,
}
impl<S> Clone for TracingSpanMiddlewareService<S> {
fn clone(&self) -> Self {
Self {
service: self.service.clone(),
}
}
}
impl<S, B> Service<ServiceRequest> for TracingSpanMiddlewareService<S>
where
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = actix_web::Error> + 'static,
S::Future: 'static,
B: 'static,
{
type Response = ServiceResponse<B>;
type Error = actix_web::Error;
type Future = std::pin::Pin<Box<dyn std::future::Future<Output = Result<Self::Response, Self::Error>> + 'static>>;
fn poll_ready(&self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
self.service.poll_ready(cx)
}
fn call(&self, req: ServiceRequest) -> Self::Future {
let method = req.method().to_string();
let path = req.path().to_string();
let service = self.service.clone();
Box::pin(async move {
let span = tracing::info_span!("HTTP {method} {path}", method = %method, path = %path);
let _guard = span.enter();
let res = service.call(req).await?;
Ok(res)
})
}
}