Debian Codegraph @

third_party/rust/glean-core/tests/labeled.rs

raw
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 mod common;
6 use crate::common::*;
7
8 use serde_json::json;
9
10 use glean_core::metrics::*;
11 use glean_core::storage::StorageManager;
12 use glean_core::{test_get_num_recorded_errors, ErrorType};
13 use glean_core::{CommonMetricData, HistogramType, LabeledMetricData, Lifetime};
14
15 #[test]
16 fn can_create_labeled_counter_metric() {
17 let (glean, _t) = new_glean(None);
18 let labeled = LabeledCounter::new(
19 LabeledMetricData::Common {
20 cmd: CommonMetricData {
21 name: "labeled_metric".into(),
22 category: "telemetry".into(),
23 send_in_pings: vec!["store1".into()],
24 disabled: false,
25 lifetime: Lifetime::Ping,
26 ..Default::default()
27 },
28 },
29 Some(vec!["label1".into()]),
30 );
31
32 let metric = labeled.get("label1");
33 metric.add_sync(&glean, 1);
34
35 let snapshot = StorageManager
36 .snapshot_as_json(glean.storage(), "store1", true)
37 .unwrap();
38
39 assert_eq!(
40 json!({
41 "labeled_counter": {
42 "telemetry.labeled_metric": { "label1": 1 }
43 }
44 }),
45 snapshot
46 );
47 }
48
49 #[test]
50 fn can_create_labeled_string_metric() {
51 let (glean, _t) = new_glean(None);
52 let labeled = LabeledString::new(
53 LabeledMetricData::Common {
54 cmd: CommonMetricData {
55 name: "labeled_metric".into(),
56 category: "telemetry".into(),
57 send_in_pings: vec!["store1".into()],
58 disabled: false,
59 lifetime: Lifetime::Ping,
60 ..Default::default()
61 },
62 },
63 Some(vec!["label1".into()]),
64 );
65
66 let metric = labeled.get("label1");
67 metric.set_sync(&glean, "text");
68
69 let snapshot = StorageManager
70 .snapshot_as_json(glean.storage(), "store1", true)
71 .unwrap();
72
73 assert_eq!(
74 json!({
75 "labeled_string": {
76 "telemetry.labeled_metric": { "label1": "text" }
77 }
78 }),
79 snapshot
80 );
81 }
82
83 #[test]
84 fn can_create_labeled_bool_metric() {
85 let (glean, _t) = new_glean(None);
86 let labeled = LabeledBoolean::new(
87 LabeledMetricData::Common {
88 cmd: CommonMetricData {
89 name: "labeled_metric".into(),
90 category: "telemetry".into(),
91 send_in_pings: vec!["store1".into()],
92 disabled: false,
93 lifetime: Lifetime::Ping,
94 ..Default::default()
95 },
96 },
97 Some(vec!["label1".into()]),
98 );
99
100 let metric = labeled.get("label1");
101 metric.set_sync(&glean, true);
102
103 let snapshot = StorageManager
104 .snapshot_as_json(glean.storage(), "store1", true)
105 .unwrap();
106
107 assert_eq!(
108 json!({
109 "labeled_boolean": {
110 "telemetry.labeled_metric": { "label1": true }
111 }
112 }),
113 snapshot
114 );
115 }
116
117 #[test]
118 fn can_create_labeled_custom_distribution_metric() {
119 let (glean, _t) = new_glean(None);
120 let labeled = LabeledCustomDistribution::new(
121 LabeledMetricData::CustomDistribution {
122 cmd: CommonMetricData {
123 name: "labeled_metric".into(),
124 category: "telemetry".into(),
125 send_in_pings: vec!["store1".into()],
126 disabled: false,
127 lifetime: Lifetime::Ping,
128 ..Default::default()
129 },
130 range_min: 0,
131 range_max: 1024,
132 bucket_count: 1,
133 histogram_type: HistogramType::Linear,
134 },
135 Some(vec!["label1".into()]),
136 );
137
138 let metric = labeled.get("label1");
139 metric.accumulate_samples_sync(&glean, &[42]);
140
141 let snapshot = StorageManager
142 .snapshot_as_json(glean.storage(), "store1", true)
143 .unwrap();
144
145 assert_eq!(
146 json!({
147 "labeled_custom_distribution": {
148 "telemetry.labeled_metric": { "label1": { "sum": 42, "values": {"0": 1} } }
149 }
150 }),
151 snapshot
152 );
153 }
154
155 #[test]
156 fn can_create_labeled_memory_distribution_metric() {
157 let (glean, _t) = new_glean(None);
158 let labeled = LabeledMemoryDistribution::new(
159 LabeledMetricData::MemoryDistribution {
160 cmd: CommonMetricData {
161 name: "labeled_metric".into(),
162 category: "telemetry".into(),
163 send_in_pings: vec!["store1".into()],
164 disabled: false,
165 lifetime: Lifetime::Ping,
166 ..Default::default()
167 },
168 unit: MemoryUnit::Byte,
169 },
170 Some(vec!["label1".into()]),
171 );
172
173 let metric = labeled.get("label1");
174 metric.accumulate_samples_sync(&glean, vec![42]);
175
176 let snapshot = StorageManager
177 .snapshot_as_json(glean.storage(), "store1", true)
178 .unwrap();
179
180 assert_eq!(
181 json!({
182 "labeled_memory_distribution": {
183 "telemetry.labeled_metric": { "label1": { "sum": 42, "values": {"41": 1} } }
184 }
185 }),
186 snapshot
187 );
188 }
189
190 #[test]
191 fn can_create_labeled_timing_distribution_metric() {
192 let (glean, _t) = new_glean(None);
193 let labeled = LabeledTimingDistribution::new(
194 LabeledMetricData::TimingDistribution {
195 cmd: CommonMetricData {
196 name: "labeled_metric".into(),
197 category: "telemetry".into(),
198 send_in_pings: vec!["store1".into()],
199 disabled: false,
200 lifetime: Lifetime::Ping,
201 ..Default::default()
202 },
203 unit: TimeUnit::Nanosecond,
204 },
205 Some(vec!["label1".into()]),
206 );
207
208 let metric = labeled.get("label1");
209 metric.accumulate_samples_sync(&glean, &[42]);
210
211 let snapshot = StorageManager
212 .snapshot_as_json(glean.storage(), "store1", true)
213 .unwrap();
214
215 assert_eq!(
216 json!({
217 "labeled_timing_distribution": {
218 "telemetry.labeled_metric": { "label1": { "sum": 42, "values": {"41": 1} } }
219 }
220 }),
221 snapshot
222 );
223 }
224
225 #[test]
226 fn can_create_labeled_quantity_metric() {
227 let (glean, _t) = new_glean(None);
228 let labeled = LabeledQuantity::new(
229 LabeledMetricData::Common {
230 cmd: CommonMetricData {
231 name: "labeled_metric".into(),
232 category: "telemetry".into(),
233 send_in_pings: vec!["store1".into()],
234 disabled: false,
235 lifetime: Lifetime::Ping,
236 ..Default::default()
237 },
238 },
239 Some(vec!["label1".into()]),
240 );
241
242 let metric = labeled.get("label1");
243 metric.set_sync(&glean, 42);
244
245 let snapshot = StorageManager
246 .snapshot_as_json(glean.storage(), "store1", true)
247 .unwrap();
248
249 assert_eq!(
250 json!({
251 "labeled_quantity": {
252 "telemetry.labeled_metric": { "label1": 42, },
253 }
254 }),
255 snapshot
256 );
257 }
258
259 #[test]
260 fn can_use_multiple_labels() {
261 let (glean, _t) = new_glean(None);
262 let labeled = LabeledCounter::new(
263 LabeledMetricData::Common {
264 cmd: CommonMetricData {
265 name: "labeled_metric".into(),
266 category: "telemetry".into(),
267 send_in_pings: vec!["store1".into()],
268 disabled: false,
269 lifetime: Lifetime::Ping,
270 ..Default::default()
271 },
272 },
273 None,
274 );
275
276 let metric = labeled.get("label1");
277 metric.add_sync(&glean, 1);
278
279 let metric = labeled.get("label2");
280 metric.add_sync(&glean, 2);
281
282 let snapshot = StorageManager
283 .snapshot_as_json(glean.storage(), "store1", true)
284 .unwrap();
285
286 assert_eq!(
287 json!({
288 "labeled_counter": {
289 "telemetry.labeled_metric": {
290 "label1": 1,
291 "label2": 2,
292 }
293 }
294 }),
295 snapshot
296 );
297 }
298
299 #[test]
300 fn can_record_error_for_submetric() {
301 let (glean, _t) = new_glean(None);
302 let labeled = LabeledString::new(
303 LabeledMetricData::Common {
304 cmd: CommonMetricData {
305 name: "labeled_metric".into(),
306 category: "telemetry".into(),
307 send_in_pings: vec!["store1".into()],
308 disabled: false,
309 lifetime: Lifetime::Ping,
310 ..Default::default()
311 },
312 },
313 Some(vec!["label1".into()]),
314 );
315
316 let metric = labeled.get("label1");
317 metric.set_sync(&glean, "01234567890".repeat(26));
318
319 // Make sure that the errors have been recorded
320 assert_eq!(
321 Ok(1),
322 test_get_num_recorded_errors(&glean, metric.meta(), ErrorType::InvalidOverflow)
323 );
324 }
325
326 #[test]
327 fn labels_are_checked_against_static_list() {
328 let (glean, _t) = new_glean(None);
329 let labeled = LabeledCounter::new(
330 LabeledMetricData::Common {
331 cmd: CommonMetricData {
332 name: "labeled_metric".into(),
333 category: "telemetry".into(),
334 send_in_pings: vec!["store1".into()],
335 disabled: false,
336 lifetime: Lifetime::Ping,
337 ..Default::default()
338 },
339 },
340 Some(vec!["label1".into(), "label2".into()]),
341 );
342
343 let metric = labeled.get("label1");
344 metric.add_sync(&glean, 1);
345
346 let metric = labeled.get("label2");
347 metric.add_sync(&glean, 2);
348
349 // All non-registed labels get mapped to the `other` label
350 let metric = labeled.get("label3");
351 metric.add_sync(&glean, 3);
352 let metric = labeled.get("label4");
353 metric.add_sync(&glean, 4);
354
355 let snapshot = StorageManager
356 .snapshot_as_json(glean.storage(), "store1", true)
357 .unwrap();
358
359 assert_eq!(
360 json!({
361 "labeled_counter": {
362 "telemetry.labeled_metric": {
363 "label1": 1,
364 "label2": 2,
365 "__other__": 7,
366 }
367 }
368 }),
369 snapshot
370 );
371 }
372
373 #[test]
374 fn dynamic_labels_too_long() {
375 let (glean, _t) = new_glean(None);
376 let labeled = LabeledCounter::new(
377 LabeledMetricData::Common {
378 cmd: CommonMetricData {
379 name: "labeled_metric".into(),
380 category: "telemetry".into(),
381 send_in_pings: vec!["store1".into()],
382 disabled: false,
383 lifetime: Lifetime::Ping,
384 ..Default::default()
385 },
386 },
387 None,
388 );
389
390 let metric = labeled.get("1".repeat(112));
391 metric.add_sync(&glean, 1);
392
393 let snapshot = StorageManager
394 .snapshot_as_json(glean.storage(), "store1", true)
395 .unwrap();
396
397 assert_eq!(
398 json!({
399 "labeled_counter": {
400 "glean.error.invalid_label": { "telemetry.labeled_metric": 1 },
401 "telemetry.labeled_metric": {
402 "__other__": 1,
403 }
404 }
405 }),
406 snapshot
407 );
408 }
409
410 #[test]
411 fn dynamic_labels_regex_mismatch() {
412 let (glean, _t) = new_glean(None);
413 let labeled = LabeledCounter::new(
414 LabeledMetricData::Common {
415 cmd: CommonMetricData {
416 name: "labeled_metric".into(),
417 category: "telemetry".into(),
418 send_in_pings: vec!["store1".into()],
419 disabled: false,
420 lifetime: Lifetime::Ping,
421 ..Default::default()
422 },
423 },
424 None,
425 );
426
427 let labels_not_validating = vec!["non-ASCII�"];
428 let num_non_validating = labels_not_validating.len();
429
430 for label in &labels_not_validating {
431 labeled.get(label).add_sync(&glean, 1);
432 }
433
434 let snapshot = StorageManager
435 .snapshot_as_json(glean.storage(), "store1", true)
436 .unwrap();
437
438 assert_eq!(
439 json!({
440 "labeled_counter": {
441 "glean.error.invalid_label": { "telemetry.labeled_metric": num_non_validating },
442 "telemetry.labeled_metric": {
443 "__other__": num_non_validating,
444 }
445 }
446 }),
447 snapshot
448 );
449 }
450
451 #[test]
452 fn dynamic_labels_regex_allowed() {
453 let (glean, _t) = new_glean(None);
454 let labeled = LabeledCounter::new(
455 LabeledMetricData::Common {
456 cmd: CommonMetricData {
457 name: "labeled_metric".into(),
458 category: "telemetry".into(),
459 send_in_pings: vec!["store1".into()],
460 disabled: false,
461 lifetime: Lifetime::Ping,
462 ..Default::default()
463 },
464 },
465 None,
466 );
467
468 let labels_validating = vec![
469 "this.is.fine",
470 "this_is_fine_too",
471 "this.is_still_fine",
472 "thisisfine",
473 "_.is_fine",
474 "this.is-fine",
475 "this-is-fine",
476 ];
477
478 for label in &labels_validating {
479 labeled.get(label).add_sync(&glean, 1);
480 }
481
482 let snapshot = StorageManager
483 .snapshot_as_json(glean.storage(), "store1", true)
484 .unwrap();
485
486 assert_eq!(
487 json!({
488 "labeled_counter": {
489 "telemetry.labeled_metric": {
490 "this.is.fine": 1,
491 "this_is_fine_too": 1,
492 "this.is_still_fine": 1,
493 "thisisfine": 1,
494 "_.is_fine": 1,
495 "this.is-fine": 1,
496 "this-is-fine": 1
497 }
498 }
499 }),
500 snapshot
501 );
502 }
503
504 #[test]
505 fn seen_labels_get_reloaded_from_disk() {
506 let (mut tempdir, _) = tempdir();
507
508 let (glean, dir) = new_glean(Some(tempdir));
509 tempdir = dir;
510
511 let labeled = LabeledCounter::new(
512 LabeledMetricData::Common {
513 cmd: CommonMetricData {
514 name: "labeled_metric".into(),
515 category: "telemetry".into(),
516 send_in_pings: vec!["store1".into()],
517 disabled: false,
518 lifetime: Lifetime::Ping,
519 ..Default::default()
520 },
521 },
522 None,
523 );
524
525 // Store some data into labeled metrics
526 {
527 // Set the maximum number of labels
528 for i in 1..=16 {
529 let label = format!("label{i}");
530 labeled.get(label).add_sync(&glean, i);
531 }
532
533 let snapshot = StorageManager
534 .snapshot_as_json(glean.storage(), "store1", false)
535 .unwrap();
536
537 // Check that the data is there
538 for i in 1..=16 {
539 let label = format!("label{i}");
540 assert_eq!(
541 i,
542 snapshot["labeled_counter"]["telemetry.labeled_metric"][&label]
543 );
544 }
545
546 drop(glean);
547 }
548
549 // Force a reload
550 {
551 let (glean, _t) = new_glean(Some(tempdir));
552
553 // Try to store another label
554 labeled.get("new_label").add_sync(&glean, 40);
555
556 let snapshot = StorageManager
557 .snapshot_as_json(glean.storage(), "store1", false)
558 .unwrap();
559
560 // Check that the old data is still there
561 for i in 1..=16 {
562 let label = format!("label{i}");
563 assert_eq!(
564 i,
565 snapshot["labeled_counter"]["telemetry.labeled_metric"][&label]
566 );
567 }
568
569 // The new label lands in the __other__ bucket, due to too many labels
570 assert_eq!(
571 40,
572 snapshot["labeled_counter"]["telemetry.labeled_metric"]["__other__"]
573 );
574 }
575 }
576
577 #[test]
578 fn caching_metrics_with_dynamic_labels() {
579 let (glean, _t) = new_glean(None);
580 let labeled = LabeledCounter::new(
581 LabeledMetricData::Common {
582 cmd: CommonMetricData {
583 name: "cached_labels".into(),
584 category: "telemetry".into(),
585 send_in_pings: vec!["store1".into()],
586 disabled: false,
587 lifetime: Lifetime::Ping,
588 ..Default::default()
589 },
590 },
591 None,
592 );
593
594 // Create multiple metric instances and cache them for later use.
595 let metrics = (1..=20)
596 .map(|i| {
597 let label = format!("label{i}");
598 labeled.get(label)
599 })
600 .collect::<Vec<_>>();
601
602 // Only now use them.
603 for metric in metrics {
604 metric.add_sync(&glean, 1);
605 }
606
607 // The maximum number of labels we store is 16.
608 // So we should have put 4 metrics in the __other__ bucket.
609 let other = labeled.get("__other__");
610 assert_eq!(Some(4), other.get_value(&glean, Some("store1")));
611 }
612
613 #[test]
614 fn caching_metrics_with_dynamic_labels_across_pings() {
615 let (glean, _t) = new_glean(None);
616 let labeled = LabeledCounter::new(
617 LabeledMetricData::Common {
618 cmd: CommonMetricData {
619 name: "cached_labels2".into(),
620 category: "telemetry".into(),
621 send_in_pings: vec!["store1".into()],
622 disabled: false,
623 lifetime: Lifetime::Ping,
624 ..Default::default()
625 },
626 },
627 None,
628 );
629
630 // Create multiple metric instances and cache them for later use.
631 let metrics = (1..=20)
632 .map(|i| {
633 let label = format!("label{i}");
634 labeled.get(label)
635 })
636 .collect::<Vec<_>>();
637
638 // Only now use them.
639 for metric in &metrics {
640 metric.add_sync(&glean, 1);
641 }
642
643 // The maximum number of labels we store is 16.
644 // So we should have put 4 metrics in the __other__ bucket.
645 let other = labeled.get("__other__");
646 assert_eq!(Some(4), other.get_value(&glean, Some("store1")));
647
648 // Snapshot (so we can inspect the JSON)
649 // and clear out storage (the same way submitting a ping would)
650 let snapshot = StorageManager
651 .snapshot_as_json(glean.storage(), "store1", true)
652 .unwrap();
653
654 // We didn't send the 20th label
655 assert_eq!(
656 json!(null),
657 snapshot["labeled_counter"]["telemetry.cached_labels2"]["label20"]
658 );
659
660 // We now set the ones that ended up in `__other__` before.
661 // Note: indexing is zero-based,
662 // but we later check the names, so let's offset it by 1.
663 metrics[16].add_sync(&glean, 17);
664 metrics[17].add_sync(&glean, 18);
665 metrics[18].add_sync(&glean, 19);
666 metrics[19].add_sync(&glean, 20);
667
668 assert_eq!(Some(17), metrics[16].get_value(&glean, Some("store1")));
669 assert_eq!(Some(18), metrics[17].get_value(&glean, Some("store1")));
670 assert_eq!(Some(19), metrics[18].get_value(&glean, Some("store1")));
671 assert_eq!(Some(20), metrics[19].get_value(&glean, Some("store1")));
672 assert_eq!(None, other.get_value(&glean, Some("store1")));
673
674 let snapshot = StorageManager
675 .snapshot_as_json(glean.storage(), "store1", true)
676 .unwrap();
677
678 let cached_labels = &snapshot["labeled_counter"]["telemetry.cached_labels2"];
679 assert_eq!(json!(17), cached_labels["label17"]);
680 assert_eq!(json!(18), cached_labels["label18"]);
681 assert_eq!(json!(19), cached_labels["label19"]);
682 assert_eq!(json!(20), cached_labels["label20"]);
683 assert_eq!(json!(null), cached_labels["__other__"]);
684 }