lxy 0.1.1

A convenient async http and RPC framework in Rust
Documentation
//! Common tracing layer structure for OpenTelemetry.
//!
//! This module provides the base tracing layer and service that are implemented
//! by protocol-specific modules (HTTP, gRPC).

use std::{future::Future, time::Instant};

use tower::Layer;

/// Generic tower layer for OpenTelemetry tracing.
///
/// Protocol-specific implementations of the Service trait are in:
/// - `http::middlewares::tracing_layer` for HTTP
/// - `grpc::middlewares::tracing_layer` for gRPC
#[derive(Clone, Default)]
pub struct TracingLayer;

impl<S> Layer<S> for TracingLayer {
  type Service = TracingService<S>;

  fn layer(&self, inner: S) -> Self::Service {
    TracingService { inner }
  }
}

/// Generic service wrapper for tracing.
///
/// Protocol-specific Service implementations are in their respective modules.
#[derive(Clone)]
pub struct TracingService<S> {
  pub inner: S,
}

/// Helper function to execute a future with timing.
///
/// Returns a tuple of (result, latency_in_milliseconds).
pub(crate) async fn with_timing<F, T>(fut: F) -> (T, i64)
where
  F: Future<Output = T>,
{
  let start = Instant::now();
  let result = fut.await;
  let latency_ms = start.elapsed().as_millis() as i64;
  (result, latency_ms)
}