Skip to content

Commit a442d62

Browse files
authored
fix: Byte arrays represented as Anyvalue::Byte instead of string (#2807)
1 parent 6e58810 commit a442d62

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

opentelemetry-appender-tracing/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ transparent to most users.
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.
49-
49+
- Byte arrays are stored as `opentelemetry::logs::AnyValue::Bytes` instead
50+
of string.
5051
- perf - small perf improvement by avoiding string allocation of `target`
5152

5253
## 0.28.1

opentelemetry-appender-tracing/src/layer.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ impl<LR: LogRecord> tracing::field::Visit for EventVisitor<'_, LR> {
8080
}
8181
}
8282

83+
fn record_bytes(&mut self, field: &tracing_core::Field, value: &[u8]) {
84+
self.log_record
85+
.add_attribute(Key::new(field.name()), AnyValue::from(value));
86+
}
87+
8388
fn record_str(&mut self, field: &tracing_core::Field, value: &str) {
8489
#[cfg(feature = "experimental_metadata_attributes")]
8590
if is_duplicated_metadata(field.name()) {
@@ -350,7 +355,7 @@ mod tests {
350355
let big_u64value: u64 = u64::MAX;
351356
let small_usizevalue: usize = 42;
352357
let big_usizevalue: usize = usize::MAX;
353-
error!(name: "my-event-name", target: "my-system", event_id = 20, small_u64value, big_u64value, small_usizevalue, big_usizevalue, user_name = "otel", user_email = "otel@opentelemetry.io");
358+
error!(name: "my-event-name", target: "my-system", event_id = 20, bytes = &b"abc"[..], small_u64value, big_u64value, small_usizevalue, big_usizevalue, user_name = "otel", user_email = "otel@opentelemetry.io");
354359
assert!(logger_provider.force_flush().is_ok());
355360

356361
// Assert TODO: move to helper methods
@@ -381,9 +386,9 @@ mod tests {
381386

382387
// Validate attributes
383388
#[cfg(not(feature = "experimental_metadata_attributes"))]
384-
assert_eq!(log.record.attributes_iter().count(), 7);
389+
assert_eq!(log.record.attributes_iter().count(), 8);
385390
#[cfg(feature = "experimental_metadata_attributes")]
386-
assert_eq!(log.record.attributes_iter().count(), 11);
391+
assert_eq!(log.record.attributes_iter().count(), 12);
387392
assert!(attributes_contains(
388393
&log.record,
389394
&Key::new("event_id"),
@@ -419,6 +424,11 @@ mod tests {
419424
&Key::new("big_usizevalue"),
420425
&AnyValue::String(format!("{}", u64::MAX).into())
421426
));
427+
assert!(attributes_contains(
428+
&log.record,
429+
&Key::new("bytes"),
430+
&AnyValue::Bytes(Box::new(b"abc".to_vec()))
431+
));
422432
#[cfg(feature = "experimental_metadata_attributes")]
423433
{
424434
assert!(attributes_contains(

opentelemetry/src/logs/record.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,12 @@ impl_trivial_from!(StringValue, AnyValue::String);
115115

116116
impl_trivial_from!(bool, AnyValue::Boolean);
117117

118+
impl From<&[u8]> for AnyValue {
119+
fn from(val: &[u8]) -> AnyValue {
120+
AnyValue::Bytes(Box::new(val.to_vec()))
121+
}
122+
}
123+
118124
impl<T: Into<AnyValue>> FromIterator<T> for AnyValue {
119125
/// Creates an [`AnyValue::ListAny`] value from a sequence of `Into<AnyValue>` values.
120126
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {

0 commit comments

Comments
 (0)