fuchsia_cobalt_builders/
metric_event_builder.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// Copyright 2019 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

//! Helper builder for constructing a `MetricEvent`.

use cobalt_client::traits::AsEventCodes;
use fidl_fuchsia_metrics::{HistogramBucket, MetricEvent, MetricEventPayload};

///  Adds the `builder()` method to `MetricEvent`.
pub trait MetricEventExt {
    /// Returns a `MetricEventBuilder` for the specified `metric_id`.
    ///
    /// # Examples
    ///
    /// ```
    /// assert_eq!(MetricEvent::builder(5).as_event().metric_id, 0);
    /// ```
    fn builder(metric_id: u32) -> MetricEventBuilder;
}

impl MetricEventExt for MetricEvent {
    fn builder(metric_id: u32) -> MetricEventBuilder {
        MetricEventBuilder { metric_id, ..MetricEventBuilder::default() }
    }
}

/// MetricEventBuilder allows for a chained construction of `MetricEvent` objects.
#[derive(Debug, Default, Clone)]
pub struct MetricEventBuilder {
    metric_id: u32,
    event_codes: Vec<u32>,
}

impl MetricEventBuilder {
    /// Appends the provided `event_code` to the `event_codes` list.
    ///
    /// # Examples
    ///
    /// ```
    /// assert_eq!(MetricEvent::builder(6).with_event_code(10).as_event().event_codes, vec![10]);
    /// ```
    pub fn with_event_code(mut self, event_code: u32) -> MetricEventBuilder {
        self.event_codes.push(event_code);
        self
    }

    /// Overrides the list of event_codes with the provided `event_codes`.
    ///
    /// # Examples
    ///
    /// ```
    /// assert_eq!(
    ///     MetricEvent::builder(7).with_event_codes([1, 2, 3]).as_event().event_codes,
    ///     vec![1,2,3]);
    /// ```
    pub fn with_event_codes<Codes: AsEventCodes>(
        mut self,
        event_codes: Codes,
    ) -> MetricEventBuilder {
        self.event_codes = event_codes.as_event_codes();
        self
    }

    /// Writes an `event_code` to a particular `index`. This method is useful when not assigning
    /// event codes in order.
    ///
    /// # Examples
    ///
    /// ```
    /// assert_eq!(
    ///     MetricEvent::builder(8).with_event_code_at(1, 10).as_event().event_codes,
    ///     vec![0, 10]);
    /// ```
    ///
    /// # Panics
    ///
    /// Panics if the `value`  is greater than or equal to 5.
    pub fn with_event_code_at(mut self, index: usize, event_code: u32) -> MetricEventBuilder {
        assert!(
            index < 5,
            "Invalid index passed to MetricEventBuilder::with_event_code. Cobalt events cannot support more than 5 event_codes."
        );
        while self.event_codes.len() <= index {
            self.event_codes.push(0);
        }
        self.event_codes[index] = event_code;
        self
    }

    /// Constructs a `MetricEvent` with the provided `MetricEventPayload`.
    ///
    /// # Examples
    /// ```
    /// let payload = MetricEventPayload::Event(fidl_fuchsia_cobalt::Event);
    /// assert_eq!(MetricEvent::builder(10).build(payload.clone()).payload, payload);
    /// ```
    pub fn build(self, payload: MetricEventPayload) -> MetricEvent {
        MetricEvent { metric_id: self.metric_id, event_codes: self.event_codes, payload }
    }

    /// Constructs a `MetricEvent` with a payload type of `MetricEventPayload::Count`.
    ///
    /// # Examples
    /// ```
    /// asert_eq!(
    ///     MetricEvent::builder(11).as_occurrence(10).payload,
    ///     MetricEventPayload::Event(fidl_fuchsia_cobalt::Count(10)));
    /// ```
    pub fn as_occurrence(self, count: u64) -> MetricEvent {
        self.build(MetricEventPayload::Count(count))
    }

    /// Constructs a `MetricEvent` with a payload type of `MetricEventPayload::IntegerValue`.
    ///
    /// # Examples
    /// ```
    /// asert_eq!(
    ///     MetricEvent::builder(12).as_integer(5).payload,
    ///     MetricEventPayload::IntegerValue(5)));
    /// ```
    pub fn as_integer(self, integer_value: i64) -> MetricEvent {
        self.build(MetricEventPayload::IntegerValue(integer_value))
    }

    /// Constructs a `MetricEvent` with a payload type of `MetricEventPayload::Histogram`.
    ///
    /// # Examples
    /// ```
    /// let histogram = vec![HistogramBucket { index: 0, count: 1 }];
    /// asert_eq!(
    ///     MetricEvent::builder(17).as_int_histogram(histogram.clone()).payload,
    ///     MetricEventPayload::Histogram(histogram));
    /// ```
    pub fn as_integer_histogram(self, histogram: Vec<HistogramBucket>) -> MetricEvent {
        self.build(MetricEventPayload::Histogram(histogram))
    }

    /// Constructs a `MetricEvent` with a payload type of `MetricEventPayload::StringValue`.
    ///
    /// # Examples
    /// ```
    /// asert_eq!(
    ///     MetricEvent::builder(17).as_string("test").payload,
    ///     MetricEventPayload::StringValue("test".to_string()));
    /// ```
    pub fn as_string<S: Into<String>>(self, string: S) -> MetricEvent {
        self.build(MetricEventPayload::StringValue(string.into()))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_builder_as_occurrence() {
        let event = MetricEvent::builder(1).with_event_code(2).as_occurrence(3);
        let expected = MetricEvent {
            metric_id: 1,
            event_codes: vec![2],
            payload: MetricEventPayload::Count(3),
        };
        assert_eq!(event, expected);
    }

    #[test]
    fn test_builder_as_integer() {
        let event = MetricEvent::builder(2).with_event_code(3).as_integer(4);
        let expected = MetricEvent {
            metric_id: 2,
            event_codes: vec![3],
            payload: MetricEventPayload::IntegerValue(4),
        };
        assert_eq!(event, expected);
    }

    #[test]
    fn test_as_integer_histogram() {
        let event = MetricEvent::builder(7)
            .with_event_code(8)
            .as_integer_histogram(vec![HistogramBucket { index: 0, count: 1 }]);
        let expected = MetricEvent {
            metric_id: 7,
            event_codes: vec![8],
            payload: MetricEventPayload::Histogram(vec![HistogramBucket { index: 0, count: 1 }]),
        };
        assert_eq!(event, expected);
    }

    #[test]
    fn test_builder_as_string() {
        let event = MetricEvent::builder(2).with_event_code(3).as_string("value");
        let expected = MetricEvent {
            metric_id: 2,
            event_codes: vec![3],
            payload: MetricEventPayload::StringValue("value".into()),
        };
        assert_eq!(event, expected);
    }

    #[test]
    #[should_panic(expected = "Invalid index")]
    fn test_bad_event_code_at_index() {
        MetricEvent::builder(8).with_event_code_at(5, 10).as_occurrence(1);
    }
}