lazydns 0.2.63

A light and fast DNS server/forwarder implementation in Rust
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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
//! Monitoring and administration HTTP server
//!
//! Provides endpoints for metrics, health checks, and stats.

use crate::metrics;
use axum::{
    Json, Router,
    extract::State,
    http::StatusCode,
    response::{IntoResponse, Response},
    routing::get,
};
use prometheus::{Encoder, TextEncoder};
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use tokio::net::TcpListener;
#[cfg(unix)]
use tokio::signal::unix::{SignalKind, signal};
use tracing::info;

/// Health check response
#[derive(Debug, Serialize, Deserialize)]
pub struct HealthResponse {
    /// Server status
    pub status: String,
    /// Server version
    pub version: String,
    /// Uptime in seconds
    pub uptime_seconds: u64,
}

/// Stats response
#[derive(Debug, Serialize, Deserialize)]
pub struct StatsResponse {
    /// Total queries processed
    pub total_queries: u64,
    /// Cache hit rate (0.0 to 1.0)
    pub cache_hit_rate: f64,
    /// Active connections
    pub active_connections: i64,
}

/// Monitoring server state
#[derive(Clone)]
pub struct MonitoringState {
    start_time: std::time::Instant,
}

impl MonitoringState {
    /// Create new monitoring state
    pub fn new() -> Self {
        Self {
            start_time: std::time::Instant::now(),
        }
    }

    /// Get uptime in seconds
    pub fn uptime_seconds(&self) -> u64 {
        self.start_time.elapsed().as_secs()
    }
}

impl Default for MonitoringState {
    fn default() -> Self {
        Self::new()
    }
}

/// Monitoring server
pub struct MonitoringServer {
    addr: String,
    state: Arc<MonitoringState>,
}

impl MonitoringServer {
    /// Create a new monitoring server
    ///
    /// # Arguments
    ///
    /// * `addr` - Address to bind to (e.g., "0.0.0.0:9090")
    ///
    /// # Example
    ///
    /// ```no_run
    /// use lazydns::server::monitoring::MonitoringServer;
    ///
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let server = MonitoringServer::new("0.0.0.0:9090");
    /// // server.run().await?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn new(addr: impl Into<String>) -> Self {
        Self {
            addr: addr.into(),
            state: Arc::new(MonitoringState::new()),
        }
    }

    /// Start the monitoring server, with optional startup signal and optional shutdown receiver
    ///
    /// If `startup_tx` is provided the function will send a `()` once the server has bound
    /// to the configured address. If `shutdown_rx` is provided the server will run until
    /// that channel receives a value, at which point it performs a graceful shutdown.
    pub async fn run_with_signal(
        self,
        startup_tx: Option<tokio::sync::oneshot::Sender<()>>,
        mut shutdown_rx: Option<tokio::sync::oneshot::Receiver<()>>,
    ) -> Result<(), std::io::Error> {
        let app = Router::new()
            .route("/metrics", get(metrics_handler))
            .route("/health", get(health_handler))
            .route("/stats", get(stats_handler))
            .with_state(self.state);

        info!("Monitoring server listening on {}", self.addr);

        let listener = TcpListener::bind(&self.addr).await?;

        // Signal startup if requested
        if let Some(tx) = startup_tx {
            let _ = tx.send(());
        }

        // Prepare graceful shutdown future. If an external shutdown receiver is
        // provided we await it. Otherwise, listen to OS signals (Ctrl-C / SIGTERM / SIGHUP)
        // so the monitoring server can shut itself down like the admin server.
        let shutdown_fut = async move {
            if let Some(rx) = shutdown_rx.as_mut() {
                let _ = rx.await;
            } else {
                #[cfg(unix)]
                {
                    let mut sigterm = signal(SignalKind::terminate()).unwrap();
                    let mut sighup = signal(SignalKind::hangup()).unwrap();

                    tokio::select! {
                        _ = tokio::signal::ctrl_c() => {},
                        _ = sigterm.recv() => {},
                        _ = sighup.recv() => {},
                    }
                }

                #[cfg(not(unix))]
                {
                    let _ = tokio::signal::ctrl_c().await;
                }
            }
        };

        // Run server with graceful shutdown
        axum::serve(listener, app)
            .with_graceful_shutdown(shutdown_fut)
            .await?;

        Ok(())
    }

    /// Start the monitoring server (no startup/shutdown channels)
    pub async fn run(self) -> Result<(), std::io::Error> {
        self.run_with_signal(None, None).await
    }
}

/// Handle metrics endpoint (Prometheus format)
async fn metrics_handler(State(state): State<Arc<MonitoringState>>) -> Response {
    // Update uptime gauge before scraping
    metrics::SERVER_UPTIME_SECONDS.set(state.uptime_seconds() as i64);
    let metrics_text = metrics::gather_metrics();
    (StatusCode::OK, metrics_text).into_response()
}

/// Handle health check endpoint
async fn health_handler(State(state): State<Arc<MonitoringState>>) -> Response {
    let health = HealthResponse {
        status: "healthy".to_string(),
        version: env!("CARGO_PKG_VERSION").to_string(),
        uptime_seconds: state.uptime_seconds(),
    };

    Json(health).into_response()
}

/// Handle stats endpoint
///
/// Notes:
/// - `total_queries` is computed by summing all samples of the `dns_queries_total` metric
///   across label combinations. This avoids maintaining separate aggregation state and
///   automatically covers new labels/protocols (e.g., `udp`, `tcp`, `doh`, `dot`, `doq`).
/// - `active_connections` is computed by summing all samples of the `dns_active_connections`
///   gauge metric; this includes all protocol labels and thus reflects the total active
///   connections across protocols.
/// - This approach reads the current Prometheus registry (`METRICS_REGISTRY.gather()`)
///   and performs an immediate aggregation; for very high-throughput scenarios a dedicated
///   aggregated metric maintained at the source may be preferable for performance and
///   absolute accuracy.
async fn stats_handler() -> Response {
    // Gather statistics from metrics
    let cache_hits = metrics::CACHE_HITS_TOTAL.get();
    let cache_misses = metrics::CACHE_MISSES_TOTAL.get();
    let cache_hit_rate = if cache_hits + cache_misses > 0 {
        cache_hits as f64 / (cache_hits + cache_misses) as f64
    } else {
        0.0
    };

    // Aggregate total queries and active connections by inspecting the registry
    let mut total_queries: u64 = 0;
    let mut active_conns: i64 = 0;

    // Encode each relevant metric family to Prometheus text format and parse numeric samples.
    // This avoids using non-public proto helper traits and works with the public encoder API.
    let encoder = TextEncoder::new();
    let metric_families = metrics::METRICS_REGISTRY.gather();
    for mf in metric_families.iter() {
        match mf.name() {
            "dns_queries_total" | "dns_active_connections" => {
                let mut buf = Vec::new();
                // use a slice reference to avoid cloning the MetricFamily
                if encoder.encode(std::slice::from_ref(mf), &mut buf).is_ok() {
                    let text = match String::from_utf8(buf) {
                        Ok(s) => s,
                        Err(_) => continue,
                    };

                    for line in text.lines() {
                        let line = line.trim();
                        if line.is_empty() || line.starts_with('#') {
                            continue;
                        }
                        // line format: metric_name{labels} <value>
                        if let Some(pos) = line.rfind(' ') {
                            let value_str = &line[pos + 1..];
                            if let Ok(v) = value_str.parse::<f64>() {
                                if mf.name() == "dns_queries_total" {
                                    total_queries = total_queries.saturating_add(v as u64);
                                } else {
                                    active_conns = active_conns.saturating_add(v as i64);
                                }
                            }
                        }
                    }
                }
            }
            _ => {}
        }
    }

    let stats = StatsResponse {
        total_queries,
        cache_hit_rate,
        active_connections: active_conns,
    };

    Json(stats).into_response()
}

#[cfg(test)]
fn parse_metric_family_values(mf: &prometheus::proto::MetricFamily) -> Vec<f64> {
    let encoder = TextEncoder::new();
    let mut buf = Vec::new();
    if encoder.encode(std::slice::from_ref(mf), &mut buf).is_err() {
        return Vec::new();
    }
    let text = match String::from_utf8(buf) {
        Ok(s) => s,
        Err(_) => return Vec::new(),
    };

    let mut values = Vec::new();
    for line in text.lines() {
        let line = line.trim();
        if line.is_empty() || line.starts_with('#') {
            continue;
        }
        if let Some(pos) = line.rfind(' ') {
            let value_str = &line[pos + 1..];
            if let Ok(v) = value_str.parse::<f64>() {
                values.push(v);
            }
        }
    }

    values
}

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

    #[test]
    fn test_monitoring_state_creation() {
        let state = MonitoringState::new();
        assert_eq!(state.uptime_seconds(), 0);
    }

    #[test]
    fn test_monitoring_state_default() {
        let state = MonitoringState::default();
        assert_eq!(state.uptime_seconds(), 0);
    }

    #[test]
    fn test_monitoring_state_uptime() {
        let state = MonitoringState::new();
        std::thread::sleep(std::time::Duration::from_secs(1));
        assert!(state.uptime_seconds() >= 1);
    }

    #[test]
    fn test_monitoring_state_uptime_zero() {
        let state = MonitoringState::new();
        // Immediately check uptime - should be 0
        assert_eq!(state.uptime_seconds(), 0);
    }

    #[test]
    fn test_monitoring_server_creation() {
        let server = MonitoringServer::new("127.0.0.1:9090");
        assert_eq!(server.addr, "127.0.0.1:9090");
        assert_eq!(server.state.uptime_seconds(), 0);
    }

    #[test]
    fn test_monitoring_server_creation_with_string() {
        let addr = "0.0.0.0:8080".to_string();
        let server = MonitoringServer::new(addr.clone());
        assert_eq!(server.addr, addr);
    }

    #[test]
    fn test_health_response_serialization() {
        let health = HealthResponse {
            status: "healthy".to_string(),
            version: "0.1.0".to_string(),
            uptime_seconds: 100,
        };

        let json = serde_json::to_string(&health).unwrap();
        assert!(json.contains("healthy"));
        assert!(json.contains("0.1.0"));
        assert!(json.contains("100"));
    }

    #[test]
    fn test_health_response_deserialization() {
        let json = r#"{"status":"healthy","version":"0.1.0","uptime_seconds":100}"#;
        let health: HealthResponse = serde_json::from_str(json).unwrap();

        assert_eq!(health.status, "healthy");
        assert_eq!(health.version, "0.1.0");
        assert_eq!(health.uptime_seconds, 100);
    }

    #[test]
    fn test_health_response_empty_values() {
        let health = HealthResponse {
            status: String::new(),
            version: String::new(),
            uptime_seconds: 0,
        };

        let json = serde_json::to_string(&health).unwrap();
        let deserialized: HealthResponse = serde_json::from_str(&json).unwrap();

        assert_eq!(deserialized.status, "");
        assert_eq!(deserialized.version, "");
        assert_eq!(deserialized.uptime_seconds, 0);
    }

    #[test]
    fn test_stats_response_serialization() {
        let stats = StatsResponse {
            total_queries: 1000,
            cache_hit_rate: 0.75,
            active_connections: 10,
        };

        let json = serde_json::to_string(&stats).unwrap();
        assert!(json.contains("1000"));
        assert!(json.contains("0.75"));
        assert!(json.contains("10"));
    }

    #[test]
    fn test_stats_response_deserialization() {
        let json = r#"{"total_queries":1000,"cache_hit_rate":0.75,"active_connections":10}"#;
        let stats: StatsResponse = serde_json::from_str(json).unwrap();

        assert_eq!(stats.total_queries, 1000);
        assert_eq!(stats.cache_hit_rate, 0.75);
        assert_eq!(stats.active_connections, 10);
    }

    #[test]
    fn test_stats_response_edge_cases() {
        // Test zero values
        let stats_zero = StatsResponse {
            total_queries: 0,
            cache_hit_rate: 0.0,
            active_connections: 0,
        };
        let json_zero = serde_json::to_string(&stats_zero).unwrap();
        assert!(json_zero.contains("0"));

        // Test maximum hit rate
        let stats_perfect = StatsResponse {
            total_queries: 1000,
            cache_hit_rate: 1.0,
            active_connections: -1, // Negative connections (edge case)
        };
        let json_perfect = serde_json::to_string(&stats_perfect).unwrap();
        assert!(json_perfect.contains("1.0"));
        assert!(json_perfect.contains("-1"));
    }

    #[test]
    fn test_stats_response_negative_connections() {
        let stats = StatsResponse {
            total_queries: 500,
            cache_hit_rate: 0.5,
            active_connections: -5,
        };

        let json = serde_json::to_string(&stats).unwrap();
        let deserialized: StatsResponse = serde_json::from_str(&json).unwrap();

        assert_eq!(deserialized.active_connections, -5);
    }

    #[test]
    fn test_health_handler_response_structure() {
        // Test that health handler creates proper response structure
        // We can't easily test the async handler directly without axum test framework,
        // but we can test the response structure it creates
        let state = Arc::new(MonitoringState::new());
        let expected_version = env!("CARGO_PKG_VERSION");

        // Simulate what health_handler does
        let health = HealthResponse {
            status: "healthy".to_string(),
            version: expected_version.to_string(),
            uptime_seconds: state.uptime_seconds(),
        };

        assert_eq!(health.status, "healthy");
        assert_eq!(health.version, expected_version);
        assert_eq!(health.uptime_seconds, 0); // Just created
    }

    #[test]
    fn test_stats_handler_response_structure() {
        // Test that stats handler creates proper response structure
        // We can't easily test the async handler directly without axum test framework,
        // but we can test the logic it uses

        // Test cache hit rate calculation logic
        let cache_hits = 75;
        let cache_misses = 25;
        let expected_hit_rate = cache_hits as f64 / (cache_hits + cache_misses) as f64;
        assert_eq!(expected_hit_rate, 0.75);

        // Test zero division case
        let zero_hit_rate = if 0 > 0 { 1.0 } else { 0.0 };
        assert_eq!(zero_hit_rate, 0.0);

        // Test active connections summation logic
        let udp_conns = 5;
        let tcp_conns = 3;
        let total_conns = udp_conns + tcp_conns;
        assert_eq!(total_conns, 8);
    }

    #[test]
    fn test_parse_metric_family_values_and_sum() {
        // Snapshot current totals to avoid flakes due to global metric state
        let mfs_before = metrics::METRICS_REGISTRY.gather();
        let q_mf_before = mfs_before
            .iter()
            .find(|mf| mf.name() == "dns_queries_total");
        let prev_total_queries: u64 = q_mf_before
            .map(|mf| {
                parse_metric_family_values(mf)
                    .iter()
                    .map(|v| *v as u64)
                    .sum()
            })
            .unwrap_or(0);

        // Set up counters/gauges
        for _ in 0..5 {
            metrics::DNS_QUERIES_TOTAL
                .with_label_values(&["udp", "A"])
                .inc();
        }
        for _ in 0..3 {
            metrics::DNS_QUERIES_TOTAL
                .with_label_values(&["tcp", "A"])
                .inc();
        }

        metrics::ACTIVE_CONNECTIONS
            .with_label_values(&["udp"])
            .set(2);
        metrics::ACTIVE_CONNECTIONS
            .with_label_values(&["tcp"])
            .set(4);

        // Find metric families and parse
        let mfs = metrics::METRICS_REGISTRY.gather();
        let q_mf = mfs
            .iter()
            .find(|mf| mf.name() == "dns_queries_total")
            .unwrap();
        let a_mf = mfs
            .iter()
            .find(|mf| mf.name() == "dns_active_connections")
            .unwrap();

        let q_vals = parse_metric_family_values(q_mf);
        assert!(q_vals.len() >= 2);
        let total_queries_after: u64 = q_vals.iter().map(|v| *v as u64).sum();
        assert_eq!(total_queries_after, prev_total_queries + 8);

        let a_vals = parse_metric_family_values(a_mf);
        assert!(a_vals.len() >= 2);
        assert_eq!(a_vals.iter().map(|v| *v as i64).sum::<i64>(), 6);
    }

    #[tokio::test]
    async fn test_stats_handler_aggregates_metrics() {
        // Tests share global Prometheus metrics. Snapshot current values and
        // assert that our increments change the aggregated totals by the
        // expected deltas so the test is robust against other tests.

        // Snapshot current totals for dns_queries_total and dns_active_connections
        let mfs_before = metrics::METRICS_REGISTRY.gather();
        let q_mf_before = mfs_before
            .iter()
            .find(|mf| mf.name() == "dns_queries_total");
        let prev_total_queries: u64 = q_mf_before
            .map(|mf| {
                parse_metric_family_values(mf)
                    .iter()
                    .map(|v| *v as u64)
                    .sum()
            })
            .unwrap_or(0);

        let a_mf_before = mfs_before
            .iter()
            .find(|mf| mf.name() == "dns_active_connections");
        let prev_total_active: i64 = a_mf_before
            .map(|mf| {
                parse_metric_family_values(mf)
                    .iter()
                    .map(|v| *v as i64)
                    .sum()
            })
            .unwrap_or(0);

        let prev_cache_hits = metrics::CACHE_HITS_TOTAL.get();
        let prev_cache_misses = metrics::CACHE_MISSES_TOTAL.get();

        // Increment queries: 5 udp_test + 3 tcp_test (use unique labels to avoid
        // clobbering other tests' label values)
        for _ in 0..5 {
            metrics::DNS_QUERIES_TOTAL
                .with_label_values(&["udp_test", "A"])
                .inc();
        }
        for _ in 0..3 {
            metrics::DNS_QUERIES_TOTAL
                .with_label_values(&["tcp_test", "A"])
                .inc();
        }

        // Set cache hits/misses to compute hit rate
        metrics::CACHE_HITS_TOTAL.inc_by(75);
        metrics::CACHE_MISSES_TOTAL.inc_by(25);

        // Record existing per-label active connection values for our test labels
        let prev_udp_val = metrics::ACTIVE_CONNECTIONS
            .with_label_values(&["udp_test"])
            .get();
        let prev_tcp_val = metrics::ACTIVE_CONNECTIONS
            .with_label_values(&["tcp_test"])
            .get();

        // Set active connections for our test labels
        metrics::ACTIVE_CONNECTIONS
            .with_label_values(&["udp_test"])
            .set(2);
        metrics::ACTIVE_CONNECTIONS
            .with_label_values(&["tcp_test"])
            .set(4);

        let response = stats_handler().await;
        let (parts, body) = response.into_parts();
        assert_eq!(parts.status, StatusCode::OK);

        let body_bytes = axum::body::to_bytes(body, usize::MAX).await.unwrap();
        let body_str = String::from_utf8(body_bytes.to_vec()).unwrap();

        let stats: StatsResponse = serde_json::from_str(&body_str).unwrap();

        // Verify totals increased by the amounts we added
        let expected_total_queries = prev_total_queries + 8;
        assert_eq!(stats.total_queries, expected_total_queries);

        // Verify cache hit rate reflects our increments on top of previous counts
        let expected_hit_rate = if (prev_cache_hits + prev_cache_misses + 100) > 0 {
            (prev_cache_hits + 75) as f64 / (prev_cache_hits + prev_cache_misses + 100) as f64
        } else {
            0.0
        };
        assert!((stats.cache_hit_rate - expected_hit_rate).abs() < 1e-6);

        // Verify active connections: replace previous values for our labels with the new ones
        let expected_active = prev_total_active - prev_udp_val - prev_tcp_val + 2 + 4;
        assert_eq!(stats.active_connections, expected_active);
    }

    #[tokio::test]
    async fn test_monitoring_server_bind_address_validation() {
        // Test that server creation accepts various valid address formats
        let valid_addresses = vec![
            "127.0.0.1:9090",
            "0.0.0.0:8080",
            "localhost:3000",
            "[::1]:9090", // IPv6
        ];

        for addr in valid_addresses {
            let server = MonitoringServer::new(addr);
            assert_eq!(server.addr, addr);
        }
    }
}