iota_aws_orchestrator/
logs.rs

1// Copyright (c) Mysten Labs, Inc.
2// Modifications Copyright (c) 2024 IOTA Stiftung
3// SPDX-License-Identifier: Apache-2.0
4
5use std::cmp::max;
6
7use crate::display;
8
9/// A simple log analyzer counting the number of errors and panics.
10#[derive(Default)]
11pub struct LogsAnalyzer {
12    /// The number of errors in the nodes' log files.
13    pub node_errors: usize,
14    /// Whether a node panicked.
15    pub node_panic: bool,
16    /// The number of errors int he clients' log files.
17    pub client_errors: usize,
18    /// Whether a client panicked.
19    pub client_panic: bool,
20}
21
22impl LogsAnalyzer {
23    /// Deduce the number of nodes errors from the logs.
24    pub fn set_node_errors(&mut self, log: &str) {
25        self.node_errors = log.matches(" ERROR").count();
26        self.node_panic = log.contains("panic");
27    }
28
29    /// Deduce the number of clients errors from the logs.
30    pub fn set_client_errors(&mut self, log: &str) {
31        self.client_errors = max(self.client_errors, log.matches(" ERROR").count());
32        self.client_panic = log.contains("panic");
33    }
34
35    /// Aggregate multiple log analyzers into one, based on the analyzer that
36    /// found the most serious errors.
37    pub fn aggregate(counters: Vec<Self>) -> Self {
38        let mut highest = Self::default();
39        for counter in counters {
40            if counter.node_panic || counter.client_panic {
41                return counter;
42            } else if counter.client_errors > highest.client_errors
43                || counter.node_errors > highest.node_errors
44            {
45                highest = counter;
46            }
47        }
48        highest
49    }
50
51    /// Print a summary of the errors.
52    pub fn print_summary(&self) {
53        if self.node_panic {
54            display::error("Node(s) panicked!");
55        } else if self.client_panic {
56            display::error("Client(s) panicked!");
57        } else if self.node_errors != 0 || self.client_errors != 0 {
58            display::newline();
59            display::warn(format!(
60                "Logs contain errors (node: {}, client: {})",
61                self.node_errors, self.client_errors
62            ));
63        }
64    }
65}