glean_core/
lib.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
5#![allow(clippy::doc_overindented_list_items)]
6#![allow(clippy::significant_drop_in_scrutinee)]
7#![allow(clippy::uninlined_format_args)]
8#![deny(rustdoc::broken_intra_doc_links)]
9#![deny(missing_docs)]
10
11//! Glean is a modern approach for recording and sending Telemetry data.
12//!
13//! It's in use at Mozilla.
14//!
15//! All documentation can be found online:
16//!
17//! ## [The Glean SDK Book](https://mozilla.github.io/glean)
18
19use std::borrow::Cow;
20use std::collections::HashMap;
21use std::path::Path;
22use std::sync::atomic::{AtomicBool, Ordering};
23use std::sync::{Arc, Mutex};
24use std::time::{Duration, UNIX_EPOCH};
25use std::{fmt, fs};
26
27use crossbeam_channel::unbounded;
28use log::LevelFilter;
29use malloc_size_of_derive::MallocSizeOf;
30use once_cell::sync::{Lazy, OnceCell};
31use uuid::Uuid;
32
33use metrics::RemoteSettingsConfig;
34
35mod common_metric_data;
36mod core;
37mod core_metrics;
38mod coverage;
39mod database;
40mod debug;
41mod dispatcher;
42mod error;
43mod error_recording;
44mod event_database;
45mod glean_metrics;
46mod histogram;
47mod internal_metrics;
48mod internal_pings;
49pub mod metrics;
50pub mod ping;
51mod scheduler;
52pub mod storage;
53mod system;
54#[doc(hidden)]
55pub mod thread;
56pub mod traits;
57pub mod upload;
58mod util;
59
60#[cfg(all(not(target_os = "android"), not(target_os = "ios")))]
61mod fd_logger;
62
63pub use crate::common_metric_data::{CommonMetricData, DynamicLabelType, Lifetime};
64pub use crate::core::Glean;
65pub use crate::core_metrics::{AttributionMetrics, ClientInfoMetrics, DistributionMetrics};
66use crate::dispatcher::is_test_mode;
67pub use crate::error::{Error, ErrorKind, Result};
68pub use crate::error_recording::{test_get_num_recorded_errors, ErrorType};
69pub use crate::histogram::HistogramType;
70use crate::internal_metrics::DataDirectoryInfoObject;
71pub use crate::metrics::labeled::{
72    AllowLabeled, LabeledBoolean, LabeledCounter, LabeledCustomDistribution,
73    LabeledMemoryDistribution, LabeledMetric, LabeledMetricData, LabeledQuantity, LabeledString,
74    LabeledTimingDistribution,
75};
76pub use crate::metrics::{
77    BooleanMetric, CounterMetric, CustomDistributionMetric, Datetime, DatetimeMetric,
78    DenominatorMetric, DistributionData, DualLabeledCounterMetric, EventMetric,
79    LocalCustomDistribution, LocalMemoryDistribution, LocalTimingDistribution,
80    MemoryDistributionMetric, MemoryUnit, NumeratorMetric, ObjectMetric, PingType, QuantityMetric,
81    Rate, RateMetric, RecordedEvent, RecordedExperiment, StringListMetric, StringMetric,
82    TestGetValue, TextMetric, TimeUnit, TimerId, TimespanMetric, TimingDistributionMetric,
83    UrlMetric, UuidMetric,
84};
85pub use crate::upload::{PingRequest, PingUploadTask, UploadResult, UploadTaskAction};
86
87const GLEAN_VERSION: &str = env!("CARGO_PKG_VERSION");
88const GLEAN_SCHEMA_VERSION: u32 = 1;
89const DEFAULT_MAX_EVENTS: u32 = 500;
90static KNOWN_CLIENT_ID: Lazy<Uuid> =
91    Lazy::new(|| Uuid::parse_str("c0ffeec0-ffee-c0ff-eec0-ffeec0ffeec0").unwrap());
92
93// The names of the pings directories.
94pub(crate) const PENDING_PINGS_DIRECTORY: &str = "pending_pings";
95pub(crate) const DELETION_REQUEST_PINGS_DIRECTORY: &str = "deletion_request";
96
97/// Set when `glean::initialize()` returns.
98/// This allows to detect calls that happen before `glean::initialize()` was called.
99/// Note: The initialization might still be in progress, as it runs in a separate thread.
100static INITIALIZE_CALLED: AtomicBool = AtomicBool::new(false);
101
102/// Keep track of the debug features before Glean is initialized.
103static PRE_INIT_DEBUG_VIEW_TAG: Mutex<String> = Mutex::new(String::new());
104static PRE_INIT_LOG_PINGS: AtomicBool = AtomicBool::new(false);
105static PRE_INIT_SOURCE_TAGS: Mutex<Vec<String>> = Mutex::new(Vec::new());
106
107/// Keep track of pings registered before Glean is initialized.
108static PRE_INIT_PING_REGISTRATION: Mutex<Vec<metrics::PingType>> = Mutex::new(Vec::new());
109static PRE_INIT_PING_ENABLED: Mutex<Vec<(metrics::PingType, bool)>> = Mutex::new(Vec::new());
110
111/// Keep track of attribution and distribution supplied before Glean is initialized.
112static PRE_INIT_ATTRIBUTION: Mutex<Option<AttributionMetrics>> = Mutex::new(None);
113static PRE_INIT_DISTRIBUTION: Mutex<Option<DistributionMetrics>> = Mutex::new(None);
114
115/// Global singleton of the handles of the glean.init threads.
116/// For joining. For tests.
117/// (Why a Vec? There might be more than one concurrent call to initialize.)
118static INIT_HANDLES: Lazy<Arc<Mutex<Vec<std::thread::JoinHandle<()>>>>> =
119    Lazy::new(|| Arc::new(Mutex::new(Vec::new())));
120
121/// Configuration for Glean
122#[derive(Debug, Clone, MallocSizeOf)]
123pub struct InternalConfiguration {
124    /// Whether upload should be enabled.
125    pub upload_enabled: bool,
126    /// Path to a directory to store all data in.
127    pub data_path: String,
128    /// The application ID (will be sanitized during initialization).
129    pub application_id: String,
130    /// The name of the programming language used by the binding creating this instance of Glean.
131    pub language_binding_name: String,
132    /// The maximum number of events to store before sending a ping containing events.
133    pub max_events: Option<u32>,
134    /// Whether Glean should delay persistence of data from metrics with ping lifetime.
135    pub delay_ping_lifetime_io: bool,
136    /// The application's build identifier. If this is different from the one provided for a previous init,
137    /// and use_core_mps is `true`, we will trigger a "metrics" ping.
138    pub app_build: String,
139    /// Whether Glean should schedule "metrics" pings.
140    pub use_core_mps: bool,
141    /// Whether Glean should, on init, trim its event storage to only the registered pings.
142    pub trim_data_to_registered_pings: bool,
143    /// The internal logging level.
144    /// ignore
145    #[ignore_malloc_size_of = "external non-allocating type"]
146    pub log_level: Option<LevelFilter>,
147    /// The rate at which pings may be uploaded before they are throttled.
148    pub rate_limit: Option<PingRateLimit>,
149    /// Whether to add a wallclock timestamp to all events.
150    pub enable_event_timestamps: bool,
151    /// An experimentation identifier derived by the application to be sent with all pings, it should
152    /// be noted that this has an underlying StringMetric and so should conform to the limitations that
153    /// StringMetric places on length, etc.
154    pub experimentation_id: Option<String>,
155    /// Whether to enable internal pings. Default: true
156    pub enable_internal_pings: bool,
157    /// A ping schedule map.
158    /// Maps a ping name to a list of pings to schedule along with it.
159    /// Only used if the ping's own ping schedule list is empty.
160    pub ping_schedule: HashMap<String, Vec<String>>,
161
162    /// Write count threshold when to auto-flush. `0` disables it.
163    pub ping_lifetime_threshold: u64,
164    /// After what time to auto-flush. 0 disables it.
165    pub ping_lifetime_max_time: u64,
166}
167
168/// How to specify the rate at which pings may be uploaded before they are throttled.
169#[derive(Debug, Clone, MallocSizeOf)]
170pub struct PingRateLimit {
171    /// Length of time in seconds of a ping uploading interval.
172    pub seconds_per_interval: u64,
173    /// Number of pings that may be uploaded in a ping uploading interval.
174    pub pings_per_interval: u32,
175}
176
177/// Launches a new task on the global dispatch queue with a reference to the Glean singleton.
178fn launch_with_glean(callback: impl FnOnce(&Glean) + Send + 'static) {
179    dispatcher::launch(|| core::with_glean(callback));
180}
181
182/// Launches a new task on the global dispatch queue with a mutable reference to the
183/// Glean singleton.
184fn launch_with_glean_mut(callback: impl FnOnce(&mut Glean) + Send + 'static) {
185    dispatcher::launch(|| core::with_glean_mut(callback));
186}
187
188/// Block on the dispatcher emptying.
189///
190/// This will panic if called before Glean is initialized.
191fn block_on_dispatcher() {
192    dispatcher::block_on_queue()
193}
194
195/// Returns a timestamp corresponding to "now" with millisecond precision.
196pub fn get_timestamp_ms() -> u64 {
197    const NANOS_PER_MILLI: u64 = 1_000_000;
198    zeitstempel::now() / NANOS_PER_MILLI
199}
200
201/// State to keep track for the Rust Language bindings.
202///
203/// This is useful for setting Glean SDK-owned metrics when
204/// the state of the upload is toggled.
205struct State {
206    /// Client info metrics set by the application.
207    client_info: ClientInfoMetrics,
208
209    callbacks: Box<dyn OnGleanEvents>,
210}
211
212/// A global singleton storing additional state for Glean.
213///
214/// Requires a Mutex, because in tests we can actual reset this.
215static STATE: OnceCell<Mutex<State>> = OnceCell::new();
216
217/// Get a reference to the global state object.
218///
219/// Panics if no global state object was set.
220#[track_caller] // If this fails we're interested in the caller.
221fn global_state() -> &'static Mutex<State> {
222    STATE.get().unwrap()
223}
224
225/// Attempt to get a reference to the global state object.
226///
227/// If it hasn't been set yet, we return None.
228#[track_caller] // If this fails we're interested in the caller.
229fn maybe_global_state() -> Option<&'static Mutex<State>> {
230    STATE.get()
231}
232
233/// Set or replace the global bindings State object.
234fn setup_state(state: State) {
235    // The `OnceCell` type wrapping our state is thread-safe and can only be set once.
236    // Therefore even if our check for it being empty succeeds, setting it could fail if a
237    // concurrent thread is quicker in setting it.
238    // However this will not cause a bigger problem, as the second `set` operation will just fail.
239    // We can log it and move on.
240    //
241    // For all wrappers this is not a problem, as the State object is intialized exactly once on
242    // calling `initialize` on the global singleton and further operations check that it has been
243    // initialized.
244    if STATE.get().is_none() {
245        if STATE.set(Mutex::new(state)).is_err() {
246            log::error!(
247                "Global Glean state object is initialized already. This probably happened concurrently."
248            );
249        }
250    } else {
251        // We allow overriding the global State object to support test mode.
252        // In test mode the State object is fully destroyed and recreated.
253        // This all happens behind a mutex and is therefore also thread-safe.
254        let mut lock = STATE.get().unwrap().lock().unwrap();
255        *lock = state;
256    }
257}
258
259/// A global singleton that stores listener callbacks registered with Glean
260/// to receive event recording notifications.
261static EVENT_LISTENERS: OnceCell<Mutex<HashMap<String, Box<dyn GleanEventListener>>>> =
262    OnceCell::new();
263
264fn event_listeners() -> &'static Mutex<HashMap<String, Box<dyn GleanEventListener>>> {
265    EVENT_LISTENERS.get_or_init(|| Mutex::new(HashMap::new()))
266}
267
268fn register_event_listener(tag: String, listener: Box<dyn GleanEventListener>) {
269    let mut lock = event_listeners().lock().unwrap();
270    lock.insert(tag, listener);
271}
272
273fn unregister_event_listener(tag: String) {
274    let mut lock = event_listeners().lock().unwrap();
275    lock.remove(&tag);
276}
277
278/// An error returned from callbacks.
279#[derive(Debug)]
280pub enum CallbackError {
281    /// An unexpected error occured.
282    UnexpectedError,
283}
284
285impl fmt::Display for CallbackError {
286    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
287        write!(f, "Unexpected error")
288    }
289}
290
291impl std::error::Error for CallbackError {}
292
293impl From<uniffi::UnexpectedUniFFICallbackError> for CallbackError {
294    fn from(_: uniffi::UnexpectedUniFFICallbackError) -> CallbackError {
295        CallbackError::UnexpectedError
296    }
297}
298
299/// A callback object used to trigger actions on the foreign-language side.
300///
301/// A callback object is stored in glean-core for the entire lifetime of the application.
302pub trait OnGleanEvents: Send {
303    /// Initialization finished.
304    ///
305    /// The language SDK can do additional things from within the same initializer thread,
306    /// e.g. starting to observe application events for foreground/background behavior.
307    /// The observer then needs to call the respective client activity API.
308    fn initialize_finished(&self);
309
310    /// Trigger the uploader whenever a ping was submitted.
311    ///
312    /// This should not block.
313    /// The uploader needs to asynchronously poll Glean for new pings to upload.
314    fn trigger_upload(&self) -> Result<(), CallbackError>;
315
316    /// Start the Metrics Ping Scheduler.
317    fn start_metrics_ping_scheduler(&self) -> bool;
318
319    /// Called when upload is disabled and uploads should be stopped
320    fn cancel_uploads(&self) -> Result<(), CallbackError>;
321
322    /// Called on shutdown, before glean-core is fully shutdown.
323    ///
324    /// * This MUST NOT put any new tasks on the dispatcher.
325    ///   * New tasks will be ignored.
326    /// * This SHOULD NOT block arbitrarily long.
327    ///   * Shutdown waits for a maximum of 30 seconds.
328    fn shutdown(&self) -> Result<(), CallbackError> {
329        // empty by default
330        Ok(())
331    }
332}
333
334/// A callback handler that receives the base identifier of recorded events
335/// The identifier is in the format: `<category>.<name>`
336pub trait GleanEventListener: Send {
337    /// Called when an event is recorded, indicating the id of the event
338    fn on_event_recorded(&self, id: String);
339}
340
341/// Initializes Glean.
342///
343/// # Arguments
344///
345/// * `cfg` - the [`InternalConfiguration`] options to initialize with.
346/// * `client_info` - the [`ClientInfoMetrics`] values used to set Glean
347///   core metrics.
348/// * `callbacks` - A callback object, stored for the entire application lifetime.
349pub fn glean_initialize(
350    cfg: InternalConfiguration,
351    client_info: ClientInfoMetrics,
352    callbacks: Box<dyn OnGleanEvents>,
353) {
354    initialize_inner(cfg, client_info, callbacks);
355}
356
357/// Shuts down Glean in an orderly fashion.
358pub fn glean_shutdown() {
359    shutdown();
360}
361
362/// Creates and initializes a new Glean object for use in a subprocess.
363///
364/// Importantly, this will not send any pings at startup, since that
365/// sort of management should only happen in the main process.
366pub fn glean_initialize_for_subprocess(cfg: InternalConfiguration) -> bool {
367    let glean = match Glean::new_for_subprocess(&cfg, true) {
368        Ok(glean) => glean,
369        Err(err) => {
370            log::error!("Failed to initialize Glean: {}", err);
371            return false;
372        }
373    };
374    if core::setup_glean(glean).is_err() {
375        return false;
376    }
377    log::info!("Glean initialized for subprocess");
378    true
379}
380
381fn initialize_inner(
382    cfg: InternalConfiguration,
383    client_info: ClientInfoMetrics,
384    callbacks: Box<dyn OnGleanEvents>,
385) {
386    if was_initialize_called() {
387        log::error!("Glean should not be initialized multiple times");
388        return;
389    }
390
391    let init_handle = thread::spawn("glean.init", move || {
392        let upload_enabled = cfg.upload_enabled;
393        let trim_data_to_registered_pings = cfg.trim_data_to_registered_pings;
394
395        // Set the internal logging level.
396        if let Some(level) = cfg.log_level {
397            log::set_max_level(level)
398        }
399
400        let data_path_str = cfg.data_path.clone();
401        let data_path = Path::new(&data_path_str);
402        let internal_pings_enabled = cfg.enable_internal_pings;
403        let dir_info = if !is_test_mode() && internal_pings_enabled {
404            collect_directory_info(Path::new(&data_path))
405        } else {
406            None
407        };
408
409        let glean = match Glean::new(cfg) {
410            Ok(glean) => glean,
411            Err(err) => {
412                log::error!("Failed to initialize Glean: {}", err);
413                return;
414            }
415        };
416        if core::setup_glean(glean).is_err() {
417            return;
418        }
419
420        log::info!("Glean initialized");
421
422        core::with_glean(|glean| {
423            glean.health_metrics.init_count.add_sync(glean, 1);
424        });
425
426        setup_state(State {
427            client_info,
428            callbacks,
429        });
430
431        let mut is_first_run = false;
432        let mut dirty_flag = false;
433        let mut pings_submitted = false;
434        core::with_glean_mut(|glean| {
435            // The debug view tag might have been set before initialize,
436            // get the cached value and set it.
437            let debug_tag = PRE_INIT_DEBUG_VIEW_TAG.lock().unwrap();
438            if !debug_tag.is_empty() {
439                glean.set_debug_view_tag(&debug_tag);
440            }
441
442            // The log pings debug option might have been set before initialize,
443            // get the cached value and set it.
444            let log_pigs = PRE_INIT_LOG_PINGS.load(Ordering::SeqCst);
445            if log_pigs {
446                glean.set_log_pings(log_pigs);
447            }
448
449            // The source tags might have been set before initialize,
450            // get the cached value and set them.
451            let source_tags = PRE_INIT_SOURCE_TAGS.lock().unwrap();
452            if !source_tags.is_empty() {
453                glean.set_source_tags(source_tags.to_vec());
454            }
455
456            // Get the current value of the dirty flag so we know whether to
457            // send a dirty startup baseline ping below.  Immediately set it to
458            // `false` so that dirty startup pings won't be sent if Glean
459            // initialization does not complete successfully.
460            dirty_flag = glean.is_dirty_flag_set();
461            glean.set_dirty_flag(false);
462
463            // Perform registration of pings that were attempted to be
464            // registered before init.
465            let pings = PRE_INIT_PING_REGISTRATION.lock().unwrap();
466            for ping in pings.iter() {
467                glean.register_ping_type(ping);
468            }
469            let pings = PRE_INIT_PING_ENABLED.lock().unwrap();
470            for (ping, enabled) in pings.iter() {
471                glean.set_ping_enabled(ping, *enabled);
472            }
473
474            // The attribution and distribution might have been set before initialize,
475            // take the cached values and set them.
476            if let Some(attribution) = PRE_INIT_ATTRIBUTION.lock().unwrap().take() {
477                glean.update_attribution(attribution);
478            }
479            if let Some(distribution) = PRE_INIT_DISTRIBUTION.lock().unwrap().take() {
480                glean.update_distribution(distribution);
481            }
482
483            // If this is the first time ever the Glean SDK runs, make sure to set
484            // some initial core metrics in case we need to generate early pings.
485            // The next times we start, we would have them around already.
486            is_first_run = glean.is_first_run();
487            if is_first_run {
488                let state = global_state().lock().unwrap();
489                initialize_core_metrics(glean, &state.client_info);
490            }
491
492            // Deal with any pending events so we can start recording new ones
493            pings_submitted = glean.on_ready_to_submit_pings(trim_data_to_registered_pings);
494        });
495
496        {
497            let state = global_state().lock().unwrap();
498            // We need to kick off upload in these cases:
499            // 1. Pings were submitted through Glean and it is ready to upload those pings;
500            // 2. Upload is disabled, to upload a possible deletion-request ping.
501            if pings_submitted || !upload_enabled {
502                if let Err(e) = state.callbacks.trigger_upload() {
503                    log::error!("Triggering upload failed. Error: {}", e);
504                }
505            }
506        }
507
508        core::with_glean(|glean| {
509            // Start the MPS if its handled within Rust.
510            glean.start_metrics_ping_scheduler();
511        });
512
513        // The metrics ping scheduler might _synchronously_ submit a ping
514        // so that it runs before we clear application-lifetime metrics further below.
515        // For that it needs access to the `Glean` object.
516        // Thus we need to unlock that by leaving the context above,
517        // then re-lock it afterwards.
518        // That's safe because user-visible functions will be queued and thus not execute until
519        // we unblock later anyway.
520        {
521            let state = global_state().lock().unwrap();
522
523            // Set up information and scheduling for Glean owned pings. Ideally, the "metrics"
524            // ping startup check should be performed before any other ping, since it relies
525            // on being dispatched to the API context before any other metric.
526            if state.callbacks.start_metrics_ping_scheduler() {
527                if let Err(e) = state.callbacks.trigger_upload() {
528                    log::error!("Triggering upload failed. Error: {}", e);
529                }
530            }
531        }
532
533        core::with_glean_mut(|glean| {
534            let state = global_state().lock().unwrap();
535
536            // Check if the "dirty flag" is set. That means the product was probably
537            // force-closed. If that's the case, submit a 'baseline' ping with the
538            // reason "dirty_startup". We only do that from the second run.
539            if !is_first_run && dirty_flag {
540                // The `submit_ping_by_name_sync` function cannot be used, otherwise
541                // startup will cause a dead-lock, since that function requests a
542                // write lock on the `glean` object.
543                // Note that unwrapping below is safe: the function will return an
544                // `Ok` value for a known ping.
545                if glean.submit_ping_by_name("baseline", Some("dirty_startup")) {
546                    if let Err(e) = state.callbacks.trigger_upload() {
547                        log::error!("Triggering upload failed. Error: {}", e);
548                    }
549                }
550            }
551
552            // From the second time we run, after all startup pings are generated,
553            // make sure to clear `lifetime: application` metrics and set them again.
554            // Any new value will be sent in newly generated pings after startup.
555            if !is_first_run {
556                glean.clear_application_lifetime_metrics();
557                initialize_core_metrics(glean, &state.client_info);
558            }
559        });
560
561        // Signal Dispatcher that init is complete
562        // bug 1839433: It is important that this happens after any init tasks
563        // that shutdown() depends on. At time of writing that's only setting up
564        // the global Glean, but it is probably best to flush the preinit queue
565        // as late as possible in the glean.init thread.
566        match dispatcher::flush_init() {
567            Ok(task_count) if task_count > 0 => {
568                core::with_glean(|glean| {
569                    glean_metrics::error::preinit_tasks_overflow.add_sync(glean, task_count as i32);
570                });
571            }
572            Ok(_) => {}
573            Err(err) => log::error!("Unable to flush the preinit queue: {}", err),
574        }
575
576        if !is_test_mode() && internal_pings_enabled {
577            // Now that Glean is initialized, we can capture the directory info from the pre_init phase and send it in
578            // a health ping with reason "pre_init".
579            record_dir_info_and_submit_health_ping(dir_info, "pre_init");
580
581            // Now capture a post_init snapshot of the state of Glean's data directories after initialization to send
582            // in a health ping with reason "post_init".
583            record_dir_info_and_submit_health_ping(collect_directory_info(data_path), "post_init");
584        }
585        let state = global_state().lock().unwrap();
586        state.callbacks.initialize_finished();
587    })
588    .expect("Failed to spawn Glean's init thread");
589
590    // For test purposes, store the glean init thread's JoinHandle.
591    INIT_HANDLES.lock().unwrap().push(init_handle);
592
593    // Mark the initialization as called: this needs to happen outside of the
594    // dispatched block!
595    INITIALIZE_CALLED.store(true, Ordering::SeqCst);
596
597    // In test mode we wait for initialization to finish.
598    // This needs to run after we set `INITIALIZE_CALLED`, so it's similar to normal behavior.
599    if dispatcher::global::is_test_mode() {
600        join_init();
601    }
602}
603
604/// Return the heap usage of the `Glean` object and all descendant heap-allocated structures.
605///
606/// Value is in bytes.
607pub fn alloc_size(ops: &mut malloc_size_of::MallocSizeOfOps) -> usize {
608    use malloc_size_of::MallocSizeOf;
609    core::with_glean(|glean| glean.size_of(ops))
610}
611
612/// TEST ONLY FUNCTION
613/// Waits on all the glean.init threads' join handles.
614pub fn join_init() {
615    let mut handles = INIT_HANDLES.lock().unwrap();
616    for handle in handles.drain(..) {
617        handle.join().unwrap();
618    }
619}
620
621/// Call the `shutdown` callback.
622///
623/// This calls the shutdown in a separate thread and waits up to 30s for it to finish.
624/// If not finished in that time frame it continues.
625///
626/// Under normal operation that is fine, as the main process will end
627/// and thus the thread will get killed.
628fn uploader_shutdown() {
629    let timer_id = core::with_glean(|glean| glean.additional_metrics.shutdown_wait.start_sync());
630    let (tx, rx) = unbounded();
631
632    let handle = thread::spawn("glean.shutdown", move || {
633        let state = global_state().lock().unwrap();
634        if let Err(e) = state.callbacks.shutdown() {
635            log::error!("Shutdown callback failed: {e:?}");
636        }
637
638        // Best-effort sending. The other side might have timed out already.
639        let _ = tx.send(()).ok();
640    })
641    .expect("Unable to spawn thread to wait on shutdown");
642
643    // TODO: 30 seconds? What's a good default here? Should this be configurable?
644    // Reasoning:
645    //   * If we shut down early we might still be processing pending pings.
646    //     In this case we wait at most 3 times for 1s = 3s before we upload.
647    //   * If we're rate-limited the uploader sleeps for up to 60s.
648    //     Thus waiting 30s will rarely allow another upload.
649    //   * We don't know how long uploads take until we get data from bug 1814592.
650    let result = rx.recv_timeout(Duration::from_secs(30));
651
652    let stop_time = zeitstempel::now();
653    core::with_glean(|glean| {
654        glean
655            .additional_metrics
656            .shutdown_wait
657            .set_stop_and_accumulate(glean, timer_id, stop_time);
658    });
659
660    if result.is_err() {
661        log::warn!("Waiting for upload failed. We're shutting down.");
662    } else {
663        let _ = handle.join().ok();
664    }
665}
666
667/// Shuts down Glean in an orderly fashion.
668pub fn shutdown() {
669    // Shutdown might have been called
670    // 1) Before init was called
671    //    * (data loss, oh well. Not enough time to do squat)
672    // 2) After init was called, but before it completed
673    //    * (we're willing to wait a little bit for init to complete)
674    // 3) After init completed
675    //    * (we can shut down immediately)
676
677    // Case 1: "Before init was called"
678    if !was_initialize_called() {
679        log::warn!("Shutdown called before Glean is initialized");
680        if let Err(e) = dispatcher::kill() {
681            log::error!("Can't kill dispatcher thread: {:?}", e);
682        }
683        return;
684    }
685
686    // Case 2: "After init was called, but before it completed"
687    if core::global_glean().is_none() {
688        log::warn!("Shutdown called before Glean is initialized. Waiting.");
689        // We can't join on the `glean.init` thread because there's no (easy) way
690        // to do that with a timeout. Instead, we wait for the preinit queue to
691        // empty, which is the last meaningful thing we do on that thread.
692
693        // TODO: Make the timeout configurable?
694        // We don't need the return value, as we're less interested in whether
695        // this times out than we are in whether there's a Global Glean at the end.
696        let _ = dispatcher::block_on_queue_timeout(Duration::from_secs(10));
697    }
698    // We can't shut down Glean if there's no Glean to shut down.
699    if core::global_glean().is_none() {
700        log::warn!("Waiting for Glean initialization timed out. Exiting.");
701        if let Err(e) = dispatcher::kill() {
702            log::error!("Can't kill dispatcher thread: {:?}", e);
703        }
704        return;
705    }
706
707    // Case 3: "After init completed"
708    crate::launch_with_glean_mut(|glean| {
709        glean.cancel_metrics_ping_scheduler();
710        glean.set_dirty_flag(false);
711    });
712
713    // We need to wait for above task to finish,
714    // but we also don't wait around forever.
715    //
716    // TODO: Make the timeout configurable?
717    // The default hang watchdog on Firefox waits 60s,
718    // Glean's `uploader_shutdown` further below waits up to 30s.
719    let timer_id = core::with_glean(|glean| {
720        glean
721            .additional_metrics
722            .shutdown_dispatcher_wait
723            .start_sync()
724    });
725    let blocked = dispatcher::block_on_queue_timeout(Duration::from_secs(10));
726
727    // Always record the dispatcher wait, regardless of the timeout.
728    let stop_time = zeitstempel::now();
729    core::with_glean(|glean| {
730        glean
731            .additional_metrics
732            .shutdown_dispatcher_wait
733            .set_stop_and_accumulate(glean, timer_id, stop_time);
734    });
735    if blocked.is_err() {
736        log::error!(
737            "Timeout while blocking on the dispatcher. No further shutdown cleanup will happen."
738        );
739        return;
740    }
741
742    if let Err(e) = dispatcher::shutdown() {
743        log::error!("Can't shutdown dispatcher thread: {:?}", e);
744    }
745
746    uploader_shutdown();
747
748    // Be sure to call this _after_ draining the dispatcher
749    core::with_glean(|glean| {
750        if let Err(e) = glean.persist_ping_lifetime_data() {
751            log::error!("Can't persist ping lifetime data: {:?}", e);
752        }
753    });
754}
755
756/// Asks the database to persist ping-lifetime data to disk.
757///
758/// Probably expensive to call.
759/// Only has effect when Glean is configured with `delay_ping_lifetime_io: true`.
760/// If Glean hasn't been initialized this will dispatch and return Ok(()),
761/// otherwise it will block until the persist is done and return its Result.
762pub fn glean_persist_ping_lifetime_data() {
763    // This is async, we can't get the Error back to the caller.
764    crate::launch_with_glean(|glean| {
765        let _ = glean.persist_ping_lifetime_data();
766    });
767}
768
769fn initialize_core_metrics(glean: &Glean, client_info: &ClientInfoMetrics) {
770    core_metrics::internal_metrics::app_build.set_sync(glean, &client_info.app_build[..]);
771    core_metrics::internal_metrics::app_display_version
772        .set_sync(glean, &client_info.app_display_version[..]);
773    core_metrics::internal_metrics::app_build_date
774        .set_sync(glean, Some(client_info.app_build_date.clone()));
775    if let Some(app_channel) = client_info.channel.as_ref() {
776        core_metrics::internal_metrics::app_channel.set_sync(glean, app_channel);
777    }
778
779    core_metrics::internal_metrics::os_version.set_sync(glean, &client_info.os_version);
780    core_metrics::internal_metrics::architecture.set_sync(glean, &client_info.architecture);
781
782    if let Some(android_sdk_version) = client_info.android_sdk_version.as_ref() {
783        core_metrics::internal_metrics::android_sdk_version.set_sync(glean, android_sdk_version);
784    }
785    if let Some(windows_build_number) = client_info.windows_build_number.as_ref() {
786        core_metrics::internal_metrics::windows_build_number.set_sync(glean, *windows_build_number);
787    }
788    if let Some(device_manufacturer) = client_info.device_manufacturer.as_ref() {
789        core_metrics::internal_metrics::device_manufacturer.set_sync(glean, device_manufacturer);
790    }
791    if let Some(device_model) = client_info.device_model.as_ref() {
792        core_metrics::internal_metrics::device_model.set_sync(glean, device_model);
793    }
794    if let Some(locale) = client_info.locale.as_ref() {
795        core_metrics::internal_metrics::locale.set_sync(glean, locale);
796    }
797}
798
799/// Checks if [`initialize`] was ever called.
800///
801/// # Returns
802///
803/// `true` if it was, `false` otherwise.
804fn was_initialize_called() -> bool {
805    INITIALIZE_CALLED.load(Ordering::SeqCst)
806}
807
808/// Initialize the logging system based on the target platform. This ensures
809/// that logging is shown when executing the Glean SDK unit tests.
810#[no_mangle]
811pub extern "C" fn glean_enable_logging() {
812    #[cfg(target_os = "android")]
813    {
814        let _ = std::panic::catch_unwind(|| {
815            let filter = android_logger::FilterBuilder::new()
816                .filter_module("glean_ffi", log::LevelFilter::Debug)
817                .filter_module("glean_core", log::LevelFilter::Debug)
818                .filter_module("glean", log::LevelFilter::Debug)
819                .filter_module("glean_core::ffi", log::LevelFilter::Info)
820                .build();
821            android_logger::init_once(
822                android_logger::Config::default()
823                    .with_max_level(log::LevelFilter::Debug)
824                    .with_filter(filter)
825                    .with_tag("libglean_ffi"),
826            );
827            log::trace!("Android logging should be hooked up!")
828        });
829    }
830
831    // On iOS enable logging with a level filter.
832    #[cfg(target_os = "ios")]
833    {
834        // Debug logging in debug mode.
835        // (Note: `debug_assertions` is the next best thing to determine if this is a debug build)
836        #[cfg(debug_assertions)]
837        let level = log::LevelFilter::Debug;
838        #[cfg(not(debug_assertions))]
839        let level = log::LevelFilter::Info;
840
841        let logger = oslog::OsLogger::new("org.mozilla.glean")
842            .level_filter(level)
843            // Filter UniFFI log messages
844            .category_level_filter("glean_core::ffi", log::LevelFilter::Info);
845
846        match logger.init() {
847            Ok(_) => log::trace!("os_log should be hooked up!"),
848            // Please note that this is only expected to fail during unit tests,
849            // where the logger might have already been initialized by a previous
850            // test. So it's fine to print with the "logger".
851            Err(_) => log::warn!("os_log was already initialized"),
852        };
853    }
854
855    // When specifically requested make sure logging does something on non-Android platforms as well.
856    // Use the RUST_LOG environment variable to set the desired log level,
857    // e.g. setting RUST_LOG=debug sets the log level to debug.
858    #[cfg(all(
859        not(target_os = "android"),
860        not(target_os = "ios"),
861        feature = "enable_env_logger"
862    ))]
863    {
864        match env_logger::try_init() {
865            Ok(_) => log::trace!("stdout logging should be hooked up!"),
866            // Please note that this is only expected to fail during unit tests,
867            // where the logger might have already been initialized by a previous
868            // test. So it's fine to print with the "logger".
869            Err(_) => log::warn!("stdout logging was already initialized"),
870        };
871    }
872}
873
874/// **DEPRECATED** Sets whether upload is enabled or not.
875///
876/// **DEPRECATION NOTICE**:
877/// This API is deprecated. Use `set_collection_enabled` instead.
878pub fn glean_set_upload_enabled(enabled: bool) {
879    if !was_initialize_called() {
880        return;
881    }
882
883    crate::launch_with_glean_mut(move |glean| {
884        let state = global_state().lock().unwrap();
885        let original_enabled = glean.is_upload_enabled();
886
887        if !enabled {
888            // Stop the MPS if its handled within Rust.
889            glean.cancel_metrics_ping_scheduler();
890            // Stop wrapper-controlled uploader.
891            if let Err(e) = state.callbacks.cancel_uploads() {
892                log::error!("Canceling upload failed. Error: {}", e);
893            }
894        }
895
896        glean.set_upload_enabled(enabled);
897
898        if !original_enabled && enabled {
899            initialize_core_metrics(glean, &state.client_info);
900        }
901
902        if original_enabled && !enabled {
903            if let Err(e) = state.callbacks.trigger_upload() {
904                log::error!("Triggering upload failed. Error: {}", e);
905            }
906        }
907    })
908}
909
910/// Sets whether collection is enabled or not.
911///
912/// This replaces `set_upload_enabled`.
913pub fn glean_set_collection_enabled(enabled: bool) {
914    glean_set_upload_enabled(enabled)
915}
916
917/// Enable or disable a ping.
918///
919/// Disabling a ping causes all data for that ping to be removed from storage
920/// and all pending pings of that type to be deleted.
921pub fn set_ping_enabled(ping: &PingType, enabled: bool) {
922    let ping = ping.clone();
923    if was_initialize_called() && core::global_glean().is_some() {
924        crate::launch_with_glean_mut(move |glean| glean.set_ping_enabled(&ping, enabled));
925    } else {
926        let m = &PRE_INIT_PING_ENABLED;
927        let mut lock = m.lock().unwrap();
928        lock.push((ping, enabled));
929    }
930}
931
932/// Register a new [`PingType`](PingType).
933pub(crate) fn register_ping_type(ping: &PingType) {
934    // If this happens after Glean.initialize is called (and returns),
935    // we dispatch ping registration on the thread pool.
936    // Registering a ping should not block the application.
937    // Submission itself is also dispatched, so it will always come after the registration.
938    if was_initialize_called() && core::global_glean().is_some() {
939        let ping = ping.clone();
940        crate::launch_with_glean_mut(move |glean| {
941            glean.register_ping_type(&ping);
942        })
943    } else {
944        // We need to keep track of pings, so they get re-registered after a reset or
945        // if ping registration is attempted before Glean initializes.
946        // This state is kept across Glean resets, which should only ever happen in test mode.
947        // It's a set and keeping them around forever should not have much of an impact.
948        let m = &PRE_INIT_PING_REGISTRATION;
949        let mut lock = m.lock().unwrap();
950        lock.push(ping.clone());
951    }
952}
953
954/// Gets a list of currently registered ping names.
955///
956/// # Returns
957///
958/// The list of ping names that are currently registered.
959pub fn glean_get_registered_ping_names() -> Vec<String> {
960    block_on_dispatcher();
961    core::with_glean(|glean| {
962        glean
963            .get_registered_ping_names()
964            .iter()
965            .map(|ping| ping.to_string())
966            .collect()
967    })
968}
969
970/// Indicate that an experiment is running.  Glean will then add an
971/// experiment annotation to the environment which is sent with pings. This
972/// infomration is not persisted between runs.
973///
974/// See [`core::Glean::set_experiment_active`].
975pub fn glean_set_experiment_active(
976    experiment_id: String,
977    branch: String,
978    extra: HashMap<String, String>,
979) {
980    launch_with_glean(|glean| glean.set_experiment_active(experiment_id, branch, extra))
981}
982
983/// Indicate that an experiment is no longer running.
984///
985/// See [`core::Glean::set_experiment_inactive`].
986pub fn glean_set_experiment_inactive(experiment_id: String) {
987    launch_with_glean(|glean| glean.set_experiment_inactive(experiment_id))
988}
989
990/// TEST ONLY FUNCTION.
991/// Returns the [`RecordedExperiment`] for the given `experiment_id`
992/// or `None` if the id isn't found.
993pub fn glean_test_get_experiment_data(experiment_id: String) -> Option<RecordedExperiment> {
994    block_on_dispatcher();
995    core::with_glean(|glean| glean.test_get_experiment_data(experiment_id.to_owned()))
996}
997
998/// Set an experimentation identifier dynamically.
999///
1000/// Note: it's probably a good idea to unenroll from any experiments when identifiers change.
1001pub fn glean_set_experimentation_id(experimentation_id: String) {
1002    launch_with_glean(move |glean| {
1003        glean
1004            .additional_metrics
1005            .experimentation_id
1006            .set(experimentation_id);
1007    });
1008}
1009
1010/// TEST ONLY FUNCTION.
1011/// Gets stored experimentation id annotation.
1012pub fn glean_test_get_experimentation_id() -> Option<String> {
1013    block_on_dispatcher();
1014    core::with_glean(|glean| glean.test_get_experimentation_id())
1015}
1016
1017/// Sets a remote configuration to override metrics' default enabled/disabled
1018/// state
1019///
1020/// See [`core::Glean::apply_server_knobs_config`].
1021pub fn glean_apply_server_knobs_config(json: String) {
1022    // An empty config means it is not set,
1023    // so we avoid logging an error about it.
1024    if json.is_empty() {
1025        return;
1026    }
1027
1028    match RemoteSettingsConfig::try_from(json) {
1029        Ok(cfg) => launch_with_glean(|glean| {
1030            glean.apply_server_knobs_config(cfg);
1031        }),
1032        Err(e) => {
1033            log::error!("Error setting metrics feature config: {:?}", e);
1034        }
1035    }
1036}
1037
1038/// Sets a debug view tag.
1039///
1040/// When the debug view tag is set, pings are sent with a `X-Debug-ID` header with the
1041/// value of the tag and are sent to the ["Ping Debug Viewer"](https://mozilla.github.io/glean/book/dev/core/internal/debug-pings.html).
1042///
1043/// # Arguments
1044///
1045/// * `tag` - A valid HTTP header value. Must match the regex: "[a-zA-Z0-9-]{1,20}".
1046///
1047/// # Returns
1048///
1049/// This will return `false` in case `tag` is not a valid tag and `true` otherwise.
1050/// If called before Glean is initialized it will always return `true`.
1051pub fn glean_set_debug_view_tag(tag: String) -> bool {
1052    if was_initialize_called() && core::global_glean().is_some() {
1053        crate::launch_with_glean_mut(move |glean| {
1054            glean.set_debug_view_tag(&tag);
1055        });
1056        true
1057    } else {
1058        // Glean has not been initialized yet. Cache the provided tag value.
1059        let m = &PRE_INIT_DEBUG_VIEW_TAG;
1060        let mut lock = m.lock().unwrap();
1061        *lock = tag;
1062        // When setting the debug view tag before initialization,
1063        // we don't validate the tag, thus this function always returns true.
1064        true
1065    }
1066}
1067
1068/// Gets the currently set debug view tag.
1069///
1070/// # Returns
1071///
1072/// Return the value for the debug view tag or [`None`] if it hasn't been set.
1073pub fn glean_get_debug_view_tag() -> Option<String> {
1074    block_on_dispatcher();
1075    core::with_glean(|glean| glean.debug_view_tag().map(|tag| tag.to_string()))
1076}
1077
1078/// Sets source tags.
1079///
1080/// Overrides any existing source tags.
1081/// Source tags will show in the destination datasets, after ingestion.
1082///
1083/// **Note** If one or more tags are invalid, all tags are ignored.
1084///
1085/// # Arguments
1086///
1087/// * `tags` - A vector of at most 5 valid HTTP header values. Individual
1088///   tags must match the regex: "[a-zA-Z0-9-]{1,20}".
1089pub fn glean_set_source_tags(tags: Vec<String>) -> bool {
1090    if was_initialize_called() && core::global_glean().is_some() {
1091        crate::launch_with_glean_mut(|glean| {
1092            glean.set_source_tags(tags);
1093        });
1094        true
1095    } else {
1096        // Glean has not been initialized yet. Cache the provided source tags.
1097        let m = &PRE_INIT_SOURCE_TAGS;
1098        let mut lock = m.lock().unwrap();
1099        *lock = tags;
1100        // When setting the source tags before initialization,
1101        // we don't validate the tags, thus this function always returns true.
1102        true
1103    }
1104}
1105
1106/// Sets the log pings debug option.
1107///
1108/// When the log pings debug option is `true`,
1109/// we log the payload of all succesfully assembled pings.
1110///
1111/// # Arguments
1112///
1113/// * `value` - The value of the log pings option
1114pub fn glean_set_log_pings(value: bool) {
1115    if was_initialize_called() && core::global_glean().is_some() {
1116        crate::launch_with_glean_mut(move |glean| {
1117            glean.set_log_pings(value);
1118        });
1119    } else {
1120        PRE_INIT_LOG_PINGS.store(value, Ordering::SeqCst);
1121    }
1122}
1123
1124/// Gets the current log pings value.
1125///
1126/// # Returns
1127///
1128/// Return the value for the log pings debug option.
1129pub fn glean_get_log_pings() -> bool {
1130    block_on_dispatcher();
1131    core::with_glean(|glean| glean.log_pings())
1132}
1133
1134/// Performs the collection/cleanup operations required by becoming active.
1135///
1136/// This functions generates a baseline ping with reason `active`
1137/// and then sets the dirty bit.
1138/// This should be called whenever the consuming product becomes active (e.g.
1139/// getting to foreground).
1140pub fn glean_handle_client_active() {
1141    dispatcher::launch(|| {
1142        core::with_glean_mut(|glean| {
1143            glean.handle_client_active();
1144        });
1145
1146        // The above call may generate pings, so we need to trigger
1147        // the uploader. It's fine to trigger it if no ping was generated:
1148        // it will bail out.
1149        let state = global_state().lock().unwrap();
1150        if let Err(e) = state.callbacks.trigger_upload() {
1151            log::error!("Triggering upload failed. Error: {}", e);
1152        }
1153    });
1154
1155    // The previous block of code may send a ping containing the `duration` metric,
1156    // in `glean.handle_client_active`. We intentionally start recording a new
1157    // `duration` after that happens, so that the measurement gets reported when
1158    // calling `handle_client_inactive`.
1159    core_metrics::internal_metrics::baseline_duration.start();
1160}
1161
1162/// Performs the collection/cleanup operations required by becoming inactive.
1163///
1164/// This functions generates a baseline and an events ping with reason
1165/// `inactive` and then clears the dirty bit.
1166/// This should be called whenever the consuming product becomes inactive (e.g.
1167/// getting to background).
1168pub fn glean_handle_client_inactive() {
1169    // This needs to be called before the `handle_client_inactive` api: it stops
1170    // measuring the duration of the previous activity time, before any ping is sent
1171    // by the next call.
1172    core_metrics::internal_metrics::baseline_duration.stop();
1173
1174    dispatcher::launch(|| {
1175        core::with_glean_mut(|glean| {
1176            glean.handle_client_inactive();
1177        });
1178
1179        // The above call may generate pings, so we need to trigger
1180        // the uploader. It's fine to trigger it if no ping was generated:
1181        // it will bail out.
1182        let state = global_state().lock().unwrap();
1183        if let Err(e) = state.callbacks.trigger_upload() {
1184            log::error!("Triggering upload failed. Error: {}", e);
1185        }
1186    })
1187}
1188
1189/// Collect and submit a ping for eventual upload by name.
1190pub fn glean_submit_ping_by_name(ping_name: String, reason: Option<String>) {
1191    dispatcher::launch(|| {
1192        let sent =
1193            core::with_glean(move |glean| glean.submit_ping_by_name(&ping_name, reason.as_deref()));
1194
1195        if sent {
1196            let state = global_state().lock().unwrap();
1197            if let Err(e) = state.callbacks.trigger_upload() {
1198                log::error!("Triggering upload failed. Error: {}", e);
1199            }
1200        }
1201    })
1202}
1203
1204/// Collect and submit a ping (by its name) for eventual upload, synchronously.
1205///
1206/// Note: This does not trigger the uploader. The caller is responsible to do this.
1207pub fn glean_submit_ping_by_name_sync(ping_name: String, reason: Option<String>) -> bool {
1208    if !was_initialize_called() {
1209        return false;
1210    }
1211
1212    core::with_opt_glean(|glean| glean.submit_ping_by_name(&ping_name, reason.as_deref()))
1213        .unwrap_or(false)
1214}
1215
1216/// EXPERIMENTAL: Register a listener object to recieve notifications of event recordings.
1217///
1218/// # Arguments
1219///
1220/// * `tag` - A string identifier used to later unregister the listener
1221/// * `listener` - Implements the `GleanEventListener` trait
1222pub fn glean_register_event_listener(tag: String, listener: Box<dyn GleanEventListener>) {
1223    register_event_listener(tag, listener);
1224}
1225
1226/// Unregister an event listener from recieving notifications.
1227///
1228/// Does not panic if the listener doesn't exist.
1229///
1230/// # Arguments
1231///
1232/// * `tag` - The tag used when registering the listener to be unregistered
1233pub fn glean_unregister_event_listener(tag: String) {
1234    unregister_event_listener(tag);
1235}
1236
1237/// **TEST-ONLY Method**
1238///
1239/// Set test mode
1240pub fn glean_set_test_mode(enabled: bool) {
1241    dispatcher::global::TESTING_MODE.store(enabled, Ordering::SeqCst);
1242}
1243
1244/// **TEST-ONLY Method**
1245///
1246/// Destroy the underlying database.
1247pub fn glean_test_destroy_glean(clear_stores: bool, data_path: Option<String>) {
1248    if was_initialize_called() {
1249        // Just because initialize was called doesn't mean it's done.
1250        join_init();
1251
1252        dispatcher::reset_dispatcher();
1253
1254        // Only useful if Glean initialization finished successfully
1255        // and set up the storage.
1256        let has_storage = core::with_opt_glean(|glean| {
1257            // We need to flush the ping lifetime data before a full shutdown.
1258            glean
1259                .storage_opt()
1260                .map(|storage| storage.persist_ping_lifetime_data())
1261                .is_some()
1262        })
1263        .unwrap_or(false);
1264        if has_storage {
1265            uploader_shutdown();
1266        }
1267
1268        if core::global_glean().is_some() {
1269            core::with_glean_mut(|glean| {
1270                if clear_stores {
1271                    glean.test_clear_all_stores()
1272                }
1273                glean.destroy_db()
1274            });
1275        }
1276
1277        // Allow us to go through initialization again.
1278        INITIALIZE_CALLED.store(false, Ordering::SeqCst);
1279    } else if clear_stores {
1280        if let Some(data_path) = data_path {
1281            let _ = std::fs::remove_dir_all(data_path).ok();
1282        } else {
1283            log::warn!("Asked to clear stores before initialization, but no data path given.");
1284        }
1285    }
1286}
1287
1288/// Get the next upload task
1289pub fn glean_get_upload_task() -> PingUploadTask {
1290    core::with_opt_glean(|glean| glean.get_upload_task()).unwrap_or_else(PingUploadTask::done)
1291}
1292
1293/// Processes the response from an attempt to upload a ping.
1294pub fn glean_process_ping_upload_response(uuid: String, result: UploadResult) -> UploadTaskAction {
1295    core::with_glean(|glean| glean.process_ping_upload_response(&uuid, result))
1296}
1297
1298/// **TEST-ONLY Method**
1299///
1300/// Set the dirty flag
1301pub fn glean_set_dirty_flag(new_value: bool) {
1302    core::with_glean(|glean| glean.set_dirty_flag(new_value))
1303}
1304
1305/// Updates attribution fields with new values.
1306/// AttributionMetrics fields with `None` values will not overwrite older values.
1307pub fn glean_update_attribution(attribution: AttributionMetrics) {
1308    if was_initialize_called() && core::global_glean().is_some() {
1309        core::with_glean(|glean| glean.update_attribution(attribution));
1310    } else {
1311        PRE_INIT_ATTRIBUTION
1312            .lock()
1313            .unwrap()
1314            .get_or_insert(Default::default())
1315            .update(attribution);
1316    }
1317}
1318
1319/// **TEST-ONLY Method**
1320///
1321/// Returns the current attribution metrics.
1322/// Panics if called before init.
1323pub fn glean_test_get_attribution() -> AttributionMetrics {
1324    join_init();
1325    core::with_glean(|glean| glean.test_get_attribution())
1326}
1327
1328/// Updates distribution fields with new values.
1329/// DistributionMetrics fields with `None` values will not overwrite older values.
1330pub fn glean_update_distribution(distribution: DistributionMetrics) {
1331    if was_initialize_called() && core::global_glean().is_some() {
1332        core::with_glean(|glean| glean.update_distribution(distribution));
1333    } else {
1334        PRE_INIT_DISTRIBUTION
1335            .lock()
1336            .unwrap()
1337            .get_or_insert(Default::default())
1338            .update(distribution);
1339    }
1340}
1341
1342/// **TEST-ONLY Method**
1343///
1344/// Returns the current distribution metrics.
1345/// Panics if called before init.
1346pub fn glean_test_get_distribution() -> DistributionMetrics {
1347    join_init();
1348    core::with_glean(|glean| glean.test_get_distribution())
1349}
1350
1351#[cfg(all(not(target_os = "android"), not(target_os = "ios")))]
1352static FD_LOGGER: OnceCell<fd_logger::FdLogger> = OnceCell::new();
1353
1354/// Initialize the logging system to send JSON messages to a file descriptor
1355/// (Unix) or file handle (Windows).
1356///
1357/// Not available on Android and iOS.
1358///
1359/// `fd` is a writable file descriptor (on Unix) or file handle (on Windows).
1360///
1361/// # Safety
1362///
1363/// `fd` MUST be a valid open file descriptor (Unix) or file handle (Windows).
1364/// This function is marked safe,
1365/// because we can't call unsafe functions from generated UniFFI code.
1366#[cfg(all(not(target_os = "android"), not(target_os = "ios")))]
1367pub fn glean_enable_logging_to_fd(fd: u64) {
1368    // SAFETY:
1369    // This functions is unsafe.
1370    // Due to UniFFI restrictions we cannot mark it as such.
1371    //
1372    // `fd` MUST be a valid open file descriptor (Unix) or file handle (Windows).
1373    unsafe {
1374        // Set up logging to a file descriptor/handle. For this usage, the
1375        // language binding should setup a pipe and pass in the descriptor to
1376        // the writing side of the pipe as the `fd` parameter. Log messages are
1377        // written as JSON to the file descriptor.
1378        let logger = FD_LOGGER.get_or_init(|| fd_logger::FdLogger::new(fd));
1379        // Set the level so everything goes through to the language
1380        // binding side where it will be filtered by the language
1381        // binding's logging system.
1382        if log::set_logger(logger).is_ok() {
1383            log::set_max_level(log::LevelFilter::Debug);
1384        }
1385    }
1386}
1387
1388/// Collects information about the data directories used by FOG.
1389fn collect_directory_info(path: &Path) -> Option<serde_json::Value> {
1390    // List of child directories to check
1391    let subdirs = ["db", "events", "pending_pings"];
1392    let mut directories_info: crate::internal_metrics::DataDirectoryInfoObject =
1393        DataDirectoryInfoObject::with_capacity(subdirs.len());
1394
1395    for subdir in subdirs.iter() {
1396        let dir_path = path.join(subdir);
1397
1398        // Initialize a DataDirectoryInfoObjectItem for each directory
1399        let mut directory_info = crate::internal_metrics::DataDirectoryInfoObjectItem {
1400            dir_name: Some(subdir.to_string()),
1401            dir_exists: None,
1402            dir_created: None,
1403            dir_modified: None,
1404            file_count: None,
1405            files: Vec::new(),
1406            error_message: None,
1407        };
1408
1409        // Check if the directory exists
1410        if dir_path.is_dir() {
1411            directory_info.dir_exists = Some(true);
1412
1413            // Get directory metadata
1414            match fs::metadata(&dir_path) {
1415                Ok(metadata) => {
1416                    if let Ok(created) = metadata.created() {
1417                        directory_info.dir_created = Some(
1418                            created
1419                                .duration_since(UNIX_EPOCH)
1420                                .unwrap_or(Duration::ZERO)
1421                                .as_secs() as i64,
1422                        );
1423                    }
1424                    if let Ok(modified) = metadata.modified() {
1425                        directory_info.dir_modified = Some(
1426                            modified
1427                                .duration_since(UNIX_EPOCH)
1428                                .unwrap_or(Duration::ZERO)
1429                                .as_secs() as i64,
1430                        );
1431                    }
1432                }
1433                Err(error) => {
1434                    let msg = format!("Unable to get metadata: {}", error.kind());
1435                    directory_info.error_message = Some(msg.clone());
1436                    log::warn!("{}", msg);
1437                    continue;
1438                }
1439            }
1440
1441            // Read the directory's contents
1442            let mut file_count = 0;
1443            let entries = match fs::read_dir(&dir_path) {
1444                Ok(entries) => entries,
1445                Err(error) => {
1446                    let msg = format!("Unable to read subdir: {}", error.kind());
1447                    directory_info.error_message = Some(msg.clone());
1448                    log::warn!("{}", msg);
1449                    continue;
1450                }
1451            };
1452            for entry in entries {
1453                directory_info.files.push(
1454                    crate::internal_metrics::DataDirectoryInfoObjectItemItemFilesItem {
1455                        file_name: None,
1456                        file_created: None,
1457                        file_modified: None,
1458                        file_size: None,
1459                        error_message: None,
1460                    },
1461                );
1462                // Safely get and unwrap the file_info we just pushed so we can populate it
1463                let file_info = directory_info.files.last_mut().unwrap();
1464                let entry = match entry {
1465                    Ok(entry) => entry,
1466                    Err(error) => {
1467                        let msg = format!("Unable to read file: {}", error.kind());
1468                        file_info.error_message = Some(msg.clone());
1469                        log::warn!("{}", msg);
1470                        continue;
1471                    }
1472                };
1473                let file_name = match entry.file_name().into_string() {
1474                    Ok(file_name) => file_name,
1475                    _ => {
1476                        let msg = "Unable to convert file name to string".to_string();
1477                        file_info.error_message = Some(msg.clone());
1478                        log::warn!("{}", msg);
1479                        continue;
1480                    }
1481                };
1482                let metadata = match entry.metadata() {
1483                    Ok(metadata) => metadata,
1484                    Err(error) => {
1485                        let msg = format!("Unable to read file metadata: {}", error.kind());
1486                        file_info.file_name = Some(file_name);
1487                        file_info.error_message = Some(msg.clone());
1488                        log::warn!("{}", msg);
1489                        continue;
1490                    }
1491                };
1492
1493                // Check if the entry is a file
1494                if metadata.is_file() {
1495                    file_count += 1;
1496
1497                    // Collect file details
1498                    file_info.file_name = Some(file_name);
1499                    file_info.file_created = Some(
1500                        metadata
1501                            .created()
1502                            .unwrap_or(UNIX_EPOCH)
1503                            .duration_since(UNIX_EPOCH)
1504                            .unwrap_or(Duration::ZERO)
1505                            .as_secs() as i64,
1506                    );
1507                    file_info.file_modified = Some(
1508                        metadata
1509                            .modified()
1510                            .unwrap_or(UNIX_EPOCH)
1511                            .duration_since(UNIX_EPOCH)
1512                            .unwrap_or(Duration::ZERO)
1513                            .as_secs() as i64,
1514                    );
1515                    file_info.file_size = Some(metadata.len() as i64);
1516                } else {
1517                    let msg = format!("Skipping non-file entry: {}", file_name.clone());
1518                    file_info.file_name = Some(file_name);
1519                    file_info.error_message = Some(msg.clone());
1520                    log::warn!("{}", msg);
1521                }
1522            }
1523
1524            directory_info.file_count = Some(file_count as i64);
1525        } else {
1526            directory_info.dir_exists = Some(false);
1527        }
1528
1529        // Add the directory info to the final collection
1530        directories_info.push(directory_info);
1531    }
1532
1533    if let Ok(directories_info_json) = serde_json::to_value(directories_info) {
1534        Some(directories_info_json)
1535    } else {
1536        log::error!("Failed to serialize data directory info");
1537        None
1538    }
1539}
1540
1541fn record_dir_info_and_submit_health_ping(dir_info: Option<serde_json::Value>, reason: &str) {
1542    core::with_glean(|glean| {
1543        glean
1544            .health_metrics
1545            .data_directory_info
1546            .set_sync(glean, dir_info.unwrap_or(serde_json::json!({})));
1547        glean.internal_pings.health.submit_sync(glean, Some(reason));
1548    });
1549}
1550
1551/// Unused function. Not used on Android or iOS.
1552#[cfg(any(target_os = "android", target_os = "ios"))]
1553pub fn glean_enable_logging_to_fd(_fd: u64) {
1554    // intentionally left empty
1555}
1556
1557#[allow(missing_docs)]
1558// uniffi-generated code should not be checked.
1559#[allow(clippy::all)]
1560mod ffi {
1561    use super::*;
1562    uniffi::include_scaffolding!("glean");
1563
1564    type CowString = Cow<'static, str>;
1565
1566    uniffi::custom_type!(CowString, String, {
1567        remote,
1568        lower: |s| s.into_owned(),
1569        try_lift: |s| Ok(Cow::from(s))
1570    });
1571
1572    type JsonValue = serde_json::Value;
1573
1574    uniffi::custom_type!(JsonValue, String, {
1575        remote,
1576        lower: |s| serde_json::to_string(&s).unwrap(),
1577        try_lift: |s| Ok(serde_json::from_str(&s)?)
1578    });
1579}
1580pub use ffi::*;
1581
1582// Split unit tests to a separate file, to reduce the file of this one.
1583#[cfg(test)]
1584#[path = "lib_unit_tests.rs"]
1585mod tests;