dbpulse 0.9.1

command line tool to monitor that database is available for read & write
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
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
use crate::{
    metrics::{
        DATABASE_HOST_INFO, DATABASE_UPTIME_SECONDS, DATABASE_VERSION_INFO, DB_ERRORS, DB_READONLY,
        ITERATIONS_TOTAL, LAST_RUNTIME_MS, LAST_SUCCESS, PANICS_RECOVERED, PULSE, RUNTIME,
        TLS_CERT_EXPIRY_DAYS, TLS_CONNECTION_ERRORS, TLS_INFO, encode_metrics,
    },
    queries::{HealthCheckResult, mysql, postgres},
    tls::{TlsConfig, cache::CertCache},
};
use axum::{Router, http::StatusCode, response::IntoResponse, routing::get};
use chrono::{Duration, Utc, prelude::*};
use dsn::DSN;
use futures::FutureExt;
use serde::{Deserialize, Serialize};
use std::{env::var, net::IpAddr, sync::Arc};
use tokio::{net::TcpListener, sync::mpsc, task, time};

#[derive(Serialize, Deserialize, Debug, Default)]
struct Pulse {
    runtime_ms: i64,
    time: String,
    version: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    uptime_seconds: Option<i64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    tls_version: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    tls_cipher: Option<String>,
}

/// Start the monitoring service
///
/// # Errors
///
/// Returns an error if the service fails to start or bind to the port
pub async fn start(
    dsn: DSN,
    interval: u16,
    listen: Option<IpAddr>,
    port: u16,
    range: u32,
    tls: TlsConfig,
) -> anyhow::Result<()> {
    // Metrics are already registered with REGISTRY via lazy_static macros
    let app = Router::new().route("/metrics", get(metrics_handler));

    // Bind to socket with smart fallback
    let (listener, bind_addr) = match listen {
        Some(addr) => {
            // Explicit address specified - bind to it
            let socket_addr = format!("{addr}:{port}");
            let listener = TcpListener::bind(&socket_addr).await?;
            (listener, socket_addr)
        }
        None => {
            // Auto mode: try IPv6 first, fallback to IPv4
            if let Ok(l) = TcpListener::bind(format!("::0:{port}")).await {
                (l, format!("[::]:{port}"))
            } else {
                // Fallback to IPv4 if IPv6 fails
                let socket_addr = format!("0.0.0.0:{port}");
                (TcpListener::bind(&socket_addr).await?, socket_addr)
            }
        }
    };

    println!(
        "{} - Listening on {}, interval: {}",
        Utc::now().to_rfc3339_opts(SecondsFormat::Secs, true),
        bind_addr,
        interval
    );

    // Initialize TLS certificate cache with configurable TTL (default: 1 hour)
    let cert_cache_ttl_secs = var("DBPULSE_TLS_CERT_CACHE_TTL")
        .ok()
        .and_then(|s| s.parse::<u64>().ok())
        .unwrap_or(3600); // Default: 1 hour
    let cert_cache = Arc::new(CertCache::new(std::time::Duration::from_secs(
        cert_cache_ttl_secs,
    )));

    println!(
        "{} - TLS certificate cache TTL: {}s",
        Utc::now().to_rfc3339_opts(SecondsFormat::Secs, true),
        cert_cache_ttl_secs
    );

    // shutdown signal
    let (tx, mut rx) = mpsc::unbounded_channel();

    // check db pulse - keep JoinHandle to detect task failures
    let monitor_handle =
        task::spawn(async move { run_loop(dsn, interval, range, tls, cert_cache, tx).await });

    // Race between normal operation and monitoring task failure
    let server =
        axum::serve(listener, app.into_make_service()).with_graceful_shutdown(async move {
            rx.recv().await;
        });

    tokio::select! {
        result = server => {
            result?;
        }
        result = monitor_handle => {
            match result {
                Ok(()) => {
                    eprintln!("Monitoring loop exited unexpectedly");
                    anyhow::bail!("Monitoring loop stopped");
                }
                Err(e) => {
                    eprintln!("Monitoring loop panicked: {e}");
                    anyhow::bail!("Monitoring loop panicked: {e}");
                }
            }
        }
    }

    Ok(())
}

async fn metrics_handler() -> impl IntoResponse {
    match encode_metrics() {
        Ok(buffer) => (StatusCode::OK, buffer),
        Err(e) => {
            eprintln!("{e}");
            (StatusCode::INTERNAL_SERVER_ERROR, Vec::new())
        }
    }
}

/// Check if an error is TLS-related without multiple allocations
#[inline]
fn is_tls_error(error: &anyhow::Error) -> bool {
    let error_str = format!("{error:#}");
    // Check both lowercase and uppercase variants to avoid to_lowercase() allocation
    error_str.contains("ssl")
        || error_str.contains("SSL")
        || error_str.contains("tls")
        || error_str.contains("TLS")
        || error_str.contains("certificate")
        || error_str.contains("Certificate")
}

#[inline]
fn is_database_read_only(db: &str, version: &str) -> bool {
    match db {
        // PostgreSQL replicas report recovery mode in version string.
        "postgres" | "postgresql" => {
            version.contains("recovery mode") || version.contains("read-only")
        }
        _ => version.contains("read-only"),
    }
}

#[inline]
fn update_database_version_metric(
    database: &str,
    version: &str,
    last_version: &mut Option<String>,
) {
    if let Some(previous_version) = last_version.as_deref()
        && previous_version != version
    {
        let _ = DATABASE_VERSION_INFO.remove_label_values(&[database, previous_version]);
    }

    DATABASE_VERSION_INFO
        .with_label_values(&[database, version])
        .set(1);

    *last_version = Some(version.to_string());
}

#[inline]
fn update_database_host_metric(database: &str, host: Option<&str>, last_host: &mut Option<String>) {
    if let Some(previous_host) = last_host.as_deref()
        && Some(previous_host) != host
    {
        let _ = DATABASE_HOST_INFO.remove_label_values(&[database, previous_host]);
    }

    if let Some(current_host) = host {
        DATABASE_HOST_INFO
            .with_label_values(&[database, current_host])
            .set(1);
        *last_host = Some(current_host.to_string());
    } else {
        *last_host = None;
    }
}

#[inline]
fn remaining_sleep_duration(wait_time: Duration, runtime: Duration) -> Option<time::Duration> {
    wait_time
        .checked_sub(&runtime)
        .and_then(|remaining| remaining.to_std().ok())
        .filter(|duration| !duration.is_zero())
}

#[derive(Default)]
struct LoopLabels {
    last_version_label: Option<String>,
    last_host_label: Option<String>,
}

#[inline]
fn metric_database(driver: &str) -> Option<&'static str> {
    match driver {
        "postgres" | "postgresql" => Some("postgres"),
        "mysql" => Some("mysql"),
        _ => None,
    }
}

async fn run_health_check(
    database: &str,
    dsn: &DSN,
    now: DateTime<Utc>,
    range: u32,
    tls: &TlsConfig,
    cert_cache: &CertCache,
) -> anyhow::Result<HealthCheckResult> {
    match database {
        "postgres" => postgres::test_rw(dsn, now, range, tls, cert_cache).await,
        "mysql" => mysql::test_rw(dsn, now, range, tls, cert_cache).await,
        _ => unreachable!("unsupported database label"),
    }
}

fn apply_tls_metrics(database: &str, pulse: &mut Pulse, result: &HealthCheckResult) {
    if let Some(metadata) = result.tls_metadata.as_ref() {
        metadata.version.clone_into(&mut pulse.tls_version);
        metadata.cipher.clone_into(&mut pulse.tls_cipher);

        if let (Some(version), Some(cipher)) = (&metadata.version, &metadata.cipher) {
            TLS_INFO
                .with_label_values(&[database, version.as_str(), cipher.as_str()])
                .set(1);
        }

        if let Some(days) = metadata.cert_expiry_days {
            TLS_CERT_EXPIRY_DAYS
                .with_label_values(&[database])
                .set(days);
        }
    }
}

fn record_success(
    database: &str,
    now: DateTime<Utc>,
    pulse: &mut Pulse,
    result: &HealthCheckResult,
    labels: &mut LoopLabels,
) {
    result.version.clone_into(&mut pulse.version);
    pulse.uptime_seconds = result.uptime_seconds;

    update_database_version_metric(
        database,
        result.version.as_str(),
        &mut labels.last_version_label,
    );
    update_database_host_metric(
        database,
        result.db_host.as_deref(),
        &mut labels.last_host_label,
    );
    if let Some(uptime) = result.uptime_seconds {
        DATABASE_UPTIME_SECONDS
            .with_label_values(&[database])
            .set(uptime);
    }

    if is_database_read_only(database, &result.version) {
        DB_READONLY.with_label_values(&[database]).set(1);
        PULSE.set(0);
        ITERATIONS_TOTAL
            .with_label_values(&[database, "error"])
            .inc();
        DB_ERRORS.with_label_values(&[database, "query"]).inc();
    } else {
        DB_READONLY.with_label_values(&[database]).set(0);
        PULSE.set(1);
        ITERATIONS_TOTAL
            .with_label_values(&[database, "success"])
            .inc();
        LAST_SUCCESS
            .with_label_values(&[database])
            .set(now.timestamp());
    }

    apply_tls_metrics(database, pulse, result);
}

fn classify_error_type(database: &str, error: &anyhow::Error) -> &'static str {
    let error_str = format!("{error:#}");
    if error_str.contains("authentication")
        || error_str.contains("password")
        || (database == "mysql" && error_str.contains("Access denied"))
    {
        "authentication"
    } else if error_str.contains("timeout") {
        "timeout"
    } else if error_str.contains("connection") || error_str.contains("refused") {
        "connection"
    } else if error_str.contains("transaction") {
        "transaction"
    } else {
        "query"
    }
}

fn record_error(database: &str, error: &anyhow::Error, tls: &TlsConfig, labels: &mut LoopLabels) {
    PULSE.set(0);
    eprintln!("{error}");
    update_database_host_metric(database, None, &mut labels.last_host_label);
    ITERATIONS_TOTAL
        .with_label_values(&[database, "error"])
        .inc();
    DB_ERRORS
        .with_label_values(&[database, classify_error_type(database, error)])
        .inc();

    if tls.mode.is_enabled() && is_tls_error(error) {
        TLS_CONNECTION_ERRORS
            .with_label_values(&[database, "handshake"])
            .inc();
    }
}

async fn run_iteration(
    dsn: &DSN,
    every: u16,
    range: u32,
    tls: &TlsConfig,
    cert_cache: &CertCache,
    tx: &mpsc::UnboundedSender<()>,
    labels: &mut LoopLabels,
) {
    let mut pulse = Pulse::default();
    let now = Utc::now();
    let wait_time = Duration::seconds(every.into());
    pulse.time = now.to_rfc3339();
    let timer = RUNTIME.start_timer();

    let Some(database) = metric_database(dsn.driver.as_str()) else {
        eprintln!("unsupported driver");
        let _ = tx.send(());
        return;
    };

    match run_health_check(database, dsn, now, range, tls, cert_cache).await {
        Ok(result) => record_success(database, now, &mut pulse, &result, labels),
        Err(error) => record_error(database, &error, tls, labels),
    }

    timer.observe_duration();
    let runtime = Utc::now().signed_duration_since(now);
    pulse.runtime_ms = runtime.num_milliseconds();
    LAST_RUNTIME_MS
        .with_label_values(&[database])
        .set(pulse.runtime_ms);

    if let Ok(serialized) = serde_json::to_string(&pulse) {
        println!("{serialized}");
    }
    if let Some(remaining) = remaining_sleep_duration(wait_time, runtime) {
        time::sleep(remaining).await;
    }
}

async fn run_loop(
    dsn: DSN,
    every: u16,
    range: u32,
    tls: TlsConfig,
    cert_cache: Arc<CertCache>,
    tx: mpsc::UnboundedSender<()>,
) {
    let mut labels = LoopLabels::default();

    loop {
        let iteration_result = std::panic::AssertUnwindSafe(run_iteration(
            &dsn,
            every,
            range,
            &tls,
            cert_cache.as_ref(),
            &tx,
            &mut labels,
        ))
        .catch_unwind()
        .await;

        if let Err(panic_info) = iteration_result {
            eprintln!("Panic in monitoring loop iteration: {panic_info:?}");
            PULSE.set(0);
            PANICS_RECOVERED.inc();
            time::sleep(time::Duration::from_secs(every.into())).await;
        }
    }
}

#[cfg(test)]
mod tests {
    #![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]

    use super::*;
    use anyhow::anyhow;

    #[test]
    fn test_is_tls_error_lowercase_ssl() {
        let error = anyhow!("Connection failed: ssl handshake error");
        assert!(is_tls_error(&error));
    }

    #[test]
    fn test_is_tls_error_uppercase_ssl() {
        let error = anyhow!("Connection failed: SSL handshake error");
        assert!(is_tls_error(&error));
    }

    #[test]
    fn test_is_tls_error_lowercase_tls() {
        let error = anyhow!("tls connection refused");
        assert!(is_tls_error(&error));
    }

    #[test]
    fn test_is_tls_error_uppercase_tls() {
        let error = anyhow!("TLS connection refused");
        assert!(is_tls_error(&error));
    }

    #[test]
    fn test_is_tls_error_lowercase_certificate() {
        let error = anyhow!("Invalid certificate chain");
        assert!(is_tls_error(&error));
    }

    #[test]
    fn test_is_tls_error_uppercase_certificate() {
        let error = anyhow!("Certificate verification failed");
        assert!(is_tls_error(&error));
    }

    #[test]
    fn test_is_not_tls_error() {
        let error = anyhow!("Connection timeout");
        assert!(!is_tls_error(&error));

        let error = anyhow!("Authentication failed");
        assert!(!is_tls_error(&error));

        let error = anyhow!("Database not found");
        assert!(!is_tls_error(&error));
    }

    #[test]
    fn test_is_database_read_only_postgres_recovery() {
        assert!(is_database_read_only(
            "postgres",
            "PostgreSQL 16.0 - Database is in recovery mode",
        ));
    }

    #[test]
    fn test_is_database_read_only_postgres_read_only_text() {
        assert!(is_database_read_only(
            "postgres",
            "PostgreSQL 16.0 - Transaction read-only mode enabled",
        ));
    }

    #[test]
    fn test_is_database_read_only_mysql() {
        assert!(is_database_read_only(
            "mysql",
            "MariaDB 11.4.5 - Database is in read-only mode",
        ));
        assert!(!is_database_read_only("mysql", "MariaDB 11.4.5"));
    }

    #[test]
    fn test_is_database_read_only_writable() {
        assert!(!is_database_read_only("postgres", "PostgreSQL 16.0"));
    }

    fn version_metric_exists(database: &str, version: &str) -> bool {
        crate::metrics::REGISTRY.gather().into_iter().any(|family| {
            family.name() == "dbpulse_database_version_info"
                && family.get_metric().iter().any(|metric| {
                    let labels = metric.get_label();
                    labels
                        .iter()
                        .any(|lp| lp.name() == "database" && lp.value() == database)
                        && labels
                            .iter()
                            .any(|lp| lp.name() == "version" && lp.value() == version)
                })
        })
    }

    fn version_metric_count_for_database(database: &str) -> usize {
        crate::metrics::REGISTRY
            .gather()
            .into_iter()
            .find(|family| family.name() == "dbpulse_database_version_info")
            .map_or(0, |family| {
                family
                    .get_metric()
                    .iter()
                    .filter(|metric| {
                        metric
                            .get_label()
                            .iter()
                            .any(|lp| lp.name() == "database" && lp.value() == database)
                    })
                    .count()
            })
    }

    fn host_metric_exists(database: &str, host: &str) -> bool {
        crate::metrics::REGISTRY.gather().into_iter().any(|family| {
            family.name() == "dbpulse_database_host_info"
                && family.get_metric().iter().any(|metric| {
                    let labels = metric.get_label();
                    labels
                        .iter()
                        .any(|lp| lp.name() == "database" && lp.value() == database)
                        && labels
                            .iter()
                            .any(|lp| lp.name() == "host" && lp.value() == host)
                })
        })
    }

    fn host_metric_count_for_database(database: &str) -> usize {
        crate::metrics::REGISTRY
            .gather()
            .into_iter()
            .find(|family| family.name() == "dbpulse_database_host_info")
            .map_or(0, |family| {
                family
                    .get_metric()
                    .iter()
                    .filter(|metric| {
                        metric
                            .get_label()
                            .iter()
                            .any(|lp| lp.name() == "database" && lp.value() == database)
                    })
                    .count()
            })
    }

    #[test]
    fn test_update_database_version_metric_replaces_old_version_label() {
        let database = "test-version-transition";
        let v1 = "MariaDB 11.4.5 - Database is in read-only mode";
        let v2 = "MariaDB 11.4.5";
        let mut last_version = None;

        update_database_version_metric(database, v1, &mut last_version);
        assert!(version_metric_exists(database, v1));
        assert_eq!(version_metric_count_for_database(database), 1);

        update_database_version_metric(database, v2, &mut last_version);
        assert!(version_metric_exists(database, v2));
        assert!(!version_metric_exists(database, v1));
        assert_eq!(version_metric_count_for_database(database), 1);
    }

    #[test]
    fn test_update_database_version_metric_same_version_keeps_single_series() {
        let database = "test-version-same";
        let version = "PostgreSQL 16.3";
        let mut last_version = None;

        update_database_version_metric(database, version, &mut last_version);
        update_database_version_metric(database, version, &mut last_version);

        assert!(version_metric_exists(database, version));
        assert_eq!(version_metric_count_for_database(database), 1);
    }

    #[test]
    fn test_update_database_host_metric_replaces_old_host_label() {
        let database = "test-host-transition";
        let h1 = "db-a";
        let h2 = "db-b";
        let mut last_host = None;

        update_database_host_metric(database, Some(h1), &mut last_host);
        assert!(host_metric_exists(database, h1));
        assert_eq!(host_metric_count_for_database(database), 1);

        update_database_host_metric(database, Some(h2), &mut last_host);
        assert!(host_metric_exists(database, h2));
        assert!(!host_metric_exists(database, h1));
        assert_eq!(host_metric_count_for_database(database), 1);
    }

    #[test]
    fn test_update_database_host_metric_same_host_keeps_single_series() {
        let database = "test-host-same";
        let host = "db-primary";
        let mut last_host = None;

        update_database_host_metric(database, Some(host), &mut last_host);
        update_database_host_metric(database, Some(host), &mut last_host);

        assert!(host_metric_exists(database, host));
        assert_eq!(host_metric_count_for_database(database), 1);
    }

    #[test]
    fn test_update_database_host_metric_none_clears_previous_label() {
        let database = "test-host-clear";
        let host = "db-primary";
        let mut last_host = None;

        update_database_host_metric(database, Some(host), &mut last_host);
        assert!(host_metric_exists(database, host));

        update_database_host_metric(database, None, &mut last_host);
        assert!(!host_metric_exists(database, host));
        assert_eq!(host_metric_count_for_database(database), 0);
    }

    #[test]
    fn test_remaining_sleep_duration_preserves_subsecond_interval() {
        let wait_time = Duration::seconds(1);
        let runtime = Duration::milliseconds(250);

        let remaining = remaining_sleep_duration(wait_time, runtime).unwrap();
        assert_eq!(remaining, std::time::Duration::from_millis(750));
    }

    #[test]
    fn test_remaining_sleep_duration_one_millisecond_remainder() {
        // Regression test for `-i 1`: runtime just under 1s must still sleep.
        let wait_time = Duration::seconds(1);
        let runtime = Duration::milliseconds(999);

        let remaining = remaining_sleep_duration(wait_time, runtime).unwrap();
        assert_eq!(remaining, std::time::Duration::from_millis(1));
    }

    #[test]
    fn test_remaining_sleep_duration_subsecond_remainder_for_longer_interval() {
        let wait_time = Duration::seconds(2);
        let runtime = Duration::milliseconds(1500);

        let remaining = remaining_sleep_duration(wait_time, runtime).unwrap();
        assert_eq!(remaining, std::time::Duration::from_millis(500));
    }

    #[test]
    fn test_remaining_sleep_duration_none_when_runtime_exceeds_interval() {
        let wait_time = Duration::seconds(1);
        let runtime = Duration::milliseconds(1200);

        let remaining = remaining_sleep_duration(wait_time, runtime);
        assert!(remaining.is_none());
    }

    #[test]
    fn test_remaining_sleep_duration_none_when_runtime_matches_interval() {
        let wait_time = Duration::seconds(1);
        let runtime = Duration::seconds(1);

        let remaining = remaining_sleep_duration(wait_time, runtime);
        assert!(remaining.is_none());
    }

    #[test]
    fn test_pulse_default() {
        let pulse = Pulse::default();
        assert_eq!(pulse.runtime_ms, 0);
        assert_eq!(pulse.time, "");
        assert_eq!(pulse.version, "");
        assert!(pulse.tls_version.is_none());
        assert!(pulse.tls_cipher.is_none());
    }

    #[test]
    fn test_pulse_serialization() {
        let pulse = Pulse {
            uptime_seconds: None,
            runtime_ms: 123,
            time: "2024-01-01T00:00:00Z".to_string(),
            version: "PostgreSQL 15.0".to_string(),
            tls_version: Some("TLSv1.3".to_string()),
            tls_cipher: Some("AES256-GCM-SHA384".to_string()),
        };

        let json = serde_json::to_string(&pulse).unwrap();
        assert!(json.contains("\"runtime_ms\":123"));
        assert!(json.contains("\"version\":\"PostgreSQL 15.0\""));
        assert!(json.contains("\"tls_version\":\"TLSv1.3\""));
        assert!(json.contains("\"tls_cipher\":\"AES256-GCM-SHA384\""));
    }

    #[test]
    fn test_pulse_serialization_without_tls() {
        let pulse = Pulse {
            uptime_seconds: None,
            runtime_ms: 50,
            time: "2024-01-01T00:00:00Z".to_string(),
            version: "MySQL 8.0".to_string(),
            tls_version: None,
            tls_cipher: None,
        };

        let json = serde_json::to_string(&pulse).unwrap();
        assert!(json.contains("\"runtime_ms\":50"));
        assert!(json.contains("\"version\":\"MySQL 8.0\""));
        // These fields should be omitted when None (skip_serializing_if)
        assert!(!json.contains("tls_version"));
        assert!(!json.contains("tls_cipher"));
    }

    #[test]
    fn test_pulse_deserialization_full() {
        let json = r#"{
            "runtime_ms": 123,
            "time": "2024-01-01T00:00:00Z",
            "version": "PostgreSQL 15.0",
            "tls_version": "TLSv1.3",
            "tls_cipher": "AES256-GCM-SHA384"
        }"#;

        let pulse: Pulse = serde_json::from_str(json).unwrap();
        assert_eq!(pulse.runtime_ms, 123);
        assert_eq!(pulse.time, "2024-01-01T00:00:00Z");
        assert_eq!(pulse.version, "PostgreSQL 15.0");
        assert_eq!(pulse.tls_version, Some("TLSv1.3".to_string()));
        assert_eq!(pulse.tls_cipher, Some("AES256-GCM-SHA384".to_string()));
    }

    #[test]
    fn test_pulse_deserialization_without_tls() {
        let json = r#"{
            "runtime_ms": 50,
            "time": "2024-01-01T00:00:00Z",
            "version": "MySQL 8.0"
        }"#;

        let pulse: Pulse = serde_json::from_str(json).unwrap();
        assert_eq!(pulse.runtime_ms, 50);
        assert_eq!(pulse.time, "2024-01-01T00:00:00Z");
        assert_eq!(pulse.version, "MySQL 8.0");
        assert!(pulse.tls_version.is_none());
        assert!(pulse.tls_cipher.is_none());
    }

    #[test]
    fn test_pulse_serialization_only_tls_version() {
        let pulse = Pulse {
            uptime_seconds: None,
            runtime_ms: 100,
            time: "2024-01-01T00:00:00Z".to_string(),
            version: "PostgreSQL 14.0".to_string(),
            tls_version: Some("TLSv1.2".to_string()),
            tls_cipher: None,
        };

        let json = serde_json::to_string(&pulse).unwrap();
        assert!(json.contains("\"tls_version\":\"TLSv1.2\""));
        assert!(!json.contains("tls_cipher"));
    }

    #[test]
    fn test_pulse_serialization_only_tls_cipher() {
        let pulse = Pulse {
            uptime_seconds: None,
            runtime_ms: 100,
            time: "2024-01-01T00:00:00Z".to_string(),
            version: "PostgreSQL 14.0".to_string(),
            tls_version: None,
            tls_cipher: Some("AES128-SHA".to_string()),
        };

        let json = serde_json::to_string(&pulse).unwrap();
        assert!(json.contains("\"tls_cipher\":\"AES128-SHA\""));
        assert!(!json.contains("tls_version"));
    }

    #[test]
    fn test_pulse_deserialization_partial_tls() {
        let json = r#"{
            "runtime_ms": 75,
            "time": "2024-01-01T00:00:00Z",
            "version": "MySQL 8.0",
            "tls_version": "TLSv1.2"
        }"#;

        let pulse: Pulse = serde_json::from_str(json).unwrap();
        assert_eq!(pulse.runtime_ms, 75);
        assert_eq!(pulse.tls_version, Some("TLSv1.2".to_string()));
        assert!(pulse.tls_cipher.is_none());
    }

    #[test]
    fn test_pulse_zero_runtime() {
        let pulse = Pulse {
            uptime_seconds: None,
            runtime_ms: 0,
            time: "2024-01-01T00:00:00Z".to_string(),
            version: "PostgreSQL 15.0".to_string(),
            tls_version: None,
            tls_cipher: None,
        };

        let json = serde_json::to_string(&pulse).unwrap();
        assert!(json.contains("\"runtime_ms\":0"));
    }

    #[test]
    fn test_pulse_negative_runtime() {
        let pulse = Pulse {
            uptime_seconds: None,
            runtime_ms: -1,
            time: "2024-01-01T00:00:00Z".to_string(),
            version: "PostgreSQL 15.0".to_string(),
            tls_version: None,
            tls_cipher: None,
        };

        let json = serde_json::to_string(&pulse).unwrap();
        assert!(json.contains("\"runtime_ms\":-1"));
    }

    #[test]
    fn test_pulse_empty_strings() {
        let pulse = Pulse {
            uptime_seconds: None,
            runtime_ms: 50,
            time: String::new(),
            version: String::new(),
            tls_version: Some(String::new()),
            tls_cipher: Some(String::new()),
        };

        let json = serde_json::to_string(&pulse).unwrap();
        assert!(json.contains("\"time\":\"\""));
        assert!(json.contains("\"version\":\"\""));
        assert!(json.contains("\"tls_version\":\"\""));
        assert!(json.contains("\"tls_cipher\":\"\""));
    }

    #[tokio::test]
    async fn test_metrics_handler_success() {
        // Initialize metrics by accessing them
        let _ = &*PULSE;
        let _ = &*RUNTIME;

        let response = metrics_handler().await.into_response();
        assert_eq!(response.status(), StatusCode::OK);

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

        // Verify metrics content
        assert!(body_str.contains("dbpulse_pulse"));
        assert!(body_str.contains("dbpulse_runtime"));
    }

    #[test]
    fn test_is_tls_error_mixed_case() {
        // "SSL error" contains both "SSL" and "ssl"
        let error = anyhow!("Connection failed: SSL error in ssl handshake");
        assert!(is_tls_error(&error));

        // "TLS" uppercase is detected
        let error = anyhow!("Connection failed: TLS connection refused");
        assert!(is_tls_error(&error));

        // "Certificate" with capital C is detected
        let error = anyhow!("Invalid Certificate chain");
        assert!(is_tls_error(&error));
    }

    #[test]
    fn test_is_tls_error_multiple_keywords() {
        let error = anyhow!("SSL/TLS certificate verification failed");
        assert!(is_tls_error(&error));

        let error = anyhow!("TLS handshake failed: invalid certificate");
        assert!(is_tls_error(&error));
    }

    #[test]
    fn test_is_tls_error_embedded_keywords() {
        let error = anyhow!("Error in sslconnect: handshake failed");
        assert!(is_tls_error(&error));

        let error = anyhow!("certificate_verify_failed in TLS setup");
        assert!(is_tls_error(&error));
    }

    #[test]
    fn test_pulse_large_runtime() {
        let pulse = Pulse {
            uptime_seconds: None,
            runtime_ms: i64::MAX,
            time: "2024-01-01T00:00:00Z".to_string(),
            version: "PostgreSQL 15.0".to_string(),
            tls_version: None,
            tls_cipher: None,
        };

        let json = serde_json::to_string(&pulse).unwrap();
        assert!(json.contains(&format!("\"runtime_ms\":{}", i64::MAX)));
    }

    #[test]
    fn test_pulse_special_characters_in_version() {
        let pulse = Pulse {
            uptime_seconds: None,
            runtime_ms: 50,
            time: "2024-01-01T00:00:00Z".to_string(),
            version: "PostgreSQL 15.0 \"special\" <tags> & symbols".to_string(),
            tls_version: None,
            tls_cipher: None,
        };

        let json = serde_json::to_string(&pulse).unwrap();
        // Verify JSON escaping works
        let parsed: Pulse = serde_json::from_str(&json).unwrap();
        assert_eq!(
            parsed.version,
            "PostgreSQL 15.0 \"special\" <tags> & symbols"
        );
    }

    #[test]
    fn test_pulse_unicode_in_fields() {
        let pulse = Pulse {
            runtime_ms: 50,
            time: "2024-01-01T00:00:00Z".to_string(),
            version: "PostgreSQL 15.0 🚀 数据库".to_string(),
            uptime_seconds: None,
            tls_version: Some("TLSv1.3 ✓".to_string()),
            tls_cipher: Some("AES256 🔒".to_string()),
        };

        let json = serde_json::to_string(&pulse).unwrap();
        let parsed: Pulse = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed.version, "PostgreSQL 15.0 🚀 数据库");
        assert_eq!(parsed.tls_version, Some("TLSv1.3 ✓".to_string()));
        assert_eq!(parsed.tls_cipher, Some("AES256 🔒".to_string()));
    }

    #[test]
    fn test_pulse_debug_trait() {
        let pulse = Pulse::default();
        let debug_str = format!("{pulse:?}");
        assert!(debug_str.contains("Pulse"));
        assert!(debug_str.contains("runtime_ms"));
    }
}