Skip to content

Commit a8b308a

Browse files
authored
Fix aggregation bug due to stale hash value (#1422)
1 parent 69f50f5 commit a8b308a

File tree

3 files changed

+17
-9
lines changed

3 files changed

+17
-9
lines changed

opentelemetry-sdk/CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
- Fix SimpleSpanProcessor to be consistent with log counterpart. Also removed
66
dependency on crossbeam-channel.
77
[1612](https://github.com/open-telemetry/opentelemetry-rust/pull/1612/files)
8+
- [#1422](https://github.com/open-telemetry/opentelemetry-rust/pull/1422)
9+
Fix metrics aggregation bug when using Views to drop attributes.
810

911
## v0.22.1
1012

opentelemetry-sdk/src/attributes/set.rs

+15-8
Original file line numberDiff line numberDiff line change
@@ -138,16 +138,20 @@ impl From<&Resource> for AttributeSet {
138138
}
139139
}
140140

141+
fn calculate_hash(values: &[HashKeyValue]) -> u64 {
142+
let mut hasher = DefaultHasher::new();
143+
values.iter().fold(&mut hasher, |mut hasher, item| {
144+
item.hash(&mut hasher);
145+
hasher
146+
});
147+
hasher.finish()
148+
}
149+
141150
impl AttributeSet {
142151
fn new(mut values: Vec<HashKeyValue>) -> Self {
143152
values.sort_unstable();
144-
let mut hasher = DefaultHasher::new();
145-
values.iter().fold(&mut hasher, |mut hasher, item| {
146-
item.hash(&mut hasher);
147-
hasher
148-
});
149-
150-
AttributeSet(values, hasher.finish())
153+
let hash = calculate_hash(&values);
154+
AttributeSet(values, hash)
151155
}
152156

153157
/// Returns the number of elements in the set.
@@ -165,7 +169,10 @@ impl AttributeSet {
165169
where
166170
F: Fn(&KeyValue) -> bool,
167171
{
168-
self.0.retain(|kv| f(&kv.0))
172+
self.0.retain(|kv| f(&kv.0));
173+
174+
// Recalculate the hash as elements are changed.
175+
self.1 = calculate_hash(&self.0);
169176
}
170177

171178
/// Iterate over key value pairs in the set

opentelemetry-sdk/src/metrics/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,6 @@ mod tests {
532532
// "multi_thread" tokio flavor must be used else flush won't
533533
// be able to make progress!
534534
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
535-
#[ignore = "Spatial aggregation is not yet implemented."]
536535
async fn spatial_aggregation_when_view_drops_attributes_counter() {
537536
// cargo test spatial_aggregation_when_view_drops_attributes_counter --features=metrics,testing
538537

0 commit comments

Comments
 (0)