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 { response_time: Duration, successful: bool },
28 /// Elapsed time from the previous update check to the current update check.
29 UpdateCheckInterval { interval: Duration, clock: ClockType, install_source: InstallSource },
30 /// Elapsed time from starting an update to having successfully applied it.
31 SuccessfulUpdateDuration(Duration),
32 /// Elapsed time from first seeing an update to having successfully applied it.
33 SuccessfulUpdateFromFirstSeen(Duration),
34 /// Elapsed time from starting an update to encountering a failure.
35 FailedUpdateDuration(Duration),
36 /// Why an update check failed (network, omaha, proxy, etc).
37 UpdateCheckFailureReason(UpdateCheckFailureReason),
38 /// Number of omaha request attempts until a response within a single update check attempt,
39 /// with a bool to hold whether that was a success or a failure.
40 RequestsPerCheck { count: u64, successful: bool },
41 /// Number of update check attempts to get an update check to succeed.
42 AttemptsToSuccessfulCheck(u64),
43 /// Number of install attempts to get an update to succeed.
44 AttemptsToSuccessfulInstall { count: u64, successful: bool },
45 /// Elapsed time from having finished applying the update to when finally
46 /// running that software, it is sent after the reboot (and includes the
47 /// rebooting time).
48 WaitedForRebootDuration(Duration),
49 /// Record that an Omaha event report was lost.
50 OmahaEventLost(Event),
51}
52
53#[derive(Debug, Eq, PartialEq)]
54pub enum UpdateCheckFailureReason {
55 Omaha = 0,
56 Network = 1,
57 Proxy = 2,
58 Configuration = 3,
59 Internal = 4,
60}
61
62#[derive(Debug, Eq, PartialEq)]
63pub enum ClockType {
64 Monotonic,
65 Wall,
66}
67
68pub trait MetricsReporter {
69 fn report_metrics(&mut self, metrics: Metrics) -> Result<(), Error>;
70}
71
72impl<T> MetricsReporter for &mut T
73where
74 T: MetricsReporter,
75{
76 fn report_metrics(&mut self, metrics: Metrics) -> Result<(), Error> {
77 (*self).report_metrics(metrics)
78 }
79}
80
81impl<T> MetricsReporter for Rc<RefCell<T>>
82where
83 T: MetricsReporter,
84{
85 fn report_metrics(&mut self, metrics: Metrics) -> Result<(), Error> {
86 self.borrow_mut().report_metrics(metrics)
87 }
88}