Skip to content

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

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

Open
wants to merge 23 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 79.7%. Comparing base (ad88615) to head (10cf48e).

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   79.6%   79.7%   +0.1%     
=======================================
  Files        124     124             
  Lines      23174   23400    +226     
=======================================
+ Hits       18456   18670    +214     
- Misses      4718    4730     +12     

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

🚀 New features to boost your workflow:
  • Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@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
@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
@lalitb lalitb mentioned this pull request Feb 13, 2025
@cijothomas
Copy link
Member

@lalitb Can you resurrect the PR?

@lalitb lalitb changed the title API to modify/remove an existing entry from LogRecord attributes feat: API to modify/remove an existing entry from LogRecord attributes Mar 12, 2025
@@ -64,6 +64,16 @@
`LogExporter` trait no longer requires a mutable ref to `self`. If the exporter
needs to mutate state, it should rely on interior mutability.
[2764](https://github.com/open-telemetry/opentelemetry-rust/pull/2764)
- Added Two new methods to the LogRecord struct's public API:
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
- Added Two new methods to the LogRecord struct's public API:
- Added Two new methods to the `SdkLogRecord` struct's public API:

Can you share why we are not adding these new APIs to the LogRecord trait itself?

Copy link
Contributor

Choose a reason for hiding this comment

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

Can you share why we are not adding these new APIs to the LogRecord trait itself?

+1

update_attribute(&Key, &AnyValue) -> Option<AnyValue>
```
- Updates the value of the first occurrence of an attribute with the specified key.
- If the key exists, the old value is returned. If not, the new key-value pair is added, and None is returned.
Copy link
Member

Choose a reason for hiding this comment

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

the behavior of update-or-insert might call for a different name than update_attribute. is set_attribute better sounding?

#[allow(dead_code)]
pub(crate) fn remove_at(&mut self, index: usize) -> Option<T> {
if index < self.count {
let removed_value = self.inline[index].clone();
Copy link
Member

Choose a reason for hiding this comment

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

is clone necessary? Can we "take" ownership?

@@ -87,6 +87,71 @@ impl<
self.count + self.overflow.as_ref().map_or(0, Vec::len)
}

/// Removes the element at a specific position (index) while preserving the order.
Copy link
Member

Choose a reason for hiding this comment

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

Thanks for detailed writeup!

///
/// This method searches for the first occurrence of the attribute with the given key
/// in the `attributes` collection. If the key is found, its value is updated with the
/// provided value. If the key is not found, the attribute is added.
Copy link
Contributor

Choose a reason for hiding this comment

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

If the key is not found, the attribute is added.

We already have an add_attribute method which is meant for adding a new attribute. I think we should do one of the following:

  1. Either make update_attribute strictly an update operation.
  2. Or rename the method to add_or_update_attribute to reflect its true behavior.

@@ -201,6 +202,73 @@ impl SdkLogRecord {
.flatten()
.any(|(k, v)| k == key && v == value)
}

/// Updates the first occurrence of an attribute with the specified key.
Copy link
Contributor

Choose a reason for hiding this comment

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

Similar to remove_attribute, we should update all the occurrences of the given attribute key. For example, I think it would make it easier for redaction processor authors to redact a "secret" attribute with certainty.

/// - Removes the element at the specified index.
/// - Shifts the remaining elements in the internal array to the left to fill the gap, preserving the order.
/// - If an overflow vector exists:
/// - Moves the first element from the overflow vector into the last position of the internal array, if available.
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we avoid doing this? It seems like an unnecessary expensive operation. We maintain the count of the items in the array. It should be okay to have the count less than the array length while also having the vec.

@cijothomas cijothomas removed this from the Logging SDK Stable milestone Mar 22, 2025
@cijothomas
Copy link
Member

@lalitb Could you address review comments and resurrect this when you get a chance?

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