Skip to content

Commit 9b5c7f2

Browse files
authored
Merge branch 'main' into cijothomas/otlp-error-return
2 parents 3f2a208 + 07b3b40 commit 9b5c7f2

File tree

4 files changed

+61
-14
lines changed

4 files changed

+61
-14
lines changed

opentelemetry-appender-tracing/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ 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+
50+
- perf - small perf improvement by avoiding string allocation of `target`
51+
4552
## 0.28.1
4653

4754
Released 2025-Feb-12

opentelemetry-appender-tracing/benches/logs.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
| noop_layer_disabled | 12 ns |
1111
| noop_layer_enabled | 25 ns |
1212
| ot_layer_disabled | 19 ns |
13-
| ot_layer_enabled | 167 ns |
13+
| ot_layer_enabled | 155 ns |
1414
1515
Hardware: Apple M4 Pro
1616
Total Number of Cores: 14 (10 performance and 4 efficiency)
@@ -20,7 +20,7 @@
2020
| noop_layer_disabled | 8 ns |
2121
| noop_layer_enabled | 14 ns |
2222
| ot_layer_disabled | 12 ns |
23-
| ot_layer_enabled | 186 ns |
23+
| ot_layer_enabled | 130 ns |
2424
*/
2525

2626
use criterion::{criterion_group, criterion_main, Criterion};

opentelemetry-appender-tracing/src/layer.rs

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

117+
// TODO: We might need to do similar for record_i128,record_u128 too
118+
// to avoid stringification, unless needed.
119+
fn record_u64(&mut self, field: &tracing::field::Field, value: u64) {
120+
#[cfg(feature = "experimental_metadata_attributes")]
121+
if is_duplicated_metadata(field.name()) {
122+
return;
123+
}
124+
if let Ok(signed) = i64::try_from(value) {
125+
self.log_record
126+
.add_attribute(Key::new(field.name()), AnyValue::from(signed));
127+
} else {
128+
self.log_record
129+
.add_attribute(Key::new(field.name()), AnyValue::from(format!("{value:?}")));
130+
}
131+
}
132+
117133
// TODO: Remaining field types from AnyValue : Bytes, ListAny, Boolean
118134
}
119135

@@ -173,8 +189,7 @@ where
173189

174190
let mut log_record = self.logger.create_log_record();
175191

176-
// TODO: Fix heap allocation
177-
log_record.set_target(target.to_string());
192+
log_record.set_target(target);
178193
log_record.set_event_name(name);
179194
log_record.set_severity_number(severity);
180195
log_record.set_severity_text(metadata.level().as_str());
@@ -331,7 +346,11 @@ mod tests {
331346
let _guard = tracing::subscriber::set_default(subscriber);
332347

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

337356
// Assert TODO: move to helper methods
@@ -362,9 +381,9 @@ mod tests {
362381

363382
// Validate attributes
364383
#[cfg(not(feature = "experimental_metadata_attributes"))]
365-
assert_eq!(log.record.attributes_iter().count(), 3);
366-
#[cfg(feature = "experimental_metadata_attributes")]
367384
assert_eq!(log.record.attributes_iter().count(), 7);
385+
#[cfg(feature = "experimental_metadata_attributes")]
386+
assert_eq!(log.record.attributes_iter().count(), 11);
368387
assert!(attributes_contains(
369388
&log.record,
370389
&Key::new("event_id"),
@@ -380,6 +399,26 @@ mod tests {
380399
&Key::new("user_email"),
381400
&AnyValue::String("[email protected]".into())
382401
));
402+
assert!(attributes_contains(
403+
&log.record,
404+
&Key::new("small_u64value"),
405+
&AnyValue::Int(42.into())
406+
));
407+
assert!(attributes_contains(
408+
&log.record,
409+
&Key::new("big_u64value"),
410+
&AnyValue::String(format!("{}", u64::MAX).into())
411+
));
412+
assert!(attributes_contains(
413+
&log.record,
414+
&Key::new("small_usizevalue"),
415+
&AnyValue::Int(42.into())
416+
));
417+
assert!(attributes_contains(
418+
&log.record,
419+
&Key::new("big_usizevalue"),
420+
&AnyValue::String(format!("{}", u64::MAX).into())
421+
));
383422
#[cfg(feature = "experimental_metadata_attributes")]
384423
{
385424
assert!(attributes_contains(
@@ -753,6 +792,10 @@ mod tests {
753792
TraceFlags::SAMPLED
754793
);
755794

795+
for attribute in log.record.attributes_iter() {
796+
println!("key: {:?}, value: {:?}", attribute.0, attribute.1);
797+
}
798+
756799
// Attributes can be polluted when we don't use this feature.
757800
#[cfg(feature = "experimental_metadata_attributes")]
758801
assert_eq!(log.record.attributes_iter().count(), 4);

opentelemetry-sdk/src/logs/logger.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,11 @@ impl opentelemetry::logs::Logger for SdkLogger {
3434

3535
//let mut log_record = record;
3636
if record.trace_context.is_none() {
37-
let trace_context = Context::map_current(|cx| {
38-
cx.has_active_span()
39-
.then(|| TraceContext::from(cx.span().span_context()))
37+
Context::map_current(|cx| {
38+
cx.has_active_span().then(|| {
39+
record.trace_context = Some(TraceContext::from(cx.span().span_context()))
40+
})
4041
});
41-
42-
if let Some(ref trace_context) = trace_context {
43-
record.trace_context = Some(trace_context.clone());
44-
}
4542
}
4643
if record.observed_timestamp.is_none() {
4744
record.observed_timestamp = Some(now());

0 commit comments

Comments
 (0)