fuchsia_triage/
plugins.rs

1// Copyright 2020 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use crate::act::{Action, ActionResults, Gauge, Severity};
6use crate::metrics::fetch::FileDataFetcher;
7use crate::metrics::metric_value::MetricValue;
8
9mod crashes;
10mod helpers;
11mod memory;
12mod routing;
13mod sandbox_errors;
14
15pub trait Plugin {
16    /// Returns a unique name for the plugin.
17    ///
18    /// This is the value selected and listed on the command line.
19    fn name(&self) -> &'static str;
20
21    /// Returns a human-readable name for the plugin.
22    ///
23    /// This will be displayed as the label for results from this plugin.
24    fn display_name(&self) -> &'static str;
25
26    /// Run the plugin on the given inputs to produce results.
27    fn run(&self, inputs: &FileDataFetcher<'_>) -> ActionResults {
28        let mut results = ActionResults::new();
29        results.sort_gauges = false;
30        let structured_results = self.run_structured(inputs);
31        for action in structured_results {
32            match action {
33                Action::Alert(alert) => match alert.severity {
34                    Severity::Info => results.infos.push(alert.print),
35                    Severity::Warning => results.warnings.push(alert.print),
36                    Severity::Error => results.errors.push(alert.print),
37                },
38                Action::Gauge(Gauge { tag: Some(tag), value, .. }) => {
39                    if let Some(MetricValue::String(raw_value)) = value.cached_value.into_inner() {
40                        results.gauges.push(format!("{}: {}", tag, raw_value));
41                    }
42                }
43                _ => (),
44            }
45        }
46        results
47    }
48
49    /// Run the plugin on the given inputs to produce results keyed by name.
50    fn run_structured(&self, inputs: &FileDataFetcher<'_>) -> Vec<Action>;
51}
52
53/// Retrieve the list of all plugins registered with this library.
54pub fn register_plugins() -> Vec<Box<dyn Plugin>> {
55    vec![
56        Box::new(crashes::CrashesPlugin {}),
57        Box::new(sandbox_errors::SandboxErrorsPlugin {}),
58        Box::new(routing::RoutingErrorsPlugin {}),
59        Box::new(memory::MemoryPlugin {}),
60    ]
61}