| 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 |
//! Ping collection, assembly & submission. |
| 6 |
|
| 7 |
use std::fs::{self, create_dir_all, File}; |
| 8 |
use std::io::{BufRead, BufReader, Write}; |
| 9 |
use std::path::{Path, PathBuf}; |
| 10 |
|
| 11 |
use log::info; |
| 12 |
use serde_json::{json, Value as JsonValue}; |
| 13 |
|
| 14 |
use crate::common_metric_data::{CommonMetricData, Lifetime}; |
| 15 |
use crate::metrics::{CounterMetric, DatetimeMetric, Metric, MetricType, PingType, TimeUnit}; |
| 16 |
use crate::storage::{StorageManager, INTERNAL_STORAGE}; |
| 17 |
use crate::upload::{HeaderMap, PingMetadata}; |
| 18 |
use crate::util::{get_iso_time_string, local_now_with_offset}; |
| 19 |
use crate::{Glean, Result, DELETION_REQUEST_PINGS_DIRECTORY, PENDING_PINGS_DIRECTORY}; |
| 20 |
|
| 21 |
/// Holds everything you need to store or send a ping. |
| 22 |
pub struct Ping<'a> { |
| 23 |
/// The unique document id. |
| 24 |
pub doc_id: &'a str, |
| 25 |
/// The ping's name. |
| 26 |
pub name: &'a str, |
| 27 |
/// The path on the server to use when uplaoding this ping. |
| 28 |
pub url_path: &'a str, |
| 29 |
/// The payload, including `*_info` fields. |
| 30 |
pub content: JsonValue, |
| 31 |
/// The headers to upload with the payload. |
| 32 |
pub headers: HeaderMap, |
| 33 |
/// Whether the content contains {client|ping}_info sections. |
| 34 |
pub includes_info_sections: bool, |
| 35 |
/// Other pings that should be scheduled when this ping is sent. |
| 36 |
pub schedules_pings: Vec<String>, |
| 37 |
/// Capabilities the uploader must have in order to uplaoad this ping. |
| 38 |
pub uploader_capabilities: Vec<String>, |
| 39 |
} |
| 40 |
|
| 41 |
/// Collect a ping's data, assemble it into its full payload and store it on disk. |
| 42 |
pub struct PingMaker; |
| 43 |
|
| 44 |
fn merge(a: &mut JsonValue, b: &JsonValue) { |
| 45 |
match (a, b) { |
| 46 |
(&mut JsonValue::Object(ref mut a), JsonValue::Object(b)) => { |
| 47 |
for (k, v) in b { |
| 48 |
merge(a.entry(k.clone()).or_insert(JsonValue::Null), v); |
| 49 |
} |
| 50 |
} |
| 51 |
(a, b) => { |
| 52 |
*a = b.clone(); |
| 53 |
} |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
impl Default for PingMaker { |
| 58 |
fn default() -> Self { |
| 59 |
Self::new() |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
impl PingMaker { |
| 64 |
/// Creates a new [`PingMaker`]. |
| 65 |
pub fn new() -> Self { |
| 66 |
Self |
| 67 |
} |
| 68 |
|
| 69 |
/// Gets, and then increments, the sequence number for a given ping. |
| 70 |
fn get_ping_seq(&self, glean: &Glean, storage_name: &str) -> usize { |
| 71 |
// Don't attempt to increase sequence number for disabled ping |
| 72 |
if !glean.is_ping_enabled(storage_name) { |
| 73 |
return 0; |
| 74 |
} |
| 75 |
|
| 76 |
// Sequence numbers are stored as a counter under a name that includes the storage name |
| 77 |
let seq = CounterMetric::new(CommonMetricData { |
| 78 |
name: format!("{}#sequence", storage_name), |
| 79 |
// We don't need a category, the name is already unique |
| 80 |
category: "".into(), |
| 81 |
send_in_pings: vec![INTERNAL_STORAGE.into()], |
| 82 |
lifetime: Lifetime::User, |
| 83 |
..Default::default() |
| 84 |
}); |
| 85 |
|
| 86 |
let current_seq = match StorageManager.snapshot_metric( |
| 87 |
glean.storage(), |
| 88 |
INTERNAL_STORAGE, |
| 89 |
&seq.meta().identifier(glean), |
| 90 |
seq.meta().inner.lifetime, |
| 91 |
) { |
| 92 |
Some(Metric::Counter(i)) => i, |
| 93 |
_ => 0, |
| 94 |
}; |
| 95 |
|
| 96 |
// Increase to next sequence id |
| 97 |
seq.add_sync(glean, 1); |
| 98 |
|
| 99 |
current_seq as usize |
| 100 |
} |
| 101 |
|
| 102 |
/// Gets the formatted start and end times for this ping and update for the next ping. |
| 103 |
fn get_start_end_times( |
| 104 |
&self, |
| 105 |
glean: &Glean, |
| 106 |
storage_name: &str, |
| 107 |
time_unit: TimeUnit, |
| 108 |
) -> (String, String) { |
| 109 |
let start_time = DatetimeMetric::new( |
| 110 |
CommonMetricData { |
| 111 |
name: format!("{}#start", storage_name), |
| 112 |
category: "".into(), |
| 113 |
send_in_pings: vec![INTERNAL_STORAGE.into()], |
| 114 |
lifetime: Lifetime::User, |
| 115 |
..Default::default() |
| 116 |
}, |
| 117 |
time_unit, |
| 118 |
); |
| 119 |
|
| 120 |
// "start_time" is the time the ping was generated the last time. |
| 121 |
// If not available, we use the date the Glean object was initialized. |
| 122 |
let start_time_data = start_time |
| 123 |
.get_value(glean, INTERNAL_STORAGE) |
| 124 |
.unwrap_or_else(|| glean.start_time()); |
| 125 |
let end_time_data = local_now_with_offset(); |
| 126 |
|
| 127 |
// Update the start time with the current time. |
| 128 |
start_time.set_sync_chrono(glean, end_time_data); |
| 129 |
|
| 130 |
// Format the times. |
| 131 |
let start_time_data = get_iso_time_string(start_time_data, time_unit); |
| 132 |
let end_time_data = get_iso_time_string(end_time_data, time_unit); |
| 133 |
(start_time_data, end_time_data) |
| 134 |
} |
| 135 |
|
| 136 |
fn get_ping_info( |
| 137 |
&self, |
| 138 |
glean: &Glean, |
| 139 |
storage_name: &str, |
| 140 |
reason: Option<&str>, |
| 141 |
precision: TimeUnit, |
| 142 |
) -> JsonValue { |
| 143 |
let (start_time, end_time) = self.get_start_end_times(glean, storage_name, precision); |
| 144 |
let mut map = json!({ |
| 145 |
"seq": self.get_ping_seq(glean, storage_name), |
| 146 |
"start_time": start_time, |
| 147 |
"end_time": end_time, |
| 148 |
}); |
| 149 |
|
| 150 |
if let Some(reason) = reason { |
| 151 |
map.as_object_mut() |
| 152 |
.unwrap() // safe unwrap, we created the object above |
| 153 |
.insert("reason".to_string(), JsonValue::String(reason.to_string())); |
| 154 |
}; |
| 155 |
|
| 156 |
// Get the experiment data, if available. |
| 157 |
if let Some(experiment_data) = |
| 158 |
StorageManager.snapshot_experiments_as_json(glean.storage(), INTERNAL_STORAGE) |
| 159 |
{ |
| 160 |
map.as_object_mut() |
| 161 |
.unwrap() // safe unwrap, we created the object above |
| 162 |
.insert("experiments".to_string(), experiment_data); |
| 163 |
}; |
| 164 |
|
| 165 |
map |
| 166 |
} |
| 167 |
|
| 168 |
fn get_client_info(&self, glean: &Glean, include_client_id: bool) -> JsonValue { |
| 169 |
// Add the "telemetry_sdk_build", which is the glean-core version. |
| 170 |
let mut map = json!({ |
| 171 |
"telemetry_sdk_build": crate::GLEAN_VERSION, |
| 172 |
}); |
| 173 |
|
| 174 |
// Flatten the whole thing. |
| 175 |
if let Some(client_info) = |
| 176 |
StorageManager.snapshot_as_json(glean.storage(), "glean_client_info", true) |
| 177 |
{ |
| 178 |
let client_info_obj = client_info.as_object().unwrap(); // safe unwrap, snapshot always returns an object. |
| 179 |
for (_metric_type, metrics) in client_info_obj { |
| 180 |
merge(&mut map, metrics); |
| 181 |
} |
| 182 |
let map = map.as_object_mut().unwrap(); // safe unwrap, we created the object above. |
| 183 |
let mut attribution = serde_json::Map::new(); |
| 184 |
let mut distribution = serde_json::Map::new(); |
| 185 |
map.retain(|name, value| { |
| 186 |
// Only works because we ensure no client_info metric categories contain '.'. |
| 187 |
let mut split = name.split('.'); |
| 188 |
let category = split.next(); |
| 189 |
let name = split.next(); |
| 190 |
if let (Some(category), Some(name)) = (category, name) { |
| 191 |
if category == "attribution" { |
| 192 |
attribution.insert(name.into(), value.take()); |
| 193 |
false |
| 194 |
} else if category == "distribution" { |
| 195 |
distribution.insert(name.into(), value.take()); |
| 196 |
false |
| 197 |
} else { |
| 198 |
true |
| 199 |
} |
| 200 |
} else { |
| 201 |
true |
| 202 |
} |
| 203 |
}); |
| 204 |
if !attribution.is_empty() { |
| 205 |
map.insert("attribution".into(), serde_json::Value::from(attribution)); |
| 206 |
} |
| 207 |
if !distribution.is_empty() { |
| 208 |
map.insert("distribution".into(), serde_json::Value::from(distribution)); |
| 209 |
} |
| 210 |
} else { |
| 211 |
log::warn!("Empty client info data."); |
| 212 |
} |
| 213 |
|
| 214 |
if !include_client_id { |
| 215 |
// safe unwrap, we created the object above |
| 216 |
map.as_object_mut().unwrap().remove("client_id"); |
| 217 |
} |
| 218 |
|
| 219 |
json!(map) |
| 220 |
} |
| 221 |
|
| 222 |
/// Build the headers to be persisted and sent with a ping. |
| 223 |
/// |
| 224 |
/// Currently the only headers we persist are `X-Debug-ID` and `X-Source-Tags`. |
| 225 |
/// |
| 226 |
/// # Arguments |
| 227 |
/// |
| 228 |
/// * `glean` - the [`Glean`] instance to collect headers from. |
| 229 |
/// |
| 230 |
/// # Returns |
| 231 |
/// |
| 232 |
/// A map of header names to header values. |
| 233 |
/// Might be empty if there are no extra headers to send. |
| 234 |
/// ``` |
| 235 |
fn get_headers(&self, glean: &Glean) -> HeaderMap { |
| 236 |
let mut headers_map = HeaderMap::new(); |
| 237 |
|
| 238 |
if let Some(debug_view_tag) = glean.debug_view_tag() { |
| 239 |
headers_map.insert("X-Debug-ID".to_string(), debug_view_tag.to_string()); |
| 240 |
} |
| 241 |
|
| 242 |
if let Some(source_tags) = glean.source_tags() { |
| 243 |
headers_map.insert("X-Source-Tags".to_string(), source_tags.join(",")); |
| 244 |
} |
| 245 |
|
| 246 |
headers_map |
| 247 |
} |
| 248 |
|
| 249 |
/// Collects a snapshot for the given ping from storage and attach required meta information. |
| 250 |
/// |
| 251 |
/// # Arguments |
| 252 |
/// |
| 253 |
/// * `glean` - the [`Glean`] instance to collect data from. |
| 254 |
/// * `ping` - the ping to collect for. |
| 255 |
/// * `reason` - an optional reason code to include in the ping. |
| 256 |
/// * `doc_id` - the ping's unique document identifier. |
| 257 |
/// * `url_path` - the path on the server to upload this ping to. |
| 258 |
/// |
| 259 |
/// # Returns |
| 260 |
/// |
| 261 |
/// A fully assembled representation of the ping payload and associated metadata. |
| 262 |
/// If there is no data stored for the ping, `None` is returned. |
| 263 |
pub fn collect<'a>( |
| 264 |
&self, |
| 265 |
glean: &Glean, |
| 266 |
ping: &'a PingType, |
| 267 |
reason: Option<&str>, |
| 268 |
doc_id: &'a str, |
| 269 |
url_path: &'a str, |
| 270 |
) -> Option<Ping<'a>> { |
| 271 |
info!("Collecting {}", ping.name()); |
| 272 |
let database = glean.storage(); |
| 273 |
|
| 274 |
// HACK: Only for metrics pings we add the ping timings. |
| 275 |
// But we want that to persist until the next metrics ping is actually sent. |
| 276 |
let write_samples = database.write_timings.replace(Vec::with_capacity(64)); |
| 277 |
if !write_samples.is_empty() { |
| 278 |
glean |
| 279 |
.database_metrics |
| 280 |
.write_time |
| 281 |
.accumulate_samples_sync(glean, &write_samples); |
| 282 |
} |
| 283 |
|
| 284 |
let mut metrics_data = StorageManager.snapshot_as_json(database, ping.name(), true); |
| 285 |
|
| 286 |
let events_data = glean |
| 287 |
.event_storage() |
| 288 |
.snapshot_as_json(glean, ping.name(), true); |
| 289 |
|
| 290 |
// Due to the way the experimentation identifier could link datasets that are intentionally unlinked, |
| 291 |
// it will not be included in pings that specifically exclude the Glean client-id, those pings that |
| 292 |
// should not be sent if empty, or pings that exclude the {client|ping}_info sections wholesale. |
| 293 |
if (!ping.include_client_id() || !ping.send_if_empty() || !ping.include_info_sections()) |
| 294 |
&& glean.test_get_experimentation_id().is_some() |
| 295 |
&& metrics_data.is_some() |
| 296 |
{ |
| 297 |
// There is a lot of unwrapping here, but that's fine because the `if` conditions above mean that the |
| 298 |
// experimentation id is present in the metrics. |
| 299 |
let metrics = metrics_data.as_mut().unwrap().as_object_mut().unwrap(); |
| 300 |
let metrics_count = metrics.len(); |
| 301 |
let strings = metrics.get_mut("string").unwrap().as_object_mut().unwrap(); |
| 302 |
let string_count = strings.len(); |
| 303 |
|
| 304 |
// Handle the send_if_empty case by checking if the experimentation id is the only metric in the data. |
| 305 |
let empty_payload = events_data.is_none() && metrics_count == 1 && string_count == 1; |
| 306 |
if !ping.include_client_id() || (!ping.send_if_empty() && empty_payload) { |
| 307 |
strings.remove("glean.client.annotation.experimentation_id"); |
| 308 |
} |
| 309 |
|
| 310 |
if strings.is_empty() { |
| 311 |
metrics.remove("string"); |
| 312 |
} |
| 313 |
|
| 314 |
if metrics.is_empty() { |
| 315 |
metrics_data = None; |
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
let is_empty = metrics_data.is_none() && events_data.is_none(); |
| 320 |
if !ping.send_if_empty() && is_empty { |
| 321 |
info!("Storage for {} empty. Bailing out.", ping.name()); |
| 322 |
return None; |
| 323 |
} else if ping.name() == "events" && events_data.is_none() { |
| 324 |
info!("No events for 'events' ping. Bailing out."); |
| 325 |
return None; |
| 326 |
} else if is_empty { |
| 327 |
info!( |
| 328 |
"Storage for {} empty. Ping will still be sent.", |
| 329 |
ping.name() |
| 330 |
); |
| 331 |
} |
| 332 |
|
| 333 |
let precision = if ping.precise_timestamps() { |
| 334 |
TimeUnit::Millisecond |
| 335 |
} else { |
| 336 |
TimeUnit::Minute |
| 337 |
}; |
| 338 |
|
| 339 |
let mut json = if ping.include_info_sections() { |
| 340 |
let ping_info = self.get_ping_info(glean, ping.name(), reason, precision); |
| 341 |
let client_info = self.get_client_info(glean, ping.include_client_id()); |
| 342 |
|
| 343 |
json!({ |
| 344 |
"ping_info": ping_info, |
| 345 |
"client_info": client_info |
| 346 |
}) |
| 347 |
} else { |
| 348 |
json!({}) |
| 349 |
}; |
| 350 |
|
| 351 |
let json_obj = json.as_object_mut()?; |
| 352 |
if let Some(metrics_data) = metrics_data { |
| 353 |
json_obj.insert("metrics".to_string(), metrics_data); |
| 354 |
} |
| 355 |
if let Some(events_data) = events_data { |
| 356 |
json_obj.insert("events".to_string(), events_data); |
| 357 |
} |
| 358 |
|
| 359 |
Some(Ping { |
| 360 |
content: json, |
| 361 |
name: ping.name(), |
| 362 |
doc_id, |
| 363 |
url_path, |
| 364 |
headers: self.get_headers(glean), |
| 365 |
includes_info_sections: ping.include_info_sections(), |
| 366 |
schedules_pings: ping.schedules_pings().to_vec(), |
| 367 |
uploader_capabilities: ping.uploader_capabilities().to_vec(), |
| 368 |
}) |
| 369 |
} |
| 370 |
|
| 371 |
/// Gets the path to a directory for ping storage. |
| 372 |
/// |
| 373 |
/// The directory will be created inside the `data_path`. |
| 374 |
/// The `pings` directory (and its parents) is created if it does not exist. |
| 375 |
fn get_pings_dir(&self, data_path: &Path, ping_type: Option<&str>) -> std::io::Result<PathBuf> { |
| 376 |
// Use a special directory for deletion-request pings |
| 377 |
let pings_dir = match ping_type { |
| 378 |
Some("deletion-request") => data_path.join(DELETION_REQUEST_PINGS_DIRECTORY), |
| 379 |
_ => data_path.join(PENDING_PINGS_DIRECTORY), |
| 380 |
}; |
| 381 |
|
| 382 |
create_dir_all(&pings_dir)?; |
| 383 |
Ok(pings_dir) |
| 384 |
} |
| 385 |
|
| 386 |
/// Gets path to a directory for temporary storage. |
| 387 |
/// |
| 388 |
/// The directory will be created inside the `data_path`. |
| 389 |
/// The `tmp` directory (and its parents) is created if it does not exist. |
| 390 |
fn get_tmp_dir(&self, data_path: &Path) -> std::io::Result<PathBuf> { |
| 391 |
let pings_dir = data_path.join("tmp"); |
| 392 |
create_dir_all(&pings_dir)?; |
| 393 |
Ok(pings_dir) |
| 394 |
} |
| 395 |
|
| 396 |
/// Stores a ping to disk in the pings directory. |
| 397 |
pub fn store_ping(&self, data_path: &Path, ping: &Ping) -> std::io::Result<()> { |
| 398 |
let pings_dir = self.get_pings_dir(data_path, Some(ping.name))?; |
| 399 |
let temp_dir = self.get_tmp_dir(data_path)?; |
| 400 |
|
| 401 |
// Write to a temporary location and then move when done, |
| 402 |
// for transactional writes. |
| 403 |
let temp_ping_path = temp_dir.join(ping.doc_id); |
| 404 |
let ping_path = pings_dir.join(ping.doc_id); |
| 405 |
|
| 406 |
log::debug!( |
| 407 |
"Storing ping '{}' at '{}'", |
| 408 |
ping.doc_id, |
| 409 |
ping_path.display() |
| 410 |
); |
| 411 |
|
| 412 |
{ |
| 413 |
let mut file = File::create(&temp_ping_path)?; |
| 414 |
file.write_all(ping.url_path.as_bytes())?; |
| 415 |
file.write_all(b"\n")?; |
| 416 |
file.write_all(::serde_json::to_string(&ping.content)?.as_bytes())?; |
| 417 |
file.write_all(b"\n")?; |
| 418 |
let metadata = PingMetadata { |
| 419 |
// We don't actually need to clone the headers except to match PingMetadata's ownership. |
| 420 |
// But since we're going to write a file to disk in a sec, |
| 421 |
// and HeaderMaps tend to have only like two things in them, tops, |
| 422 |
// the cost is bearable. |
| 423 |
headers: Some(ping.headers.clone()), |
| 424 |
body_has_info_sections: Some(ping.includes_info_sections), |
| 425 |
ping_name: Some(ping.name.to_string()), |
| 426 |
uploader_capabilities: Some(ping.uploader_capabilities.clone()), |
| 427 |
}; |
| 428 |
file.write_all(::serde_json::to_string(&metadata)?.as_bytes())?; |
| 429 |
} |
| 430 |
|
| 431 |
if let Err(e) = std::fs::rename(&temp_ping_path, &ping_path) { |
| 432 |
log::warn!( |
| 433 |
"Unable to move '{}' to '{}", |
| 434 |
temp_ping_path.display(), |
| 435 |
ping_path.display() |
| 436 |
); |
| 437 |
return Err(e); |
| 438 |
} |
| 439 |
|
| 440 |
Ok(()) |
| 441 |
} |
| 442 |
|
| 443 |
/// Clears any pending pings in the queue. |
| 444 |
pub fn clear_pending_pings(&self, data_path: &Path, ping_names: &[&str]) -> Result<()> { |
| 445 |
let pings_dir = self.get_pings_dir(data_path, None)?; |
| 446 |
|
| 447 |
// TODO(bug 1932909): Refactor this into its own function |
| 448 |
// and share it with `upload::directory`. |
| 449 |
let entries = pings_dir.read_dir()?; |
| 450 |
for entry in entries.filter_map(|entry| entry.ok()) { |
| 451 |
if let Ok(file_type) = entry.file_type() { |
| 452 |
if !file_type.is_file() { |
| 453 |
continue; |
| 454 |
} |
| 455 |
} else { |
| 456 |
continue; |
| 457 |
} |
| 458 |
|
| 459 |
let file = match File::open(entry.path()) { |
| 460 |
Ok(file) => file, |
| 461 |
Err(_) => { |
| 462 |
continue; |
| 463 |
} |
| 464 |
}; |
| 465 |
|
| 466 |
let mut lines = BufReader::new(file).lines(); |
| 467 |
if let (Some(Ok(path)), Some(Ok(_body)), Ok(metadata)) = |
| 468 |
(lines.next(), lines.next(), lines.next().transpose()) |
| 469 |
{ |
| 470 |
let PingMetadata { ping_name, .. } = metadata |
| 471 |
.and_then(|m| crate::upload::process_metadata(&path, &m)) |
| 472 |
.unwrap_or_default(); |
| 473 |
let ping_name = |
| 474 |
ping_name.unwrap_or_else(|| path.split('/').nth(3).unwrap_or("").into()); |
| 475 |
|
| 476 |
if ping_names.contains(&&ping_name[..]) { |
| 477 |
_ = fs::remove_file(entry.path()); |
| 478 |
} |
| 479 |
} else { |
| 480 |
continue; |
| 481 |
} |
| 482 |
} |
| 483 |
|
| 484 |
log::debug!("All pending pings deleted"); |
| 485 |
|
| 486 |
Ok(()) |
| 487 |
} |
| 488 |
} |
| 489 |
|
| 490 |
#[cfg(test)] |
| 491 |
mod test { |
| 492 |
use super::*; |
| 493 |
use crate::tests::new_glean; |
| 494 |
|
| 495 |
#[test] |
| 496 |
fn sequence_numbers_should_be_reset_when_toggling_uploading() { |
| 497 |
let (mut glean, _t) = new_glean(None); |
| 498 |
let ping_maker = PingMaker::new(); |
| 499 |
|
| 500 |
assert_eq!(0, ping_maker.get_ping_seq(&glean, "store1")); |
| 501 |
assert_eq!(1, ping_maker.get_ping_seq(&glean, "store1")); |
| 502 |
|
| 503 |
glean.set_upload_enabled(false); |
| 504 |
assert_eq!(0, ping_maker.get_ping_seq(&glean, "store1")); |
| 505 |
assert_eq!(0, ping_maker.get_ping_seq(&glean, "store1")); |
| 506 |
|
| 507 |
glean.set_upload_enabled(true); |
| 508 |
assert_eq!(0, ping_maker.get_ping_seq(&glean, "store1")); |
| 509 |
assert_eq!(1, ping_maker.get_ping_seq(&glean, "store1")); |
| 510 |
} |
| 511 |
} |