Skip to main content

kindly_guard_server/metrics/
mod.rs

1// Copyright 2025 Kindly Software Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//! Metrics collection and export for monitoring
15//! Provides trait-based abstraction for different implementations
16
17pub mod enhanced_interface;
18pub mod standard;
19
20use self::standard::StandardMetricsProvider;
21use crate::config::Config;
22use crate::traits::MetricsProvider;
23use std::sync::Arc;
24
25// Re-export the standard implementation for compatibility
26pub use standard::StandardMetricsProvider as MetricsRegistry;
27
28// Re-export traits for convenience
29pub use crate::traits::{CounterTrait, GaugeTrait, HistogramStats, HistogramTrait};
30
31/// Create a metrics provider based on configuration
32#[allow(unused_variables)]
33pub fn create_metrics_provider(config: &Config) -> Arc<dyn MetricsProvider> {
34    #[cfg(feature = "enhanced")]
35    {
36        if config.is_event_processor_enabled() {
37            // Try to use enhanced implementation
38            if let Some(provider) = try_create_enhanced_provider() {
39                tracing::info!(
40                    target: "metrics.init",
41                    mode = "enhanced",
42                    "Initializing enhanced metrics provider"
43                );
44                return provider;
45            }
46        }
47    }
48
49    tracing::info!(
50        target: "metrics.init",
51        mode = "standard",
52        "Initializing standard metrics provider"
53    );
54
55    Arc::new(StandardMetricsProvider::new())
56}
57
58#[cfg(feature = "enhanced")]
59fn try_create_enhanced_provider() -> Option<Arc<dyn MetricsProvider>> {
60    // This would load enhanced implementation if available
61    // For now, return None to use standard implementation
62    None
63}
64
65/// Timer for measuring durations
66pub struct Timer {
67    histogram: Arc<dyn HistogramTrait>,
68    start: std::time::Instant,
69}
70
71impl Timer {
72    /// Create a new timer
73    pub fn new(histogram: Arc<dyn HistogramTrait>) -> Self {
74        Self {
75            histogram,
76            start: std::time::Instant::now(),
77        }
78    }
79
80    /// Stop the timer and record the duration
81    pub fn stop(self) {
82        let duration = self.start.elapsed();
83        self.histogram.observe(duration.as_secs_f64());
84    }
85}
86
87impl Drop for Timer {
88    fn drop(&mut self) {
89        let duration = self.start.elapsed();
90        self.histogram.observe(duration.as_secs_f64());
91    }
92}