sui_aws_orchestrator/
logs.rs

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