Skip to content

Commit

Permalink
Handle special case
Browse files Browse the repository at this point in the history
  • Loading branch information
fraillt committed Nov 22, 2024
1 parent 4e0dda5 commit 89a1c0c
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions opentelemetry-sdk/src/metrics/internal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,10 @@ where

let sorted_count = sorted_trackers.len();
let new_tracker = match sorted_trackers.entry(sorted_attrs) {
Entry::Occupied(occupied_entry) => occupied_entry.get().clone(),
Entry::Occupied(occupied_entry) => {
// do not return early, because collection phase might clear `all_trackers` multiple times
occupied_entry.get().clone()
}
Entry::Vacant(vacant_entry) => {
if !is_under_cardinality_limit(sorted_count) {
sorted_trackers.entry(STREAM_OVERFLOW_ATTRIBUTES.clone())
Expand Down Expand Up @@ -197,8 +200,28 @@ where
));
}

for (attrs, tracker) in to_collect.drain() {
let tracker = Arc::into_inner(tracker).expect("the only instance");
for (attrs, mut tracker) in to_collect.drain() {
// Handles special case:
// measure-thread: get inserted tracker from `sorted_attribs` (holds tracker)
// collect-thread: replace sorted_attribs (clears sorted_attribs)
// collect-thread: clear all_attribs
// collect_thread: THIS-LOOP: loop until measure-thread still holds a tracker
// measure-thread: insert tracker into `all_attribs``
// collect_thread: exits this loop after clearing trackers
let tracker = loop {
match Arc::try_unwrap(tracker) {
Ok(inner) => {
break inner;
}
Err(reinserted) => {
tracker = reinserted;
match self.all_attribs.write() {
Ok(mut all_trackers) => all_trackers.clear(),
Err(_) => return,

Check warning on line 220 in opentelemetry-sdk/src/metrics/internal/mod.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-sdk/src/metrics/internal/mod.rs#L216-L220

Added lines #L216 - L220 were not covered by tests
};
}
};
};
dest.push(map_fn(attrs, tracker));
}
}
Expand Down

0 comments on commit 89a1c0c

Please sign in to comment.