glean/configuration.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 log::LevelFilter;
6
7use crate::net::PingUploader;
8
9use std::collections::HashMap;
10use std::path::PathBuf;
11use std::time::Duration;
12
13/// The default server pings are sent to.
14pub(crate) const DEFAULT_GLEAN_ENDPOINT: &str = "https://incoming.telemetry.mozilla.org";
15
16/// The Glean configuration.
17///
18/// Optional values will be filled in with default values.
19#[derive(Debug)]
20pub struct Configuration {
21 /// Whether upload should be enabled.
22 pub upload_enabled: bool,
23 /// Path to a directory to store all data in.
24 pub data_path: PathBuf,
25 /// The application ID (will be sanitized during initialization).
26 pub application_id: String,
27 /// The maximum number of events to store before sending a ping containing events.
28 pub max_events: Option<usize>,
29 /// Whether Glean should delay persistence of data from metrics with ping lifetime.
30 pub delay_ping_lifetime_io: bool,
31 /// The server pings are sent to.
32 pub server_endpoint: Option<String>,
33 /// The instance of the uploader used to send pings.
34 pub uploader: Option<Box<dyn PingUploader + 'static>>,
35 /// Whether Glean should schedule "metrics" pings for you.
36 pub use_core_mps: bool,
37 /// Whether Glean should limit its storage to only that of registered pings.
38 /// Unless you know that all your and your libraries' pings are appropriately registered
39 /// _before_ init, you shouldn't use this.
40 pub trim_data_to_registered_pings: bool,
41 /// The internal logging level.
42 pub log_level: Option<LevelFilter>,
43 /// The rate pings may be uploaded before they are throttled.
44 pub rate_limit: Option<crate::PingRateLimit>,
45 /// Whether to add a wallclock timestamp to all events.
46 pub enable_event_timestamps: bool,
47 /// An experimentation identifier derived by the application to be sent with all pings, it should
48 /// be noted that this has an underlying StringMetric and so should conform to the limitations that
49 /// StringMetric places on length, etc.
50 pub experimentation_id: Option<String>,
51 /// Whether to enable internal pings. Default: true
52 pub enable_internal_pings: bool,
53 /// A ping schedule map.
54 /// Maps a ping name to a list of pings to schedule along with it.
55 /// Only used if the ping's own ping schedule list is empty.
56 pub ping_schedule: HashMap<String, Vec<String>>,
57 /// Write count threshold when to auto-flush. `0` disables it.
58 pub ping_lifetime_threshold: usize,
59 /// After what time to auto-flush. 0 disables it.
60 pub ping_lifetime_max_time: Duration,
61}
62
63/// Configuration builder.
64///
65/// Let's you build a configuration from the required fields
66/// and let you set optional fields individually.
67#[derive(Debug)]
68pub struct Builder {
69 /// Required: Whether upload should be enabled.
70 pub upload_enabled: bool,
71 /// Required: Path to a directory to store all data in.
72 pub data_path: PathBuf,
73 /// Required: The application ID (will be sanitized during initialization).
74 pub application_id: String,
75 /// Optional: The maximum number of events to store before sending a ping containing events.
76 /// Default: `None`
77 pub max_events: Option<usize>,
78 /// Optional: Whether Glean should delay persistence of data from metrics with ping lifetime.
79 /// Default: `false`
80 pub delay_ping_lifetime_io: bool,
81 /// Optional: The server pings are sent to.
82 /// Default: `None`
83 pub server_endpoint: Option<String>,
84 /// Optional: The instance of the uploader used to send pings.
85 /// Default: `None`
86 pub uploader: Option<Box<dyn PingUploader + 'static>>,
87 /// Optional: Whether Glean should schedule "metrics" pings for you.
88 /// Default: `false`
89 pub use_core_mps: bool,
90 /// Optional: Whether Glean should limit its storage to only that of registered pings.
91 /// Unless you know that all your and your libraries' pings are appropriately registered
92 /// _before_ init, you shouldn't use this.
93 /// Default: `false`
94 pub trim_data_to_registered_pings: bool,
95 /// Optional: The internal logging level.
96 /// Default: `None`
97 pub log_level: Option<LevelFilter>,
98 /// Optional: The internal ping upload rate limit.
99 /// Default: `None`
100 pub rate_limit: Option<crate::PingRateLimit>,
101 /// Whether to add a wallclock timestamp to all events.
102 pub enable_event_timestamps: bool,
103 /// An experimentation identifier derived by the application to be sent with all pings, it should
104 /// be noted that this has an underlying StringMetric and so should conform to the limitations that
105 /// StringMetric places on length, etc.
106 pub experimentation_id: Option<String>,
107 /// Whether to enable internal pings. Default: true
108 pub enable_internal_pings: bool,
109 /// A ping schedule map.
110 /// Maps a ping name to a list of pings to schedule along with it.
111 /// Only used if the ping's own ping schedule list is empty.
112 pub ping_schedule: HashMap<String, Vec<String>>,
113 /// Write count threshold when to auto-flush. `0` disables it.
114 pub ping_lifetime_threshold: usize,
115 /// After what time to auto-flush. 0 disables it.
116 pub ping_lifetime_max_time: Duration,
117}
118
119impl Builder {
120 /// A new configuration builder.
121 pub fn new<P: Into<PathBuf>, S: Into<String>>(
122 upload_enabled: bool,
123 data_path: P,
124 application_id: S,
125 ) -> Self {
126 Self {
127 upload_enabled,
128 data_path: data_path.into(),
129 application_id: application_id.into(),
130 max_events: None,
131 delay_ping_lifetime_io: false,
132 server_endpoint: None,
133 uploader: None,
134 use_core_mps: false,
135 trim_data_to_registered_pings: false,
136 log_level: None,
137 rate_limit: None,
138 enable_event_timestamps: true,
139 experimentation_id: None,
140 enable_internal_pings: true,
141 ping_schedule: HashMap::new(),
142 ping_lifetime_threshold: 0,
143 ping_lifetime_max_time: Duration::ZERO,
144 }
145 }
146
147 /// Generate the full configuration.
148 pub fn build(self) -> Configuration {
149 Configuration {
150 upload_enabled: self.upload_enabled,
151 data_path: self.data_path,
152 application_id: self.application_id,
153 max_events: self.max_events,
154 delay_ping_lifetime_io: self.delay_ping_lifetime_io,
155 server_endpoint: self.server_endpoint,
156 uploader: self.uploader,
157 use_core_mps: self.use_core_mps,
158 trim_data_to_registered_pings: self.trim_data_to_registered_pings,
159 log_level: self.log_level,
160 rate_limit: self.rate_limit,
161 enable_event_timestamps: self.enable_event_timestamps,
162 experimentation_id: self.experimentation_id,
163 enable_internal_pings: self.enable_internal_pings,
164 ping_schedule: self.ping_schedule,
165 ping_lifetime_threshold: self.ping_lifetime_threshold,
166 ping_lifetime_max_time: self.ping_lifetime_max_time,
167 }
168 }
169
170 /// Set the maximum number of events to store before sending a ping containing events.
171 pub fn with_max_events(mut self, max_events: usize) -> Self {
172 self.max_events = Some(max_events);
173 self
174 }
175
176 /// Set whether Glean should delay persistence of data from metrics with ping lifetime.
177 pub fn with_delay_ping_lifetime_io(mut self, value: bool) -> Self {
178 self.delay_ping_lifetime_io = value;
179 self
180 }
181
182 /// Set the server pings are sent to.
183 pub fn with_server_endpoint<S: Into<String>>(mut self, server_endpoint: S) -> Self {
184 self.server_endpoint = Some(server_endpoint.into());
185 self
186 }
187
188 /// Set the instance of the uploader used to send pings.
189 pub fn with_uploader<U: PingUploader + 'static>(mut self, uploader: U) -> Self {
190 self.uploader = Some(Box::new(uploader));
191 self
192 }
193
194 /// Set whether Glean should schedule "metrics" pings for you.
195 pub fn with_use_core_mps(mut self, value: bool) -> Self {
196 self.use_core_mps = value;
197 self
198 }
199
200 /// Set whether Glean should limit its storage to only that of registered pings.
201 pub fn with_trim_data_to_registered_pings(mut self, value: bool) -> Self {
202 self.trim_data_to_registered_pings = value;
203 self
204 }
205
206 /// Set whether to add a wallclock timestamp to all events (experimental).
207 pub fn with_event_timestamps(mut self, value: bool) -> Self {
208 self.enable_event_timestamps = value;
209 self
210 }
211
212 /// Set whether to add a wallclock timestamp to all events (experimental).
213 pub fn with_experimentation_id(mut self, value: String) -> Self {
214 self.experimentation_id = Some(value);
215 self
216 }
217
218 /// Set whether to enable internal pings.
219 pub fn with_internal_pings(mut self, value: bool) -> Self {
220 self.enable_internal_pings = value;
221 self
222 }
223
224 /// Set the ping schedule map.
225 pub fn with_ping_schedule(mut self, value: HashMap<String, Vec<String>>) -> Self {
226 self.ping_schedule = value;
227 self
228 }
229
230 /// Write count threshold when to auto-flush. `0` disables it.
231 pub fn with_ping_lifetime_threshold(mut self, value: usize) -> Self {
232 self.ping_lifetime_threshold = value;
233 self
234 }
235
236 /// After what time to auto-flush. 0 disables it.
237 pub fn with_ping_lifetime_max_time(mut self, value: Duration) -> Self {
238 self.ping_lifetime_max_time = value;
239 self
240 }
241}