Skip to main content

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