Skip to content

Commit 9308e48

Browse files
committed
support rest of fields too
1 parent 5ce73fc commit 9308e48

File tree

3 files changed

+57
-5
lines changed

3 files changed

+57
-5
lines changed

opentelemetry-appender-tracing/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ 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`
45+
- `u64`, `i128`, `u128` and `usize` values are stored as `opentelemetry::logs::AnyValue::Int`
4646
when conversion is feasible. Otherwise stored as
4747
`opentelemetry::logs::AnyValue::String`. This avoids unnecessary string
4848
allocation when values can be represented in their original types.

opentelemetry-appender-tracing/src/layer.rs

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,34 @@ impl<LR: LogRecord> tracing::field::Visit for EventVisitor<'_, LR> {
147147
}
148148
}
149149

150+
fn record_i128(&mut self, field: &tracing::field::Field, value: i128) {
151+
#[cfg(feature = "experimental_metadata_attributes")]
152+
if is_duplicated_metadata(field.name()) {
153+
return;
154+
}
155+
if let Ok(signed) = i64::try_from(value) {
156+
self.log_record
157+
.add_attribute(Key::new(field.name()), AnyValue::from(signed));
158+
} else {
159+
self.log_record
160+
.add_attribute(Key::new(field.name()), AnyValue::from(format!("{value:?}")));
161+
}
162+
}
163+
164+
fn record_u128(&mut self, field: &tracing::field::Field, value: u128) {
165+
#[cfg(feature = "experimental_metadata_attributes")]
166+
if is_duplicated_metadata(field.name()) {
167+
return;
168+
}
169+
if let Ok(signed) = i64::try_from(value) {
170+
self.log_record
171+
.add_attribute(Key::new(field.name()), AnyValue::from(signed));
172+
} else {
173+
self.log_record
174+
.add_attribute(Key::new(field.name()), AnyValue::from(format!("{value:?}")));
175+
}
176+
}
177+
150178
// TODO: Remaining field types from AnyValue : Bytes, ListAny, Boolean
151179
}
152180

@@ -367,7 +395,11 @@ mod tests {
367395
let big_u64value: u64 = u64::MAX;
368396
let small_usizevalue: usize = 42;
369397
let big_usizevalue: usize = usize::MAX;
370-
error!(name: "my-event-name", target: "my-system", event_id = 20, bytes = &b"abc"[..], error = &OTelSdkError::AlreadyShutdown as &dyn std::error::Error, small_u64value, big_u64value, small_usizevalue, big_usizevalue, user_name = "otel", user_email = "[email protected]");
398+
let small_u128value: u128 = 42;
399+
let big_u128value: u128 = u128::MAX;
400+
let small_i128value: i128 = 42;
401+
let big_i128value: i128 = i128::MAX;
402+
error!(name: "my-event-name", target: "my-system", event_id = 20, bytes = &b"abc"[..], error = &OTelSdkError::AlreadyShutdown as &dyn std::error::Error, small_u64value, big_u64value, small_usizevalue, big_usizevalue, small_u128value, big_u128value, small_i128value, big_i128value, user_name = "otel", user_email = "[email protected]");
371403
assert!(logger_provider.force_flush().is_ok());
372404

373405
// Assert TODO: move to helper methods
@@ -398,9 +430,9 @@ mod tests {
398430

399431
// Validate attributes
400432
#[cfg(not(feature = "experimental_metadata_attributes"))]
401-
assert_eq!(log.record.attributes_iter().count(), 9);
402-
#[cfg(feature = "experimental_metadata_attributes")]
403433
assert_eq!(log.record.attributes_iter().count(), 13);
434+
#[cfg(feature = "experimental_metadata_attributes")]
435+
assert_eq!(log.record.attributes_iter().count(), 17);
404436
assert!(attributes_contains(
405437
&log.record,
406438
&Key::new("event_id"),
@@ -441,6 +473,26 @@ mod tests {
441473
&Key::new("big_usizevalue"),
442474
&AnyValue::String(format!("{}", u64::MAX).into())
443475
));
476+
assert!(attributes_contains(
477+
&log.record,
478+
&Key::new("small_u128value"),
479+
&AnyValue::Int(42.into())
480+
));
481+
assert!(attributes_contains(
482+
&log.record,
483+
&Key::new("big_u128value"),
484+
&AnyValue::String(format!("{}", u128::MAX).into())
485+
));
486+
assert!(attributes_contains(
487+
&log.record,
488+
&Key::new("small_i128value"),
489+
&AnyValue::Int(42.into())
490+
));
491+
assert!(attributes_contains(
492+
&log.record,
493+
&Key::new("big_i128value"),
494+
&AnyValue::String(format!("{}", i128::MAX).into())
495+
));
444496
assert!(attributes_contains(
445497
&log.record,
446498
&Key::new("bytes"),

opentelemetry-appender-tracing/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@
112112
//! |----------------|-------------------------------|
113113
//! | `i64` | `Int` |
114114
//! | `f32`, `f64` | `Double` |
115-
//! | `u64` | `Int` (if convertible without loss) else `String` |
115+
//! | `u64`,`u128` ,`i128` | `Int` (if convertible without loss) else `String` |
116116
//! | `&str` | `String` |
117117
//! | `bool` | `Bool` |
118118
//! | `&[u8]` | `Bytes` |

0 commit comments

Comments
 (0)