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 /// Record that an Omaha event report was lost.
57 OmahaEventLost(Event),
58}
59
60#[derive(Debug, Eq, PartialEq)]
61pub enum UpdateCheckFailureReason {
62 Omaha = 0,
63 Network = 1,
64 Proxy = 2,
65 Configuration = 3,
66 Internal = 4,
67}
68
69#[derive(Debug, Eq, PartialEq)]
70pub enum ClockType {
71 Monotonic,
72 Wall,
73}
74
75pub trait MetricsReporter {
76 fn report_metrics(&mut self, metrics: Metrics) -> Result<(), Error>;
77}
78
79impl<T> MetricsReporter for &mut T
80where
81 T: MetricsReporter,
82{
83 fn report_metrics(&mut self, metrics: Metrics) -> Result<(), Error> {
84 (*self).report_metrics(metrics)
85 }
86}
87
88impl<T> MetricsReporter for Rc<RefCell<T>>
89where
90 T: MetricsReporter,
91{
92 fn report_metrics(&mut self, metrics: Metrics) -> Result<(), Error> {
93 self.borrow_mut().report_metrics(metrics)
94 }
95}