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

API to modify/remove an existing entry from LogRecord attributes #2103

Open
wants to merge 20 commits into
base: main
Choose a base branch
from

Conversation

lalitb
Copy link
Member

@lalitb lalitb commented Sep 11, 2024

Fixes #1986

Changes

This PR adds two new methods, update_attribute and delete_attribute, to the public API of the LogRecord struct.

pub fn update_attribute(&Key, &AnyValue) -> Option<AnyValue>
  • Updates the value of the first occurrence of an attribute with the specified key.
  • If the attribute already exists, its value is replaced, and the old value is returned.
  • If the attribute is not found, it is added as a new key-value pair, and None is returned.
pub fn remove_attribute(&mut self, key: &Key) -> usize
  • Removes all the occurrence of attributes with the specified key.
  • The count of deleted attributes is returned .

Merge requirement checklist

  • CONTRIBUTING guidelines followed
  • Unit tests added/updated (if applicable)
  • Appropriate CHANGELOG.md files updated for non-trivial, user-facing changes
  • Changes in public API reviewed (if applicable)

Copy link

codecov bot commented Sep 11, 2024

Codecov Report

Attention: Patch coverage is 94.71366% with 12 lines in your changes missing coverage. Please review.

Project coverage is 77.0%. Comparing base (f911ed5) to head (d67da9f).

Files with missing lines Patch % Lines
opentelemetry-sdk/src/growable_array.rs 92.1% 9 Missing ⚠️
opentelemetry-sdk/src/logs/log_processor.rs 94.5% 3 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##            main   #2103     +/-   ##
=======================================
+ Coverage   76.9%   77.0%   +0.1%     
=======================================
  Files        122     122             
  Lines      22213   22439    +226     
=======================================
+ Hits       17082   17296    +214     
- Misses      5131    5143     +12     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@lalitb lalitb changed the title [WIP] API to modify/remove an existing entry from LogRecord attributes API to modify/remove an existing entry from LogRecord attributes Sep 12, 2024
@lalitb lalitb marked this pull request as ready for review September 12, 2024 16:17
@lalitb lalitb requested a review from a team September 12, 2024 16:17
Co-authored-by: Cijo Thomas <cithomas@microsoft.com>
Comment on lines +147 to +160
if let Some(attr) = self
.attributes
.iter_mut()
.find(|opt| opt.as_ref().map(|(k, _)| k == key).unwrap_or(false))
{
// Take the old value and update the attribute
let old_value = attr.take().map(|(_, v)| v);
*attr = Some((key.clone(), value));
return old_value;
}

// If not found, add a new attribute
self.add_attribute(key.clone(), value.clone());
None
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small nit on code style

maybe easier to read if we do

match self
            .attributes
            .iter_mut()
            .find(|opt| opt.as_ref().map(|(k, _)| k == key).unwrap_or(false)) {
                     Some(attr) => {
                               // Take the old value and update the attribute
                               let old_value = attr.take().map(|(_, v)| v);
                               *attr = Some((key.clone(), value));
                               old_value
                        }
                     None => self.add_attribute(key.clone(), value.clone());
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was little confused why we need to add_attribute regardless until I see the early return

Copy link
Contributor

@TommyCpp TommyCpp Sep 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or you have already tried that and compiler complains because the we are still holding mutable reference to self when iterator found nothing?

Techiniqually if we returned None, by that point the mutable reference to self should be dropped, allowing us to call add_attribute. But not sure if the refernece checker is smart enough

Copy link
Member Author

@lalitb lalitb Sep 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or you have already tried that and compiler complains because the we are still holding mutable reference to self when iterator found nothing?

@TommyCpp yes, i initially tried that approach, and got the error for still holding the mutable reference to self. Looks like the borrow checker is bit conservative and doesn't automatically drop iterators/temporaries until the end of the scope where they are used.

@cijothomas
Copy link
Member

This is important, but trying to wrap up critical metric stuff/internal logging for the upcoming release, and will get back to this right after that.

@lalitb lalitb requested a review from a team as a code owner December 19, 2024 06:34
@cijothomas cijothomas modified the milestones: 0.28.0, Logging SDK Stable Jan 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
integration tests Run integration tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

API to modify/remove an existing entry from LogRecord attributes
3 participants