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::path::{Path, PathBuf};
7use std::sync::atomic::{AtomicU8, Ordering};
8use std::sync::{Arc, Mutex};
9use std::time::Duration;
10
11use chrono::{DateTime, FixedOffset};
12use malloc_size_of_derive::MallocSizeOf;
13use once_cell::sync::OnceCell;
14
15use crate::database::Database;
16use crate::debug::DebugOptions;
17use crate::event_database::EventDatabase;
18use crate::internal_metrics::{AdditionalMetrics, CoreMetrics, DatabaseMetrics};
19use crate::internal_pings::InternalPings;
20use crate::metrics::{
21    self, ExperimentMetric, Metric, MetricType, PingType, RecordedExperiment, RemoteSettingsConfig,
22};
23use crate::ping::PingMaker;
24use crate::storage::{StorageManager, INTERNAL_STORAGE};
25use crate::upload::{PingUploadManager, PingUploadTask, UploadResult, UploadTaskAction};
26use crate::util::{local_now_with_offset, sanitize_application_id};
27use crate::{
28    scheduler, system, AttributionMetrics, CommonMetricData, DistributionMetrics, ErrorKind,
29    InternalConfiguration, Lifetime, PingRateLimit, Result, DEFAULT_MAX_EVENTS,
30    GLEAN_SCHEMA_VERSION, GLEAN_VERSION, KNOWN_CLIENT_ID,
31};
32
33static GLEAN: OnceCell<Mutex<Glean>> = OnceCell::new();
34
35pub fn global_glean() -> Option<&'static Mutex<Glean>> {
36    GLEAN.get()
37}
38
39/// Sets or replaces the global Glean object.
40pub fn setup_glean(glean: Glean) -> Result<()> {
41    // The `OnceCell` type wrapping our Glean is thread-safe and can only be set once.
42    // Therefore even if our check for it being empty succeeds, setting it could fail if a
43    // concurrent thread is quicker in setting it.
44    // However this will not cause a bigger problem, as the second `set` operation will just fail.
45    // We can log it and move on.
46    //
47    // For all wrappers this is not a problem, as the Glean object is intialized exactly once on
48    // calling `initialize` on the global singleton and further operations check that it has been
49    // initialized.
50    if GLEAN.get().is_none() {
51        if GLEAN.set(Mutex::new(glean)).is_err() {
52            log::warn!(
53                "Global Glean object is initialized already. This probably happened concurrently."
54            )
55        }
56    } else {
57        // We allow overriding the global Glean object to support test mode.
58        // In test mode the Glean object is fully destroyed and recreated.
59        // This all happens behind a mutex and is therefore also thread-safe..
60        let mut lock = GLEAN.get().unwrap().lock().unwrap();
61        *lock = glean;
62    }
63    Ok(())
64}
65
66/// Execute `f` passing the global Glean object.
67///
68/// Panics if the global Glean object has not been set.
69pub fn with_glean<F, R>(f: F) -> R
70where
71    F: FnOnce(&Glean) -> R,
72{
73    let glean = global_glean().expect("Global Glean object not initialized");
74    let lock = glean.lock().unwrap();
75    f(&lock)
76}
77
78/// Execute `f` passing the global Glean object mutable.
79///
80/// Panics if the global Glean object has not been set.
81pub fn with_glean_mut<F, R>(f: F) -> R
82where
83    F: FnOnce(&mut Glean) -> R,
84{
85    let glean = global_glean().expect("Global Glean object not initialized");
86    let mut lock = glean.lock().unwrap();
87    f(&mut lock)
88}
89
90/// Execute `f` passing the global Glean object if it has been set.
91///
92/// Returns `None` if the global Glean object has not been set.
93/// Returns `Some(T)` otherwise.
94pub fn with_opt_glean<F, R>(f: F) -> Option<R>
95where
96    F: FnOnce(&Glean) -> R,
97{
98    let glean = global_glean()?;
99    let lock = glean.lock().unwrap();
100    Some(f(&lock))
101}
102
103/// The object holding meta information about a Glean instance.
104///
105/// ## Example
106///
107/// Create a new Glean instance, register a ping, record a simple counter and then send the final
108/// ping.
109///
110/// ```rust,no_run
111/// # use glean_core::{Glean, InternalConfiguration, CommonMetricData, metrics::*};
112/// let cfg = InternalConfiguration {
113///     data_path: "/tmp/glean".into(),
114///     application_id: "glean.sample.app".into(),
115///     language_binding_name: "Rust".into(),
116///     upload_enabled: true,
117///     max_events: None,
118///     delay_ping_lifetime_io: false,
119///     app_build: "".into(),
120///     use_core_mps: false,
121///     trim_data_to_registered_pings: false,
122///     log_level: None,
123///     rate_limit: None,
124///     enable_event_timestamps: true,
125///     experimentation_id: None,
126///     enable_internal_pings: true,
127///     ping_schedule: Default::default(),
128///     ping_lifetime_threshold: 1000,
129///     ping_lifetime_max_time: 2000,
130/// };
131/// let mut glean = Glean::new(cfg).unwrap();
132/// let ping = PingType::new("sample", true, false, true, true, true, vec![], vec![], true, vec![]);
133/// glean.register_ping_type(&ping);
134///
135/// let call_counter: CounterMetric = CounterMetric::new(CommonMetricData {
136///     name: "calls".into(),
137///     category: "local".into(),
138///     send_in_pings: vec!["sample".into()],
139///     ..Default::default()
140/// });
141///
142/// call_counter.add_sync(&glean, 1);
143///
144/// ping.submit_sync(&glean, None);
145/// ```
146///
147/// ## Note
148///
149/// In specific language bindings, this is usually wrapped in a singleton and all metric recording goes to a single instance of this object.
150/// In the Rust core, it is possible to create multiple instances, which is used in testing.
151#[derive(Debug, MallocSizeOf)]
152pub struct Glean {
153    upload_enabled: bool,
154    pub(crate) data_store: Option<Database>,
155    event_data_store: EventDatabase,
156    pub(crate) core_metrics: CoreMetrics,
157    pub(crate) additional_metrics: AdditionalMetrics,
158    pub(crate) database_metrics: DatabaseMetrics,
159    pub(crate) internal_pings: InternalPings,
160    data_path: PathBuf,
161    application_id: String,
162    ping_registry: HashMap<String, PingType>,
163    #[ignore_malloc_size_of = "external non-allocating type"]
164    start_time: DateTime<FixedOffset>,
165    max_events: u32,
166    is_first_run: bool,
167    pub(crate) upload_manager: PingUploadManager,
168    debug: DebugOptions,
169    pub(crate) app_build: String,
170    pub(crate) schedule_metrics_pings: bool,
171    pub(crate) remote_settings_epoch: AtomicU8,
172    #[ignore_malloc_size_of = "TODO: Expose Glean's inner memory allocations (bug 1960592)"]
173    pub(crate) remote_settings_config: Arc<Mutex<RemoteSettingsConfig>>,
174    pub(crate) with_timestamps: bool,
175    pub(crate) ping_schedule: HashMap<String, Vec<String>>,
176}
177
178impl Glean {
179    /// Creates and initializes a new Glean object for use in a subprocess.
180    ///
181    /// Importantly, this will not send any pings at startup, since that
182    /// sort of management should only happen in the main process.
183    pub fn new_for_subprocess(cfg: &InternalConfiguration, scan_directories: bool) -> Result<Self> {
184        log::info!("Creating new Glean v{}", GLEAN_VERSION);
185
186        let application_id = sanitize_application_id(&cfg.application_id);
187        if application_id.is_empty() {
188            return Err(ErrorKind::InvalidConfig.into());
189        }
190
191        let data_path = Path::new(&cfg.data_path);
192        let event_data_store = EventDatabase::new(data_path)?;
193
194        // Create an upload manager with rate limiting of 15 pings every 60 seconds.
195        let mut upload_manager = PingUploadManager::new(&cfg.data_path, &cfg.language_binding_name);
196        let rate_limit = cfg.rate_limit.as_ref().unwrap_or(&PingRateLimit {
197            seconds_per_interval: 60,
198            pings_per_interval: 15,
199        });
200        upload_manager.set_rate_limiter(
201            rate_limit.seconds_per_interval,
202            rate_limit.pings_per_interval,
203        );
204
205        // We only scan the pending ping directories when calling this from a subprocess,
206        // when calling this from ::new we need to scan the directories after dealing with the upload state.
207        if scan_directories {
208            let _scanning_thread = upload_manager.scan_pending_pings_directories(false);
209        }
210
211        let start_time = local_now_with_offset();
212        let mut this = Self {
213            upload_enabled: cfg.upload_enabled,
214            // In the subprocess, we want to avoid accessing the database entirely.
215            // The easiest way to ensure that is to just not initialize it.
216            data_store: None,
217            event_data_store,
218            core_metrics: CoreMetrics::new(),
219            additional_metrics: AdditionalMetrics::new(),
220            database_metrics: DatabaseMetrics::new(),
221            internal_pings: InternalPings::new(cfg.enable_internal_pings),
222            upload_manager,
223            data_path: PathBuf::from(&cfg.data_path),
224            application_id,
225            ping_registry: HashMap::new(),
226            start_time,
227            max_events: cfg.max_events.unwrap_or(DEFAULT_MAX_EVENTS),
228            is_first_run: false,
229            debug: DebugOptions::new(),
230            app_build: cfg.app_build.to_string(),
231            // Subprocess doesn't use "metrics" pings so has no need for a scheduler.
232            schedule_metrics_pings: false,
233            remote_settings_epoch: AtomicU8::new(0),
234            remote_settings_config: Arc::new(Mutex::new(RemoteSettingsConfig::new())),
235            with_timestamps: cfg.enable_event_timestamps,
236            ping_schedule: cfg.ping_schedule.clone(),
237        };
238
239        // Ensuring these pings are registered.
240        let pings = this.internal_pings.clone();
241        this.register_ping_type(&pings.baseline);
242        this.register_ping_type(&pings.metrics);
243        this.register_ping_type(&pings.events);
244        this.register_ping_type(&pings.deletion_request);
245
246        Ok(this)
247    }
248
249    /// Creates and initializes a new Glean object.
250    ///
251    /// This will create the necessary directories and files in
252    /// [`cfg.data_path`](InternalConfiguration::data_path). This will also initialize
253    /// the core metrics.
254    pub fn new(cfg: InternalConfiguration) -> Result<Self> {
255        let mut glean = Self::new_for_subprocess(&cfg, false)?;
256
257        // Creating the data store creates the necessary path as well.
258        // If that fails we bail out and don't initialize further.
259        let data_path = Path::new(&cfg.data_path);
260        let ping_lifetime_threshold = cfg.ping_lifetime_threshold as usize;
261        let ping_lifetime_max_time = Duration::from_millis(cfg.ping_lifetime_max_time);
262        glean.data_store = Some(Database::new(
263            data_path,
264            cfg.delay_ping_lifetime_io,
265            ping_lifetime_threshold,
266            ping_lifetime_max_time,
267        )?);
268
269        // Set experimentation identifier (if any)
270        if let Some(experimentation_id) = &cfg.experimentation_id {
271            glean
272                .additional_metrics
273                .experimentation_id
274                .set_sync(&glean, experimentation_id.to_string());
275        }
276
277        // The upload enabled flag may have changed since the last run, for
278        // example by the changing of a config file.
279        if cfg.upload_enabled {
280            // If upload is enabled, just follow the normal code path to
281            // instantiate the core metrics.
282            glean.on_upload_enabled();
283        } else {
284            // If upload is disabled, then clear the metrics
285            // but do not send a deletion request ping.
286            // If we have run before, and we have an old client_id,
287            // do the full upload disabled operations to clear metrics
288            // and send a deletion request ping.
289            match glean
290                .core_metrics
291                .client_id
292                .get_value(&glean, Some("glean_client_info"))
293            {
294                None => glean.clear_metrics(),
295                Some(uuid) => {
296                    if uuid == *KNOWN_CLIENT_ID {
297                        // Previously Glean kept the KNOWN_CLIENT_ID stored.
298                        // Let's ensure we erase it now.
299                        if let Some(data) = glean.data_store.as_ref() {
300                            _ = data.remove_single_metric(
301                                Lifetime::User,
302                                "glean_client_info",
303                                "client_id",
304                            );
305                        }
306                    } else {
307                        // Temporarily enable uploading so we can submit a
308                        // deletion request ping.
309                        glean.upload_enabled = true;
310                        glean.on_upload_disabled(true);
311                    }
312                }
313            }
314        }
315
316        // We set this only for non-subprocess situations.
317        // If internal pings are disabled, we don't set up the MPS either,
318        // it wouldn't send any data anyway.
319        glean.schedule_metrics_pings = cfg.enable_internal_pings && cfg.use_core_mps;
320
321        // We only scan the pendings pings directories **after** dealing with the upload state.
322        // If upload is disabled, we delete all pending pings files
323        // and we need to do that **before** scanning the pending pings folder
324        // to ensure we don't enqueue pings before their files are deleted.
325        let _scanning_thread = glean.upload_manager.scan_pending_pings_directories(true);
326
327        Ok(glean)
328    }
329
330    /// For tests make it easy to create a Glean object using only the required configuration.
331    #[cfg(test)]
332    pub(crate) fn with_options(
333        data_path: &str,
334        application_id: &str,
335        upload_enabled: bool,
336        enable_internal_pings: bool,
337    ) -> Self {
338        let cfg = InternalConfiguration {
339            data_path: data_path.into(),
340            application_id: application_id.into(),
341            language_binding_name: "Rust".into(),
342            upload_enabled,
343            max_events: None,
344            delay_ping_lifetime_io: false,
345            app_build: "Unknown".into(),
346            use_core_mps: false,
347            trim_data_to_registered_pings: false,
348            log_level: None,
349            rate_limit: None,
350            enable_event_timestamps: true,
351            experimentation_id: None,
352            enable_internal_pings,
353            ping_schedule: Default::default(),
354            ping_lifetime_threshold: 0,
355            ping_lifetime_max_time: 0,
356        };
357
358        let mut glean = Self::new(cfg).unwrap();
359
360        // Disable all upload manager policies for testing
361        glean.upload_manager = PingUploadManager::no_policy(data_path);
362
363        glean
364    }
365
366    /// Destroys the database.
367    ///
368    /// After this Glean needs to be reinitialized.
369    pub fn destroy_db(&mut self) {
370        self.data_store = None;
371    }
372
373    /// Initializes the core metrics managed by Glean's Rust core.
374    fn initialize_core_metrics(&mut self) {
375        let need_new_client_id = match self
376            .core_metrics
377            .client_id
378            .get_value(self, Some("glean_client_info"))
379        {
380            None => true,
381            Some(uuid) => uuid == *KNOWN_CLIENT_ID,
382        };
383        if need_new_client_id {
384            self.core_metrics.client_id.generate_and_set_sync(self);
385        }
386
387        if self
388            .core_metrics
389            .first_run_date
390            .get_value(self, "glean_client_info")
391            .is_none()
392        {
393            self.core_metrics.first_run_date.set_sync(self, None);
394            // The `first_run_date` field is generated on the very first run
395            // and persisted across upload toggling. We can assume that, the only
396            // time it is set, that's indeed our "first run".
397            self.is_first_run = true;
398        }
399
400        self.set_application_lifetime_core_metrics();
401    }
402
403    /// Initializes the database metrics managed by Glean's Rust core.
404    fn initialize_database_metrics(&mut self) {
405        log::trace!("Initializing database metrics");
406
407        if let Some(size) = self
408            .data_store
409            .as_ref()
410            .and_then(|database| database.file_size())
411        {
412            log::trace!("Database file size: {}", size.get());
413            self.database_metrics
414                .size
415                .accumulate_sync(self, size.get() as i64)
416        }
417
418        if let Some(rkv_load_state) = self
419            .data_store
420            .as_ref()
421            .and_then(|database| database.rkv_load_state())
422        {
423            self.database_metrics
424                .rkv_load_error
425                .set_sync(self, rkv_load_state)
426        }
427    }
428
429    /// Signals that the environment is ready to submit pings.
430    ///
431    /// Should be called when Glean is initialized to the point where it can correctly assemble pings.
432    /// Usually called from the language binding after all of the core metrics have been set
433    /// and the ping types have been registered.
434    ///
435    /// # Arguments
436    ///
437    /// * `trim_data_to_registered_pings` - Whether we should limit to storing data only for
438    ///   data belonging to pings previously registered via `register_ping_type`.
439    ///
440    /// # Returns
441    ///
442    /// Whether the "events" ping was submitted.
443    pub fn on_ready_to_submit_pings(&mut self, trim_data_to_registered_pings: bool) -> bool {
444        // When upload is disabled on init we already clear out metrics.
445        // However at that point not all pings are registered and so we keep that data around.
446        // By the time we would be ready to submit we try again cleaning out metrics from
447        // now-known pings.
448        if !self.upload_enabled {
449            log::debug!("on_ready_to_submit_pings. let's clear pings once again.");
450            self.clear_metrics();
451        }
452
453        self.event_data_store
454            .flush_pending_events_on_startup(self, trim_data_to_registered_pings)
455    }
456
457    /// Sets whether upload is enabled or not.
458    ///
459    /// When uploading is disabled, metrics aren't recorded at all and no
460    /// data is uploaded.
461    ///
462    /// When disabling, all pending metrics, events and queued pings are cleared.
463    ///
464    /// When enabling, the core Glean metrics are recreated.
465    ///
466    /// If the value of this flag is not actually changed, this is a no-op.
467    ///
468    /// # Arguments
469    ///
470    /// * `flag` - When true, enable metric collection.
471    ///
472    /// # Returns
473    ///
474    /// Whether the flag was different from the current value,
475    /// and actual work was done to clear or reinstate metrics.
476    pub fn set_upload_enabled(&mut self, flag: bool) -> bool {
477        log::info!("Upload enabled: {:?}", flag);
478
479        if self.upload_enabled != flag {
480            if flag {
481                self.on_upload_enabled();
482            } else {
483                self.on_upload_disabled(false);
484            }
485            true
486        } else {
487            false
488        }
489    }
490
491    /// Enable or disable a ping.
492    ///
493    /// Disabling a ping causes all data for that ping to be removed from storage
494    /// and all pending pings of that type to be deleted.
495    ///
496    /// **Note**: Do not use directly. Call `PingType::set_enabled` instead.
497    #[doc(hidden)]
498    pub fn set_ping_enabled(&mut self, ping: &PingType, enabled: bool) {
499        ping.store_enabled(enabled);
500        if !enabled {
501            if let Some(data) = self.data_store.as_ref() {
502                _ = data.clear_ping_lifetime_storage(ping.name());
503                _ = data.clear_lifetime_storage(Lifetime::User, ping.name());
504                _ = data.clear_lifetime_storage(Lifetime::Application, ping.name());
505            }
506            let ping_maker = PingMaker::new();
507            let disabled_pings = &[ping.name()][..];
508            if let Err(err) = ping_maker.clear_pending_pings(self.get_data_path(), disabled_pings) {
509                log::warn!("Error clearing pending pings: {}", err);
510            }
511        }
512    }
513
514    /// Determines whether upload is enabled.
515    ///
516    /// When upload is disabled, no data will be recorded.
517    pub fn is_upload_enabled(&self) -> bool {
518        self.upload_enabled
519    }
520
521    /// Check if a ping is enabled.
522    ///
523    /// Note that some internal "ping" names are considered to be always enabled.
524    ///
525    /// If a ping is not known to Glean ("unregistered") it is always considered disabled.
526    /// If a ping is known, it can be enabled/disabled at any point.
527    /// Only data for enabled pings is recorded.
528    /// Disabled pings are never submitted.
529    pub fn is_ping_enabled(&self, ping: &str) -> bool {
530        // We "abuse" pings/storage names for internal data.
531        const DEFAULT_ENABLED: &[&str] = &[
532            "glean_client_info",
533            "glean_internal_info",
534            // for `experimentation_id`.
535            // That should probably have gone into `glean_internal_info` instead.
536            "all-pings",
537        ];
538
539        // `client_info`-like stuff is always enabled.
540        if DEFAULT_ENABLED.contains(&ping) {
541            return true;
542        }
543
544        let Some(ping) = self.ping_registry.get(ping) else {
545            log::trace!("Unknown ping {ping}. Assuming disabled.");
546            return false;
547        };
548
549        ping.enabled(self)
550    }
551
552    /// Handles the changing of state from upload disabled to enabled.
553    ///
554    /// Should only be called when the state actually changes.
555    ///
556    /// The `upload_enabled` flag is set to true and the core Glean metrics are
557    /// recreated.
558    fn on_upload_enabled(&mut self) {
559        self.upload_enabled = true;
560        self.initialize_core_metrics();
561        self.initialize_database_metrics();
562    }
563
564    /// Handles the changing of state from upload enabled to disabled.
565    ///
566    /// Should only be called when the state actually changes.
567    ///
568    /// A deletion_request ping is sent, all pending metrics, events and queued
569    /// pings are cleared, and the client_id is set to KNOWN_CLIENT_ID.
570    /// Afterward, the upload_enabled flag is set to false.
571    fn on_upload_disabled(&mut self, during_init: bool) {
572        // The upload_enabled flag should be true here, or the deletion ping
573        // won't be submitted.
574        let reason = if during_init {
575            Some("at_init")
576        } else {
577            Some("set_upload_enabled")
578        };
579        if !self
580            .internal_pings
581            .deletion_request
582            .submit_sync(self, reason)
583        {
584            log::error!("Failed to submit deletion-request ping on optout.");
585        }
586        self.clear_metrics();
587        self.upload_enabled = false;
588    }
589
590    /// Clear any pending metrics when telemetry is disabled.
591    fn clear_metrics(&mut self) {
592        // Clear the pending pings queue and acquire the lock
593        // so that it can't be accessed until this function is done.
594        let _lock = self.upload_manager.clear_ping_queue();
595
596        // Clear any pending pings that follow `collection_enabled`.
597        let ping_maker = PingMaker::new();
598        let disabled_pings = self
599            .ping_registry
600            .iter()
601            .filter(|&(_ping_name, ping)| ping.follows_collection_enabled())
602            .map(|(ping_name, _ping)| &ping_name[..])
603            .collect::<Vec<_>>();
604        if let Err(err) = ping_maker.clear_pending_pings(self.get_data_path(), &disabled_pings) {
605            log::warn!("Error clearing pending pings: {}", err);
606        }
607
608        // Delete all stored metrics.
609        // Note that this also includes the ping sequence numbers, so it has
610        // the effect of resetting those to their initial values.
611        if let Some(data) = self.data_store.as_ref() {
612            _ = data.clear_lifetime_storage(Lifetime::User, "glean_internal_info");
613            _ = data.remove_single_metric(Lifetime::User, "glean_client_info", "client_id");
614            for (ping_name, ping) in &self.ping_registry {
615                if ping.follows_collection_enabled() {
616                    _ = data.clear_ping_lifetime_storage(ping_name);
617                    _ = data.clear_lifetime_storage(Lifetime::User, ping_name);
618                    _ = data.clear_lifetime_storage(Lifetime::Application, ping_name);
619                }
620            }
621        }
622        if let Err(err) = self.event_data_store.clear_all() {
623            log::warn!("Error clearing pending events: {}", err);
624        }
625
626        // This does not clear the experiments store (which isn't managed by the
627        // StorageEngineManager), since doing so would mean we would have to have the
628        // application tell us again which experiments are active if telemetry is
629        // re-enabled.
630    }
631
632    /// Gets the application ID as specified on instantiation.
633    pub fn get_application_id(&self) -> &str {
634        &self.application_id
635    }
636
637    /// Gets the data path of this instance.
638    pub fn get_data_path(&self) -> &Path {
639        &self.data_path
640    }
641
642    /// Gets a handle to the database.
643    #[track_caller] // If this fails we're interested in the caller.
644    pub fn storage(&self) -> &Database {
645        self.data_store.as_ref().expect("No database found")
646    }
647
648    /// Gets an optional handle to the database.
649    pub fn storage_opt(&self) -> Option<&Database> {
650        self.data_store.as_ref()
651    }
652
653    /// Gets a handle to the event database.
654    pub fn event_storage(&self) -> &EventDatabase {
655        &self.event_data_store
656    }
657
658    pub(crate) fn with_timestamps(&self) -> bool {
659        self.with_timestamps
660    }
661
662    /// Gets the maximum number of events to store before sending a ping.
663    pub fn get_max_events(&self) -> usize {
664        let remote_settings_config = self.remote_settings_config.lock().unwrap();
665
666        if let Some(max_events) = remote_settings_config.event_threshold {
667            max_events as usize
668        } else {
669            self.max_events as usize
670        }
671    }
672
673    /// Gets the next task for an uploader.
674    ///
675    /// This can be one of:
676    ///
677    /// * [`Wait`](PingUploadTask::Wait) - which means the requester should ask
678    ///   again later;
679    /// * [`Upload(PingRequest)`](PingUploadTask::Upload) - which means there is
680    ///   a ping to upload. This wraps the actual request object;
681    /// * [`Done`](PingUploadTask::Done) - which means requester should stop
682    ///   asking for now.
683    ///
684    /// # Returns
685    ///
686    /// A [`PingUploadTask`] representing the next task.
687    pub fn get_upload_task(&self) -> PingUploadTask {
688        self.upload_manager.get_upload_task(self, self.log_pings())
689    }
690
691    /// Processes the response from an attempt to upload a ping.
692    ///
693    /// # Arguments
694    ///
695    /// * `uuid` - The UUID of the ping in question.
696    /// * `status` - The upload result.
697    pub fn process_ping_upload_response(
698        &self,
699        uuid: &str,
700        status: UploadResult,
701    ) -> UploadTaskAction {
702        self.upload_manager
703            .process_ping_upload_response(self, uuid, status)
704    }
705
706    /// Takes a snapshot for the given store and optionally clear it.
707    ///
708    /// # Arguments
709    ///
710    /// * `store_name` - The store to snapshot.
711    /// * `clear_store` - Whether to clear the store after snapshotting.
712    ///
713    /// # Returns
714    ///
715    /// The snapshot in a string encoded as JSON. If the snapshot is empty, returns an empty string.
716    pub fn snapshot(&mut self, store_name: &str, clear_store: bool) -> String {
717        StorageManager
718            .snapshot(self.storage(), store_name, clear_store)
719            .unwrap_or_else(|| String::from(""))
720    }
721
722    pub(crate) fn make_path(&self, ping_name: &str, doc_id: &str) -> String {
723        format!(
724            "/submit/{}/{}/{}/{}",
725            self.get_application_id(),
726            ping_name,
727            GLEAN_SCHEMA_VERSION,
728            doc_id
729        )
730    }
731
732    /// Collects and submits a ping by name for eventual uploading.
733    ///
734    /// The ping content is assembled as soon as possible, but upload is not
735    /// guaranteed to happen immediately, as that depends on the upload policies.
736    ///
737    /// If the ping currently contains no content, it will not be sent,
738    /// unless it is configured to be sent if empty.
739    ///
740    /// # Arguments
741    ///
742    /// * `ping_name` - The name of the ping to submit
743    /// * `reason` - A reason code to include in the ping
744    ///
745    /// # Returns
746    ///
747    /// Whether the ping was succesfully assembled and queued.
748    ///
749    /// # Errors
750    ///
751    /// If collecting or writing the ping to disk failed.
752    pub fn submit_ping_by_name(&self, ping_name: &str, reason: Option<&str>) -> bool {
753        match self.get_ping_by_name(ping_name) {
754            None => {
755                log::error!("Attempted to submit unknown ping '{}'", ping_name);
756                false
757            }
758            Some(ping) => ping.submit_sync(self, reason),
759        }
760    }
761
762    /// Gets a [`PingType`] by name.
763    ///
764    /// # Returns
765    ///
766    /// The [`PingType`] of a ping if the given name was registered before, [`None`]
767    /// otherwise.
768    pub fn get_ping_by_name(&self, ping_name: &str) -> Option<&PingType> {
769        self.ping_registry.get(ping_name)
770    }
771
772    /// Register a new [`PingType`](metrics/struct.PingType.html).
773    pub fn register_ping_type(&mut self, ping: &PingType) {
774        if self.ping_registry.contains_key(ping.name()) {
775            log::debug!("Duplicate ping named '{}'", ping.name())
776        }
777
778        self.ping_registry
779            .insert(ping.name().to_string(), ping.clone());
780    }
781
782    /// Gets a list of currently registered ping names.
783    ///
784    /// # Returns
785    ///
786    /// The list of ping names that are currently registered.
787    pub fn get_registered_ping_names(&self) -> Vec<&str> {
788        self.ping_registry.keys().map(String::as_str).collect()
789    }
790
791    /// Get create time of the Glean object.
792    pub(crate) fn start_time(&self) -> DateTime<FixedOffset> {
793        self.start_time
794    }
795
796    /// Indicates that an experiment is running.
797    ///
798    /// Glean will then add an experiment annotation to the environment
799    /// which is sent with pings. This information is not persisted between runs.
800    ///
801    /// # Arguments
802    ///
803    /// * `experiment_id` - The id of the active experiment (maximum 30 bytes).
804    /// * `branch` - The experiment branch (maximum 30 bytes).
805    /// * `extra` - Optional metadata to output with the ping.
806    pub fn set_experiment_active(
807        &self,
808        experiment_id: String,
809        branch: String,
810        extra: HashMap<String, String>,
811    ) {
812        let metric = ExperimentMetric::new(self, experiment_id);
813        metric.set_active_sync(self, branch, extra);
814    }
815
816    /// Indicates that an experiment is no longer running.
817    ///
818    /// # Arguments
819    ///
820    /// * `experiment_id` - The id of the active experiment to deactivate (maximum 30 bytes).
821    pub fn set_experiment_inactive(&self, experiment_id: String) {
822        let metric = ExperimentMetric::new(self, experiment_id);
823        metric.set_inactive_sync(self);
824    }
825
826    /// **Test-only API (exported for FFI purposes).**
827    ///
828    /// Gets stored data for the requested experiment.
829    ///
830    /// # Arguments
831    ///
832    /// * `experiment_id` - The id of the active experiment (maximum 30 bytes).
833    pub fn test_get_experiment_data(&self, experiment_id: String) -> Option<RecordedExperiment> {
834        let metric = ExperimentMetric::new(self, experiment_id);
835        metric.test_get_value(self)
836    }
837
838    /// **Test-only API (exported for FFI purposes).**
839    ///
840    /// Gets stored experimentation id annotation.
841    pub fn test_get_experimentation_id(&self) -> Option<String> {
842        self.additional_metrics
843            .experimentation_id
844            .get_value(self, None)
845    }
846
847    /// Set configuration to override the default state, typically initiated from a
848    /// remote_settings experiment or rollout
849    ///
850    /// # Arguments
851    ///
852    /// * `cfg` - The stringified JSON representation of a `RemoteSettingsConfig` object
853    pub fn apply_server_knobs_config(&self, cfg: RemoteSettingsConfig) {
854        // Set the current RemoteSettingsConfig, keeping the lock until the epoch is
855        // updated to prevent against reading a "new" config but an "old" epoch
856        let mut remote_settings_config = self.remote_settings_config.lock().unwrap();
857
858        // Merge the exising metrics configuration with the supplied one
859        remote_settings_config
860            .metrics_enabled
861            .extend(cfg.metrics_enabled);
862
863        // Merge the exising ping configuration with the supplied one
864        remote_settings_config
865            .pings_enabled
866            .extend(cfg.pings_enabled);
867
868        remote_settings_config.event_threshold = cfg.event_threshold;
869
870        // Update remote_settings epoch
871        self.remote_settings_epoch.fetch_add(1, Ordering::SeqCst);
872    }
873
874    /// Persists [`Lifetime::Ping`] data that might be in memory in case
875    /// [`delay_ping_lifetime_io`](InternalConfiguration::delay_ping_lifetime_io) is set
876    /// or was set at a previous time.
877    ///
878    /// If there is no data to persist, this function does nothing.
879    pub fn persist_ping_lifetime_data(&self) -> Result<()> {
880        if let Some(data) = self.data_store.as_ref() {
881            return data.persist_ping_lifetime_data();
882        }
883
884        Ok(())
885    }
886
887    /// Sets internally-handled application lifetime metrics.
888    fn set_application_lifetime_core_metrics(&self) {
889        self.core_metrics.os.set_sync(self, system::OS);
890    }
891
892    /// **This is not meant to be used directly.**
893    ///
894    /// Clears all the metrics that have [`Lifetime::Application`].
895    pub fn clear_application_lifetime_metrics(&self) {
896        log::trace!("Clearing Lifetime::Application metrics");
897        if let Some(data) = self.data_store.as_ref() {
898            data.clear_lifetime(Lifetime::Application);
899        }
900
901        // Set internally handled app lifetime metrics again.
902        self.set_application_lifetime_core_metrics();
903    }
904
905    /// Whether or not this is the first run on this profile.
906    pub fn is_first_run(&self) -> bool {
907        self.is_first_run
908    }
909
910    /// Sets a debug view tag.
911    ///
912    /// This will return `false` in case `value` is not a valid tag.
913    ///
914    /// When the debug view tag is set, pings are sent with a `X-Debug-ID` header with the value of the tag
915    /// and are sent to the ["Ping Debug Viewer"](https://mozilla.github.io/glean/book/dev/core/internal/debug-pings.html).
916    ///
917    /// # Arguments
918    ///
919    /// * `value` - A valid HTTP header value. Must match the regex: "[a-zA-Z0-9-]{1,20}".
920    pub fn set_debug_view_tag(&mut self, value: &str) -> bool {
921        self.debug.debug_view_tag.set(value.into())
922    }
923
924    /// Return the value for the debug view tag or [`None`] if it hasn't been set.
925    ///
926    /// The `debug_view_tag` may be set from an environment variable
927    /// (`GLEAN_DEBUG_VIEW_TAG`) or through the `set_debug_view_tag` function.
928    pub fn debug_view_tag(&self) -> Option<&String> {
929        self.debug.debug_view_tag.get()
930    }
931
932    /// Sets source tags.
933    ///
934    /// This will return `false` in case `value` contains invalid tags.
935    ///
936    /// Ping tags will show in the destination datasets, after ingestion.
937    ///
938    /// **Note** If one or more tags are invalid, all tags are ignored.
939    ///
940    /// # Arguments
941    ///
942    /// * `value` - A vector of at most 5 valid HTTP header values. Individual tags must match the regex: "[a-zA-Z0-9-]{1,20}".
943    pub fn set_source_tags(&mut self, value: Vec<String>) -> bool {
944        self.debug.source_tags.set(value)
945    }
946
947    /// Return the value for the source tags or [`None`] if it hasn't been set.
948    ///
949    /// The `source_tags` may be set from an environment variable (`GLEAN_SOURCE_TAGS`)
950    /// or through the [`set_source_tags`] function.
951    pub(crate) fn source_tags(&self) -> Option<&Vec<String>> {
952        self.debug.source_tags.get()
953    }
954
955    /// Sets the log pings debug option.
956    ///
957    /// This will return `false` in case we are unable to set the option.
958    ///
959    /// When the log pings debug option is `true`,
960    /// we log the payload of all succesfully assembled pings.
961    ///
962    /// # Arguments
963    ///
964    /// * `value` - The value of the log pings option
965    pub fn set_log_pings(&mut self, value: bool) -> bool {
966        self.debug.log_pings.set(value)
967    }
968
969    /// Return the value for the log pings debug option or `false` if it hasn't been set.
970    ///
971    /// The `log_pings` option may be set from an environment variable (`GLEAN_LOG_PINGS`)
972    /// or through the `set_log_pings` function.
973    pub fn log_pings(&self) -> bool {
974        self.debug.log_pings.get().copied().unwrap_or(false)
975    }
976
977    fn get_dirty_bit_metric(&self) -> metrics::BooleanMetric {
978        metrics::BooleanMetric::new(CommonMetricData {
979            name: "dirtybit".into(),
980            // We don't need a category, the name is already unique
981            category: "".into(),
982            send_in_pings: vec![INTERNAL_STORAGE.into()],
983            lifetime: Lifetime::User,
984            ..Default::default()
985        })
986    }
987
988    /// **This is not meant to be used directly.**
989    ///
990    /// Sets the value of a "dirty flag" in the permanent storage.
991    ///
992    /// The "dirty flag" is meant to have the following behaviour, implemented
993    /// by the consumers of the FFI layer:
994    ///
995    /// - on mobile: set to `false` when going to background or shutting down,
996    ///   set to `true` at startup and when going to foreground.
997    /// - on non-mobile platforms: set to `true` at startup and `false` at
998    ///   shutdown.
999    ///
1000    /// At startup, before setting its new value, if the "dirty flag" value is
1001    /// `true`, then Glean knows it did not exit cleanly and can implement
1002    /// coping mechanisms (e.g. sending a `baseline` ping).
1003    pub fn set_dirty_flag(&self, new_value: bool) {
1004        self.get_dirty_bit_metric().set_sync(self, new_value);
1005    }
1006
1007    /// **This is not meant to be used directly.**
1008    ///
1009    /// Checks the stored value of the "dirty flag".
1010    pub fn is_dirty_flag_set(&self) -> bool {
1011        let dirty_bit_metric = self.get_dirty_bit_metric();
1012        match StorageManager.snapshot_metric(
1013            self.storage(),
1014            INTERNAL_STORAGE,
1015            &dirty_bit_metric.meta().identifier(self),
1016            dirty_bit_metric.meta().inner.lifetime,
1017        ) {
1018            Some(Metric::Boolean(b)) => b,
1019            _ => false,
1020        }
1021    }
1022
1023    /// Performs the collection/cleanup operations required by becoming active.
1024    ///
1025    /// This functions generates a baseline ping with reason `active`
1026    /// and then sets the dirty bit.
1027    pub fn handle_client_active(&mut self) {
1028        if !self
1029            .internal_pings
1030            .baseline
1031            .submit_sync(self, Some("active"))
1032        {
1033            log::info!("baseline ping not submitted on active");
1034        }
1035
1036        self.set_dirty_flag(true);
1037    }
1038
1039    /// Performs the collection/cleanup operations required by becoming inactive.
1040    ///
1041    /// This functions generates a baseline and an events ping with reason
1042    /// `inactive` and then clears the dirty bit.
1043    pub fn handle_client_inactive(&mut self) {
1044        if !self
1045            .internal_pings
1046            .baseline
1047            .submit_sync(self, Some("inactive"))
1048        {
1049            log::info!("baseline ping not submitted on inactive");
1050        }
1051
1052        if !self
1053            .internal_pings
1054            .events
1055            .submit_sync(self, Some("inactive"))
1056        {
1057            log::info!("events ping not submitted on inactive");
1058        }
1059
1060        self.set_dirty_flag(false);
1061    }
1062
1063    /// **Test-only API (exported for FFI purposes).**
1064    ///
1065    /// Deletes all stored metrics.
1066    ///
1067    /// Note that this also includes the ping sequence numbers, so it has
1068    /// the effect of resetting those to their initial values.
1069    pub fn test_clear_all_stores(&self) {
1070        if let Some(data) = self.data_store.as_ref() {
1071            data.clear_all()
1072        }
1073        // We don't care about this failing, maybe the data does just not exist.
1074        let _ = self.event_data_store.clear_all();
1075    }
1076
1077    /// Instructs the Metrics Ping Scheduler's thread to exit cleanly.
1078    /// If Glean was configured with `use_core_mps: false`, this has no effect.
1079    pub fn cancel_metrics_ping_scheduler(&self) {
1080        if self.schedule_metrics_pings {
1081            scheduler::cancel();
1082        }
1083    }
1084
1085    /// Instructs the Metrics Ping Scheduler to being scheduling metrics pings.
1086    /// If Glean wsa configured with `use_core_mps: false`, this has no effect.
1087    pub fn start_metrics_ping_scheduler(&self) {
1088        if self.schedule_metrics_pings {
1089            scheduler::schedule(self);
1090        }
1091    }
1092
1093    /// Updates attribution fields with new values.
1094    /// AttributionMetrics fields with `None` values will not overwrite older values.
1095    pub fn update_attribution(&self, attribution: AttributionMetrics) {
1096        if let Some(source) = attribution.source {
1097            self.core_metrics.attribution_source.set_sync(self, source);
1098        }
1099        if let Some(medium) = attribution.medium {
1100            self.core_metrics.attribution_medium.set_sync(self, medium);
1101        }
1102        if let Some(campaign) = attribution.campaign {
1103            self.core_metrics
1104                .attribution_campaign
1105                .set_sync(self, campaign);
1106        }
1107        if let Some(term) = attribution.term {
1108            self.core_metrics.attribution_term.set_sync(self, term);
1109        }
1110        if let Some(content) = attribution.content {
1111            self.core_metrics
1112                .attribution_content
1113                .set_sync(self, content);
1114        }
1115    }
1116
1117    /// **TEST-ONLY Method**
1118    ///
1119    /// Returns the current attribution metrics.
1120    pub fn test_get_attribution(&self) -> AttributionMetrics {
1121        AttributionMetrics {
1122            source: self
1123                .core_metrics
1124                .attribution_source
1125                .get_value(self, Some("glean_client_info")),
1126            medium: self
1127                .core_metrics
1128                .attribution_medium
1129                .get_value(self, Some("glean_client_info")),
1130            campaign: self
1131                .core_metrics
1132                .attribution_campaign
1133                .get_value(self, Some("glean_client_info")),
1134            term: self
1135                .core_metrics
1136                .attribution_term
1137                .get_value(self, Some("glean_client_info")),
1138            content: self
1139                .core_metrics
1140                .attribution_content
1141                .get_value(self, Some("glean_client_info")),
1142        }
1143    }
1144
1145    /// Updates distribution fields with new values.
1146    /// DistributionMetrics fields with `None` values will not overwrite older values.
1147    pub fn update_distribution(&self, distribution: DistributionMetrics) {
1148        if let Some(name) = distribution.name {
1149            self.core_metrics.distribution_name.set_sync(self, name);
1150        }
1151    }
1152
1153    /// **TEST-ONLY Method**
1154    ///
1155    /// Returns the current distribution metrics.
1156    pub fn test_get_distribution(&self) -> DistributionMetrics {
1157        DistributionMetrics {
1158            name: self
1159                .core_metrics
1160                .distribution_name
1161                .get_value(self, Some("glean_client_info")),
1162        }
1163    }
1164}