-
Notifications
You must be signed in to change notification settings - Fork 602
fix: Avoid stringifying int values unless necessary #2795
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
Merged
cijothomas
merged 7 commits into
open-telemetry:main
from
cijothomas:cijothomas/appender-avoid-str
Mar 13, 2025
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c454832
fix: Avoid stringifying int values unless necessary
cijothomas 34413ce
Merge branch 'main' into cijothomas/appender-avoid-str
cijothomas e7eb7d1
fix experimental stuff
cijothomas fbe687a
code comment
cijothomas 1149861
use more idiomatic match
cijothomas 3726087
Merge branch 'main' into cijothomas/appender-avoid-str
cijothomas 5f4b837
fmt
cijothomas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -114,6 +114,22 @@ impl<LR: LogRecord> tracing::field::Visit for EventVisitor<'_, LR> { | |
| .add_attribute(Key::new(field.name()), AnyValue::from(value)); | ||
| } | ||
|
|
||
| // TODO: We might need to do similar for record_i128,record_u128 too | ||
| // to avoid stringification, unless needed. | ||
| fn record_u64(&mut self, field: &tracing::field::Field, value: u64) { | ||
| #[cfg(feature = "experimental_metadata_attributes")] | ||
| if is_duplicated_metadata(field.name()) { | ||
| return; | ||
| } | ||
| if let Ok(signed) = i64::try_from(value) { | ||
| self.log_record | ||
| .add_attribute(Key::new(field.name()), AnyValue::from(signed)); | ||
| } else { | ||
| self.log_record | ||
| .add_attribute(Key::new(field.name()), AnyValue::from(format!("{value:?}"))); | ||
| } | ||
| } | ||
|
|
||
| // TODO: Remaining field types from AnyValue : Bytes, ListAny, Boolean | ||
| } | ||
|
|
||
|
|
@@ -331,7 +347,11 @@ mod tests { | |
| let _guard = tracing::subscriber::set_default(subscriber); | ||
|
|
||
| // Act | ||
| error!(name: "my-event-name", target: "my-system", event_id = 20, user_name = "otel", user_email = "[email protected]"); | ||
| let small_u64value: u64 = 42; | ||
| let big_u64value: u64 = u64::MAX; | ||
| let small_usizevalue: usize = 42; | ||
| let big_usizevalue: usize = usize::MAX; | ||
| error!(name: "my-event-name", target: "my-system", event_id = 20, small_u64value, big_u64value, small_usizevalue, big_usizevalue, user_name = "otel", user_email = "[email protected]"); | ||
| assert!(logger_provider.force_flush().is_ok()); | ||
|
|
||
| // Assert TODO: move to helper methods | ||
|
|
@@ -362,9 +382,9 @@ mod tests { | |
|
|
||
| // Validate attributes | ||
| #[cfg(not(feature = "experimental_metadata_attributes"))] | ||
| assert_eq!(log.record.attributes_iter().count(), 3); | ||
| #[cfg(feature = "experimental_metadata_attributes")] | ||
| assert_eq!(log.record.attributes_iter().count(), 7); | ||
| #[cfg(feature = "experimental_metadata_attributes")] | ||
| assert_eq!(log.record.attributes_iter().count(), 11); | ||
| assert!(attributes_contains( | ||
| &log.record, | ||
| &Key::new("event_id"), | ||
|
|
@@ -380,6 +400,26 @@ mod tests { | |
| &Key::new("user_email"), | ||
| &AnyValue::String("[email protected]".into()) | ||
| )); | ||
| assert!(attributes_contains( | ||
| &log.record, | ||
| &Key::new("small_u64value"), | ||
| &AnyValue::Int(42.into()) | ||
| )); | ||
| assert!(attributes_contains( | ||
| &log.record, | ||
| &Key::new("big_u64value"), | ||
| &AnyValue::String(format!("{}", u64::MAX).into()) | ||
| )); | ||
| assert!(attributes_contains( | ||
| &log.record, | ||
| &Key::new("small_usizevalue"), | ||
| &AnyValue::Int(42.into()) | ||
| )); | ||
| assert!(attributes_contains( | ||
| &log.record, | ||
| &Key::new("big_usizevalue"), | ||
| &AnyValue::String(format!("{}", u64::MAX).into()) | ||
| )); | ||
| #[cfg(feature = "experimental_metadata_attributes")] | ||
| { | ||
| assert!(attributes_contains( | ||
|
|
@@ -753,6 +793,10 @@ mod tests { | |
| TraceFlags::SAMPLED | ||
| ); | ||
|
|
||
| for attribute in log.record.attributes_iter() { | ||
cijothomas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| println!("key: {:?}, value: {:?}", attribute.0, attribute.1); | ||
| } | ||
|
|
||
| // Attributes can be polluted when we don't use this feature. | ||
| #[cfg(feature = "experimental_metadata_attributes")] | ||
| assert_eq!(log.record.attributes_iter().count(), 4); | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit - indicate it as breaking change, for customers who were expecting u64 to be serialized as string in backends?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am unsure about that. I think it is a bug to convert a u64 to string by default, so this is more like a bug fix. And naturally, every bug fix can be thought of as breaking change to those whole relied on buggy behavior 🤣
We may still do more tweaks here..Do you think it should NOT be done post 1.0?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm curious. It looks like you have only added
record_u64method implementation. How does it helpusizevalues?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tracingfired the u64 callbacks for usize. (they don't have a usize callback)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if we treat this as bug fix. - converting u64 to string, or u64 to i64 both are type conversion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if we get an int, and instead of storing it as int and converting to string - this is what I treat as bug.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe as long as end-user is losing its type - it's still a bug. It's a breaking change for me :)