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
/// # Arguments
///
/// - `key`: A reference to the key of the attribute to update.
/// - `value`: A reference to the new value for the attribute.
Copy link
Member

Choose a reason for hiding this comment

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

why ref, and not owned value?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good point. Have made it owned now.

None
}

/// Deletes the first occurrence of an attribute with the specified key.
Copy link
Member

Choose a reason for hiding this comment

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

should it delete all occurrences instead of first? I understand that we don't de-dup, so if a user believes they accidentally put a PII/something into LogRecord, and if they ended up with duplicates too, the expectation when one calls delete is that that key/value is completely eliminated.

Copy link
Member Author

@lalitb lalitb Sep 13, 2024

Choose a reason for hiding this comment

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

I thought delete first provides flexibility, and can be called repeatedly to delete all. But thinking more, its difficult to find use-case where user would like to delete first entry. Have update it to delete all occurrences. Also renamed it to remove_attribute, to be consistent with remove method of Vector and HashMap.

Copy link
Member

Choose a reason for hiding this comment

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

(I was thinking purely from the perspective of a app owner who realized that some PII/other things got leaked into Logs. For large codebases, it maybe hard to track down the producer of that, so people usually opt for a LogRecordProcessor to "delete" that as a mitigation! And they would be surprised if the delete didn't delete all occurrences, unless they kept calling it in a loop until no longer exists..)

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
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