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

Fix aggregation bug due to stale hash value #1422

Merged
merged 7 commits into from
Mar 13, 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
2 changes: 2 additions & 0 deletions opentelemetry-sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
- Fix SimpleSpanProcessor to be consistent with log counterpart. Also removed
dependency on crossbeam-channel.
[1612](https://github.com/open-telemetry/opentelemetry-rust/pull/1612/files)
- [#1422](https://github.com/open-telemetry/opentelemetry-rust/pull/1422)
Fix metrics aggregation bug when using Views to drop attributes.

## v0.22.1

Expand Down
23 changes: 15 additions & 8 deletions opentelemetry-sdk/src/attributes/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,16 +138,20 @@ impl From<&Resource> for AttributeSet {
}
}

fn calculate_hash(values: &[HashKeyValue]) -> u64 {
let mut hasher = DefaultHasher::new();
values.iter().fold(&mut hasher, |mut hasher, item| {
item.hash(&mut hasher);
hasher
});
hasher.finish()
}

impl AttributeSet {
fn new(mut values: Vec<HashKeyValue>) -> Self {
values.sort_unstable();
let mut hasher = DefaultHasher::new();
values.iter().fold(&mut hasher, |mut hasher, item| {
item.hash(&mut hasher);
hasher
});

AttributeSet(values, hasher.finish())
let hash = calculate_hash(&values);
AttributeSet(values, hash)
}

/// Returns the number of elements in the set.
Expand All @@ -165,7 +169,10 @@ impl AttributeSet {
where
F: Fn(&KeyValue) -> bool,
{
self.0.retain(|kv| f(&kv.0))
self.0.retain(|kv| f(&kv.0));

// Recalculate the hash as elements are changed.
self.1 = calculate_hash(&self.0);
}

/// Iterate over key value pairs in the set
Expand Down
1 change: 0 additions & 1 deletion opentelemetry-sdk/src/metrics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,6 @@ mod tests {
// "multi_thread" tokio flavor must be used else flush won't
// be able to make progress!
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
#[ignore = "Spatial aggregation is not yet implemented."]
async fn spatial_aggregation_when_view_drops_attributes_counter() {
// cargo test spatial_aggregation_when_view_drops_attributes_counter --features=metrics,testing

Expand Down
Loading