glean_core/metrics/remote_settings_config.rs
1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at https://mozilla.org/MPL/2.0/.
4
5use std::collections::HashMap;
6
7use malloc_size_of_derive::MallocSizeOf;
8use serde::{Deserialize, Serialize};
9
10/// Represents a list of metrics and an associated boolean property
11/// indicating if the metric is enabled from the remote-settings
12/// configuration store.
13///
14/// The expected format of this data is stringified JSON in the following format:
15///
16/// ```json
17/// {
18/// "category.metric_name": true
19/// }
20/// ```
21#[derive(Serialize, Deserialize, Debug, Clone, Default, MallocSizeOf)]
22pub struct RemoteSettingsConfig {
23 /// This is a `HashMap` consisting of base_identifiers as keys
24 /// and bool values representing an override for the `disabled`
25 /// property of the metric, only inverted to reduce confusion.
26 /// If a particular metric has a value of `true` here, it means
27 /// the default of the metric will be overriden and set to the
28 /// enabled state.
29 #[serde(default, skip_serializing_if = "HashMap::is_empty")]
30 pub metrics_enabled: HashMap<String, bool>,
31
32 /// This is a `HashMap` consisting of ping names as keys and
33 /// boolean values representing on override for the default
34 /// enabled state of the ping of the same name.
35 #[serde(default, skip_serializing_if = "HashMap::is_empty")]
36 pub pings_enabled: HashMap<String, bool>,
37
38 /// The threshold of events that will be buffered before an events ping is
39 /// collected and submitted.
40 /// It overrides the value configured at initialization time.
41 #[serde(default, skip_serializing_if = "Option::is_none")]
42 pub event_threshold: Option<u32>,
43
44 /// Remote override for the session sampling rate (0.0–1.0).
45 /// When set, this overrides the value configured at initialization time.
46 /// Changes take effect at the next session start.
47 #[serde(default)]
48 pub session_sample_rate: Option<f64>,
49}
50
51impl RemoteSettingsConfig {
52 /// Creates a new RemoteSettingsConfig
53 pub fn new() -> Self {
54 Default::default()
55 }
56}
57
58impl TryFrom<String> for RemoteSettingsConfig {
59 type Error = crate::ErrorKind;
60
61 fn try_from(json: String) -> Result<Self, Self::Error> {
62 match serde_json::from_str(json.as_str()) {
63 Ok(config) => Ok(config),
64 Err(e) => Err(crate::ErrorKind::Json(e)),
65 }
66 }
67}