Skip to content

Commit c454832

Browse files
committed
fix: Avoid stringifying int values unless necessary
1 parent 8aa5b00 commit c454832

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

opentelemetry-appender-tracing/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ transparent to most users.
4242
implementations (SDK, processor, exporters) to leverage this additional
4343
information to determine if an event is enabled.
4444

45+
- `u64` and `usize` values are stored as `opentelemetry::logs::AnyValue::Int`
46+
when conversion is feasible. Otherwise stored as
47+
`opentelemetry::logs::AnyValue::String`. This avoids unnecessary string
48+
allocation when values can be represented in their original types.
49+
4550
## 0.28.1
4651

4752
Released 2025-Feb-12

opentelemetry-appender-tracing/src/layer.rs

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,19 @@ impl<LR: LogRecord> tracing::field::Visit for EventVisitor<'_, LR> {
114114
.add_attribute(Key::new(field.name()), AnyValue::from(value));
115115
}
116116

117+
fn record_u64(&mut self, field: &tracing::field::Field, value: u64) {
118+
match i64::try_from(value) {
119+
Ok(signed) => {
120+
self.log_record
121+
.add_attribute(Key::new(field.name()), AnyValue::from(signed));
122+
}
123+
Err(_) => {
124+
self.log_record
125+
.add_attribute(Key::new(field.name()), AnyValue::from(format!("{value:?}")));
126+
}
127+
}
128+
}
129+
117130
// TODO: Remaining field types from AnyValue : Bytes, ListAny, Boolean
118131
}
119132

@@ -331,7 +344,11 @@ mod tests {
331344
let _guard = tracing::subscriber::set_default(subscriber);
332345

333346
// Act
334-
error!(name: "my-event-name", target: "my-system", event_id = 20, user_name = "otel", user_email = "[email protected]");
347+
let small_u64value: u64 = 42;
348+
let big_u64value: u64 = u64::MAX;
349+
let small_usizevalue: usize = 42;
350+
let big_usizevalue: usize = usize::MAX;
351+
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]");
335352
assert!(logger_provider.force_flush().is_ok());
336353

337354
// Assert TODO: move to helper methods
@@ -362,9 +379,9 @@ mod tests {
362379

363380
// Validate attributes
364381
#[cfg(not(feature = "experimental_metadata_attributes"))]
365-
assert_eq!(log.record.attributes_iter().count(), 3);
366-
#[cfg(feature = "experimental_metadata_attributes")]
367382
assert_eq!(log.record.attributes_iter().count(), 7);
383+
#[cfg(feature = "experimental_metadata_attributes")]
384+
assert_eq!(log.record.attributes_iter().count(), 11);
368385
assert!(attributes_contains(
369386
&log.record,
370387
&Key::new("event_id"),
@@ -380,6 +397,26 @@ mod tests {
380397
&Key::new("user_email"),
381398
&AnyValue::String("[email protected]".into())
382399
));
400+
assert!(attributes_contains(
401+
&log.record,
402+
&Key::new("small_u64value"),
403+
&AnyValue::Int(42.into())
404+
));
405+
assert!(attributes_contains(
406+
&log.record,
407+
&Key::new("big_u64value"),
408+
&AnyValue::String(format!("{}", u64::MAX).into())
409+
));
410+
assert!(attributes_contains(
411+
&log.record,
412+
&Key::new("small_usizevalue"),
413+
&AnyValue::Int(42.into())
414+
));
415+
assert!(attributes_contains(
416+
&log.record,
417+
&Key::new("big_usizevalue"),
418+
&AnyValue::String(format!("{}", u64::MAX).into())
419+
));
383420
#[cfg(feature = "experimental_metadata_attributes")]
384421
{
385422
assert!(attributes_contains(

0 commit comments

Comments
 (0)