-
Notifications
You must be signed in to change notification settings - Fork 159
Expand file tree
/
Copy pathmod.rs
More file actions
1443 lines (1316 loc) · 54.2 KB
/
mod.rs
File metadata and controls
1443 lines (1316 loc) · 54.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
use std::collections::HashMap;
use std::fs::{self, File};
use std::io::{self, Write};
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicU8, Ordering};
use std::sync::{Arc, Mutex};
use std::time::Duration;
use chrono::{DateTime, FixedOffset};
use malloc_size_of_derive::MallocSizeOf;
use once_cell::sync::OnceCell;
use uuid::Uuid;
use crate::database::Database;
use crate::debug::DebugOptions;
use crate::error::ClientIdFileError;
use crate::event_database::EventDatabase;
use crate::internal_metrics::{
AdditionalMetrics, CoreMetrics, DatabaseMetrics, ExceptionState, HealthMetrics,
};
use crate::internal_pings::InternalPings;
use crate::metrics::{
self, ExperimentMetric, Metric, MetricType, PingType, RecordedExperiment, RemoteSettingsConfig,
};
use crate::ping::PingMaker;
use crate::storage::{StorageManager, INTERNAL_STORAGE};
use crate::upload::{PingUploadManager, PingUploadTask, UploadResult, UploadTaskAction};
use crate::util::{local_now_with_offset, sanitize_application_id};
use crate::{
scheduler, system, AttributionMetrics, CommonMetricData, DistributionMetrics, ErrorKind,
InternalConfiguration, Lifetime, PingRateLimit, Result, DEFAULT_MAX_EVENTS,
GLEAN_SCHEMA_VERSION, GLEAN_VERSION, KNOWN_CLIENT_ID,
};
const CLIENT_ID_PLAIN_FILENAME: &str = "client_id.txt";
static GLEAN: OnceCell<Mutex<Glean>> = OnceCell::new();
/// Rate limiting defaults
/// 15 pings every 60 seconds.
pub const DEFAULT_SECONDS_PER_INTERVAL: u64 = 60;
pub const DEFAULT_PINGS_PER_INTERVAL: u32 = 15;
pub fn global_glean() -> Option<&'static Mutex<Glean>> {
GLEAN.get()
}
/// Sets or replaces the global Glean object.
pub fn setup_glean(glean: Glean) -> Result<()> {
// The `OnceCell` type wrapping our Glean is thread-safe and can only be set once.
// Therefore even if our check for it being empty succeeds, setting it could fail if a
// concurrent thread is quicker in setting it.
// However this will not cause a bigger problem, as the second `set` operation will just fail.
// We can log it and move on.
//
// For all wrappers this is not a problem, as the Glean object is intialized exactly once on
// calling `initialize` on the global singleton and further operations check that it has been
// initialized.
if GLEAN.get().is_none() {
if GLEAN.set(Mutex::new(glean)).is_err() {
log::warn!(
"Global Glean object is initialized already. This probably happened concurrently."
)
}
} else {
// We allow overriding the global Glean object to support test mode.
// In test mode the Glean object is fully destroyed and recreated.
// This all happens behind a mutex and is therefore also thread-safe..
let mut lock = GLEAN.get().unwrap().lock().unwrap();
*lock = glean;
}
Ok(())
}
/// Execute `f` passing the global Glean object.
///
/// Panics if the global Glean object has not been set.
pub fn with_glean<F, R>(f: F) -> R
where
F: FnOnce(&Glean) -> R,
{
let glean = global_glean().expect("Global Glean object not initialized");
let lock = glean.lock().unwrap();
f(&lock)
}
/// Execute `f` passing the global Glean object mutable.
///
/// Panics if the global Glean object has not been set.
pub fn with_glean_mut<F, R>(f: F) -> R
where
F: FnOnce(&mut Glean) -> R,
{
let glean = global_glean().expect("Global Glean object not initialized");
let mut lock = glean.lock().unwrap();
f(&mut lock)
}
/// Execute `f` passing the global Glean object if it has been set.
///
/// Returns `None` if the global Glean object has not been set.
/// Returns `Some(T)` otherwise.
pub fn with_opt_glean<F, R>(f: F) -> Option<R>
where
F: FnOnce(&Glean) -> R,
{
let glean = global_glean()?;
let lock = glean.lock().unwrap();
Some(f(&lock))
}
/// The object holding meta information about a Glean instance.
///
/// ## Example
///
/// Create a new Glean instance, register a ping, record a simple counter and then send the final
/// ping.
///
/// ```rust,no_run
/// # use glean_core::{Glean, InternalConfiguration, CommonMetricData, metrics::*};
/// let cfg = InternalConfiguration {
/// data_path: "/tmp/glean".into(),
/// application_id: "glean.sample.app".into(),
/// language_binding_name: "Rust".into(),
/// upload_enabled: true,
/// max_events: None,
/// delay_ping_lifetime_io: false,
/// app_build: "".into(),
/// use_core_mps: false,
/// trim_data_to_registered_pings: false,
/// log_level: None,
/// rate_limit: None,
/// enable_event_timestamps: true,
/// experimentation_id: None,
/// enable_internal_pings: true,
/// ping_schedule: Default::default(),
/// ping_lifetime_threshold: 1000,
/// ping_lifetime_max_time: 2000,
/// };
/// let mut glean = Glean::new(cfg).unwrap();
/// let ping = PingType::new("sample", true, false, true, true, true, vec![], vec![], true, vec![]);
/// glean.register_ping_type(&ping);
///
/// let call_counter: CounterMetric = CounterMetric::new(CommonMetricData {
/// name: "calls".into(),
/// category: "local".into(),
/// send_in_pings: vec!["sample".into()],
/// ..Default::default()
/// });
///
/// call_counter.add_sync(&glean, 1);
///
/// ping.submit_sync(&glean, None);
/// ```
///
/// ## Note
///
/// In specific language bindings, this is usually wrapped in a singleton and all metric recording goes to a single instance of this object.
/// In the Rust core, it is possible to create multiple instances, which is used in testing.
#[derive(Debug, MallocSizeOf)]
pub struct Glean {
upload_enabled: bool,
pub(crate) data_store: Option<Database>,
event_data_store: EventDatabase,
pub(crate) core_metrics: CoreMetrics,
pub(crate) additional_metrics: AdditionalMetrics,
pub(crate) database_metrics: DatabaseMetrics,
pub(crate) health_metrics: HealthMetrics,
pub(crate) internal_pings: InternalPings,
data_path: PathBuf,
application_id: String,
ping_registry: HashMap<String, PingType>,
#[ignore_malloc_size_of = "external non-allocating type"]
start_time: DateTime<FixedOffset>,
max_events: u32,
is_first_run: bool,
pub(crate) upload_manager: PingUploadManager,
debug: DebugOptions,
pub(crate) app_build: String,
pub(crate) schedule_metrics_pings: bool,
pub(crate) remote_settings_epoch: AtomicU8,
#[ignore_malloc_size_of = "TODO: Expose Glean's inner memory allocations (bug 1960592)"]
pub(crate) remote_settings_config: Arc<Mutex<RemoteSettingsConfig>>,
pub(crate) with_timestamps: bool,
pub(crate) ping_schedule: HashMap<String, Vec<String>>,
}
impl Glean {
/// Creates and initializes a new Glean object for use in a subprocess.
///
/// Importantly, this will not send any pings at startup, since that
/// sort of management should only happen in the main process.
pub fn new_for_subprocess(cfg: &InternalConfiguration, scan_directories: bool) -> Result<Self> {
log::info!("Creating new Glean v{}", GLEAN_VERSION);
let application_id = sanitize_application_id(&cfg.application_id);
if application_id.is_empty() {
return Err(ErrorKind::InvalidConfig.into());
}
let data_path = Path::new(&cfg.data_path);
let event_data_store = EventDatabase::new(data_path)?;
// Create an upload manager with rate limiting of 15 pings every 60 seconds.
let mut upload_manager = PingUploadManager::new(&cfg.data_path, &cfg.language_binding_name);
let rate_limit = cfg.rate_limit.as_ref().unwrap_or(&PingRateLimit {
seconds_per_interval: DEFAULT_SECONDS_PER_INTERVAL,
pings_per_interval: DEFAULT_PINGS_PER_INTERVAL,
});
upload_manager.set_rate_limiter(
rate_limit.seconds_per_interval,
rate_limit.pings_per_interval,
);
// We only scan the pending ping directories when calling this from a subprocess,
// when calling this from ::new we need to scan the directories after dealing with the upload state.
if scan_directories {
let _scanning_thread = upload_manager.scan_pending_pings_directories(false);
}
let start_time = local_now_with_offset();
let mut this = Self {
upload_enabled: cfg.upload_enabled,
// In the subprocess, we want to avoid accessing the database entirely.
// The easiest way to ensure that is to just not initialize it.
data_store: None,
event_data_store,
core_metrics: CoreMetrics::new(),
additional_metrics: AdditionalMetrics::new(),
database_metrics: DatabaseMetrics::new(),
health_metrics: HealthMetrics::new(),
internal_pings: InternalPings::new(cfg.enable_internal_pings),
upload_manager,
data_path: PathBuf::from(&cfg.data_path),
application_id,
ping_registry: HashMap::new(),
start_time,
max_events: cfg.max_events.unwrap_or(DEFAULT_MAX_EVENTS),
is_first_run: false,
debug: DebugOptions::new(),
app_build: cfg.app_build.to_string(),
// Subprocess doesn't use "metrics" pings so has no need for a scheduler.
schedule_metrics_pings: false,
remote_settings_epoch: AtomicU8::new(0),
remote_settings_config: Arc::new(Mutex::new(RemoteSettingsConfig::new())),
with_timestamps: cfg.enable_event_timestamps,
ping_schedule: cfg.ping_schedule.clone(),
};
// Ensuring these pings are registered.
let pings = this.internal_pings.clone();
this.register_ping_type(&pings.baseline);
this.register_ping_type(&pings.metrics);
this.register_ping_type(&pings.events);
this.register_ping_type(&pings.health);
this.register_ping_type(&pings.deletion_request);
Ok(this)
}
/// Creates and initializes a new Glean object.
///
/// This will create the necessary directories and files in
/// [`cfg.data_path`](InternalConfiguration::data_path). This will also initialize
/// the core metrics.
pub fn new(cfg: InternalConfiguration) -> Result<Self> {
let mut glean = Self::new_for_subprocess(&cfg, false)?;
// Creating the data store creates the necessary path as well.
// If that fails we bail out and don't initialize further.
let data_path = Path::new(&cfg.data_path);
let ping_lifetime_threshold = cfg.ping_lifetime_threshold as usize;
let ping_lifetime_max_time = Duration::from_millis(cfg.ping_lifetime_max_time);
glean.data_store = Some(Database::new(
data_path,
cfg.delay_ping_lifetime_io,
ping_lifetime_threshold,
ping_lifetime_max_time,
)?);
// This code references different states from the "Client ID recovery" flowchart.
// See https://mozilla.github.io/glean/dev/core/internal/client_id_recovery.html for details.
// We don't have the database yet when we first encounter the error,
// so we store it and apply it later.
// state (a)
let stored_client_id = match glean.client_id_from_file() {
Ok(id) if id == *KNOWN_CLIENT_ID => {
glean
.health_metrics
.file_read_error
.get("c0ffee-in-file")
.add_sync(&glean, 1);
None
}
Ok(id) => Some(id),
Err(ClientIdFileError::NotFound) => {
// That's ok, the file might just not exist yet.
glean
.health_metrics
.file_read_error
.get("file-not-found")
.add_sync(&glean, 1);
None
}
Err(ClientIdFileError::PermissionDenied) => {
// state (b)
// Uhm ... who removed our permission?
glean
.health_metrics
.file_read_error
.get("permission-denied")
.add_sync(&glean, 1);
None
}
Err(ClientIdFileError::ParseError(e)) => {
// state (b)
log::trace!("reading cliend_id.txt. Could not parse into UUID: {e}");
glean
.health_metrics
.file_read_error
.get("parse")
.add_sync(&glean, 1);
None
}
Err(ClientIdFileError::IoError(e)) => {
// state (b)
// We can't handle other IO errors (most couldn't occur on this operation anyway)
log::trace!("reading client_id.txt. Unexpected io error: {e}");
glean
.health_metrics
.file_read_error
.get("io")
.add_sync(&glean, 1);
None
}
};
{
let data_store = glean.data_store.as_ref().unwrap();
let file_size = data_store.file_size.map(|n| n.get()).unwrap_or(0);
// If we have a client ID on disk, we check the database
if let Some(stored_client_id) = stored_client_id {
// state (c)
if file_size == 0 {
log::trace!("no database. database size={file_size}. stored_client_id={stored_client_id}");
// state (d)
glean
.health_metrics
.recovered_client_id
.set_from_uuid_sync(&glean, stored_client_id);
glean
.health_metrics
.exception_state
.set_sync(&glean, ExceptionState::EmptyDb);
// state (e) -- mitigation: store recovered client ID in DB
glean
.core_metrics
.client_id
.set_from_uuid_sync(&glean, stored_client_id);
} else {
let db_client_id = glean
.core_metrics
.client_id
.get_value(&glean, Some("glean_client_info"));
match db_client_id {
None => {
// state (f)
log::trace!("no client_id in DB. stored_client_id={stored_client_id}");
glean
.health_metrics
.exception_state
.set_sync(&glean, ExceptionState::RegenDb);
// state (e) -- mitigation: store recovered client ID in DB
glean
.core_metrics
.client_id
.set_from_uuid_sync(&glean, stored_client_id);
}
Some(db_client_id) if db_client_id == *KNOWN_CLIENT_ID => {
// state (i)
log::trace!(
"c0ffee client_id in DB, stored_client_id={stored_client_id}"
);
glean
.health_metrics
.recovered_client_id
.set_from_uuid_sync(&glean, stored_client_id);
glean
.health_metrics
.exception_state
.set_sync(&glean, ExceptionState::C0ffeeInDb);
// If we have a recovered client ID we also overwrite the database.
// state (e)
glean
.core_metrics
.client_id
.set_from_uuid_sync(&glean, stored_client_id);
}
Some(db_client_id) if db_client_id == stored_client_id => {
// all valid. nothing to do
log::trace!("database consistent. db_client_id == stored_client_id: {db_client_id}");
}
Some(db_client_id) => {
// state (g)
log::trace!(
"client_id mismatch. db_client_id{db_client_id}, stored_client_id={stored_client_id}. Overwriting file with db's client_id."
);
glean
.health_metrics
.recovered_client_id
.set_from_uuid_sync(&glean, stored_client_id);
glean
.health_metrics
.exception_state
.set_sync(&glean, ExceptionState::ClientIdMismatch);
// state (h)
glean.store_client_id_with_reporting(
db_client_id,
"client_id mismatch will re-occur.",
);
}
}
}
} else {
log::trace!("No stored client ID. Database might have it.");
let db_client_id = glean
.core_metrics
.client_id
.get_value(&glean, Some("glean_client_info"));
if let Some(db_client_id) = db_client_id {
// state (h)
glean.store_client_id_with_reporting(
db_client_id,
"Might happen on next init then.",
);
} else {
log::trace!("Database has no client ID either. We might be fresh!");
}
}
}
// Set experimentation identifier (if any)
if let Some(experimentation_id) = &cfg.experimentation_id {
glean
.additional_metrics
.experimentation_id
.set_sync(&glean, experimentation_id.to_string());
}
// The upload enabled flag may have changed since the last run, for
// example by the changing of a config file.
if cfg.upload_enabled {
// If upload is enabled, just follow the normal code path to
// instantiate the core metrics.
glean.on_upload_enabled();
} else {
// If upload is disabled, then clear the metrics
// but do not send a deletion request ping.
// If we have run before, and we have an old client_id,
// do the full upload disabled operations to clear metrics
// and send a deletion request ping.
match glean
.core_metrics
.client_id
.get_value(&glean, Some("glean_client_info"))
{
None => glean.clear_metrics(),
Some(uuid) => {
if let Err(e) = glean.remove_stored_client_id() {
log::error!("Couldn't remove client ID on disk. This might lead to a resurrection of this client ID later. Error: {e}");
}
if uuid == *KNOWN_CLIENT_ID {
// Previously Glean kept the KNOWN_CLIENT_ID stored.
// Let's ensure we erase it now.
if let Some(data) = glean.data_store.as_ref() {
_ = data.remove_single_metric(
Lifetime::User,
"glean_client_info",
"client_id",
);
}
} else {
// Temporarily enable uploading so we can submit a
// deletion request ping.
glean.upload_enabled = true;
glean.on_upload_disabled(true);
}
}
}
}
// We set this only for non-subprocess situations.
// If internal pings are disabled, we don't set up the MPS either,
// it wouldn't send any data anyway.
glean.schedule_metrics_pings = cfg.enable_internal_pings && cfg.use_core_mps;
// We only scan the pendings pings directories **after** dealing with the upload state.
// If upload is disabled, we delete all pending pings files
// and we need to do that **before** scanning the pending pings folder
// to ensure we don't enqueue pings before their files are deleted.
let _scanning_thread = glean.upload_manager.scan_pending_pings_directories(true);
Ok(glean)
}
/// For tests make it easy to create a Glean object using only the required configuration.
#[cfg(test)]
pub(crate) fn with_options(
data_path: &str,
application_id: &str,
upload_enabled: bool,
enable_internal_pings: bool,
) -> Self {
let cfg = InternalConfiguration {
data_path: data_path.into(),
application_id: application_id.into(),
language_binding_name: "Rust".into(),
upload_enabled,
max_events: None,
delay_ping_lifetime_io: false,
app_build: "Unknown".into(),
use_core_mps: false,
trim_data_to_registered_pings: false,
log_level: None,
rate_limit: None,
enable_event_timestamps: true,
experimentation_id: None,
enable_internal_pings,
ping_schedule: Default::default(),
ping_lifetime_threshold: 0,
ping_lifetime_max_time: 0,
};
let mut glean = Self::new(cfg).unwrap();
// Disable all upload manager policies for testing
glean.upload_manager = PingUploadManager::no_policy(data_path);
glean
}
/// Destroys the database.
///
/// After this Glean needs to be reinitialized.
pub fn destroy_db(&mut self) {
self.data_store = None;
}
fn client_id_file_path(&self) -> PathBuf {
self.data_path.join(CLIENT_ID_PLAIN_FILENAME)
}
/// Write the client ID to a separate plain file on disk
///
/// Use `store_client_id_with_reporting` to handle the error cases.
fn store_client_id(&self, client_id: Uuid) -> Result<(), ClientIdFileError> {
let mut fp = File::create(self.client_id_file_path())?;
let mut buffer = Uuid::encode_buffer();
let uuid_str = client_id.hyphenated().encode_lower(&mut buffer);
fp.write_all(uuid_str.as_bytes())?;
fp.sync_all()?;
Ok(())
}
/// Write the client ID to a separate plain file on disk
///
/// When an error occurs an error message is logged and the error is counted in a metric.
fn store_client_id_with_reporting(&self, client_id: Uuid, msg: &str) {
if let Err(err) = self.store_client_id(client_id) {
log::error!(
"Could not write {client_id} to state file. {} Error: {err}",
msg
);
match err {
ClientIdFileError::NotFound => {
self.health_metrics
.file_write_error
.get("not-found")
.add_sync(self, 1);
}
ClientIdFileError::PermissionDenied => {
self.health_metrics
.file_write_error
.get("permission-denied")
.add_sync(self, 1);
}
ClientIdFileError::IoError(..) => {
self.health_metrics
.file_write_error
.get("io")
.add_sync(self, 1);
}
ClientIdFileError::ParseError(..) => {
log::error!("Parse error encountered on file write. This is impossible.");
}
}
}
}
/// Try to load a client ID from the plain file on disk.
fn client_id_from_file(&self) -> Result<Uuid, ClientIdFileError> {
let uuid_str = fs::read_to_string(self.client_id_file_path())?;
// We don't write a newline, but we still trim it. Who knows who else touches that file by accident.
// We're also a bit more lenient in what we accept here:
// uppercase, lowercase, with or without dashes, urn, braced (and whatever else `Uuid`
// parses by default).
let uuid = Uuid::try_parse(uuid_str.trim_end())?;
Ok(uuid)
}
/// Remove the stored client ID from disk.
/// Should only be called when the client ID is also removed from the database.
fn remove_stored_client_id(&self) -> Result<(), ClientIdFileError> {
match fs::remove_file(self.client_id_file_path()) {
Ok(()) => Ok(()),
Err(e) if e.kind() == io::ErrorKind::NotFound => {
// File was already missing. No need to report that.
Ok(())
}
Err(e) => Err(e.into()),
}
}
/// Initializes the core metrics managed by Glean's Rust core.
fn initialize_core_metrics(&mut self) {
let need_new_client_id = match self
.core_metrics
.client_id
.get_value(self, Some("glean_client_info"))
{
None => true,
Some(uuid) => uuid == *KNOWN_CLIENT_ID,
};
if need_new_client_id {
let new_clientid = self.core_metrics.client_id.generate_and_set_sync(self);
self.store_client_id_with_reporting(new_clientid, "New client in database only.");
}
if self
.core_metrics
.first_run_date
.get_value(self, "glean_client_info")
.is_none()
{
self.core_metrics.first_run_date.set_sync(self, None);
// The `first_run_date` field is generated on the very first run
// and persisted across upload toggling. We can assume that, the only
// time it is set, that's indeed our "first run".
self.is_first_run = true;
}
self.set_application_lifetime_core_metrics();
}
/// Initializes the database metrics managed by Glean's Rust core.
fn initialize_database_metrics(&mut self) {
log::trace!("Initializing database metrics");
if let Some(size) = self
.data_store
.as_ref()
.and_then(|database| database.file_size())
{
log::trace!("Database file size: {}", size.get());
self.database_metrics
.size
.accumulate_sync(self, size.get() as i64)
}
if let Some(rkv_load_state) = self
.data_store
.as_ref()
.and_then(|database| database.rkv_load_state())
{
self.database_metrics
.rkv_load_error
.set_sync(self, rkv_load_state)
}
}
/// Signals that the environment is ready to submit pings.
///
/// Should be called when Glean is initialized to the point where it can correctly assemble pings.
/// Usually called from the language binding after all of the core metrics have been set
/// and the ping types have been registered.
///
/// # Arguments
///
/// * `trim_data_to_registered_pings` - Whether we should limit to storing data only for
/// data belonging to pings previously registered via `register_ping_type`.
///
/// # Returns
///
/// Whether the "events" ping was submitted.
pub fn on_ready_to_submit_pings(&mut self, trim_data_to_registered_pings: bool) -> bool {
// When upload is disabled on init we already clear out metrics.
// However at that point not all pings are registered and so we keep that data around.
// By the time we would be ready to submit we try again cleaning out metrics from
// now-known pings.
if !self.upload_enabled {
log::debug!("on_ready_to_submit_pings. let's clear pings once again.");
self.clear_metrics();
}
self.event_data_store
.flush_pending_events_on_startup(self, trim_data_to_registered_pings)
}
/// Sets whether upload is enabled or not.
///
/// When uploading is disabled, metrics aren't recorded at all and no
/// data is uploaded.
///
/// When disabling, all pending metrics, events and queued pings are cleared.
///
/// When enabling, the core Glean metrics are recreated.
///
/// If the value of this flag is not actually changed, this is a no-op.
///
/// # Arguments
///
/// * `flag` - When true, enable metric collection.
///
/// # Returns
///
/// Whether the flag was different from the current value,
/// and actual work was done to clear or reinstate metrics.
pub fn set_upload_enabled(&mut self, flag: bool) -> bool {
log::info!("Upload enabled: {:?}", flag);
if self.upload_enabled != flag {
if flag {
self.on_upload_enabled();
} else {
self.on_upload_disabled(false);
}
true
} else {
false
}
}
/// Enable or disable a ping.
///
/// Disabling a ping causes all data for that ping to be removed from storage
/// and all pending pings of that type to be deleted.
///
/// **Note**: Do not use directly. Call `PingType::set_enabled` instead.
#[doc(hidden)]
pub fn set_ping_enabled(&mut self, ping: &PingType, enabled: bool) {
ping.store_enabled(enabled);
if !enabled {
if let Some(data) = self.data_store.as_ref() {
_ = data.clear_ping_lifetime_storage(ping.name());
_ = data.clear_lifetime_storage(Lifetime::User, ping.name());
_ = data.clear_lifetime_storage(Lifetime::Application, ping.name());
}
let ping_maker = PingMaker::new();
let disabled_pings = &[ping.name()][..];
if let Err(err) = ping_maker.clear_pending_pings(self.get_data_path(), disabled_pings) {
log::warn!("Error clearing pending pings: {}", err);
}
}
}
/// Determines whether upload is enabled.
///
/// When upload is disabled, no data will be recorded.
pub fn is_upload_enabled(&self) -> bool {
self.upload_enabled
}
/// Check if a ping is enabled.
///
/// Note that some internal "ping" names are considered to be always enabled.
///
/// If a ping is not known to Glean ("unregistered") it is always considered disabled.
/// If a ping is known, it can be enabled/disabled at any point.
/// Only data for enabled pings is recorded.
/// Disabled pings are never submitted.
pub fn is_ping_enabled(&self, ping: &str) -> bool {
// We "abuse" pings/storage names for internal data.
const DEFAULT_ENABLED: &[&str] = &[
"glean_client_info",
"glean_internal_info",
// for `experimentation_id`.
// That should probably have gone into `glean_internal_info` instead.
"all-pings",
];
// `client_info`-like stuff is always enabled.
if DEFAULT_ENABLED.contains(&ping) {
return true;
}
let Some(ping) = self.ping_registry.get(ping) else {
log::trace!("Unknown ping {ping}. Assuming disabled.");
return false;
};
ping.enabled(self)
}
/// Handles the changing of state from upload disabled to enabled.
///
/// Should only be called when the state actually changes.
///
/// The `upload_enabled` flag is set to true and the core Glean metrics are
/// recreated.
fn on_upload_enabled(&mut self) {
self.upload_enabled = true;
self.initialize_core_metrics();
self.initialize_database_metrics();
}
/// Handles the changing of state from upload enabled to disabled.
///
/// Should only be called when the state actually changes.
///
/// A deletion_request ping is sent, all pending metrics, events and queued
/// pings are cleared, and the client_id is set to KNOWN_CLIENT_ID.
/// Afterward, the upload_enabled flag is set to false.
fn on_upload_disabled(&mut self, during_init: bool) {
// The upload_enabled flag should be true here, or the deletion ping
// won't be submitted.
let reason = if during_init {
Some("at_init")
} else {
Some("set_upload_enabled")
};
if !self
.internal_pings
.deletion_request
.submit_sync(self, reason)
{
log::error!("Failed to submit deletion-request ping on optout.");
}
self.clear_metrics();
self.upload_enabled = false;
}
/// Clear any pending metrics when telemetry is disabled.
fn clear_metrics(&mut self) {
// Clear the pending pings queue and acquire the lock
// so that it can't be accessed until this function is done.
let _lock = self.upload_manager.clear_ping_queue();
// Clear any pending pings that follow `collection_enabled`.
let ping_maker = PingMaker::new();
let disabled_pings = self
.ping_registry
.iter()
.filter(|&(_ping_name, ping)| ping.follows_collection_enabled())
.map(|(ping_name, _ping)| &ping_name[..])
.collect::<Vec<_>>();
if let Err(err) = ping_maker.clear_pending_pings(self.get_data_path(), &disabled_pings) {
log::warn!("Error clearing pending pings: {}", err);
}
if let Err(e) = self.remove_stored_client_id() {
log::error!("Couldn't remove client ID on disk. This might lead to a resurrection of this client ID later. Error: {e}");
}
// Delete all stored metrics.
// Note that this also includes the ping sequence numbers, so it has
// the effect of resetting those to their initial values.
if let Some(data) = self.data_store.as_ref() {
_ = data.clear_lifetime_storage(Lifetime::User, "glean_internal_info");
_ = data.remove_single_metric(Lifetime::User, "glean_client_info", "client_id");
for (ping_name, ping) in &self.ping_registry {
if ping.follows_collection_enabled() {
_ = data.clear_ping_lifetime_storage(ping_name);
_ = data.clear_lifetime_storage(Lifetime::User, ping_name);
_ = data.clear_lifetime_storage(Lifetime::Application, ping_name);
}
}
}
if let Err(err) = self.event_data_store.clear_all() {
log::warn!("Error clearing pending events: {}", err);
}
// This does not clear the experiments store (which isn't managed by the
// StorageEngineManager), since doing so would mean we would have to have the
// application tell us again which experiments are active if telemetry is
// re-enabled.
}
/// Gets the application ID as specified on instantiation.
pub fn get_application_id(&self) -> &str {
&self.application_id
}
/// Gets the data path of this instance.
pub fn get_data_path(&self) -> &Path {
&self.data_path
}
/// Gets a handle to the database.
#[track_caller] // If this fails we're interested in the caller.
pub fn storage(&self) -> &Database {
self.data_store.as_ref().expect("No database found")
}
/// Gets an optional handle to the database.
pub fn storage_opt(&self) -> Option<&Database> {
self.data_store.as_ref()
}
/// Gets a handle to the event database.
pub fn event_storage(&self) -> &EventDatabase {
&self.event_data_store
}
pub(crate) fn with_timestamps(&self) -> bool {
self.with_timestamps
}
/// Gets the maximum number of events to store before sending a ping.
pub fn get_max_events(&self) -> usize {
let remote_settings_config = self.remote_settings_config.lock().unwrap();
if let Some(max_events) = remote_settings_config.event_threshold {
max_events as usize
} else {
self.max_events as usize
}
}
/// Gets the next task for an uploader.
///
/// This can be one of:
///
/// * [`Wait`](PingUploadTask::Wait) - which means the requester should ask
/// again later;
/// * [`Upload(PingRequest)`](PingUploadTask::Upload) - which means there is
/// a ping to upload. This wraps the actual request object;
/// * [`Done`](PingUploadTask::Done) - which means requester should stop
/// asking for now.
///
/// # Returns
///
/// A [`PingUploadTask`] representing the next task.
pub fn get_upload_task(&self) -> PingUploadTask {
self.upload_manager.get_upload_task(self, self.log_pings())
}
/// Processes the response from an attempt to upload a ping.
///
/// # Arguments
///
/// * `uuid` - The UUID of the ping in question.
/// * `status` - The upload result.
pub fn process_ping_upload_response(
&self,
uuid: &str,
status: UploadResult,
) -> UploadTaskAction {
self.upload_manager
.process_ping_upload_response(self, uuid, status)
}
/// Takes a snapshot for the given store and optionally clear it.
///
/// # Arguments
///
/// * `store_name` - The store to snapshot.
/// * `clear_store` - Whether to clear the store after snapshotting.
///
/// # Returns
///
/// The snapshot in a string encoded as JSON. If the snapshot is empty, returns an empty string.
pub fn snapshot(&mut self, store_name: &str, clear_store: bool) -> String {
StorageManager
.snapshot(self.storage(), store_name, clear_store)
.unwrap_or_else(|| String::from(""))
}
pub(crate) fn make_path(&self, ping_name: &str, doc_id: &str) -> String {
format!(
"/submit/{}/{}/{}/{}",
self.get_application_id(),
ping_name,
GLEAN_SCHEMA_VERSION,
doc_id
)
}