Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug 1898336 - (Memory|Timing) Distribution: snapshot only contains non-zero buckets now #2848

Merged
merged 1 commit into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* General
* **BREAKING**: Remove LMDB-to-safe-mode migration.
Safe-mode became the default in Glean v51. ([#TODO]())
* **BREAKING**: Stop sending buckets with 0 counts in memory_distribution and timing_distribution metric payloads ([bug 1898336](https://bugzilla.mozilla.org/show_bug.cgi?id=1898336))

# v61.2.0 (2024-10-07)

Expand Down
40 changes: 2 additions & 38 deletions glean-core/src/histogram/functional.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,44 +85,8 @@ impl Histogram<Functional> {
/// **Caution** This is a more specific implementation of `snapshot_values` on functional
/// histograms. `snapshot_values` cannot be used with those, due to buckets not being
/// precomputed.
pub fn snapshot(&self) -> HashMap<u64, u64> {
if self.values.is_empty() {
return HashMap::new();
}

let mut min_key = None;
let mut max_key = None;

// `Iterator#min` and `Iterator#max` would do the same job independently,
// but we want to avoid iterating the keys twice, so we loop ourselves.
for key in self.values.keys() {
let key = *key;

// safe unwrap, we checked it's not none
if min_key.is_none() || key < min_key.unwrap() {
min_key = Some(key);
}

// safe unwrap, we checked it's not none
if max_key.is_none() || key > max_key.unwrap() {
max_key = Some(key);
}
}

// Non-empty values, therefore minimum/maximum exists.
// safe unwraps, we set it at least once.
let min_bucket = self.bucketing.sample_to_bucket_index(min_key.unwrap());
let max_bucket = self.bucketing.sample_to_bucket_index(max_key.unwrap()) + 1;

let mut values = self.values.clone();

for idx in min_bucket..=max_bucket {
// Fill in missing entries.
let min_bucket = self.bucketing.bucket_index_to_bucket_minimum(idx);
let _ = values.entry(min_bucket).or_insert(0);
}

values
pub fn snapshot(&self) -> &HashMap<u64, u64> {
&self.values
}
}

Expand Down
4 changes: 2 additions & 2 deletions glean-core/src/metrics/memory_distribution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ pub(crate) fn snapshot(hist: &Histogram<Functional>) -> DistributionData {
// specialized snapshot function.
values: hist
.snapshot()
.into_iter()
.map(|(k, v)| (k as i64, v as i64))
.iter()
.map(|(&k, &v)| (k as i64, v as i64))
.collect(),
sum: hist.sum() as i64,
count: hist.count() as i64,
Expand Down
8 changes: 2 additions & 6 deletions glean-core/src/metrics/timing_distribution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ pub(crate) fn snapshot(hist: &Histogram<Functional>) -> DistributionData {
// specialized snapshot function.
values: hist
.snapshot()
.into_iter()
.map(|(k, v)| (k as i64, v as i64))
.iter()
.map(|(&k, &v)| (k as i64, v as i64))
.collect(),
sum: hist.sum() as i64,
count: hist.count() as i64,
Expand Down Expand Up @@ -713,7 +713,6 @@ mod test {
"8": 1,
"9": 1,
"10": 1,
"11": 0,
},
});

Expand All @@ -738,10 +737,7 @@ mod test {
"values": {
"1024": 2,
"1116": 1,
"1217": 0,
"1327": 0,
"1448": 1,
"1579": 0,
},
});

Expand Down
4 changes: 2 additions & 2 deletions glean-core/tests/labeled.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ fn can_create_labeled_memory_distribution_metric() {
assert_eq!(
json!({
"labeled_memory_distribution": {
"telemetry.labeled_metric": { "label1": { "sum": 42, "values": {"41": 1, "43": 0} } }
"telemetry.labeled_metric": { "label1": { "sum": 42, "values": {"41": 1} } }
}
}),
snapshot
Expand Down Expand Up @@ -215,7 +215,7 @@ fn can_create_labeled_timing_distribution_metric() {
assert_eq!(
json!({
"labeled_timing_distribution": {
"telemetry.labeled_metric": { "label1": { "sum": 42, "values": {"41": 1, "45": 0} } }
"telemetry.labeled_metric": { "label1": { "sum": 42, "values": {"41": 1} } }
}
}),
snapshot
Expand Down
2 changes: 0 additions & 2 deletions glean-core/tests/memory_distribution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ fn serializer_should_correctly_serialize_memory_distribution() {
"sum": 100_000 * kb,
"values": {
"99108124": 1,
"103496016": 0,
}
});
assert_eq!(
Expand Down Expand Up @@ -93,7 +92,6 @@ fn set_value_properly_sets_the_value_in_all_stores() {
"sum": 100_000,
"values": {
"96785": 1,
"101070": 0,
}
});
for store_name in store_names {
Expand Down
2 changes: 0 additions & 2 deletions glean-core/tests/timing_distribution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ fn serializer_should_correctly_serialize_timing_distribution() {
"sum": duration,
"values": {
"58": 1,
"64": 0,
}
});
assert_eq!(
Expand Down Expand Up @@ -102,7 +101,6 @@ fn set_value_properly_sets_the_value_in_all_stores() {
"sum": 1,
"values": {
"1": 1,
"2": 0,
}
});
for store_name in store_names {
Expand Down
Loading