Skip to content

Commit

Permalink
sample MovingUploader: count and type in filename
Browse files Browse the repository at this point in the history
This makes local debugging easier by:

1. Keeping the files in the order as they arrive
2. Putting the ping name into the file, making it easy to pick out the
   right file to look at.
  • Loading branch information
badboy committed Nov 12, 2024
1 parent c01df7b commit ebb63a0
Showing 1 changed file with 35 additions and 7 deletions.
42 changes: 35 additions & 7 deletions samples/rust/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::env;
use std::fs::File;
use std::io::{Read, Write};
use std::path::PathBuf;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::thread;
use std::time::Duration;

Expand All @@ -19,10 +20,31 @@ pub mod glean_metrics {
}

#[derive(Debug)]
struct MovingUploader(String);
struct MovingUploader {
out_path: String,
recv_cnt: AtomicUsize,
}

impl MovingUploader {
fn new(out_path: String) -> Self {
let mut cnt = 0;

let mut dir = PathBuf::from(&out_path);
dir.push("sent_pings");
if let Ok(entries) = dir.read_dir() {
cnt = entries.filter_map(|entry| entry.ok()).count();
}

Self {
out_path,
recv_cnt: AtomicUsize::new(cnt),
}
}
}

impl net::PingUploader for MovingUploader {
fn upload(&self, upload_request: net::PingUploadRequest) -> net::UploadResult {
let cnt = self.recv_cnt.fetch_add(1, Ordering::Relaxed) + 1;
let net::PingUploadRequest {
body, url, headers, ..
} = upload_request;
Expand All @@ -36,22 +58,28 @@ impl net::PingUploader for MovingUploader {
.or_else(|| std::str::from_utf8(&body).ok())
.unwrap();

let mut out_path = PathBuf::from(&self.0);
let mut out_path = PathBuf::from(&self.out_path);
out_path.push("sent_pings");
std::fs::create_dir_all(&out_path).unwrap();

let docid = url.rsplit('/').next().unwrap();
out_path.push(format!("{docid}.json"));
let mut components = url.rsplit('/');
let docid = components.next().unwrap();
let _doc_version = components.next().unwrap();
let doctype = components.next().unwrap();
out_path.push(format!("{cnt:0>3}-{doctype}-{docid}.json"));
let mut fp = File::create(out_path).unwrap();

// pseudo-JSON, let's hope this works.
writeln!(fp, "{{").unwrap();
writeln!(fp, " \"url\": {url},").unwrap();
writeln!(fp, " \"url\": {url:?},").unwrap();
for (key, val) in headers {
writeln!(fp, " \"{key}\": \"{val}\",").unwrap();
}
writeln!(fp, "}}").unwrap();
writeln!(fp, "{data}").unwrap();

let data: serde_json::Value = serde_json::from_str(data).unwrap();
let json = serde_json::to_string_pretty(&data).unwrap();
writeln!(fp, "{json}").unwrap();

net::UploadResult::http_status(200)
}
Expand All @@ -69,7 +97,7 @@ fn main() {
root.path().to_path_buf()
};

let uploader = MovingUploader(data_path.display().to_string());
let uploader = MovingUploader::new(data_path.display().to_string());
let cfg = ConfigurationBuilder::new(true, data_path, "org.mozilla.glean_core.example")
.with_server_endpoint("invalid-test-host")
.with_use_core_mps(true)
Expand Down

0 comments on commit ebb63a0

Please sign in to comment.