| 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::collections::HashMap; |
| 9 |
use std::fs; |
| 10 |
|
| 11 |
use serde_json::json; |
| 12 |
|
| 13 |
use glean_core::{ |
| 14 |
get_timestamp_ms, test_get_num_recorded_errors, CommonMetricData, ErrorType, Lifetime, |
| 15 |
}; |
| 16 |
use glean_core::{metrics::*, Glean}; |
| 17 |
|
| 18 |
#[test] |
| 19 |
fn record_properly_records_without_optional_arguments() { |
| 20 |
let store_names = vec!["store1".into(), "store2".into()]; |
| 21 |
|
| 22 |
let (glean, _t) = new_glean(None); |
| 23 |
|
| 24 |
let metric = EventMetric::new( |
| 25 |
CommonMetricData { |
| 26 |
name: "test_event_no_optional".into(), |
| 27 |
category: "telemetry".into(), |
| 28 |
send_in_pings: store_names.clone(), |
| 29 |
disabled: false, |
| 30 |
lifetime: Lifetime::Ping, |
| 31 |
..Default::default() |
| 32 |
}, |
| 33 |
vec![], |
| 34 |
); |
| 35 |
|
| 36 |
metric.record_sync(&glean, 1000, HashMap::new(), 0); |
| 37 |
|
| 38 |
for store_name in store_names { |
| 39 |
let events = metric.get_value(&glean, &*store_name).unwrap(); |
| 40 |
assert_eq!(1, events.len()); |
| 41 |
assert_eq!("telemetry", events[0].category); |
| 42 |
assert_eq!("test_event_no_optional", events[0].name); |
| 43 |
assert!(events[0].extra.is_none()); |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
#[test] |
| 48 |
fn record_properly_records_with_optional_arguments() { |
| 49 |
let (glean, _t) = new_glean(None); |
| 50 |
|
| 51 |
let store_names = vec!["store1".into(), "store2".into()]; |
| 52 |
|
| 53 |
let metric = EventMetric::new( |
| 54 |
CommonMetricData { |
| 55 |
name: "test_event_no_optional".into(), |
| 56 |
category: "telemetry".into(), |
| 57 |
send_in_pings: store_names.clone(), |
| 58 |
disabled: false, |
| 59 |
lifetime: Lifetime::Ping, |
| 60 |
..Default::default() |
| 61 |
}, |
| 62 |
vec!["key1".into(), "key2".into()], |
| 63 |
); |
| 64 |
|
| 65 |
let extra = [ |
| 66 |
("key1".into(), "value1".into()), |
| 67 |
("key2".into(), "value2".into()), |
| 68 |
] |
| 69 |
.iter() |
| 70 |
.cloned() |
| 71 |
.collect(); |
| 72 |
|
| 73 |
metric.record_sync(&glean, 1000, extra, 0); |
| 74 |
|
| 75 |
for store_name in store_names { |
| 76 |
let events = metric.get_value(&glean, &*store_name).unwrap(); |
| 77 |
let event = events[0].clone(); |
| 78 |
assert_eq!(1, events.len()); |
| 79 |
assert_eq!("telemetry", event.category); |
| 80 |
assert_eq!("test_event_no_optional", event.name); |
| 81 |
let extra = event.extra.unwrap(); |
| 82 |
assert_eq!(2, extra.len()); |
| 83 |
assert_eq!("value1", extra["key1"]); |
| 84 |
assert_eq!("value2", extra["key2"]); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
// SKIPPED record() computes the correct time between events |
| 89 |
// Timing is now handled in the language-specific part. |
| 90 |
|
| 91 |
#[test] |
| 92 |
fn snapshot_returns_none_if_nothing_is_recorded_in_the_store() { |
| 93 |
let (glean, _t) = new_glean(None); |
| 94 |
|
| 95 |
assert!(glean |
| 96 |
.event_storage() |
| 97 |
.snapshot_as_json(&glean, "store1", false) |
| 98 |
.is_none()) |
| 99 |
} |
| 100 |
|
| 101 |
#[test] |
| 102 |
fn snapshot_correctly_clears_the_stores() { |
| 103 |
let (glean, _t) = new_glean(None); |
| 104 |
|
| 105 |
let store_names = vec!["store1".into(), "store2".into()]; |
| 106 |
|
| 107 |
let metric = EventMetric::new( |
| 108 |
CommonMetricData { |
| 109 |
name: "test_event_clear".into(), |
| 110 |
category: "telemetry".into(), |
| 111 |
send_in_pings: store_names, |
| 112 |
disabled: false, |
| 113 |
lifetime: Lifetime::Ping, |
| 114 |
..Default::default() |
| 115 |
}, |
| 116 |
vec![], |
| 117 |
); |
| 118 |
|
| 119 |
metric.record_sync(&glean, 1000, HashMap::new(), 0); |
| 120 |
|
| 121 |
let snapshot = glean |
| 122 |
.event_storage() |
| 123 |
.snapshot_as_json(&glean, "store1", true); |
| 124 |
assert!(snapshot.is_some()); |
| 125 |
|
| 126 |
assert!(glean |
| 127 |
.event_storage() |
| 128 |
.snapshot_as_json(&glean, "store1", false) |
| 129 |
.is_none()); |
| 130 |
|
| 131 |
let files: Vec<fs::DirEntry> = fs::read_dir(&glean.event_storage().path) |
| 132 |
.unwrap() |
| 133 |
.filter_map(|x| x.ok()) |
| 134 |
.collect(); |
| 135 |
assert_eq!(1, files.len()); |
| 136 |
assert_eq!("store2", files[0].file_name()); |
| 137 |
|
| 138 |
let snapshot2 = glean |
| 139 |
.event_storage() |
| 140 |
.snapshot_as_json(&glean, "store2", false); |
| 141 |
for s in [snapshot, snapshot2] { |
| 142 |
assert!(s.is_some()); |
| 143 |
let s = s.unwrap(); |
| 144 |
assert_eq!(1, s.as_array().unwrap().len()); |
| 145 |
assert_eq!("telemetry", s[0]["category"]); |
| 146 |
assert_eq!("test_event_clear", s[0]["name"]); |
| 147 |
assert!(s[0].get("extra").is_none()); |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
// SKIPPED: Events are serialized in the correct JSON format (no extra) |
| 152 |
// SKIPPED: Events are serialized in the correct JSON format (with extra) |
| 153 |
// This test won't work as-is since Rust doesn't maintain the insertion order in |
| 154 |
// a JSON object, therefore you can't check the JSON output directly against a |
| 155 |
// string. This check is redundant with other tests, anyway, and checking against |
| 156 |
// the schema is much more useful. |
| 157 |
|
| 158 |
#[test] |
| 159 |
fn test_sending_of_event_ping_when_it_fills_up() { |
| 160 |
let (mut glean, _t) = new_glean(None); |
| 161 |
|
| 162 |
let store_names: Vec<String> = vec!["events".into()]; |
| 163 |
|
| 164 |
for store_name in &store_names { |
| 165 |
glean.register_ping_type( |
| 166 |
&PingBuilder::new(store_name) |
| 167 |
.with_reasons(vec!["max_capacity".to_string()]) |
| 168 |
.build(), |
| 169 |
); |
| 170 |
} |
| 171 |
|
| 172 |
let click = EventMetric::new( |
| 173 |
CommonMetricData { |
| 174 |
name: "click".into(), |
| 175 |
category: "ui".into(), |
| 176 |
send_in_pings: store_names, |
| 177 |
disabled: false, |
| 178 |
lifetime: Lifetime::Ping, |
| 179 |
..Default::default() |
| 180 |
}, |
| 181 |
vec!["test_event_number".into()], |
| 182 |
); |
| 183 |
|
| 184 |
// We send 510 events. We expect to get the first 500 in the ping and 10 |
| 185 |
// remaining afterward |
| 186 |
for i in 0..510 { |
| 187 |
let mut extra = HashMap::new(); |
| 188 |
extra.insert("test_event_number".to_string(), i.to_string()); |
| 189 |
click.record_sync(&glean, i, extra, 0); |
| 190 |
} |
| 191 |
|
| 192 |
assert_eq!(10, click.get_value(&glean, "events").unwrap().len()); |
| 193 |
|
| 194 |
let (url, json, _) = &get_queued_pings(glean.get_data_path()).unwrap()[0]; |
| 195 |
assert!(url.starts_with(format!("/submit/{}/events/", glean.get_application_id()).as_str())); |
| 196 |
assert_eq!(500, json["events"].as_array().unwrap().len()); |
| 197 |
assert_eq!( |
| 198 |
"max_capacity", |
| 199 |
json["ping_info"].as_object().unwrap()["reason"] |
| 200 |
.as_str() |
| 201 |
.unwrap() |
| 202 |
); |
| 203 |
|
| 204 |
for i in 0..500 { |
| 205 |
let event = &json["events"].as_array().unwrap()[i]; |
| 206 |
assert_eq!(i.to_string(), event["extra"]["test_event_number"]); |
| 207 |
} |
| 208 |
|
| 209 |
let snapshot = glean |
| 210 |
.event_storage() |
| 211 |
.snapshot_as_json(&glean, "events", false) |
| 212 |
.unwrap(); |
| 213 |
assert_eq!(10, snapshot.as_array().unwrap().len()); |
| 214 |
for i in 0..10 { |
| 215 |
let event = &snapshot.as_array().unwrap()[i]; |
| 216 |
assert_eq!((i + 500).to_string(), event["extra"]["test_event_number"]); |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
#[test] |
| 221 |
fn test_server_knobs_config_changing_max_events() { |
| 222 |
let (mut glean, _t) = new_glean(None); |
| 223 |
|
| 224 |
let store_names: Vec<String> = vec!["events".into()]; |
| 225 |
|
| 226 |
for store_name in &store_names { |
| 227 |
glean.register_ping_type( |
| 228 |
&PingBuilder::new(store_name) |
| 229 |
.with_reasons(vec!["max_capacity".to_string()]) |
| 230 |
.build(), |
| 231 |
); |
| 232 |
} |
| 233 |
|
| 234 |
// 1. Set up an event to record |
| 235 |
let click = EventMetric::new( |
| 236 |
CommonMetricData { |
| 237 |
name: "click".into(), |
| 238 |
category: "ui".into(), |
| 239 |
send_in_pings: store_names, |
| 240 |
disabled: false, |
| 241 |
lifetime: Lifetime::Ping, |
| 242 |
..Default::default() |
| 243 |
}, |
| 244 |
vec!["test_event_number".into()], |
| 245 |
); |
| 246 |
|
| 247 |
// 2. Set a Server Knobs configuration to disable the metrics |
| 248 |
let remote_settings_config = json!( |
| 249 |
{ |
| 250 |
"event_threshold": 50 |
| 251 |
} |
| 252 |
) |
| 253 |
.to_string(); |
| 254 |
glean |
| 255 |
.apply_server_knobs_config(RemoteSettingsConfig::try_from(remote_settings_config).unwrap()); |
| 256 |
|
| 257 |
// 3. Record 51 events. We expect to get the first 50 in the first ping and 1 |
| 258 |
// remaining afterward |
| 259 |
for i in 0..51 { |
| 260 |
let mut extra = HashMap::new(); |
| 261 |
extra.insert("test_event_number".to_string(), i.to_string()); |
| 262 |
click.record_sync(&glean, i, extra, 0); |
| 263 |
} |
| 264 |
|
| 265 |
assert_eq!(1, click.get_value(&glean, "events").unwrap().len()); |
| 266 |
|
| 267 |
let (url, json, _) = &get_queued_pings(glean.get_data_path()).unwrap()[0]; |
| 268 |
assert!(url.starts_with(format!("/submit/{}/events/", glean.get_application_id()).as_str())); |
| 269 |
assert_eq!(50, json["events"].as_array().unwrap().len()); |
| 270 |
assert_eq!( |
| 271 |
"max_capacity", |
| 272 |
json["ping_info"].as_object().unwrap()["reason"] |
| 273 |
.as_str() |
| 274 |
.unwrap() |
| 275 |
); |
| 276 |
|
| 277 |
for i in 0..50 { |
| 278 |
let event = &json["events"].as_array().unwrap()[i]; |
| 279 |
assert_eq!(i.to_string(), event["extra"]["test_event_number"]); |
| 280 |
} |
| 281 |
|
| 282 |
let snapshot = glean |
| 283 |
.event_storage() |
| 284 |
.snapshot_as_json(&glean, "events", false) |
| 285 |
.unwrap(); |
| 286 |
assert_eq!(1, snapshot.as_array().unwrap().len()); |
| 287 |
let event = &snapshot.as_array().unwrap()[0]; |
| 288 |
assert_eq!(50.to_string(), event["extra"]["test_event_number"]); |
| 289 |
} |
| 290 |
|
| 291 |
#[test] |
| 292 |
fn extra_keys_must_be_recorded_and_truncated_if_needed() { |
| 293 |
let (glean, _t) = new_glean(None); |
| 294 |
|
| 295 |
let store_names: Vec<String> = vec!["store1".into()]; |
| 296 |
|
| 297 |
let test_event = EventMetric::new( |
| 298 |
CommonMetricData { |
| 299 |
name: "testEvent".into(), |
| 300 |
category: "ui".into(), |
| 301 |
send_in_pings: store_names, |
| 302 |
disabled: false, |
| 303 |
lifetime: Lifetime::Ping, |
| 304 |
..Default::default() |
| 305 |
}, |
| 306 |
vec!["extra1".into(), "truncatedExtra".into()], |
| 307 |
); |
| 308 |
|
| 309 |
let test_value = "LeanGleanByFrank"; |
| 310 |
let test_value_long = test_value.to_string().repeat(32); |
| 311 |
// max length for extra values. |
| 312 |
let test_value_cap = 500; |
| 313 |
assert!( |
| 314 |
test_value_long.len() > test_value_cap, |
| 315 |
"test value is not long enough" |
| 316 |
); |
| 317 |
let mut extra = HashMap::new(); |
| 318 |
extra.insert("extra1".into(), test_value.to_string()); |
| 319 |
extra.insert("truncatedExtra".into(), test_value_long.clone()); |
| 320 |
|
| 321 |
test_event.record_sync(&glean, 0, extra, 0); |
| 322 |
|
| 323 |
let snapshot = glean |
| 324 |
.event_storage() |
| 325 |
.snapshot_as_json(&glean, "store1", false) |
| 326 |
.unwrap(); |
| 327 |
assert_eq!(1, snapshot.as_array().unwrap().len()); |
| 328 |
let event = &snapshot.as_array().unwrap()[0]; |
| 329 |
assert_eq!("ui", event["category"]); |
| 330 |
assert_eq!("testEvent", event["name"]); |
| 331 |
assert_eq!(2, event["extra"].as_object().unwrap().len()); |
| 332 |
assert_eq!(test_value, event["extra"]["extra1"]); |
| 333 |
assert_eq!( |
| 334 |
test_value_long[0..test_value_cap], |
| 335 |
event["extra"]["truncatedExtra"] |
| 336 |
); |
| 337 |
} |
| 338 |
|
| 339 |
#[test] |
| 340 |
fn snapshot_sorts_the_timestamps() { |
| 341 |
let (glean, _t) = new_glean(None); |
| 342 |
|
| 343 |
let metric = EventMetric::new( |
| 344 |
CommonMetricData { |
| 345 |
name: "test_event_clear".into(), |
| 346 |
category: "telemetry".into(), |
| 347 |
send_in_pings: vec!["store1".into()], |
| 348 |
disabled: false, |
| 349 |
lifetime: Lifetime::Ping, |
| 350 |
..Default::default() |
| 351 |
}, |
| 352 |
vec![], |
| 353 |
); |
| 354 |
|
| 355 |
metric.record_sync(&glean, 1000, HashMap::new(), 0); |
| 356 |
metric.record_sync(&glean, 100, HashMap::new(), 0); |
| 357 |
metric.record_sync(&glean, 10000, HashMap::new(), 0); |
| 358 |
|
| 359 |
let snapshot = glean |
| 360 |
.event_storage() |
| 361 |
.snapshot_as_json(&glean, "store1", true) |
| 362 |
.unwrap(); |
| 363 |
|
| 364 |
assert_eq!( |
| 365 |
0, |
| 366 |
snapshot.as_array().unwrap()[0]["timestamp"] |
| 367 |
.as_i64() |
| 368 |
.unwrap() |
| 369 |
); |
| 370 |
assert_eq!( |
| 371 |
900, |
| 372 |
snapshot.as_array().unwrap()[1]["timestamp"] |
| 373 |
.as_i64() |
| 374 |
.unwrap() |
| 375 |
); |
| 376 |
assert_eq!( |
| 377 |
9900, |
| 378 |
snapshot.as_array().unwrap()[2]["timestamp"] |
| 379 |
.as_i64() |
| 380 |
.unwrap() |
| 381 |
); |
| 382 |
} |
| 383 |
|
| 384 |
#[test] |
| 385 |
fn ensure_custom_ping_events_dont_overflow() { |
| 386 |
let (glean, _dir) = new_glean(None); |
| 387 |
|
| 388 |
let store_name = "store1"; |
| 389 |
let event_meta = CommonMetricData { |
| 390 |
name: "name".into(), |
| 391 |
category: "category".into(), |
| 392 |
send_in_pings: vec![store_name.into()], |
| 393 |
lifetime: Lifetime::Ping, |
| 394 |
..Default::default() |
| 395 |
}; |
| 396 |
let event = EventMetric::new(event_meta.clone(), vec![]); |
| 397 |
|
| 398 |
assert!(test_get_num_recorded_errors( |
| 399 |
&glean, |
| 400 |
&(event_meta.clone()).into(), |
| 401 |
ErrorType::InvalidOverflow |
| 402 |
) |
| 403 |
.is_err()); |
| 404 |
|
| 405 |
for _ in 0..500 { |
| 406 |
event.record_sync(&glean, 0, HashMap::new(), 0); |
| 407 |
} |
| 408 |
assert!(test_get_num_recorded_errors( |
| 409 |
&glean, |
| 410 |
&(event_meta.clone()).into(), |
| 411 |
ErrorType::InvalidOverflow |
| 412 |
) |
| 413 |
.is_err()); |
| 414 |
|
| 415 |
// That should top us right up to the limit. Now for going over. |
| 416 |
event.record_sync(&glean, 0, HashMap::new(), 0); |
| 417 |
assert!( |
| 418 |
test_get_num_recorded_errors(&glean, &event_meta.into(), ErrorType::InvalidOverflow) |
| 419 |
.is_err() |
| 420 |
); |
| 421 |
assert_eq!(501, event.get_value(&glean, store_name).unwrap().len()); |
| 422 |
} |
| 423 |
|
| 424 |
/// Ensure that events from multiple runs serialize properly. |
| 425 |
/// |
| 426 |
/// Records an event once each in two separate sessions, |
| 427 |
/// ensuring they both show (and an inserted `glean.restarted` event) |
| 428 |
/// in the serialized result. |
| 429 |
#[test] |
| 430 |
fn ensure_custom_ping_events_from_multiple_runs_work() { |
| 431 |
let (mut tempdir, _) = tempdir(); |
| 432 |
|
| 433 |
let store_name = "store1"; |
| 434 |
let event = EventMetric::new( |
| 435 |
CommonMetricData { |
| 436 |
name: "name".into(), |
| 437 |
category: "category".into(), |
| 438 |
send_in_pings: vec![store_name.into()], |
| 439 |
lifetime: Lifetime::Ping, |
| 440 |
..Default::default() |
| 441 |
}, |
| 442 |
vec![], |
| 443 |
); |
| 444 |
|
| 445 |
{ |
| 446 |
let (glean, dir) = new_glean(Some(tempdir)); |
| 447 |
// We don't need a full init. Just to deal with on-disk events: |
| 448 |
assert!(!glean |
| 449 |
.event_storage() |
| 450 |
.flush_pending_events_on_startup(&glean, false)); |
| 451 |
tempdir = dir; |
| 452 |
|
| 453 |
event.record_sync(&glean, 10, HashMap::new(), 0); |
| 454 |
} |
| 455 |
|
| 456 |
{ |
| 457 |
let (glean, _dir) = new_glean(Some(tempdir)); |
| 458 |
// We don't need a full init. Just to deal with on-disk events: |
| 459 |
assert!(!glean |
| 460 |
.event_storage() |
| 461 |
.flush_pending_events_on_startup(&glean, false)); |
| 462 |
|
| 463 |
// Gotta use get_timestamp_ms or this event won't happen "after" the injected |
| 464 |
// glean.restarted event from `flush_pending_events_on_startup`. |
| 465 |
event.record_sync(&glean, get_timestamp_ms(), HashMap::new(), 0); |
| 466 |
|
| 467 |
let json = glean |
| 468 |
.event_storage() |
| 469 |
.snapshot_as_json(&glean, store_name, false) |
| 470 |
.unwrap(); |
| 471 |
assert_eq!(json.as_array().unwrap().len(), 3); |
| 472 |
assert_eq!(json[0]["category"], "category"); |
| 473 |
assert_eq!(json[0]["name"], "name"); |
| 474 |
assert_eq!(json[1]["category"], "glean"); |
| 475 |
assert_eq!(json[1]["name"], "restarted"); |
| 476 |
assert_eq!(json[2]["category"], "category"); |
| 477 |
assert_eq!(json[2]["name"], "name"); |
| 478 |
} |
| 479 |
} |
| 480 |
|
| 481 |
/// Ensure events in an unregistered, non-"events" (ie Custom) ping are trimmed on a subsequent init |
| 482 |
/// when we pass `true ` for `trim_data_to_registered_pings` in `on_ready_to_submit_pings`. |
| 483 |
#[test] |
| 484 |
fn event_storage_trimming() { |
| 485 |
let (mut tempdir, _) = tempdir(); |
| 486 |
|
| 487 |
let store_name = "store-name"; |
| 488 |
let store_name_2 = "store-name-2"; |
| 489 |
let event = EventMetric::new( |
| 490 |
CommonMetricData { |
| 491 |
name: "name".into(), |
| 492 |
category: "category".into(), |
| 493 |
send_in_pings: vec![store_name.into(), store_name_2.into()], |
| 494 |
lifetime: Lifetime::Ping, |
| 495 |
..Default::default() |
| 496 |
}, |
| 497 |
vec![], |
| 498 |
); |
| 499 |
|
| 500 |
let new_ping = |glean: &mut Glean, ping: &str| { |
| 501 |
// In Rust, pings are registered via construction. |
| 502 |
// But that's done asynchronously, so we do it synchronously here: |
| 503 |
glean.register_ping_type(&PingBuilder::new(ping).build()); |
| 504 |
}; |
| 505 |
|
| 506 |
// First, register both pings, so that we can record the event in the two pings. |
| 507 |
{ |
| 508 |
let (mut glean, dir) = new_glean(Some(tempdir)); |
| 509 |
tempdir = dir; |
| 510 |
|
| 511 |
new_ping(&mut glean, store_name); |
| 512 |
new_ping(&mut glean, store_name_2); |
| 513 |
|
| 514 |
event.record_sync(&glean, 10, HashMap::new(), 0); |
| 515 |
|
| 516 |
assert_eq!(1, event.get_value(&glean, store_name).unwrap().len()); |
| 517 |
assert_eq!(1, event.get_value(&glean, store_name_2).unwrap().len()); |
| 518 |
} |
| 519 |
// Second, construct (but don't init) Glean again. |
| 520 |
// Register exactly one of the two pings. |
| 521 |
// Then process the part of init that does the trimming (`on_ready_to_submit_pings`). |
| 522 |
{ |
| 523 |
let (mut glean, _dir) = new_glean(Some(tempdir)); |
| 524 |
new_ping(&mut glean, store_name); |
| 525 |
|
| 526 |
glean.on_ready_to_submit_pings(true); |
| 527 |
|
| 528 |
assert_eq!(1, event.get_value(&glean, store_name).unwrap().len()); |
| 529 |
assert!(event.get_value(&glean, store_name_2).is_none()); |
| 530 |
} |
| 531 |
} |
| 532 |
|
| 533 |
#[test] |
| 534 |
fn with_event_timestamps() { |
| 535 |
use glean_core::{Glean, InternalConfiguration}; |
| 536 |
|
| 537 |
let dir = tempfile::tempdir().unwrap(); |
| 538 |
let cfg = InternalConfiguration { |
| 539 |
data_path: dir.path().display().to_string(), |
| 540 |
application_id: GLOBAL_APPLICATION_ID.into(), |
| 541 |
language_binding_name: "Rust".into(), |
| 542 |
upload_enabled: true, |
| 543 |
max_events: None, |
| 544 |
delay_ping_lifetime_io: false, |
| 545 |
app_build: "Unknown".into(), |
| 546 |
use_core_mps: false, |
| 547 |
trim_data_to_registered_pings: false, |
| 548 |
log_level: None, |
| 549 |
rate_limit: None, |
| 550 |
enable_event_timestamps: true, |
| 551 |
experimentation_id: None, // Enabling event timestamps |
| 552 |
enable_internal_pings: true, |
| 553 |
ping_schedule: Default::default(), |
| 554 |
ping_lifetime_threshold: 0, |
| 555 |
ping_lifetime_max_time: 0, |
| 556 |
}; |
| 557 |
let mut glean = Glean::new(cfg).unwrap(); |
| 558 |
let ping = PingBuilder::new("store1").build(); |
| 559 |
glean.register_ping_type(&ping); |
| 560 |
|
| 561 |
let store_name = "store1"; |
| 562 |
let event = EventMetric::new( |
| 563 |
CommonMetricData { |
| 564 |
name: "name".into(), |
| 565 |
category: "category".into(), |
| 566 |
send_in_pings: vec![store_name.into()], |
| 567 |
lifetime: Lifetime::Ping, |
| 568 |
..Default::default() |
| 569 |
}, |
| 570 |
vec![], |
| 571 |
); |
| 572 |
|
| 573 |
event.record_sync(&glean, get_timestamp_ms(), HashMap::new(), 12345); |
| 574 |
|
| 575 |
let json = glean |
| 576 |
.event_storage() |
| 577 |
.snapshot_as_json(&glean, store_name, false) |
| 578 |
.unwrap(); |
| 579 |
assert_eq!(json.as_array().unwrap().len(), 1); |
| 580 |
assert_eq!(json[0]["category"], "category"); |
| 581 |
assert_eq!(json[0]["name"], "name"); |
| 582 |
|
| 583 |
let glean_timestamp = json[0]["extra"]["glean_timestamp"] |
| 584 |
.as_str() |
| 585 |
.expect("glean_timestamp should exist"); |
| 586 |
let glean_timestamp: i64 = glean_timestamp |
| 587 |
.parse() |
| 588 |
.expect("glean_timestamp should be an integer"); |
| 589 |
assert_eq!(12345, glean_timestamp); |
| 590 |
} |