|
| 1 | +// This file is Copyright its original authors, visible in version control |
| 2 | +// history. |
| 3 | +// |
| 4 | +// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE |
| 5 | +// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 6 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option. |
| 7 | +// You may not use this file except in accordance with one or both of these |
| 8 | +// licenses. |
| 9 | + |
| 10 | +use std::time::Duration; |
| 11 | + |
| 12 | +use lazy_static::lazy_static; |
| 13 | +use ldk_node::Node; |
| 14 | +use prometheus::{ |
| 15 | + default_registry, gather, register_int_gauge_with_registry, Encoder, IntGauge, Opts, Registry, |
| 16 | + TextEncoder, |
| 17 | +}; |
| 18 | + |
| 19 | +use crate::api::error::LdkServerError; |
| 20 | + |
| 21 | +pub const BUILD_METRICS_INTERVAL: Duration = Duration::from_secs(60); |
| 22 | + |
| 23 | +lazy_static! { |
| 24 | + pub static ref METRICS: Metrics = Metrics::new(default_registry()); |
| 25 | +} |
| 26 | + |
| 27 | +pub struct Metrics { |
| 28 | + pub service_health_score: IntGauge, |
| 29 | +} |
| 30 | + |
| 31 | +impl Metrics { |
| 32 | + pub fn new(registry: &Registry) -> Self { |
| 33 | + Self { |
| 34 | + service_health_score: register_int_gauge_with_registry!( |
| 35 | + Opts::new("ldk_health_score", "Current health score (0-100)"), |
| 36 | + registry |
| 37 | + ) |
| 38 | + .expect("Failed to register metric"), |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + pub fn update_service_health_score(&self, node: &Node) { |
| 43 | + let score = self.calculate_ldk_server_health_score(node); |
| 44 | + self.service_health_score.set(score); |
| 45 | + } |
| 46 | + |
| 47 | + /// The health score computation is pretty basic for now and simply |
| 48 | + /// calculated based on the impacted events on the components of the |
| 49 | + /// `Node`. The events severity and weightage value are as follows: |
| 50 | + /// |
| 51 | + /// - Critical: 0 (Total failure) |
| 52 | + /// - Major: 35% |
| 53 | + /// - Minor: 25% |
| 54 | + /// |
| 55 | + /// Using the assigned score above, the health score of the `Node` is |
| 56 | + /// computed as: |
| 57 | + /// |
| 58 | + /// Health score = Maximum health score - Sum(Event severity score) |
| 59 | + /// |
| 60 | + /// Where: |
| 61 | + /// |
| 62 | + /// - Maximum health score = 100 |
| 63 | + /// |
| 64 | + /// If the `Node` is not running/online, i.e `is_running` is false, |
| 65 | + /// the severity is critical with a weightage value of -100%. |
| 66 | + /// |
| 67 | + /// If the `Node` is running but isn't connected to any peer yet, |
| 68 | + /// the severity is major with a weightage value of -35%. |
| 69 | + /// |
| 70 | + /// If the `Node` is running but the Lightning Wallet hasn't been synced |
| 71 | + /// yet, the severity is minor with a weightage value of -25%. |
| 72 | + pub fn calculate_ldk_server_health_score(&self, node: &Node) -> i64 { |
| 73 | + Self::compute_health_score( |
| 74 | + node.status().is_running, |
| 75 | + !node.list_peers().is_empty(), |
| 76 | + node.status().latest_lightning_wallet_sync_timestamp.is_some(), |
| 77 | + ) |
| 78 | + } |
| 79 | + |
| 80 | + pub fn gather_metrics(&self) -> Result<String, LdkServerError> { |
| 81 | + let mut buffer = Vec::new(); |
| 82 | + let encoder = TextEncoder::new(); |
| 83 | + |
| 84 | + let all_metrics = gather(); |
| 85 | + encoder.encode(&all_metrics, &mut buffer)?; |
| 86 | + Ok(String::from_utf8(buffer)?) |
| 87 | + } |
| 88 | + |
| 89 | + fn compute_health_score(is_running: bool, has_peers: bool, is_wallet_synced: bool) -> i64 { |
| 90 | + if !is_running { |
| 91 | + return 0; |
| 92 | + } |
| 93 | + |
| 94 | + let mut health_score = 100; |
| 95 | + |
| 96 | + if !has_peers { |
| 97 | + health_score -= 35; |
| 98 | + } |
| 99 | + |
| 100 | + if !is_wallet_synced { |
| 101 | + health_score -= 25; |
| 102 | + } |
| 103 | + |
| 104 | + health_score |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +#[cfg(test)] |
| 109 | +mod tests { |
| 110 | + use super::*; |
| 111 | + |
| 112 | + #[test] |
| 113 | + fn test_compute_health_score() { |
| 114 | + // Node is not running |
| 115 | + assert_eq!(Metrics::compute_health_score(false, true, true), 0); |
| 116 | + assert_eq!(Metrics::compute_health_score(false, false, false), 0); |
| 117 | + |
| 118 | + // Node is running, connected to a peer and wallet is synced |
| 119 | + assert_eq!(Metrics::compute_health_score(true, true, true), 100); |
| 120 | + |
| 121 | + // Node is running, not connected to a peer but wallet is synced |
| 122 | + assert_eq!(Metrics::compute_health_score(true, false, true), 65); |
| 123 | + |
| 124 | + // Node is running, connected to a peer but wallet is not synced |
| 125 | + assert_eq!(Metrics::compute_health_score(true, true, false), 75); |
| 126 | + |
| 127 | + // Node is running, not connected to a peer and wallet is not synced |
| 128 | + assert_eq!(Metrics::compute_health_score(true, false, false), 40); |
| 129 | + } |
| 130 | + |
| 131 | + #[test] |
| 132 | + fn test_gather_metrics_format() { |
| 133 | + let result = METRICS.gather_metrics(); |
| 134 | + assert!(result.is_ok()); |
| 135 | + let output = result.unwrap(); |
| 136 | + assert!(output.contains("ldk_health_score")); |
| 137 | + } |
| 138 | +} |
0 commit comments