| 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 std::time::Duration; |
| 9 |
|
| 10 |
use serde_json::json; |
| 11 |
|
| 12 |
use glean_core::metrics::*; |
| 13 |
use glean_core::storage::StorageManager; |
| 14 |
use glean_core::{test_get_num_recorded_errors, ErrorType}; |
| 15 |
use glean_core::{CommonMetricData, Lifetime}; |
| 16 |
|
| 17 |
// Tests ported from glean-ac |
| 18 |
|
| 19 |
#[test] |
| 20 |
fn serializer_should_correctly_serialize_timing_distribution() { |
| 21 |
let (mut tempdir, _) = tempdir(); |
| 22 |
|
| 23 |
let duration = 60; |
| 24 |
let time_unit = TimeUnit::Nanosecond; |
| 25 |
|
| 26 |
{ |
| 27 |
let (glean, dir) = new_glean(Some(tempdir)); |
| 28 |
tempdir = dir; |
| 29 |
|
| 30 |
let metric = TimingDistributionMetric::new( |
| 31 |
CommonMetricData { |
| 32 |
name: "distribution".into(), |
| 33 |
category: "telemetry".into(), |
| 34 |
send_in_pings: vec!["store1".into()], |
| 35 |
disabled: false, |
| 36 |
lifetime: Lifetime::Ping, |
| 37 |
..Default::default() |
| 38 |
}, |
| 39 |
time_unit, |
| 40 |
); |
| 41 |
|
| 42 |
let id = 4u64.into(); |
| 43 |
metric.set_start(id, 0); |
| 44 |
metric.set_stop_and_accumulate(&glean, id, duration); |
| 45 |
|
| 46 |
let snapshot = metric |
| 47 |
.get_value(&glean, "store1") |
| 48 |
.expect("Value should be stored"); |
| 49 |
|
| 50 |
assert_eq!(snapshot.count, 1); |
| 51 |
assert_eq!(snapshot.sum, duration as i64); |
| 52 |
} |
| 53 |
|
| 54 |
// Make a new Glean instance here, which should force reloading of the data from disk |
| 55 |
// so we can ensure it persisted, because it has User lifetime |
| 56 |
{ |
| 57 |
let (glean, _t) = new_glean(Some(tempdir)); |
| 58 |
let snapshot = StorageManager |
| 59 |
.snapshot_as_json(glean.storage(), "store1", true) |
| 60 |
.unwrap(); |
| 61 |
|
| 62 |
// We check the exact format to catch changes to the serialization. |
| 63 |
let expected = json!({ |
| 64 |
"sum": duration, |
| 65 |
"values": { |
| 66 |
"58": 1, |
| 67 |
} |
| 68 |
}); |
| 69 |
assert_eq!( |
| 70 |
expected, |
| 71 |
snapshot["timing_distribution"]["telemetry.distribution"] |
| 72 |
); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
#[test] |
| 77 |
fn set_value_properly_sets_the_value_in_all_stores() { |
| 78 |
let (glean, _t) = new_glean(None); |
| 79 |
let store_names: Vec<String> = vec!["store1".into(), "store2".into()]; |
| 80 |
|
| 81 |
let duration = 1; |
| 82 |
|
| 83 |
let metric = TimingDistributionMetric::new( |
| 84 |
CommonMetricData { |
| 85 |
name: "distribution".into(), |
| 86 |
category: "telemetry".into(), |
| 87 |
send_in_pings: store_names.clone(), |
| 88 |
disabled: false, |
| 89 |
lifetime: Lifetime::Ping, |
| 90 |
..Default::default() |
| 91 |
}, |
| 92 |
TimeUnit::Nanosecond, |
| 93 |
); |
| 94 |
|
| 95 |
let id = 4u64.into(); |
| 96 |
metric.set_start(id, 0); |
| 97 |
metric.set_stop_and_accumulate(&glean, id, duration); |
| 98 |
|
| 99 |
// We check the exact format to catch changes to the serialization. |
| 100 |
let expected = json!({ |
| 101 |
"sum": 1, |
| 102 |
"values": { |
| 103 |
"1": 1, |
| 104 |
} |
| 105 |
}); |
| 106 |
for store_name in store_names { |
| 107 |
let snapshot = StorageManager |
| 108 |
.snapshot_as_json(glean.storage(), &store_name, true) |
| 109 |
.unwrap(); |
| 110 |
|
| 111 |
assert_eq!( |
| 112 |
expected, |
| 113 |
snapshot["timing_distribution"]["telemetry.distribution"] |
| 114 |
); |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
#[test] |
| 119 |
fn timing_distributions_must_not_accumulate_negative_values() { |
| 120 |
let (glean, _t) = new_glean(None); |
| 121 |
|
| 122 |
let duration = 60; |
| 123 |
let time_unit = TimeUnit::Nanosecond; |
| 124 |
|
| 125 |
let metric = TimingDistributionMetric::new( |
| 126 |
CommonMetricData { |
| 127 |
name: "distribution".into(), |
| 128 |
category: "telemetry".into(), |
| 129 |
send_in_pings: vec!["store1".into()], |
| 130 |
disabled: false, |
| 131 |
lifetime: Lifetime::Ping, |
| 132 |
..Default::default() |
| 133 |
}, |
| 134 |
time_unit, |
| 135 |
); |
| 136 |
|
| 137 |
// Flip around the timestamps, this should result in a negative value which should be |
| 138 |
// discarded. |
| 139 |
let id = 4u64.into(); |
| 140 |
metric.set_start(id, duration); |
| 141 |
metric.set_stop_and_accumulate(&glean, id, 0); |
| 142 |
|
| 143 |
assert!(metric.get_value(&glean, "store1").is_none()); |
| 144 |
|
| 145 |
// Make sure that the errors have been recorded |
| 146 |
assert_eq!( |
| 147 |
Ok(1), |
| 148 |
test_get_num_recorded_errors(&glean, metric.meta(), ErrorType::InvalidValue) |
| 149 |
); |
| 150 |
} |
| 151 |
|
| 152 |
#[test] |
| 153 |
fn the_accumulate_samples_api_correctly_stores_timing_values() { |
| 154 |
let (glean, _t) = new_glean(None); |
| 155 |
|
| 156 |
let metric = TimingDistributionMetric::new( |
| 157 |
CommonMetricData { |
| 158 |
name: "distribution".into(), |
| 159 |
category: "telemetry".into(), |
| 160 |
send_in_pings: vec!["store1".into()], |
| 161 |
disabled: false, |
| 162 |
lifetime: Lifetime::Ping, |
| 163 |
..Default::default() |
| 164 |
}, |
| 165 |
TimeUnit::Second, |
| 166 |
); |
| 167 |
|
| 168 |
// Accumulate the samples. We intentionally do not report |
| 169 |
// negative values to not trigger error reporting. |
| 170 |
metric.accumulate_samples_sync(&glean, &[1, 2, 3]); |
| 171 |
|
| 172 |
let snapshot = metric |
| 173 |
.get_value(&glean, "store1") |
| 174 |
.expect("Value should be stored"); |
| 175 |
|
| 176 |
let seconds_to_nanos = 1000 * 1000 * 1000; |
| 177 |
|
| 178 |
// Check that we got the right sum. |
| 179 |
assert_eq!(snapshot.sum, 6 * seconds_to_nanos); |
| 180 |
|
| 181 |
// Check that we got the right number of samples. |
| 182 |
assert_eq!(snapshot.count, 3); |
| 183 |
|
| 184 |
// We should get a sample in 3 buckets. |
| 185 |
// These numbers are a bit magic, but they correspond to |
| 186 |
// `hist.sample_to_bucket_minimum(i * seconds_to_nanos)` for `i = 1..=3`. |
| 187 |
assert_eq!(1, snapshot.values[&984625593]); |
| 188 |
assert_eq!(1, snapshot.values[&1969251187]); |
| 189 |
assert_eq!(1, snapshot.values[&2784941737]); |
| 190 |
|
| 191 |
// No errors should be reported. |
| 192 |
assert!(test_get_num_recorded_errors(&glean, metric.meta(), ErrorType::InvalidValue).is_err()); |
| 193 |
} |
| 194 |
|
| 195 |
#[test] |
| 196 |
fn the_accumulate_samples_api_correctly_handles_negative_values() { |
| 197 |
let (glean, _t) = new_glean(None); |
| 198 |
|
| 199 |
let metric = TimingDistributionMetric::new( |
| 200 |
CommonMetricData { |
| 201 |
name: "distribution".into(), |
| 202 |
category: "telemetry".into(), |
| 203 |
send_in_pings: vec!["store1".into()], |
| 204 |
disabled: false, |
| 205 |
lifetime: Lifetime::Ping, |
| 206 |
..Default::default() |
| 207 |
}, |
| 208 |
TimeUnit::Nanosecond, |
| 209 |
); |
| 210 |
|
| 211 |
// Accumulate the samples. |
| 212 |
metric.accumulate_samples_sync(&glean, &[-1, 1, 2, 3]); |
| 213 |
|
| 214 |
let snapshot = metric |
| 215 |
.get_value(&glean, "store1") |
| 216 |
.expect("Value should be stored"); |
| 217 |
|
| 218 |
// Check that we got the right sum. |
| 219 |
assert_eq!(snapshot.sum, 6); |
| 220 |
|
| 221 |
// Check that we got the right number of samples. |
| 222 |
assert_eq!(snapshot.count, 3); |
| 223 |
|
| 224 |
// We should get a sample in each of the first 3 buckets. |
| 225 |
assert_eq!(1, snapshot.values[&1]); |
| 226 |
assert_eq!(1, snapshot.values[&2]); |
| 227 |
assert_eq!(1, snapshot.values[&3]); |
| 228 |
|
| 229 |
// 1 error should be reported. |
| 230 |
assert_eq!( |
| 231 |
Ok(1), |
| 232 |
test_get_num_recorded_errors(&glean, metric.meta(), ErrorType::InvalidValue) |
| 233 |
); |
| 234 |
} |
| 235 |
|
| 236 |
#[test] |
| 237 |
fn the_accumulate_samples_api_correctly_handles_overflowing_values() { |
| 238 |
let (glean, _t) = new_glean(None); |
| 239 |
|
| 240 |
let metric = TimingDistributionMetric::new( |
| 241 |
CommonMetricData { |
| 242 |
name: "distribution".into(), |
| 243 |
category: "telemetry".into(), |
| 244 |
send_in_pings: vec!["store1".into()], |
| 245 |
disabled: false, |
| 246 |
lifetime: Lifetime::Ping, |
| 247 |
..Default::default() |
| 248 |
}, |
| 249 |
TimeUnit::Nanosecond, |
| 250 |
); |
| 251 |
|
| 252 |
// The MAX_SAMPLE_TIME is the same from `metrics/timing_distribution.rs`. |
| 253 |
const MAX_SAMPLE_TIME: u64 = 1000 * 1000 * 1000 * 60 * 10; |
| 254 |
let overflowing_val = MAX_SAMPLE_TIME as i64 + 1; |
| 255 |
// Accumulate the samples. |
| 256 |
metric.accumulate_samples_sync(&glean, &[overflowing_val, 1, 2, 3]); |
| 257 |
|
| 258 |
let snapshot = metric |
| 259 |
.get_value(&glean, "store1") |
| 260 |
.expect("Value should be stored"); |
| 261 |
|
| 262 |
// Overflowing values are truncated to MAX_SAMPLE_TIME and recorded. |
| 263 |
assert_eq!(snapshot.sum as u64, MAX_SAMPLE_TIME + 6); |
| 264 |
|
| 265 |
// Check that we got the right number of samples. |
| 266 |
assert_eq!(snapshot.count, 4); |
| 267 |
|
| 268 |
// We should get a sample in each of the first 3 buckets. |
| 269 |
assert_eq!(1, snapshot.values[&1]); |
| 270 |
assert_eq!(1, snapshot.values[&2]); |
| 271 |
assert_eq!(1, snapshot.values[&3]); |
| 272 |
|
| 273 |
// 1 error should be reported. |
| 274 |
assert_eq!( |
| 275 |
Ok(1), |
| 276 |
test_get_num_recorded_errors(&glean, metric.meta(), ErrorType::InvalidOverflow) |
| 277 |
); |
| 278 |
} |
| 279 |
|
| 280 |
#[test] |
| 281 |
fn large_nanoseconds_values() { |
| 282 |
let (glean, _t) = new_glean(None); |
| 283 |
|
| 284 |
let metric = TimingDistributionMetric::new( |
| 285 |
CommonMetricData { |
| 286 |
name: "distribution".into(), |
| 287 |
category: "telemetry".into(), |
| 288 |
send_in_pings: vec!["store1".into()], |
| 289 |
disabled: false, |
| 290 |
lifetime: Lifetime::Ping, |
| 291 |
..Default::default() |
| 292 |
}, |
| 293 |
TimeUnit::Nanosecond, |
| 294 |
); |
| 295 |
|
| 296 |
let time = Duration::from_secs(10).as_nanos() as u64; |
| 297 |
assert!(time > u64::from(u32::MAX)); |
| 298 |
|
| 299 |
let id = 4u64.into(); |
| 300 |
metric.set_start(id, 0); |
| 301 |
metric.set_stop_and_accumulate(&glean, id, time); |
| 302 |
|
| 303 |
let val = metric |
| 304 |
.get_value(&glean, "store1") |
| 305 |
.expect("Value should be stored"); |
| 306 |
|
| 307 |
// Check that we got the right sum and number of samples. |
| 308 |
assert_eq!(val.sum, time as i64); |
| 309 |
} |
| 310 |
|
| 311 |
#[test] |
| 312 |
fn stopping_non_existing_id_records_an_error() { |
| 313 |
let (glean, _t) = new_glean(None); |
| 314 |
|
| 315 |
let metric = TimingDistributionMetric::new( |
| 316 |
CommonMetricData { |
| 317 |
name: "non_existing_id".into(), |
| 318 |
category: "test".into(), |
| 319 |
send_in_pings: vec!["store1".into()], |
| 320 |
disabled: false, |
| 321 |
lifetime: Lifetime::Ping, |
| 322 |
..Default::default() |
| 323 |
}, |
| 324 |
TimeUnit::Nanosecond, |
| 325 |
); |
| 326 |
|
| 327 |
let id = 3785u64.into(); |
| 328 |
metric.set_stop_and_accumulate(&glean, id, 60); |
| 329 |
|
| 330 |
// 1 error should be reported. |
| 331 |
assert_eq!( |
| 332 |
Ok(1), |
| 333 |
test_get_num_recorded_errors(&glean, metric.meta(), ErrorType::InvalidState) |
| 334 |
); |
| 335 |
} |
| 336 |
|
| 337 |
#[test] |
| 338 |
fn the_accumulate_raw_samples_api_correctly_stores_timing_values() { |
| 339 |
let (glean, _t) = new_glean(None); |
| 340 |
|
| 341 |
let metric = TimingDistributionMetric::new( |
| 342 |
CommonMetricData { |
| 343 |
name: "distribution".into(), |
| 344 |
category: "telemetry".into(), |
| 345 |
send_in_pings: vec!["store1".into()], |
| 346 |
disabled: false, |
| 347 |
lifetime: Lifetime::Ping, |
| 348 |
..Default::default() |
| 349 |
}, |
| 350 |
TimeUnit::Second, |
| 351 |
); |
| 352 |
|
| 353 |
let seconds_to_nanos = 1000 * 1000 * 1000; |
| 354 |
metric.accumulate_raw_samples_nanos_sync( |
| 355 |
&glean, |
| 356 |
[seconds_to_nanos, 2 * seconds_to_nanos, 3 * seconds_to_nanos].as_ref(), |
| 357 |
); |
| 358 |
|
| 359 |
let snapshot = metric |
| 360 |
.get_value(&glean, "store1") |
| 361 |
.expect("Value should be stored"); |
| 362 |
|
| 363 |
// Check that we got the right sum. |
| 364 |
assert_eq!(snapshot.sum, 6 * seconds_to_nanos as i64); |
| 365 |
|
| 366 |
// Check that we got the right number of samples. |
| 367 |
assert_eq!(snapshot.count, 3); |
| 368 |
|
| 369 |
// We should get a sample in 3 buckets. |
| 370 |
// These numbers are a bit magic, but they correspond to |
| 371 |
// `hist.sample_to_bucket_minimum(i * seconds_to_nanos)` for `i = 1..=3`. |
| 372 |
assert_eq!(1, snapshot.values[&984625593]); |
| 373 |
assert_eq!(1, snapshot.values[&1969251187]); |
| 374 |
assert_eq!(1, snapshot.values[&2784941737]); |
| 375 |
|
| 376 |
// No errors should be reported. |
| 377 |
assert!(test_get_num_recorded_errors(&glean, metric.meta(), ErrorType::InvalidState).is_err()); |
| 378 |
} |
| 379 |
|
| 380 |
#[test] |
| 381 |
fn raw_samples_api_error_cases() { |
| 382 |
let (glean, _t) = new_glean(None); |
| 383 |
|
| 384 |
let metric = TimingDistributionMetric::new( |
| 385 |
CommonMetricData { |
| 386 |
name: "distribution".into(), |
| 387 |
category: "telemetry".into(), |
| 388 |
send_in_pings: vec!["store1".into()], |
| 389 |
disabled: false, |
| 390 |
lifetime: Lifetime::Ping, |
| 391 |
..Default::default() |
| 392 |
}, |
| 393 |
TimeUnit::Nanosecond, |
| 394 |
); |
| 395 |
|
| 396 |
// 10minutes in nanoseconds |
| 397 |
let max_sample_time = 1000 * 1000 * 1000 * 60 * 10; |
| 398 |
|
| 399 |
metric.accumulate_raw_samples_nanos_sync( |
| 400 |
&glean, |
| 401 |
&[ |
| 402 |
0, /* rounded up to 1 */ |
| 403 |
1, /* valid */ |
| 404 |
max_sample_time + 1, /* larger then the maximum, will record an error and the maximum */ |
| 405 |
], |
| 406 |
); |
| 407 |
|
| 408 |
let snapshot = metric |
| 409 |
.get_value(&glean, "store1") |
| 410 |
.expect("Value should be stored"); |
| 411 |
|
| 412 |
// Check that we got the right sum. |
| 413 |
assert_eq!(snapshot.sum, 2 + max_sample_time as i64); |
| 414 |
|
| 415 |
// Check that we got the right number of samples. |
| 416 |
assert_eq!(snapshot.count, 3); |
| 417 |
|
| 418 |
// We should get a sample in 3 buckets. |
| 419 |
// These numbers are a bit magic, but they correspond to |
| 420 |
// `hist.sample_to_bucket_minimum(i * seconds_to_nanos)` for `i = {1, max_sample_time}`. |
| 421 |
assert_eq!(2, snapshot.values[&1]); |
| 422 |
assert_eq!(1, snapshot.values[&599512966122]); |
| 423 |
|
| 424 |
// 1 error should be reported. |
| 425 |
assert_eq!( |
| 426 |
Ok(1), |
| 427 |
test_get_num_recorded_errors(&glean, metric.meta(), ErrorType::InvalidOverflow) |
| 428 |
); |
| 429 |
} |
| 430 |
|
| 431 |
#[test] |
| 432 |
fn timing_distribution_is_tracked_across_upload_toggle() { |
| 433 |
let (mut glean, _t) = new_glean(None); |
| 434 |
|
| 435 |
let metric = TimingDistributionMetric::new( |
| 436 |
CommonMetricData { |
| 437 |
name: "distribution".into(), |
| 438 |
category: "telemetry".into(), |
| 439 |
send_in_pings: vec!["store1".into()], |
| 440 |
disabled: false, |
| 441 |
lifetime: Lifetime::Ping, |
| 442 |
..Default::default() |
| 443 |
}, |
| 444 |
TimeUnit::Nanosecond, |
| 445 |
); |
| 446 |
|
| 447 |
let id = 4u64.into(); |
| 448 |
let duration = 100; |
| 449 |
|
| 450 |
// Timer is started. |
| 451 |
metric.set_start(id, 0); |
| 452 |
// User disables telemetry upload. |
| 453 |
glean.set_upload_enabled(false); |
| 454 |
// App code eventually stops the timer. |
| 455 |
// We should clear internal state as upload is disabled. |
| 456 |
metric.set_stop_and_accumulate(&glean, id, duration); |
| 457 |
|
| 458 |
assert_eq!(None, metric.get_value(&glean, "store1")); |
| 459 |
|
| 460 |
// App code eventually starts the timer again. |
| 461 |
// Upload is disabled, so this should not have any effect. |
| 462 |
metric.set_start(id, 100); |
| 463 |
// User enables telemetry upload again. |
| 464 |
glean.set_upload_enabled(true); |
| 465 |
// App code eventually stops the timer. |
| 466 |
// The full timespan is recorded. |
| 467 |
metric.set_stop_and_accumulate(&glean, id, 100 + duration); |
| 468 |
|
| 469 |
let data = metric.get_value(&glean, "store1").unwrap(); |
| 470 |
assert_eq!(1, data.count); |
| 471 |
assert_eq!(100, data.sum); |
| 472 |
|
| 473 |
// Make sure that the error has been recorded |
| 474 |
assert!(test_get_num_recorded_errors(&glean, metric.meta(), ErrorType::InvalidState).is_err()); |
| 475 |
} |