avx-telemetry 0.1.0

Observability and distributed tracing for Avila Experience Fabric - Structured logging, metrics, and scientific time series
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
/// Middleware para coleta automática de métricas de latência
///
/// Este módulo implementa middleware Axum para tracking automático de latências
/// de requisições HTTP, detecção de anomalias em tempo real e integração com
/// o sistema de telemetria AVX.
///
/// # Casos de Uso
/// - Coletar latências de todas as requisições automaticamente
/// - Detectar anomalias em tempo real (ex: latência > threshold)
/// - Exportar métricas para AvilaDB periodicamente
/// - Rastreamento distribuído com OpenTelemetry
///
/// # Exemplo
/// ```rust,no_run
/// use axum::{Router, routing::get};
/// use avx_telemetry::middleware::LatencyMiddleware;
///
/// #[tokio::main]
/// async fn main() {
///     let middleware = LatencyMiddleware::new("avx-gateway");
///
///     let app = Router::new()
///         .route("/health", get(health_check))
///         .layer(middleware.into_layer());
///
///     // Server initialization...
/// }
///
/// async fn health_check() -> &'static str {
///     "OK"
/// }
/// ```
use axum::{body::Body, extract::Request, middleware::Next, response::Response};
use std::future::Future;
use std::pin::Pin;
use std::sync::{Arc, Mutex};
use std::task::{Context, Poll};
use std::time::Instant;
use tower::{Layer, Service};

use crate::AvxMetrics;

/// Collector de latências em memória
///
/// Mantém um buffer circular das últimas N latências para análise em tempo real
/// e detecção de anomalias.
#[derive(Clone, Debug)]
pub struct LatencyCollector {
    /// Nome do serviço
    service_name: String,

    /// Buffer de latências (em millisegundos)
    latencies: Arc<Mutex<Vec<f64>>>,

    /// Tamanho máximo do buffer
    max_size: usize,

    /// AvxMetrics para detecção de anomalias
    metrics: Arc<AvxMetrics>,
}

impl LatencyCollector {
    /// Cria um novo collector
    ///
    /// # Argumentos
    /// * `service_name` - Nome do serviço
    /// * `max_size` - Tamanho máximo do buffer (default: 1000)
    pub fn new(service_name: impl Into<String>) -> Self {
        Self::with_capacity(service_name, 1000)
    }

    /// Cria um novo collector com capacidade específica
    pub fn with_capacity(service_name: impl Into<String>, max_size: usize) -> Self {
        Self {
            service_name: service_name.into(),
            latencies: Arc::new(Mutex::new(Vec::with_capacity(max_size))),
            max_size,
            metrics: Arc::new(AvxMetrics::new()),
        }
    }

    /// Registra uma latência
    ///
    /// Se o buffer estiver cheio, remove os 10% mais antigos
    pub fn record(&self, latency_ms: f64) {
        if let Ok(mut latencies) = self.latencies.lock() {
            latencies.push(latency_ms);

            // Manter buffer circular
            if latencies.len() > self.max_size {
                let drain_count = self.max_size / 10; // Remove 10%
                latencies.drain(0..drain_count);
            }

            // Log se latência estiver muito alta
            if latency_ms > 1000.0 {
                tracing::warn!(
                    service = %self.service_name,
                    latency_ms = latency_ms,
                    "High latency detected"
                );
            }
        }
    }

    /// Retorna snapshot atual das latências
    pub fn snapshot(&self) -> Vec<f64> {
        self.latencies.lock().map(|l| l.clone()).unwrap_or_default()
    }

    /// Retorna estatísticas das latências
    pub fn statistics(&self) -> LatencyStatistics {
        let latencies = self.snapshot();

        if latencies.is_empty() {
            return LatencyStatistics::default();
        }

        let mut sorted = latencies.clone();
        sorted.sort_by(|a, b| a.partial_cmp(b).unwrap());

        let sum: f64 = latencies.iter().sum();
        let mean = sum / latencies.len() as f64;

        let variance: f64 =
            latencies.iter().map(|x| (x - mean).powi(2)).sum::<f64>() / latencies.len() as f64;
        let std_dev = variance.sqrt();

        LatencyStatistics {
            count: latencies.len(),
            mean_ms: mean,
            std_dev_ms: std_dev,
            min_ms: *sorted.first().unwrap(),
            max_ms: *sorted.last().unwrap(),
            p50_ms: percentile(&sorted, 0.50),
            p95_ms: percentile(&sorted, 0.95),
            p99_ms: percentile(&sorted, 0.99),
        }
    }

    /// Detecta anomalias nas latências recentes
    ///
    /// Usa o AvxMetrics para detecção via Z-score e IQR
    pub fn detect_anomalies(&self) -> Result<Vec<crate::Anomaly>, crate::TelemetryError> {
        let latencies = self.snapshot();

        if latencies.is_empty() {
            return Ok(vec![]);
        }

        self.metrics.track_latencies(latencies)
    }

    /// Limpa o buffer de latências
    pub fn clear(&self) {
        if let Ok(mut latencies) = self.latencies.lock() {
            latencies.clear();
        }
    }

    /// Retorna o nome do serviço
    pub fn service_name(&self) -> &str {
        &self.service_name
    }
}

/// Estatísticas de latência
#[derive(Debug, Clone, Default)]
pub struct LatencyStatistics {
    pub count: usize,
    pub mean_ms: f64,
    pub std_dev_ms: f64,
    pub min_ms: f64,
    pub max_ms: f64,
    pub p50_ms: f64,
    pub p95_ms: f64,
    pub p99_ms: f64,
}

impl LatencyStatistics {
    /// Verifica se as latências estão saudáveis
    pub fn is_healthy(&self, p99_threshold_ms: f64) -> bool {
        self.p99_ms <= p99_threshold_ms
    }

    /// Retorna JSON com as estatísticas
    pub fn to_json(&self) -> serde_json::Value {
        serde_json::json!({
            "count": self.count,
            "mean_ms": self.mean_ms,
            "std_dev_ms": self.std_dev_ms,
            "min_ms": self.min_ms,
            "max_ms": self.max_ms,
            "percentiles": {
                "p50": self.p50_ms,
                "p95": self.p95_ms,
                "p99": self.p99_ms,
            }
        })
    }
}

/// Middleware Axum para tracking de latência
///
/// # Exemplo
/// ```rust,no_run
/// use axum::Router;
/// use avx_telemetry::middleware::LatencyMiddleware;
///
/// let middleware = LatencyMiddleware::new("my-service");
/// let app = Router::new()
///     .layer(middleware.into_layer());
/// ```
#[derive(Clone)]
pub struct LatencyMiddleware {
    collector: LatencyCollector,
}

impl LatencyMiddleware {
    /// Cria um novo middleware
    pub fn new(service_name: impl Into<String>) -> Self {
        Self {
            collector: LatencyCollector::new(service_name),
        }
    }

    /// Cria um novo middleware com collector customizado
    pub fn with_collector(collector: LatencyCollector) -> Self {
        Self { collector }
    }

    /// Retorna referência ao collector
    pub fn collector(&self) -> &LatencyCollector {
        &self.collector
    }

    /// Converte para Layer do Tower
    pub fn into_layer(self) -> LatencyLayer {
        LatencyLayer {
            collector: self.collector,
        }
    }
}

/// Layer do Tower para integração com Axum
#[derive(Clone)]
pub struct LatencyLayer {
    collector: LatencyCollector,
}

impl<S> Layer<S> for LatencyLayer {
    type Service = LatencyService<S>;

    fn layer(&self, inner: S) -> Self::Service {
        LatencyService {
            inner,
            collector: self.collector.clone(),
        }
    }
}

/// Service do Tower que registra latências
#[derive(Clone)]
pub struct LatencyService<S> {
    inner: S,
    collector: LatencyCollector,
}

impl<S> Service<Request<Body>> for LatencyService<S>
where
    S: Service<Request<Body>, Response = Response> + Clone + Send + 'static,
    S::Future: Send + 'static,
{
    type Response = S::Response;
    type Error = S::Error;
    type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send>>;

    fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        self.inner.poll_ready(cx)
    }

    fn call(&mut self, request: Request<Body>) -> Self::Future {
        let collector = self.collector.clone();
        let mut inner = self.inner.clone();

        // Marcar início
        let start = Instant::now();

        // Extrair informações da requisição para logging
        let method = request.method().clone();
        let uri = request.uri().clone();

        Box::pin(async move {
            // Executar requisição
            let response = inner.call(request).await?;

            // Calcular latência
            let duration = start.elapsed();
            let latency_ms = duration.as_secs_f64() * 1000.0;

            // Registrar latência
            collector.record(latency_ms);

            // Log estruturado
            tracing::debug!(
                service = %collector.service_name(),
                method = %method,
                uri = %uri,
                status = response.status().as_u16(),
                latency_ms = latency_ms,
                "Request completed"
            );

            Ok(response)
        })
    }
}

/// Helper para calcular percentil
fn percentile(sorted_data: &[f64], p: f64) -> f64 {
    if sorted_data.is_empty() {
        return 0.0;
    }

    let index = (p * (sorted_data.len() - 1) as f64).round() as usize;
    sorted_data[index.min(sorted_data.len() - 1)]
}

/// Função de middleware standalone (alternativa ao Layer)
///
/// # Exemplo
/// ```rust,no_run
/// use axum::{Router, middleware};
/// use avx_telemetry::middleware::{latency_middleware, LatencyCollector};
/// use std::sync::Arc;
///
/// #[tokio::main]
/// async fn main() {
///     let collector = Arc::new(LatencyCollector::new("my-service"));
///
///     let app = Router::new()
///         .layer(middleware::from_fn(move |req, next| {
///             latency_middleware(req, next, collector.clone())
///         }));
/// }
/// ```
pub async fn latency_middleware(
    request: Request,
    next: Next,
    collector: Arc<LatencyCollector>,
) -> Response {
    let start = Instant::now();

    // Extrair informações
    let method = request.method().clone();
    let uri = request.uri().clone();

    // Executar requisição
    let response = next.run(request).await;

    // Calcular e registrar latência
    let duration = start.elapsed();
    let latency_ms = duration.as_secs_f64() * 1000.0;
    collector.record(latency_ms);

    // Log
    tracing::debug!(
        service = %collector.service_name(),
        method = %method,
        uri = %uri,
        status = response.status().as_u16(),
        latency_ms = latency_ms,
        "Request completed"
    );

    response
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_latency_collector_creation() {
        let collector = LatencyCollector::new("test-service");
        assert_eq!(collector.service_name(), "test-service");
        assert_eq!(collector.snapshot().len(), 0);
    }

    #[test]
    fn test_latency_recording() {
        let collector = LatencyCollector::new("test");

        collector.record(10.0);
        collector.record(20.0);
        collector.record(30.0);

        let snapshot = collector.snapshot();
        assert_eq!(snapshot.len(), 3);
        assert_eq!(snapshot, vec![10.0, 20.0, 30.0]);
    }

    #[test]
    fn test_buffer_circular_behavior() {
        let collector = LatencyCollector::with_capacity("test", 10);

        // Adicionar mais que a capacidade
        for i in 0..15 {
            collector.record(i as f64);
        }

        let snapshot = collector.snapshot();

        // Deve ter removido os mais antigos
        assert!(snapshot.len() <= 10);
    }

    #[test]
    fn test_latency_statistics() {
        let collector = LatencyCollector::new("test");

        let latencies = vec![10.0, 20.0, 30.0, 40.0, 50.0];
        for latency in &latencies {
            collector.record(*latency);
        }

        let stats = collector.statistics();

        assert_eq!(stats.count, 5);
        assert_eq!(stats.mean_ms, 30.0);
        assert_eq!(stats.min_ms, 10.0);
        assert_eq!(stats.max_ms, 50.0);
        assert_eq!(stats.p50_ms, 30.0);
    }

    #[test]
    fn test_statistics_empty_collector() {
        let collector = LatencyCollector::new("test");
        let stats = collector.statistics();

        assert_eq!(stats.count, 0);
        assert_eq!(stats.mean_ms, 0.0);
    }

    #[test]
    fn test_statistics_is_healthy() {
        let collector = LatencyCollector::new("test");

        for i in 0..100 {
            collector.record((i % 20) as f64); // Latências 0-19ms
        }

        let stats = collector.statistics();

        // P99 deve estar bem abaixo de 50ms
        assert!(stats.is_healthy(50.0));
        assert!(stats.p99_ms < 50.0);
    }

    #[test]
    fn test_clear_collector() {
        let collector = LatencyCollector::new("test");

        collector.record(10.0);
        collector.record(20.0);
        assert_eq!(collector.snapshot().len(), 2);

        collector.clear();
        assert_eq!(collector.snapshot().len(), 0);
    }

    #[test]
    fn test_percentile_calculation() {
        let data = vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0];

        assert_eq!(percentile(&data, 0.50), 5.0);
        assert_eq!(percentile(&data, 0.95), 10.0);
        assert_eq!(percentile(&data, 0.00), 1.0);
        assert_eq!(percentile(&data, 1.00), 10.0);
    }

    #[test]
    fn test_statistics_to_json() {
        let stats = LatencyStatistics {
            count: 100,
            mean_ms: 15.5,
            std_dev_ms: 3.2,
            min_ms: 10.0,
            max_ms: 25.0,
            p50_ms: 15.0,
            p95_ms: 22.0,
            p99_ms: 24.0,
        };

        let json = stats.to_json();

        assert_eq!(json["count"], 100);
        assert_eq!(json["mean_ms"], 15.5);
        assert_eq!(json["percentiles"]["p50"], 15.0);
        assert_eq!(json["percentiles"]["p99"], 24.0);
    }

    #[tokio::test]
    async fn test_detect_anomalies() {
        let collector = LatencyCollector::new("test");

        // Adicionar latências normais
        for _ in 0..100 {
            collector.record(10.0);
        }

        // Adicionar anomalia
        collector.record(1000.0);

        let anomalies = collector.detect_anomalies().unwrap();

        // Deve detectar a anomalia
        assert!(!anomalies.is_empty());
    }

    #[test]
    fn test_middleware_creation() {
        let middleware = LatencyMiddleware::new("test-service");
        assert_eq!(middleware.collector().service_name(), "test-service");
    }
}