exercises

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

metrics.rs (1178B)


      1 //! Business-specific metrics for the OtelMart gateway service.
      2 //!
      3 //! Tracks the duration of proxied requests to upstream microservices
      4 //! so operators can monitor backend latency from the gateway's perspective.
      5 //! Uses the `OnceLock` singleton pattern for lazy initialization.
      6 
      7 use opentelemetry::{global, metrics::Histogram};
      8 use std::sync::OnceLock;
      9 
     10 /// Collection of gateway-related metric instruments.
     11 pub struct GatewayMetrics {
     12     /// Duration of requests forwarded to upstream services (seconds)
     13     pub upstream_request_duration: Histogram<f64>,
     14 }
     15 
     16 /// Singleton storage for the metrics instruments.
     17 static METRICS: OnceLock<GatewayMetrics> = OnceLock::new();
     18 
     19 /// Returns a reference to the lazily-initialized gateway metrics.
     20 pub fn metrics() -> &'static GatewayMetrics {
     21     METRICS.get_or_init(|| {
     22         let meter = opentelemetry::global::meter("otelmart-gateway");
     23 
     24         GatewayMetrics {
     25             upstream_request_duration: meter
     26                 .f64_histogram("http.client.request.duration")
     27                 .with_description("Duration of requests to upstream services")
     28                 .with_unit("s")
     29                 .build(),
     30         }
     31     })
     32 }