glean_core/core/mod.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;
6use std::fs::{self, File};
7use std::io::{self, Write};
8use std::path::{Path, PathBuf};
9use std::sync::atomic::{AtomicU8, Ordering};
10use std::sync::{Arc, Mutex};
11use std::time::Duration;
12
13use chrono::{DateTime, FixedOffset, SecondsFormat};
14use malloc_size_of_derive::MallocSizeOf;
15use once_cell::sync::OnceCell;
16use uuid::Uuid;
17
18use crate::database::Database;
19use crate::debug::DebugOptions;
20use crate::error::ClientIdFileError;
21use crate::event_database::EventDatabase;
22use crate::internal_metrics::{
23 AdditionalMetrics, CoreMetrics, DatabaseMetrics, ExceptionState, HealthMetrics,
24};
25use crate::internal_pings::InternalPings;
26use crate::metrics::{
27 self, ExperimentMetric, Metric, MetricType, PingType, RecordedExperiment, RemoteSettingsConfig,
28};
29use crate::ping::PingMaker;
30use crate::session::{self, EventSessionContext, SessionManager, SessionMode, SessionState};
31use crate::storage::{StorageManager, INTERNAL_STORAGE};
32use crate::upload::{PingUploadManager, PingUploadTask, UploadResult, UploadTaskAction};
33use crate::util::{local_now_with_offset, sanitize_application_id};
34use crate::{
35 scheduler, system, AttributionMetrics, CommonMetricData, DistributionMetrics, ErrorKind,
36 InternalConfiguration, Lifetime, PingRateLimit, Result, DEFAULT_MAX_EVENTS,
37 GLEAN_SCHEMA_VERSION, GLEAN_VERSION, KNOWN_CLIENT_ID,
38};
39
40const CLIENT_ID_PLAIN_FILENAME: &str = "client_id.txt";
41static GLEAN: OnceCell<Mutex<Glean>> = OnceCell::new();
42
43/// Rate limiting defaults
44/// 15 pings every 60 seconds.
45pub const DEFAULT_SECONDS_PER_INTERVAL: u64 = 60;
46pub const DEFAULT_PINGS_PER_INTERVAL: u32 = 15;
47
48pub fn global_glean() -> Option<&'static Mutex<Glean>> {
49 GLEAN.get()
50}
51
52/// Sets or replaces the global Glean object.
53pub fn setup_glean(glean: Glean) -> Result<()> {
54 // The `OnceCell` type wrapping our Glean is thread-safe and can only be set once.
55 // Therefore even if our check for it being empty succeeds, setting it could fail if a
56 // concurrent thread is quicker in setting it.
57 // However this will not cause a bigger problem, as the second `set` operation will just fail.
58 // We can log it and move on.
59 //
60 // For all wrappers this is not a problem, as the Glean object is intialized exactly once on
61 // calling `initialize` on the global singleton and further operations check that it has been
62 // initialized.
63 if GLEAN.get().is_none() {
64 if GLEAN.set(Mutex::new(glean)).is_err() {
65 log::warn!(
66 "Global Glean object is initialized already. This probably happened concurrently."
67 )
68 }
69 } else {
70 // We allow overriding the global Glean object to support test mode.
71 // In test mode the Glean object is fully destroyed and recreated.
72 // This all happens behind a mutex and is therefore also thread-safe..
73 let mut lock = GLEAN.get().unwrap().lock().unwrap();
74 *lock = glean;
75 }
76 Ok(())
77}
78
79/// Execute `f` passing the global Glean object.
80///
81/// Panics if the global Glean object has not been set.
82pub fn with_glean<F, R>(f: F) -> R
83where
84 F: FnOnce(&Glean) -> R,
85{
86 let glean = global_glean().expect("Global Glean object not initialized");
87 let lock = glean.lock().unwrap();
88 f(&lock)
89}
90
91/// Execute `f` passing the global Glean object mutable.
92///
93/// Panics if the global Glean object has not been set.
94pub fn with_glean_mut<F, R>(f: F) -> R
95where
96 F: FnOnce(&mut Glean) -> R,
97{
98 let glean = global_glean().expect("Global Glean object not initialized");
99 let mut lock = glean.lock().unwrap();
100 f(&mut lock)
101}
102
103/// Execute `f` passing the global Glean object if it has been set.
104///
105/// Returns `None` if the global Glean object has not been set.
106/// Returns `Some(T)` otherwise.
107pub fn with_opt_glean<F, R>(f: F) -> Option<R>
108where
109 F: FnOnce(&Glean) -> R,
110{
111 let glean = global_glean()?;
112 let lock = glean.lock().unwrap();
113 Some(f(&lock))
114}
115
116/// The object holding meta information about a Glean instance.
117///
118/// ## Example
119///
120/// Create a new Glean instance, register a ping, record a simple counter and then send the final
121/// ping.
122///
123/// ```rust,no_run
124/// # use glean_core::{Glean, InternalConfiguration, CommonMetricData, metrics::*};
125/// let cfg = InternalConfiguration {
126/// data_path: "/tmp/glean".into(),
127/// application_id: "glean.sample.app".into(),
128/// language_binding_name: "Rust".into(),
129/// upload_enabled: true,
130/// max_events: None,
131/// delay_ping_lifetime_io: false,
132/// app_build: "".into(),
133/// use_core_mps: false,
134/// trim_data_to_registered_pings: false,
135/// log_level: None,
136/// rate_limit: None,
137/// enable_event_timestamps: true,
138/// experimentation_id: None,
139/// enable_internal_pings: true,
140/// ping_schedule: Default::default(),
141/// ping_lifetime_threshold: 1000,
142/// ping_lifetime_max_time: 2000,
143/// max_pending_pings_count: None,
144/// max_pending_pings_directory_size: None,
145/// session_mode: glean_core::SessionMode::Auto,
146/// session_sample_rate: 1.0,
147/// session_inactivity_timeout_ms: 1_800_000,
148/// };
149/// let mut glean = Glean::new(cfg).unwrap();
150/// let ping = PingType::new("sample", true, false, true, true, true, vec![], vec![], true, vec![]);
151/// glean.register_ping_type(&ping);
152///
153/// let call_counter: CounterMetric = CounterMetric::new(CommonMetricData {
154/// name: "calls".into(),
155/// category: "local".into(),
156/// send_in_pings: vec!["sample".into()],
157/// ..Default::default()
158/// });
159///
160/// call_counter.add_sync(&glean, 1);
161///
162/// ping.submit_sync(&glean, None);
163/// ```
164///
165/// ## Note
166///
167/// In specific language bindings, this is usually wrapped in a singleton and all metric recording goes to a single instance of this object.
168/// In the Rust core, it is possible to create multiple instances, which is used in testing.
169#[derive(Debug, MallocSizeOf)]
170pub struct Glean {
171 upload_enabled: bool,
172 pub(crate) data_store: Option<Database>,
173 event_data_store: EventDatabase,
174 pub(crate) core_metrics: CoreMetrics,
175 pub(crate) additional_metrics: AdditionalMetrics,
176 pub(crate) database_metrics: DatabaseMetrics,
177 pub(crate) health_metrics: HealthMetrics,
178 pub(crate) internal_pings: InternalPings,
179 data_path: PathBuf,
180 application_id: String,
181 ping_registry: HashMap<String, PingType>,
182 #[ignore_malloc_size_of = "external non-allocating type"]
183 start_time: DateTime<FixedOffset>,
184 max_events: u32,
185 is_first_run: bool,
186 pub(crate) upload_manager: PingUploadManager,
187 debug: DebugOptions,
188 pub(crate) app_build: String,
189 pub(crate) schedule_metrics_pings: bool,
190 pub(crate) remote_settings_epoch: AtomicU8,
191 #[ignore_malloc_size_of = "TODO: Expose Glean's inner memory allocations (bug 1960592)"]
192 pub(crate) remote_settings_config: Arc<Mutex<RemoteSettingsConfig>>,
193 pub(crate) with_timestamps: bool,
194 pub(crate) ping_schedule: HashMap<String, Vec<String>>,
195 #[ignore_malloc_size_of = "TODO: Expose session memory allocations (bug 2043355)"]
196 pub(crate) session_manager: SessionManager,
197}
198
199impl Glean {
200 /// Creates and initializes a new Glean object for use in a subprocess.
201 ///
202 /// Importantly, this will not send any pings at startup, since that
203 /// sort of management should only happen in the main process.
204 pub fn new_for_subprocess(cfg: &InternalConfiguration, scan_directories: bool) -> Result<Self> {
205 log::info!("Creating new Glean v{}", GLEAN_VERSION);
206
207 let application_id = sanitize_application_id(&cfg.application_id);
208 if application_id.is_empty() {
209 return Err(ErrorKind::InvalidConfig.into());
210 }
211
212 let data_path = Path::new(&cfg.data_path);
213 let event_data_store = EventDatabase::new(data_path)?;
214
215 // Create an upload manager with rate limiting of 15 pings every 60 seconds.
216 let mut upload_manager = PingUploadManager::new(&cfg.data_path, &cfg.language_binding_name);
217 let rate_limit = cfg.rate_limit.as_ref().unwrap_or(&PingRateLimit {
218 seconds_per_interval: DEFAULT_SECONDS_PER_INTERVAL,
219 pings_per_interval: DEFAULT_PINGS_PER_INTERVAL,
220 });
221 upload_manager.set_rate_limiter(
222 rate_limit.seconds_per_interval,
223 rate_limit.pings_per_interval,
224 );
225 if let Some(n) = cfg.max_pending_pings_count {
226 upload_manager.set_max_pending_pings_count(n);
227 }
228 if let Some(n) = cfg.max_pending_pings_directory_size {
229 upload_manager.set_max_pending_pings_directory_size(n);
230 }
231
232 // We only scan the pending ping directories when calling this from a subprocess,
233 // when calling this from ::new we need to scan the directories after dealing with the upload state.
234 if scan_directories {
235 let _scanning_thread = upload_manager.scan_pending_pings_directories(false);
236 }
237
238 let start_time = local_now_with_offset();
239 let mut this = Self {
240 upload_enabled: cfg.upload_enabled,
241 // In the subprocess, we want to avoid accessing the database entirely.
242 // The easiest way to ensure that is to just not initialize it.
243 data_store: None,
244 event_data_store,
245 core_metrics: CoreMetrics::new(),
246 additional_metrics: AdditionalMetrics::new(),
247 database_metrics: DatabaseMetrics::new(),
248 health_metrics: HealthMetrics::new(),
249 internal_pings: InternalPings::new(cfg.enable_internal_pings),
250 upload_manager,
251 data_path: PathBuf::from(&cfg.data_path),
252 application_id,
253 ping_registry: HashMap::new(),
254 start_time,
255 max_events: cfg.max_events.unwrap_or(DEFAULT_MAX_EVENTS),
256 is_first_run: false,
257 debug: DebugOptions::new(),
258 app_build: cfg.app_build.to_string(),
259 // Subprocess doesn't use "metrics" pings so has no need for a scheduler.
260 schedule_metrics_pings: false,
261 remote_settings_epoch: AtomicU8::new(0),
262 remote_settings_config: Arc::new(Mutex::new(RemoteSettingsConfig::new())),
263 with_timestamps: cfg.enable_event_timestamps,
264 ping_schedule: cfg.ping_schedule.clone(),
265 // The SessionManager is deliberately left in its default (hollow)
266 // state for subprocesses. `restore_session_state_from_storage()`
267 // is only called in `Glean::new()`, not here, so the subprocess
268 // never loads or mutates the main process's persisted session
269 // state. This prevents subprocesses from interfering with the
270 // main process's session lifecycle (seq counters, dirty flags,
271 // boundary events, etc.).
272 session_manager: SessionManager::new(
273 cfg.session_mode,
274 cfg.session_sample_rate,
275 std::time::Duration::from_millis(cfg.session_inactivity_timeout_ms),
276 ),
277 };
278
279 // Ensuring these pings are registered.
280 let pings = this.internal_pings.clone();
281 this.register_ping_type(&pings.baseline);
282 this.register_ping_type(&pings.metrics);
283 this.register_ping_type(&pings.events);
284 this.register_ping_type(&pings.health);
285 this.register_ping_type(&pings.deletion_request);
286
287 Ok(this)
288 }
289
290 /// Creates and initializes a new Glean object.
291 ///
292 /// This will create the necessary directories and files in
293 /// [`cfg.data_path`](InternalConfiguration::data_path). This will also initialize
294 /// the core metrics.
295 pub fn new(cfg: InternalConfiguration) -> Result<Self> {
296 let mut glean = Self::new_for_subprocess(&cfg, false)?;
297
298 // Creating the data store creates the necessary path as well.
299 // If that fails we bail out and don't initialize further.
300 let data_path = Path::new(&cfg.data_path);
301 let ping_lifetime_threshold = cfg.ping_lifetime_threshold as usize;
302 let ping_lifetime_max_time = Duration::from_millis(cfg.ping_lifetime_max_time);
303 glean.data_store = Some(Database::new(
304 data_path,
305 cfg.delay_ping_lifetime_io,
306 ping_lifetime_threshold,
307 ping_lifetime_max_time,
308 )?);
309
310 glean.restore_session_state_from_storage();
311
312 // This code references different states from the "Client ID recovery" flowchart.
313 // See https://mozilla.github.io/glean/dev/core/internal/client_id_recovery.html for details.
314
315 // We don't have the database yet when we first encounter the error,
316 // so we store it and apply it later.
317 // state (a)
318 let stored_client_id = match glean.client_id_from_file() {
319 Ok(id) if id == *KNOWN_CLIENT_ID => {
320 glean
321 .health_metrics
322 .file_read_error
323 .get("c0ffee-in-file")
324 .add_sync(&glean, 1);
325 None
326 }
327 Ok(id) => Some(id),
328 Err(ClientIdFileError::NotFound) => {
329 // That's ok, the file might just not exist yet.
330 glean
331 .health_metrics
332 .file_read_error
333 .get("file-not-found")
334 .add_sync(&glean, 1);
335 None
336 }
337 Err(ClientIdFileError::PermissionDenied) => {
338 // state (b)
339 // Uhm ... who removed our permission?
340 glean
341 .health_metrics
342 .file_read_error
343 .get("permission-denied")
344 .add_sync(&glean, 1);
345 None
346 }
347 Err(ClientIdFileError::ParseError(e)) => {
348 // state (b)
349 log::trace!("reading cliend_id.txt. Could not parse into UUID: {e}");
350 glean
351 .health_metrics
352 .file_read_error
353 .get("parse")
354 .add_sync(&glean, 1);
355 None
356 }
357 Err(ClientIdFileError::IoError(e)) => {
358 // state (b)
359 // We can't handle other IO errors (most couldn't occur on this operation anyway)
360 log::trace!("reading client_id.txt. Unexpected io error: {e}");
361 glean
362 .health_metrics
363 .file_read_error
364 .get("io")
365 .add_sync(&glean, 1);
366 None
367 }
368 };
369
370 {
371 let data_store = glean.data_store.as_ref().unwrap();
372 let file_size = data_store.file_size.map(|n| n.get()).unwrap_or(0);
373
374 // If we have a client ID on disk, we check the database
375 if let Some(stored_client_id) = stored_client_id {
376 // state (c)
377 if file_size == 0 {
378 log::trace!("no database. database size={file_size}. stored_client_id={stored_client_id}");
379 // state (d)
380 glean
381 .health_metrics
382 .recovered_client_id
383 .set_from_uuid_sync(&glean, stored_client_id);
384 glean
385 .health_metrics
386 .exception_state
387 .set_sync(&glean, ExceptionState::EmptyDb);
388
389 // state (e) -- mitigation: store recovered client ID in DB
390 glean
391 .core_metrics
392 .client_id
393 .set_from_uuid_sync(&glean, stored_client_id);
394 } else {
395 let db_client_id = glean
396 .core_metrics
397 .client_id
398 .get_value(&glean, Some("glean_client_info"));
399
400 match db_client_id {
401 None => {
402 // state (f)
403 log::trace!("no client_id in DB. stored_client_id={stored_client_id}");
404 glean
405 .health_metrics
406 .exception_state
407 .set_sync(&glean, ExceptionState::RegenDb);
408
409 // state (e) -- mitigation: store recovered client ID in DB
410 glean
411 .core_metrics
412 .client_id
413 .set_from_uuid_sync(&glean, stored_client_id);
414 }
415 Some(db_client_id) if db_client_id == *KNOWN_CLIENT_ID => {
416 // state (i)
417 log::trace!(
418 "c0ffee client_id in DB, stored_client_id={stored_client_id}"
419 );
420 glean
421 .health_metrics
422 .recovered_client_id
423 .set_from_uuid_sync(&glean, stored_client_id);
424 glean
425 .health_metrics
426 .exception_state
427 .set_sync(&glean, ExceptionState::C0ffeeInDb);
428
429 // If we have a recovered client ID we also overwrite the database.
430 // state (e)
431 glean
432 .core_metrics
433 .client_id
434 .set_from_uuid_sync(&glean, stored_client_id);
435 }
436 Some(db_client_id) if db_client_id == stored_client_id => {
437 // all valid. nothing to do
438 log::trace!("database consistent. db_client_id == stored_client_id: {db_client_id}");
439 }
440 Some(db_client_id) => {
441 // state (g)
442 log::trace!(
443 "client_id mismatch. db_client_id{db_client_id}, stored_client_id={stored_client_id}. Overwriting file with db's client_id."
444 );
445 glean
446 .health_metrics
447 .recovered_client_id
448 .set_from_uuid_sync(&glean, stored_client_id);
449 glean
450 .health_metrics
451 .exception_state
452 .set_sync(&glean, ExceptionState::ClientIdMismatch);
453
454 // state (h)
455 glean.store_client_id_with_reporting(
456 db_client_id,
457 "client_id mismatch will re-occur.",
458 );
459 }
460 }
461 }
462 } else {
463 log::trace!("No stored client ID. Database might have it.");
464
465 let db_client_id = glean
466 .core_metrics
467 .client_id
468 .get_value(&glean, Some("glean_client_info"));
469 if let Some(db_client_id) = db_client_id {
470 // state (h)
471 glean.store_client_id_with_reporting(
472 db_client_id,
473 "Might happen on next init then.",
474 );
475 } else {
476 log::trace!("Database has no client ID either. We might be fresh!");
477 }
478 }
479 }
480
481 // Set experimentation identifier (if any)
482 if let Some(experimentation_id) = &cfg.experimentation_id {
483 glean
484 .additional_metrics
485 .experimentation_id
486 .set_sync(&glean, experimentation_id.to_string());
487 }
488
489 // The upload enabled flag may have changed since the last run, for
490 // example by the changing of a config file.
491 if cfg.upload_enabled {
492 // If upload is enabled, just follow the normal code path to
493 // instantiate the core metrics.
494 glean.on_upload_enabled();
495 } else {
496 // If upload is disabled, then clear the metrics
497 // but do not send a deletion request ping.
498 // If we have run before, and we have an old client_id,
499 // do the full upload disabled operations to clear metrics
500 // and send a deletion request ping.
501 match glean
502 .core_metrics
503 .client_id
504 .get_value(&glean, Some("glean_client_info"))
505 {
506 None => glean.clear_metrics(),
507 Some(uuid) => {
508 if let Err(e) = glean.remove_stored_client_id() {
509 log::error!("Couldn't remove client ID on disk. This might lead to a resurrection of this client ID later. Error: {e}");
510 }
511 if uuid == *KNOWN_CLIENT_ID {
512 // Previously Glean kept the KNOWN_CLIENT_ID stored.
513 // Let's ensure we erase it now.
514 if let Some(data) = glean.data_store.as_ref() {
515 _ = data.remove_single_metric(
516 Lifetime::User,
517 "glean_client_info",
518 "client_id",
519 );
520 }
521 } else {
522 // Temporarily enable uploading so we can submit a
523 // deletion request ping.
524 glean.upload_enabled = true;
525 glean.on_upload_disabled(true);
526 }
527 }
528 }
529 }
530
531 // We set this only for non-subprocess situations.
532 // If internal pings are disabled, we don't set up the MPS either,
533 // it wouldn't send any data anyway.
534 glean.schedule_metrics_pings = cfg.enable_internal_pings && cfg.use_core_mps;
535
536 // We only scan the pendings pings directories **after** dealing with the upload state.
537 // If upload is disabled, we delete all pending pings files
538 // and we need to do that **before** scanning the pending pings folder
539 // to ensure we don't enqueue pings before their files are deleted.
540 let _scanning_thread = glean.upload_manager.scan_pending_pings_directories(true);
541
542 Ok(glean)
543 }
544
545 /// For tests make it easy to create a Glean object using only the required configuration.
546 #[cfg(test)]
547 pub(crate) fn with_options(
548 data_path: &str,
549 application_id: &str,
550 upload_enabled: bool,
551 enable_internal_pings: bool,
552 ) -> Self {
553 let cfg = InternalConfiguration {
554 data_path: data_path.into(),
555 application_id: application_id.into(),
556 language_binding_name: "Rust".into(),
557 upload_enabled,
558 max_events: None,
559 delay_ping_lifetime_io: false,
560 app_build: "Unknown".into(),
561 use_core_mps: false,
562 trim_data_to_registered_pings: false,
563 log_level: None,
564 rate_limit: None,
565 enable_event_timestamps: true,
566 experimentation_id: None,
567 enable_internal_pings,
568 ping_schedule: Default::default(),
569 ping_lifetime_threshold: 0,
570 ping_lifetime_max_time: 0,
571 max_pending_pings_count: None,
572 max_pending_pings_directory_size: None,
573 session_mode: SessionMode::Auto,
574 session_sample_rate: 1.0,
575 session_inactivity_timeout_ms: 1_800_000,
576 };
577
578 let mut glean = Self::new(cfg).unwrap();
579
580 // Disable all upload manager policies for testing
581 glean.upload_manager = PingUploadManager::no_policy(data_path);
582
583 glean
584 }
585
586 /// Destroys the database.
587 ///
588 /// After this Glean needs to be reinitialized.
589 pub fn destroy_db(&mut self) {
590 self.data_store = None;
591 }
592
593 fn client_id_file_path(&self) -> PathBuf {
594 self.data_path.join(CLIENT_ID_PLAIN_FILENAME)
595 }
596
597 /// Write the client ID to a separate plain file on disk
598 ///
599 /// Use `store_client_id_with_reporting` to handle the error cases.
600 fn store_client_id(&self, client_id: Uuid) -> Result<(), ClientIdFileError> {
601 let mut fp = File::create(self.client_id_file_path())?;
602
603 let mut buffer = Uuid::encode_buffer();
604 let uuid_str = client_id.hyphenated().encode_lower(&mut buffer);
605 fp.write_all(uuid_str.as_bytes())?;
606 fp.sync_all()?;
607
608 Ok(())
609 }
610
611 /// Write the client ID to a separate plain file on disk
612 ///
613 /// When an error occurs an error message is logged and the error is counted in a metric.
614 fn store_client_id_with_reporting(&self, client_id: Uuid, msg: &str) {
615 if let Err(err) = self.store_client_id(client_id) {
616 log::error!(
617 "Could not write {client_id} to state file. {} Error: {err}",
618 msg
619 );
620 match err {
621 ClientIdFileError::NotFound => {
622 self.health_metrics
623 .file_write_error
624 .get("not-found")
625 .add_sync(self, 1);
626 }
627 ClientIdFileError::PermissionDenied => {
628 self.health_metrics
629 .file_write_error
630 .get("permission-denied")
631 .add_sync(self, 1);
632 }
633 ClientIdFileError::IoError(..) => {
634 self.health_metrics
635 .file_write_error
636 .get("io")
637 .add_sync(self, 1);
638 }
639 ClientIdFileError::ParseError(..) => {
640 log::error!("Parse error encountered on file write. This is impossible.");
641 }
642 }
643 }
644 }
645
646 /// Try to load a client ID from the plain file on disk.
647 fn client_id_from_file(&self) -> Result<Uuid, ClientIdFileError> {
648 let uuid_str = fs::read_to_string(self.client_id_file_path())?;
649 // We don't write a newline, but we still trim it. Who knows who else touches that file by accident.
650 // We're also a bit more lenient in what we accept here:
651 // uppercase, lowercase, with or without dashes, urn, braced (and whatever else `Uuid`
652 // parses by default).
653 let uuid = Uuid::try_parse(uuid_str.trim_end())?;
654 Ok(uuid)
655 }
656
657 /// Remove the stored client ID from disk.
658 /// Should only be called when the client ID is also removed from the database.
659 fn remove_stored_client_id(&self) -> Result<(), ClientIdFileError> {
660 match fs::remove_file(self.client_id_file_path()) {
661 Ok(()) => Ok(()),
662 Err(e) if e.kind() == io::ErrorKind::NotFound => {
663 // File was already missing. No need to report that.
664 Ok(())
665 }
666 Err(e) => Err(e.into()),
667 }
668 }
669
670 /// Initializes the core metrics managed by Glean's Rust core.
671 fn initialize_core_metrics(&mut self) {
672 let need_new_client_id = match self
673 .core_metrics
674 .client_id
675 .get_value(self, Some("glean_client_info"))
676 {
677 None => true,
678 Some(uuid) => uuid == *KNOWN_CLIENT_ID,
679 };
680 if need_new_client_id {
681 let new_clientid = self.core_metrics.client_id.generate_and_set_sync(self);
682 self.store_client_id_with_reporting(new_clientid, "New client in database only.");
683 }
684
685 if self
686 .core_metrics
687 .first_run_date
688 .get_value(self, "glean_client_info")
689 .is_none()
690 {
691 self.core_metrics.first_run_date.set_sync(self, None);
692 // The `first_run_date` field is generated on the very first run
693 // and persisted across upload toggling. We can assume that, the only
694 // time it is set, that's indeed our "first run".
695 self.is_first_run = true;
696 }
697
698 self.set_application_lifetime_core_metrics();
699 }
700
701 /// Initializes the database metrics managed by Glean's Rust core.
702 fn initialize_database_metrics(&mut self) {
703 log::trace!("Initializing database metrics");
704
705 if let Some(size) = self
706 .data_store
707 .as_ref()
708 .and_then(|database| database.file_size())
709 {
710 log::trace!("Database file size: {}", size.get());
711 self.database_metrics
712 .size
713 .accumulate_sync(self, size.get() as i64)
714 }
715
716 if let Some(rkv_load_state) = self
717 .data_store
718 .as_ref()
719 .and_then(|database| database.rkv_load_state())
720 {
721 self.database_metrics
722 .rkv_load_error
723 .set_sync(self, rkv_load_state)
724 }
725 }
726
727 /// Signals that the environment is ready to submit pings.
728 ///
729 /// Should be called when Glean is initialized to the point where it can correctly assemble pings.
730 /// Usually called from the language binding after all of the core metrics have been set
731 /// and the ping types have been registered.
732 ///
733 /// # Arguments
734 ///
735 /// * `trim_data_to_registered_pings` - Whether we should limit to storing data only for
736 /// data belonging to pings previously registered via `register_ping_type`.
737 ///
738 /// # Returns
739 ///
740 /// Whether the "events" ping was submitted.
741 pub fn on_ready_to_submit_pings(&mut self, trim_data_to_registered_pings: bool) -> bool {
742 // When upload is disabled on init we already clear out metrics.
743 // However at that point not all pings are registered and so we keep that data around.
744 // By the time we would be ready to submit we try again cleaning out metrics from
745 // now-known pings.
746 if !self.upload_enabled {
747 log::debug!("on_ready_to_submit_pings. let's clear pings once again.");
748 self.clear_metrics();
749 }
750
751 self.event_data_store
752 .flush_pending_events_on_startup(self, trim_data_to_registered_pings)
753 }
754
755 /// Sets whether upload is enabled or not.
756 ///
757 /// When uploading is disabled, metrics aren't recorded at all and no
758 /// data is uploaded.
759 ///
760 /// When disabling, all pending metrics, events and queued pings are cleared.
761 ///
762 /// When enabling, the core Glean metrics are recreated.
763 ///
764 /// If the value of this flag is not actually changed, this is a no-op.
765 ///
766 /// # Arguments
767 ///
768 /// * `flag` - When true, enable metric collection.
769 ///
770 /// # Returns
771 ///
772 /// Whether the flag was different from the current value,
773 /// and actual work was done to clear or reinstate metrics.
774 pub fn set_upload_enabled(&mut self, flag: bool) -> bool {
775 log::info!("Upload enabled: {:?}", flag);
776
777 if self.upload_enabled != flag {
778 if flag {
779 self.on_upload_enabled();
780 } else {
781 self.on_upload_disabled(false);
782 }
783 true
784 } else {
785 false
786 }
787 }
788
789 /// Enable or disable a ping.
790 ///
791 /// Disabling a ping causes all data for that ping to be removed from storage
792 /// and all pending pings of that type to be deleted.
793 ///
794 /// **Note**: Do not use directly. Call `PingType::set_enabled` instead.
795 #[doc(hidden)]
796 pub fn set_ping_enabled(&mut self, ping: &PingType, enabled: bool) {
797 ping.store_enabled(enabled);
798 if !enabled {
799 if let Some(data) = self.data_store.as_ref() {
800 _ = data.clear_ping_lifetime_storage(ping.name());
801 _ = data.clear_lifetime_storage(Lifetime::User, ping.name());
802 _ = data.clear_lifetime_storage(Lifetime::Application, ping.name());
803 }
804 let ping_maker = PingMaker::new();
805 let disabled_pings = &[ping.name()][..];
806 if let Err(err) = ping_maker.clear_pending_pings(self.get_data_path(), disabled_pings) {
807 log::warn!("Error clearing pending pings: {}", err);
808 }
809 }
810 }
811
812 /// Determines whether upload is enabled.
813 ///
814 /// When upload is disabled, no data will be recorded.
815 pub fn is_upload_enabled(&self) -> bool {
816 self.upload_enabled
817 }
818
819 /// Check if a ping is enabled.
820 ///
821 /// Note that some internal "ping" names are considered to be always enabled.
822 ///
823 /// If a ping is not known to Glean ("unregistered") it is always considered disabled.
824 /// If a ping is known, it can be enabled/disabled at any point.
825 /// Only data for enabled pings is recorded.
826 /// Disabled pings are never submitted.
827 pub fn is_ping_enabled(&self, ping: &str) -> bool {
828 // We "abuse" pings/storage names for internal data.
829 const DEFAULT_ENABLED: &[&str] = &[
830 "glean_client_info",
831 "glean_internal_info",
832 // for `experimentation_id`.
833 // That should probably have gone into `glean_internal_info` instead.
834 "all-pings",
835 ];
836
837 // `client_info`-like stuff is always enabled.
838 if DEFAULT_ENABLED.contains(&ping) {
839 return true;
840 }
841
842 let Some(ping) = self.ping_registry.get(ping) else {
843 log::trace!("Unknown ping {ping}. Assuming disabled.");
844 return false;
845 };
846
847 ping.enabled(self)
848 }
849
850 /// Handles the changing of state from upload disabled to enabled.
851 ///
852 /// Should only be called when the state actually changes.
853 ///
854 /// The `upload_enabled` flag is set to true and the core Glean metrics are
855 /// recreated.
856 fn on_upload_enabled(&mut self) {
857 self.upload_enabled = true;
858 self.initialize_core_metrics();
859 self.initialize_database_metrics();
860 }
861
862 /// Handles the changing of state from upload enabled to disabled.
863 ///
864 /// Should only be called when the state actually changes.
865 ///
866 /// A deletion_request ping is sent, all pending metrics, events and queued
867 /// pings are cleared, and the client_id is set to KNOWN_CLIENT_ID.
868 /// Afterward, the upload_enabled flag is set to false.
869 fn on_upload_disabled(&mut self, during_init: bool) {
870 // The upload_enabled flag should be true here, or the deletion ping
871 // won't be submitted.
872 let reason = if during_init {
873 Some("at_init")
874 } else {
875 Some("set_upload_enabled")
876 };
877 if !self
878 .internal_pings
879 .deletion_request
880 .submit_sync(self, reason)
881 {
882 log::error!("Failed to submit deletion-request ping on optout.");
883 }
884 self.clear_metrics();
885 self.upload_enabled = false;
886 }
887
888 /// Clear any pending metrics when telemetry is disabled.
889 fn clear_metrics(&mut self) {
890 // Clear the pending pings queue and acquire the lock
891 // so that it can't be accessed until this function is done.
892 let _lock = self.upload_manager.clear_ping_queue();
893
894 // Clear any pending pings that follow `collection_enabled`.
895 let ping_maker = PingMaker::new();
896 let disabled_pings = self
897 .ping_registry
898 .iter()
899 .filter(|&(_ping_name, ping)| ping.follows_collection_enabled())
900 .map(|(ping_name, _ping)| &ping_name[..])
901 .collect::<Vec<_>>();
902 if let Err(err) = ping_maker.clear_pending_pings(self.get_data_path(), &disabled_pings) {
903 log::warn!("Error clearing pending pings: {}", err);
904 }
905
906 if let Err(e) = self.remove_stored_client_id() {
907 log::error!("Couldn't remove client ID on disk. This might lead to a resurrection of this client ID later. Error: {e}");
908 }
909
910 // Delete all stored metrics.
911 // Note that this also includes the ping sequence numbers, so it has
912 // the effect of resetting those to their initial values.
913 if let Some(data) = self.data_store.as_ref() {
914 _ = data.clear_lifetime_storage(Lifetime::User, "glean_internal_info");
915 _ = data.remove_single_metric(Lifetime::User, "glean_client_info", "client_id");
916 for (ping_name, ping) in &self.ping_registry {
917 if ping.follows_collection_enabled() {
918 _ = data.clear_ping_lifetime_storage(ping_name);
919 _ = data.clear_lifetime_storage(Lifetime::User, ping_name);
920 _ = data.clear_lifetime_storage(Lifetime::Application, ping_name);
921 }
922 }
923 }
924 if let Err(err) = self.event_data_store.clear_all() {
925 log::warn!("Error clearing pending events: {}", err);
926 }
927
928 // This does not clear the experiments store (which isn't managed by the
929 // StorageEngineManager), since doing so would mean we would have to have the
930 // application tell us again which experiments are active if telemetry is
931 // re-enabled.
932 }
933
934 /// Gets the application ID as specified on instantiation.
935 pub fn get_application_id(&self) -> &str {
936 &self.application_id
937 }
938
939 /// Gets the data path of this instance.
940 pub fn get_data_path(&self) -> &Path {
941 &self.data_path
942 }
943
944 /// Gets a handle to the database.
945 #[track_caller] // If this fails we're interested in the caller.
946 pub fn storage(&self) -> &Database {
947 self.data_store.as_ref().expect("No database found")
948 }
949
950 /// Gets an optional handle to the database.
951 pub fn storage_opt(&self) -> Option<&Database> {
952 self.data_store.as_ref()
953 }
954
955 /// Gets a handle to the event database.
956 pub fn event_storage(&self) -> &EventDatabase {
957 &self.event_data_store
958 }
959
960 /// Gets a reference to the session manager.
961 pub fn session_manager(&self) -> &SessionManager {
962 &self.session_manager
963 }
964
965 pub(crate) fn with_timestamps(&self) -> bool {
966 self.with_timestamps
967 }
968
969 /// Gets the maximum number of events to store before sending a ping.
970 pub fn get_max_events(&self) -> usize {
971 let remote_settings_config = self.remote_settings_config.lock().unwrap();
972
973 if let Some(max_events) = remote_settings_config.event_threshold {
974 max_events as usize
975 } else {
976 self.max_events as usize
977 }
978 }
979
980 /// Gets the next task for an uploader.
981 ///
982 /// This can be one of:
983 ///
984 /// * [`Wait`](PingUploadTask::Wait) - which means the requester should ask
985 /// again later;
986 /// * [`Upload(PingRequest)`](PingUploadTask::Upload) - which means there is
987 /// a ping to upload. This wraps the actual request object;
988 /// * [`Done`](PingUploadTask::Done) - which means requester should stop
989 /// asking for now.
990 ///
991 /// # Returns
992 ///
993 /// A [`PingUploadTask`] representing the next task.
994 pub fn get_upload_task(&self) -> PingUploadTask {
995 self.upload_manager.get_upload_task(self, self.log_pings())
996 }
997
998 /// Processes the response from an attempt to upload a ping.
999 ///
1000 /// # Arguments
1001 ///
1002 /// * `uuid` - The UUID of the ping in question.
1003 /// * `status` - The upload result.
1004 pub fn process_ping_upload_response(
1005 &self,
1006 uuid: &str,
1007 status: UploadResult,
1008 ) -> UploadTaskAction {
1009 self.upload_manager
1010 .process_ping_upload_response(self, uuid, status)
1011 }
1012
1013 /// Takes a snapshot for the given store and optionally clear it.
1014 ///
1015 /// # Arguments
1016 ///
1017 /// * `store_name` - The store to snapshot.
1018 /// * `clear_store` - Whether to clear the store after snapshotting.
1019 ///
1020 /// # Returns
1021 ///
1022 /// The snapshot in a string encoded as JSON. If the snapshot is empty, returns an empty string.
1023 pub fn snapshot(&mut self, store_name: &str, clear_store: bool) -> String {
1024 StorageManager
1025 .snapshot(self.storage(), store_name, clear_store)
1026 .unwrap_or_else(|| String::from(""))
1027 }
1028
1029 pub(crate) fn make_path(&self, ping_name: &str, doc_id: &str) -> String {
1030 format!(
1031 "/submit/{}/{}/{}/{}",
1032 self.get_application_id(),
1033 ping_name,
1034 GLEAN_SCHEMA_VERSION,
1035 doc_id
1036 )
1037 }
1038
1039 /// Collects and submits a ping by name for eventual uploading.
1040 ///
1041 /// The ping content is assembled as soon as possible, but upload is not
1042 /// guaranteed to happen immediately, as that depends on the upload policies.
1043 ///
1044 /// If the ping currently contains no content, it will not be sent,
1045 /// unless it is configured to be sent if empty.
1046 ///
1047 /// # Arguments
1048 ///
1049 /// * `ping_name` - The name of the ping to submit
1050 /// * `reason` - A reason code to include in the ping
1051 ///
1052 /// # Returns
1053 ///
1054 /// Whether the ping was succesfully assembled and queued.
1055 ///
1056 /// # Errors
1057 ///
1058 /// If collecting or writing the ping to disk failed.
1059 pub fn submit_ping_by_name(&self, ping_name: &str, reason: Option<&str>) -> bool {
1060 match self.get_ping_by_name(ping_name) {
1061 None => {
1062 log::error!("Attempted to submit unknown ping '{}'", ping_name);
1063 false
1064 }
1065 Some(ping) => ping.submit_sync(self, reason),
1066 }
1067 }
1068
1069 /// Gets a [`PingType`] by name.
1070 ///
1071 /// # Returns
1072 ///
1073 /// The [`PingType`] of a ping if the given name was registered before, [`None`]
1074 /// otherwise.
1075 pub fn get_ping_by_name(&self, ping_name: &str) -> Option<&PingType> {
1076 self.ping_registry.get(ping_name)
1077 }
1078
1079 /// Register a new [`PingType`](metrics/struct.PingType.html).
1080 pub fn register_ping_type(&mut self, ping: &PingType) {
1081 if self.ping_registry.contains_key(ping.name()) {
1082 log::debug!("Duplicate ping named '{}'", ping.name())
1083 }
1084
1085 self.ping_registry
1086 .insert(ping.name().to_string(), ping.clone());
1087 }
1088
1089 /// Gets a list of currently registered ping names.
1090 ///
1091 /// # Returns
1092 ///
1093 /// The list of ping names that are currently registered.
1094 pub fn get_registered_ping_names(&self) -> Vec<&str> {
1095 self.ping_registry.keys().map(String::as_str).collect()
1096 }
1097
1098 /// Get create time of the Glean object.
1099 pub(crate) fn start_time(&self) -> DateTime<FixedOffset> {
1100 self.start_time
1101 }
1102
1103 /// Indicates that an experiment is running.
1104 ///
1105 /// Glean will then add an experiment annotation to the environment
1106 /// which is sent with pings. This information is not persisted between runs.
1107 ///
1108 /// # Arguments
1109 ///
1110 /// * `experiment_id` - The id of the active experiment (maximum 30 bytes).
1111 /// * `branch` - The experiment branch (maximum 30 bytes).
1112 /// * `extra` - Optional metadata to output with the ping.
1113 pub fn set_experiment_active(
1114 &self,
1115 experiment_id: String,
1116 branch: String,
1117 extra: HashMap<String, String>,
1118 ) {
1119 let metric = ExperimentMetric::new(self, experiment_id);
1120 metric.set_active_sync(self, branch, extra);
1121 }
1122
1123 /// Indicates that an experiment is no longer running.
1124 ///
1125 /// # Arguments
1126 ///
1127 /// * `experiment_id` - The id of the active experiment to deactivate (maximum 30 bytes).
1128 pub fn set_experiment_inactive(&self, experiment_id: String) {
1129 let metric = ExperimentMetric::new(self, experiment_id);
1130 metric.set_inactive_sync(self);
1131 }
1132
1133 /// **Test-only API (exported for FFI purposes).**
1134 ///
1135 /// Gets stored data for the requested experiment.
1136 ///
1137 /// # Arguments
1138 ///
1139 /// * `experiment_id` - The id of the active experiment (maximum 30 bytes).
1140 pub fn test_get_experiment_data(&self, experiment_id: String) -> Option<RecordedExperiment> {
1141 let metric = ExperimentMetric::new(self, experiment_id);
1142 metric.test_get_value(self)
1143 }
1144
1145 /// **Test-only API (exported for FFI purposes).**
1146 ///
1147 /// Gets stored experimentation id annotation.
1148 pub fn test_get_experimentation_id(&self) -> Option<String> {
1149 self.additional_metrics
1150 .experimentation_id
1151 .get_value(self, None)
1152 }
1153
1154 /// Set configuration to override the default state, typically initiated from a
1155 /// remote_settings experiment or rollout
1156 ///
1157 /// # Arguments
1158 ///
1159 /// * `cfg` - The stringified JSON representation of a `RemoteSettingsConfig` object
1160 pub fn apply_server_knobs_config(&self, cfg: RemoteSettingsConfig) {
1161 let config_value = {
1162 // Hold the lock while merging config and serializing, then release
1163 // before performing IO in set_sync.
1164 let mut remote_settings_config = self.remote_settings_config.lock().unwrap();
1165
1166 // Merge the exising metrics configuration with the supplied one
1167 remote_settings_config
1168 .metrics_enabled
1169 .extend(cfg.metrics_enabled);
1170
1171 // Merge the exising ping configuration with the supplied one
1172 remote_settings_config
1173 .pings_enabled
1174 .extend(cfg.pings_enabled);
1175
1176 remote_settings_config.event_threshold = cfg.event_threshold;
1177
1178 // Clamp to [0.0, 1.0] so callers can't accidentally set an invalid rate.
1179 //
1180 // NOTE: `session_sample_rate` is intentionally NOT applied to any
1181 // currently-active session. The override is picked up at the next
1182 // `session_start()` call. This "sticky per session" design means:
1183 // - A mid-session RS rollout does not change sampling mid-flight,
1184 // which would otherwise cause partial session data.
1185 // - To clear the override and revert to the configured rate, set
1186 // `session_sample_rate` to `null` in the RS payload. The next
1187 // session will use `configured_sample_rate` as the fallback.
1188 //
1189 // This override is intentionally NOT persisted to storage. Remote
1190 // Settings configuration is refreshed on every app startup, so the
1191 // override will be re-applied before the next session begins.
1192 // Persisting it would risk making a stale value sticky if the RS
1193 // payload changes or is removed between restarts.
1194 remote_settings_config.session_sample_rate = cfg.session_sample_rate.map(|r| {
1195 let clamped = r.clamp(0.0, 1.0);
1196 if clamped != r {
1197 log::warn!(
1198 "session_sample_rate {} out of range, clamped to {}",
1199 r,
1200 clamped
1201 );
1202 }
1203 clamped
1204 });
1205
1206 // Store the Server Knobs configuration as an ObjectMetric
1207 // Since RemoteSettingsConfig only contains maps with string keys and primitives,
1208 // serialization via the derived Serialize impl cannot fail so it is safe to unwrap.
1209 serde_json::to_value(&*remote_settings_config).unwrap()
1210 };
1211
1212 self.additional_metrics
1213 .server_knobs_config
1214 .set_sync(self, config_value);
1215
1216 // Update remote_settings epoch
1217 self.remote_settings_epoch.fetch_add(1, Ordering::SeqCst);
1218 }
1219
1220 /// Persists [`Lifetime::Ping`] data that might be in memory in case
1221 /// [`delay_ping_lifetime_io`](InternalConfiguration::delay_ping_lifetime_io) is set
1222 /// or was set at a previous time.
1223 ///
1224 /// If there is no data to persist, this function does nothing.
1225 pub fn persist_ping_lifetime_data(&self) -> Result<()> {
1226 if let Some(data) = self.data_store.as_ref() {
1227 return data.persist_ping_lifetime_data();
1228 }
1229
1230 Ok(())
1231 }
1232
1233 /// Sets internally-handled application lifetime metrics.
1234 fn set_application_lifetime_core_metrics(&self) {
1235 self.core_metrics.os.set_sync(self, system::OS);
1236 }
1237
1238 /// **This is not meant to be used directly.**
1239 ///
1240 /// Clears all the metrics that have [`Lifetime::Application`].
1241 pub fn clear_application_lifetime_metrics(&self) {
1242 log::trace!("Clearing Lifetime::Application metrics");
1243 if let Some(data) = self.data_store.as_ref() {
1244 data.clear_lifetime(Lifetime::Application);
1245 }
1246
1247 // Set internally handled app lifetime metrics again.
1248 self.set_application_lifetime_core_metrics();
1249 }
1250
1251 /// Whether or not this is the first run on this profile.
1252 pub fn is_first_run(&self) -> bool {
1253 self.is_first_run
1254 }
1255
1256 /// Sets a debug view tag.
1257 ///
1258 /// This will return `false` in case `value` is not a valid tag.
1259 ///
1260 /// When the debug view tag is set, pings are sent with a `X-Debug-ID` header with the value of the tag
1261 /// and are sent to the ["Ping Debug Viewer"](https://mozilla.github.io/glean/book/dev/core/internal/debug-pings.html).
1262 ///
1263 /// # Arguments
1264 ///
1265 /// * `value` - A valid HTTP header value. Must match the regex: "[a-zA-Z0-9-]{1,20}".
1266 pub fn set_debug_view_tag(&mut self, value: &str) -> bool {
1267 self.debug.debug_view_tag.set(value.into())
1268 }
1269
1270 /// Return the value for the debug view tag or [`None`] if it hasn't been set.
1271 ///
1272 /// The `debug_view_tag` may be set from an environment variable
1273 /// (`GLEAN_DEBUG_VIEW_TAG`) or through the [`set_debug_view_tag`](Glean::set_debug_view_tag) function.
1274 pub fn debug_view_tag(&self) -> Option<&String> {
1275 self.debug.debug_view_tag.get()
1276 }
1277
1278 /// Sets source tags.
1279 ///
1280 /// This will return `false` in case `value` contains invalid tags.
1281 ///
1282 /// Ping tags will show in the destination datasets, after ingestion.
1283 ///
1284 /// **Note** If one or more tags are invalid, all tags are ignored.
1285 ///
1286 /// # Arguments
1287 ///
1288 /// * `value` - A vector of at most 5 valid HTTP header values. Individual tags must match the regex: "[a-zA-Z0-9-]{1,20}".
1289 pub fn set_source_tags(&mut self, value: Vec<String>) -> bool {
1290 self.debug.source_tags.set(value)
1291 }
1292
1293 /// Return the value for the source tags or [`None`] if it hasn't been set.
1294 ///
1295 /// The `source_tags` may be set from an environment variable (`GLEAN_SOURCE_TAGS`)
1296 /// or through the [`set_source_tags`](Glean::set_source_tags) function.
1297 pub(crate) fn source_tags(&self) -> Option<&Vec<String>> {
1298 self.debug.source_tags.get()
1299 }
1300
1301 /// Sets the log pings debug option.
1302 ///
1303 /// This will return `false` in case we are unable to set the option.
1304 ///
1305 /// When the log pings debug option is `true`,
1306 /// we log the payload of all succesfully assembled pings.
1307 ///
1308 /// # Arguments
1309 ///
1310 /// * `value` - The value of the log pings option
1311 pub fn set_log_pings(&mut self, value: bool) -> bool {
1312 self.debug.log_pings.set(value)
1313 }
1314
1315 /// Return the value for the log pings debug option or `false` if it hasn't been set.
1316 ///
1317 /// The `log_pings` option may be set from an environment variable (`GLEAN_LOG_PINGS`)
1318 /// or through the `set_log_pings` function.
1319 pub fn log_pings(&self) -> bool {
1320 self.debug.log_pings.get().copied().unwrap_or(false)
1321 }
1322
1323 fn get_dirty_bit_metric(&self) -> metrics::BooleanMetric {
1324 metrics::BooleanMetric::new(CommonMetricData {
1325 name: "dirtybit".into(),
1326 // We don't need a category, the name is already unique
1327 category: "".into(),
1328 send_in_pings: vec![INTERNAL_STORAGE.into()],
1329 lifetime: Lifetime::User,
1330 ..Default::default()
1331 })
1332 }
1333
1334 /// **This is not meant to be used directly.**
1335 ///
1336 /// Sets the value of a "dirty flag" in the permanent storage.
1337 ///
1338 /// The "dirty flag" is meant to have the following behaviour, implemented
1339 /// by the consumers of the FFI layer:
1340 ///
1341 /// - on mobile: set to `false` when going to background or shutting down,
1342 /// set to `true` at startup and when going to foreground.
1343 /// - on non-mobile platforms: set to `true` at startup and `false` at
1344 /// shutdown.
1345 ///
1346 /// At startup, before setting its new value, if the "dirty flag" value is
1347 /// `true`, then Glean knows it did not exit cleanly and can implement
1348 /// coping mechanisms (e.g. sending a `baseline` ping).
1349 pub fn set_dirty_flag(&self, new_value: bool) {
1350 self.get_dirty_bit_metric().set_sync(self, new_value);
1351 }
1352
1353 /// **This is not meant to be used directly.**
1354 ///
1355 /// Checks the stored value of the "dirty flag".
1356 pub fn is_dirty_flag_set(&self) -> bool {
1357 let dirty_bit_metric = self.get_dirty_bit_metric();
1358 match StorageManager.snapshot_metric(
1359 self.storage(),
1360 INTERNAL_STORAGE,
1361 &dirty_bit_metric.meta().identifier(self),
1362 dirty_bit_metric.meta().inner.lifetime,
1363 ) {
1364 Some(Metric::Boolean(b)) => b,
1365 _ => false,
1366 }
1367 }
1368
1369 // -----------------------------------------------------------------------
1370 // Session lifecycle methods
1371 // -----------------------------------------------------------------------
1372
1373 /// Restores session state from persistent storage at startup.
1374 ///
1375 /// Must be called after `data_store` is initialized (i.e. after
1376 /// `Database::new` succeeds) so that the storage reads are valid.
1377 ///
1378 /// **Sequence counter**: `session_seq` is always restored so it is
1379 /// monotonically increasing across restarts. Note that if a crash occurs
1380 /// between `store_session_seq` and `persist_session_id` inside
1381 /// `session_start`, the sequence number will have been incremented but no
1382 /// session ID will be persisted. On the next restart this method will
1383 /// restore the incremented seq and the next session will be assigned
1384 /// seq+1, leaving a one-element gap. This is acceptable — downstream
1385 /// analysts should treat sequence numbers as monotonically non-decreasing,
1386 /// not strictly contiguous.
1387 ///
1388 /// **AUTO mode resumption**: requires both a persisted `session_id` **and**
1389 /// an `inactive_since` timestamp. If either is absent the previous session
1390 /// is considered abandoned and the next `handle_client_active` call will
1391 /// start a fresh session via `session_start()`. On a crash restart,
1392 /// `recover_session_on_dirty_flag()` overwrites whatever this method
1393 /// restores, so the dirty-flag path is always authoritative.
1394 fn restore_session_state_from_storage(&mut self) {
1395 // Always restore seq so new sessions increment from the last known value.
1396 self.session_manager.session_seq = session::read_session_seq(self);
1397
1398 // Check for an orphaned session from a previous build that used a
1399 // different SessionMode. If the current mode would not restore the
1400 // persisted session, emit a synthetic session_end("abandoned") and
1401 // clear all persisted session state so it doesn't leak across builds.
1402 if self.session_manager.mode != SessionMode::Auto {
1403 if let Some(id_str) = session::read_session_id(self) {
1404 log::info!(
1405 "Orphaned session {} found from a previous Auto-mode build; \
1406 emitting session_end(\"abandoned\") and clearing storage",
1407 id_str
1408 );
1409 let seq = self.session_manager.session_seq;
1410 self.record_session_end_event(&id_str, seq, Some("abandoned"));
1411 session::clear(self);
1412 }
1413 return;
1414 }
1415
1416 // AUTO mode: restore inactive session state so inactivity timeout
1417 // evaluation can happen lazily on the next handle_client_active call.
1418 if let Some(inactive_since) = session::read_inactive_since(self) {
1419 if let Some(id_str) = session::read_session_id(self) {
1420 if let Ok(id) = Uuid::parse_str(&id_str) {
1421 // Recompute sampled_in deterministically from the UUID so
1422 // the sampling decision is consistent across the resumed session.
1423 let sampled_in = session::uuid_to_sample_value(&id)
1424 < self.session_manager.configured_sample_rate;
1425 self.session_manager.session_id = Some(id);
1426 self.session_manager.inactive_since = Some(inactive_since);
1427 self.session_manager.sampled_in = sampled_in;
1428 self.session_manager.session_start_time =
1429 session::read_session_start_time(self);
1430 if self.session_manager.session_start_time.is_none() {
1431 log::warn!(
1432 "Resumed session {} has no persisted session_start_time; \
1433 events in this session will carry session_start_time: null",
1434 id
1435 );
1436 }
1437 // Restore event_seq so the resumed session issues
1438 // monotonically increasing sequence numbers even across
1439 // a clean restart.
1440 self.session_manager
1441 .event_seq
1442 .store(session::read_session_event_seq(self), Ordering::Relaxed);
1443 self.session_manager.state = SessionState::Inactive;
1444 }
1445 }
1446 }
1447 }
1448
1449 /// Injects a `glean_timestamp` key into `extra` when event timestamps are enabled.
1450 ///
1451 /// Takes the already-computed `timestamp_ms` so the glean_timestamp extra and
1452 /// the event's main timestamp are both derived from the same clock sample.
1453 fn maybe_inject_glean_timestamp(
1454 &self,
1455 extra: &mut std::collections::HashMap<String, String>,
1456 timestamp_ms: u64,
1457 ) {
1458 if self.with_timestamps {
1459 extra.insert("glean_timestamp".to_string(), timestamp_ms.to_string());
1460 }
1461 }
1462
1463 /// Records a `glean.session_start` boundary event (always, regardless of sampling).
1464 fn record_session_start_event(
1465 &self,
1466 session_id: &str,
1467 seq: u64,
1468 start_time: DateTime<FixedOffset>,
1469 sampled_in: bool,
1470 ) {
1471 let meta = CommonMetricData {
1472 name: "session_start".into(),
1473 category: "glean".into(),
1474 send_in_pings: vec!["events".into()],
1475 lifetime: Lifetime::Ping,
1476 ..Default::default()
1477 };
1478 let timestamp = crate::get_timestamp_ms();
1479 let mut extra = std::collections::HashMap::new();
1480 extra.insert("session_id".to_string(), session_id.to_string());
1481 extra.insert("session_seq".to_string(), seq.to_string());
1482 extra.insert(
1483 "session_start_time".to_string(),
1484 start_time.to_rfc3339_opts(SecondsFormat::Millis, true),
1485 );
1486 extra.insert("sampled_in".to_string(), sampled_in.to_string());
1487 self.maybe_inject_glean_timestamp(&mut extra, timestamp);
1488 self.event_data_store.record(
1489 self,
1490 &meta.into(),
1491 timestamp,
1492 Some(extra),
1493 EventSessionContext::OutOfSession,
1494 );
1495 }
1496
1497 /// Records a `glean.session_end` boundary event (always, regardless of sampling).
1498 fn record_session_end_event(&self, session_id: &str, seq: u64, reason: Option<&str>) {
1499 let meta = CommonMetricData {
1500 name: "session_end".into(),
1501 category: "glean".into(),
1502 send_in_pings: vec!["events".into()],
1503 lifetime: Lifetime::Ping,
1504 ..Default::default()
1505 };
1506 let timestamp = crate::get_timestamp_ms();
1507 let mut extra = std::collections::HashMap::new();
1508 extra.insert("session_id".to_string(), session_id.to_string());
1509 extra.insert("session_seq".to_string(), seq.to_string());
1510 if let Some(r) = reason {
1511 extra.insert("reason".to_string(), r.to_string());
1512 }
1513 self.maybe_inject_glean_timestamp(&mut extra, timestamp);
1514 self.event_data_store.record(
1515 self,
1516 &meta.into(),
1517 timestamp,
1518 Some(extra),
1519 EventSessionContext::OutOfSession,
1520 );
1521 }
1522
1523 /// Starts a new session, persists state, and records a boundary event.
1524 ///
1525 /// If a session is already active it is ended cleanly before the new one
1526 /// starts, preventing orphaned sessions with no corresponding `session_end`.
1527 pub fn session_start(&mut self) {
1528 // End any already-active session so we never orphan a session_end event.
1529 if self.session_manager.is_active() {
1530 self.session_end(Some("replaced"));
1531 }
1532
1533 // 1. Compute new seq from in-memory value (authoritative after init).
1534 let new_seq = self.session_manager.session_seq + 1;
1535
1536 // 2. Generate new session_id and compute sampling.
1537 // Prefer a remote-settings override if one has been set, falling back
1538 // to the immutable configured_sample_rate (never the last effective
1539 // rate) so RS overrides can be fully cleared without residual effects.
1540 // The rate is sampled once here and is sticky for the entire session;
1541 // any RS update received mid-session takes effect at the next session_start.
1542 let session_id = uuid::Uuid::new_v4();
1543 let sample_rate = {
1544 let remote = self.remote_settings_config.lock().unwrap();
1545 remote
1546 .session_sample_rate
1547 .unwrap_or(self.session_manager.configured_sample_rate)
1548 };
1549 let sampled_in = session::uuid_to_sample_value(&session_id) < sample_rate;
1550
1551 // 3. Update in-memory state.
1552 self.session_manager.sample_rate = sample_rate;
1553 // Truncate to millisecond precision so that in-memory and persisted
1554 // (RFC 3339 millis) representations are identical after a round-trip.
1555 let start_time = {
1556 let now = local_now_with_offset();
1557 let millis = now.timestamp_millis();
1558 DateTime::from_timestamp_millis(millis)
1559 .expect("valid timestamp")
1560 .with_timezone(now.offset())
1561 };
1562 self.session_manager.session_start_time = Some(start_time);
1563 self.session_manager.session_id = Some(session_id);
1564 self.session_manager.session_seq = new_seq;
1565 self.session_manager.event_seq.store(0, Ordering::Relaxed);
1566 self.session_manager.sampled_in = sampled_in;
1567 self.session_manager.state = SessionState::Active;
1568 self.session_manager.inactive_since = None;
1569
1570 // 4. Persist to storage.
1571 session::store_session_seq(self, new_seq);
1572 session::persist_session_id(self, &session_id.to_string());
1573 session::persist_session_start_time(self, start_time);
1574 session::clear_inactive_since(self);
1575
1576 // 5. Increment diagnostic counter.
1577 self.additional_metrics.sessions_seen.add_sync(self, 1);
1578
1579 // 6. Record boundary event.
1580 self.record_session_start_event(&session_id.to_string(), new_seq, start_time, sampled_in);
1581 }
1582
1583 /// Ends the current session, persists state, and records a boundary event.
1584 ///
1585 /// Returns the ended session's metadata, or `None` if no session was active.
1586 pub fn session_end(&mut self, reason: Option<&str>) -> Option<crate::session::SessionMetadata> {
1587 if self.session_manager.state != SessionState::Active {
1588 return None;
1589 }
1590
1591 let session_id = self.session_manager.session_id?;
1592 let seq = self.session_manager.session_seq;
1593 let event_seq = self.session_manager.event_seq.load(Ordering::Relaxed);
1594 let sample_rate = self.session_manager.sample_rate;
1595 let start_time = self.session_manager.session_start_time;
1596
1597 // Clear persistence.
1598 session::clear(self);
1599
1600 // Reset in-memory state so the next session_start gets a clean slate.
1601 self.session_manager.reset_state();
1602
1603 // Record boundary event.
1604 self.record_session_end_event(&session_id.to_string(), seq, reason);
1605
1606 Some(crate::session::SessionMetadata {
1607 session_id: session_id.to_string(),
1608 session_seq: seq,
1609 event_seq,
1610 session_sample_rate: sample_rate,
1611 session_start_time: start_time.map(|t| t.to_rfc3339_opts(SecondsFormat::Millis, true)),
1612 })
1613 }
1614
1615 /// Transitions the current session to inactive (AUTO mode).
1616 ///
1617 /// Records the `inactive_since` timestamp for timeout evaluation on next activation.
1618 /// Does NOT end the session — that happens lazily on next `handle_client_active`.
1619 pub(crate) fn session_transition_to_inactive(&mut self) {
1620 if self.session_manager.state != SessionState::Active {
1621 return;
1622 }
1623
1624 let now = local_now_with_offset();
1625 // Snapshot event_seq before changing state so the value is stable.
1626 let event_seq = self.session_manager.event_seq.load(Ordering::Relaxed);
1627 self.session_manager.state = SessionState::Inactive;
1628 self.session_manager.inactive_since = Some(now);
1629
1630 // Persist for crash recovery and clean-restart resumption.
1631 // event_seq is persisted here (rather than on every increment) because
1632 // this is the only point where events stop being recorded mid-session;
1633 // if the app crashes before the next activation, the recovered session
1634 // will at least have the correct seq baseline from the last inactive
1635 // transition.
1636 session::persist_inactive_since(self, now);
1637 session::store_session_event_seq(self, event_seq);
1638 }
1639
1640 /// Handles transitioning from inactive to active (AUTO mode).
1641 ///
1642 /// Evaluates the inactivity timeout:
1643 /// - If the timeout has NOT expired: resume the existing session.
1644 /// - If the timeout HAS expired: end the old session and start a new one.
1645 ///
1646 /// Returns `true` if a new session was started.
1647 pub(crate) fn session_transition_to_active(&mut self) -> bool {
1648 match self.session_manager.inactive_since {
1649 None => {
1650 // No inactive_since recorded: treat as a cold activation and start
1651 // a fresh session. The call site in handle_client_active guards
1652 // with `inactive_since.is_some()` so this is normally unreachable,
1653 // but we handle it safely rather than leaving state inconsistent.
1654 self.session_start();
1655 true
1656 }
1657 Some(inactive_since) => {
1658 let now = local_now_with_offset();
1659 let elapsed = (now - inactive_since).to_std().unwrap_or_default();
1660
1661 // A timeout of zero means "never time out" (session always resumes).
1662 if !self.session_manager.inactivity_timeout.is_zero()
1663 && elapsed >= self.session_manager.inactivity_timeout
1664 {
1665 // Timeout expired → end old session (emits boundary event), start new one.
1666 // The session state was set to Inactive by session_transition_to_inactive(),
1667 // but session_id is still set. Restore Active so session_end() can proceed.
1668 self.session_manager.state = SessionState::Active;
1669 self.session_end(Some("timeout"));
1670 self.session_start();
1671 true
1672 } else {
1673 // Timeout has NOT expired → resume existing session.
1674 self.session_manager.state = SessionState::Active;
1675 self.session_manager.inactive_since = None;
1676 session::clear_inactive_since(self);
1677 false
1678 }
1679 }
1680 }
1681 }
1682
1683 /// Called during initialization to recover an abnormally terminated session.
1684 ///
1685 /// If the dirty flag was set and a session ID is persisted, emits a synthetic
1686 /// `session_end` event with reason "abnormal" and clears session state.
1687 pub(crate) fn recover_session_on_dirty_flag(&mut self) {
1688 let persisted_id = match session::read_session_id(self) {
1689 Some(id) => id,
1690 None => return, // No previous session to recover.
1691 };
1692
1693 let persisted_seq = self.session_manager.session_seq;
1694 let inactive_since = session::read_inactive_since(self);
1695
1696 // Determine if the session ended while inactive (timeout may have expired).
1697 let reason = if inactive_since.is_some() {
1698 "abnormal_inactive"
1699 } else {
1700 "abnormal"
1701 };
1702
1703 log::info!(
1704 "Recovering abnormally terminated session: {} (seq={})",
1705 persisted_id,
1706 persisted_seq
1707 );
1708
1709 // Emit synthetic session_end.
1710 self.record_session_end_event(&persisted_id, persisted_seq, Some(reason));
1711
1712 // Clear persisted session state so the recovered session won't be replayed.
1713 session::clear(self);
1714
1715 // Reset in-memory state so the next session_start gets a clean slate.
1716 self.session_manager.reset_state();
1717 }
1718
1719 // -----------------------------------------------------------------------
1720 // Client lifecycle methods
1721 // -----------------------------------------------------------------------
1722
1723 /// Performs the collection/cleanup operations required by becoming active.
1724 ///
1725 /// This functions generates a baseline ping with reason `active`
1726 /// and then sets the dirty bit.
1727 pub fn handle_client_active(&mut self) {
1728 match self.session_manager.mode {
1729 SessionMode::Auto => {
1730 if !self.session_manager.is_active() {
1731 if self.session_manager.inactive_since.is_some() {
1732 // Was inactive — evaluate timeout.
1733 self.session_transition_to_active();
1734 } else {
1735 // First activation — start initial session.
1736 self.session_start();
1737 }
1738 }
1739 }
1740 SessionMode::Lifecycle => {
1741 // Only start a session on the first activation following an inactive
1742 // transition. Guard against duplicate handle_client_active calls which
1743 // are not a real lifecycle transition.
1744 if !self.session_manager.is_active() {
1745 self.session_start();
1746 }
1747 }
1748 SessionMode::Manual => {
1749 // No automatic session management.
1750 }
1751 }
1752
1753 if !self
1754 .internal_pings
1755 .baseline
1756 .submit_sync(self, Some("active"))
1757 {
1758 log::info!("baseline ping not submitted on active");
1759 }
1760
1761 self.set_dirty_flag(true);
1762 }
1763
1764 /// Performs the collection/cleanup operations required by becoming inactive.
1765 ///
1766 /// This functions generates a baseline and an events ping with reason
1767 /// `inactive` and then clears the dirty bit.
1768 pub fn handle_client_inactive(&mut self) {
1769 match self.session_manager.mode {
1770 SessionMode::Auto => {
1771 // In AUTO mode, don't end the session immediately. Instead record
1772 // inactive_since for lazy timeout evaluation on next activation.
1773 self.session_transition_to_inactive();
1774 }
1775 SessionMode::Lifecycle => {
1776 // End session immediately on going inactive.
1777 self.session_end(Some("inactive"));
1778 }
1779 SessionMode::Manual => {
1780 // No automatic session management.
1781 }
1782 }
1783
1784 if !self
1785 .internal_pings
1786 .baseline
1787 .submit_sync(self, Some("inactive"))
1788 {
1789 log::info!("baseline ping not submitted on inactive");
1790 }
1791
1792 if !self
1793 .internal_pings
1794 .events
1795 .submit_sync(self, Some("inactive"))
1796 {
1797 log::info!("events ping not submitted on inactive");
1798 }
1799
1800 self.set_dirty_flag(false);
1801 }
1802
1803 /// **Test-only API (exported for FFI purposes).**
1804 ///
1805 /// Deletes all stored metrics.
1806 ///
1807 /// Note that this also includes the ping sequence numbers, so it has
1808 /// the effect of resetting those to their initial values.
1809 pub fn test_clear_all_stores(&self) {
1810 if let Some(data) = self.data_store.as_ref() {
1811 data.clear_all()
1812 }
1813 // We don't care about this failing, maybe the data does just not exist.
1814 let _ = self.event_data_store.clear_all();
1815 }
1816
1817 /// Instructs the Metrics Ping Scheduler's thread to exit cleanly.
1818 /// If Glean was configured with `use_core_mps: false`, this has no effect.
1819 pub fn cancel_metrics_ping_scheduler(&self) {
1820 if self.schedule_metrics_pings {
1821 scheduler::cancel();
1822 }
1823 }
1824
1825 /// Instructs the Metrics Ping Scheduler to being scheduling metrics pings.
1826 /// If Glean wsa configured with `use_core_mps: false`, this has no effect.
1827 pub fn start_metrics_ping_scheduler(&self) {
1828 if self.schedule_metrics_pings {
1829 scheduler::schedule(self);
1830 }
1831 }
1832
1833 /// Clears the core attribution data.
1834 /// Does not clear glean.attribution.ext.
1835 pub fn clear_attribution(&self) {
1836 if let Some(data) = self.data_store.as_ref() {
1837 [
1838 &self.core_metrics.attribution_source,
1839 &self.core_metrics.attribution_medium,
1840 &self.core_metrics.attribution_campaign,
1841 &self.core_metrics.attribution_term,
1842 &self.core_metrics.attribution_content,
1843 ]
1844 .iter()
1845 .for_each(|metric| {
1846 let meta = metric.meta();
1847 _ = data.remove_single_metric(
1848 meta.inner.lifetime,
1849 &meta.storage_names()[0],
1850 &meta.identifier(self),
1851 );
1852 });
1853 }
1854 }
1855
1856 /// Updates attribution fields with new values.
1857 /// AttributionMetrics fields with `None` values will not overwrite older values.
1858 pub fn update_attribution(&self, attribution: AttributionMetrics) {
1859 if let Some(source) = attribution.source {
1860 self.core_metrics.attribution_source.set_sync(self, source);
1861 }
1862 if let Some(medium) = attribution.medium {
1863 self.core_metrics.attribution_medium.set_sync(self, medium);
1864 }
1865 if let Some(campaign) = attribution.campaign {
1866 self.core_metrics
1867 .attribution_campaign
1868 .set_sync(self, campaign);
1869 }
1870 if let Some(term) = attribution.term {
1871 self.core_metrics.attribution_term.set_sync(self, term);
1872 }
1873 if let Some(content) = attribution.content {
1874 self.core_metrics
1875 .attribution_content
1876 .set_sync(self, content);
1877 }
1878 }
1879
1880 /// **TEST-ONLY Method**
1881 ///
1882 /// Returns the current attribution metrics.
1883 pub fn test_get_attribution(&self) -> AttributionMetrics {
1884 AttributionMetrics {
1885 source: self
1886 .core_metrics
1887 .attribution_source
1888 .get_value(self, Some("glean_client_info")),
1889 medium: self
1890 .core_metrics
1891 .attribution_medium
1892 .get_value(self, Some("glean_client_info")),
1893 campaign: self
1894 .core_metrics
1895 .attribution_campaign
1896 .get_value(self, Some("glean_client_info")),
1897 term: self
1898 .core_metrics
1899 .attribution_term
1900 .get_value(self, Some("glean_client_info")),
1901 content: self
1902 .core_metrics
1903 .attribution_content
1904 .get_value(self, Some("glean_client_info")),
1905 }
1906 }
1907
1908 /// Clears the core distribution data.
1909 /// Does not clear glean.distribution.ext.
1910 pub fn clear_distribution(&self) {
1911 if let Some(data) = self.data_store.as_ref() {
1912 let meta = self.core_metrics.distribution_name.meta();
1913 _ = data.remove_single_metric(
1914 meta.inner.lifetime,
1915 &meta.storage_names()[0],
1916 &meta.identifier(self),
1917 );
1918 }
1919 }
1920
1921 /// Updates distribution fields with new values.
1922 /// DistributionMetrics fields with `None` values will not overwrite older values.
1923 pub fn update_distribution(&self, distribution: DistributionMetrics) {
1924 if let Some(name) = distribution.name {
1925 self.core_metrics.distribution_name.set_sync(self, name);
1926 }
1927 }
1928
1929 /// **TEST-ONLY Method**
1930 ///
1931 /// Returns the current distribution metrics.
1932 pub fn test_get_distribution(&self) -> DistributionMetrics {
1933 DistributionMetrics {
1934 name: self
1935 .core_metrics
1936 .distribution_name
1937 .get_value(self, Some("glean_client_info")),
1938 }
1939 }
1940}