omaha_client/
metrics.rs

1// Copyright 2019 The Fuchsia Authors
2//
3// Licensed under a BSD-style license <LICENSE-BSD>, Apache License, Version 2.0
4// <LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0>, or the MIT
5// license <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your option.
6// This file may not be copied, modified, or distributed except according to
7// those terms.
8
9use {
10    crate::protocol::request::{Event, InstallSource},
11    anyhow::Error,
12    std::{cell::RefCell, rc::Rc, time::Duration},
13};
14
15#[cfg(test)]
16mod mock;
17#[cfg(test)]
18pub use mock::MockMetricsReporter;
19mod stub;
20pub use stub::StubMetricsReporter;
21
22/// The list of metrics that can be reported.
23#[derive(Debug, Eq, PartialEq)]
24pub enum Metrics {
25    /// Elapsed time from sending an update check to getting a response from Omaha, with a bool to
26    /// hold whether that was a success or a failure.
27    UpdateCheckResponseTime {
28        response_time: Duration,
29        successful: bool,
30    },
31    /// Elapsed time from the previous update check to the current update check.
32    UpdateCheckInterval {
33        interval: Duration,
34        clock: ClockType,
35        install_source: InstallSource,
36    },
37    /// Elapsed time from starting an update to having successfully applied it.
38    SuccessfulUpdateDuration(Duration),
39    /// Elapsed time from first seeing an update to having successfully applied it.
40    SuccessfulUpdateFromFirstSeen(Duration),
41    /// Elapsed time from starting an update to encountering a failure.
42    FailedUpdateDuration(Duration),
43    /// Why an update check failed (network, omaha, proxy, etc).
44    UpdateCheckFailureReason(UpdateCheckFailureReason),
45    /// Number of omaha request attempts until a response within a single update check attempt,
46    /// with a bool to hold whether that was a success or a failure.
47    RequestsPerCheck { count: u64, successful: bool },
48    /// Number of update check attempts to get an update check to succeed.
49    AttemptsToSuccessfulCheck(u64),
50    /// Number of install attempts to get an update to succeed.
51    AttemptsToSuccessfulInstall { count: u64, successful: bool },
52    /// Elapsed time from having finished applying the update to when finally
53    /// running that software, it is sent after the reboot (and includes the
54    /// rebooting time).
55    WaitedForRebootDuration(Duration),
56    /// Number of times an update failed to boot into new version.
57    FailedBootAttempts(u64),
58    /// Record that an Omaha event report was lost.
59    OmahaEventLost(Event),
60}
61
62#[derive(Debug, Eq, PartialEq)]
63pub enum UpdateCheckFailureReason {
64    Omaha = 0,
65    Network = 1,
66    Proxy = 2,
67    Configuration = 3,
68    Internal = 4,
69}
70
71#[derive(Debug, Eq, PartialEq)]
72pub enum ClockType {
73    Monotonic,
74    Wall,
75}
76
77pub trait MetricsReporter {
78    fn report_metrics(&mut self, metrics: Metrics) -> Result<(), Error>;
79}
80
81impl<T> MetricsReporter for &mut T
82where
83    T: MetricsReporter,
84{
85    fn report_metrics(&mut self, metrics: Metrics) -> Result<(), Error> {
86        (*self).report_metrics(metrics)
87    }
88}
89
90impl<T> MetricsReporter for Rc<RefCell<T>>
91where
92    T: MetricsReporter,
93{
94    fn report_metrics(&mut self, metrics: Metrics) -> Result<(), Error> {
95        self.borrow_mut().report_metrics(metrics)
96    }
97}