Skip to content

Commit 8c3932d

Browse files
committed
fix: include attributes in logs when nested in tracing crate's spans
1 parent 8b3fc06 commit 8c3932d

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

opentelemetry-appender-tracing/src/layer.rs

+8
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,14 @@ where
262262
if let Some((trace_id, span_id)) = opt_trace_id.zip(opt_span_id) {
263263
log_record.set_trace_context(trace_id, span_id, None);
264264
}
265+
266+
if let Some(otd) = span.extensions().get::<OtelData>() {
267+
if let Some(attributes) = &otd.builder.attributes {
268+
for attribute in attributes {
269+
log_record.add_attribute(attribute.key.clone(), &attribute.value);
270+
}
271+
}
272+
}
265273
}
266274

267275
//emit record

opentelemetry/src/logs/record.rs

+23-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{Key, StringValue};
1+
use crate::{Array, Key, StringValue, Value};
22

33
use crate::{SpanId, TraceFlags, TraceId};
44

@@ -121,6 +121,28 @@ impl From<&[u8]> for AnyValue {
121121
}
122122
}
123123

124+
impl From<&Value> for AnyValue {
125+
fn from(value: &Value) -> Self {
126+
match value {
127+
Value::Bool(b) => AnyValue::Boolean(*b),
128+
Value::I64(i) => AnyValue::Int(*i),
129+
Value::F64(f) => AnyValue::Double(*f),
130+
Value::String(s) => AnyValue::String(s.clone()),
131+
Value::Array(a) => {
132+
let v = match a {
133+
Array::Bool(items) => items.iter().map(|b| AnyValue::Boolean(*b)).collect(),
134+
Array::I64(items) => items.iter().map(|i| AnyValue::Int(*i)).collect(),
135+
Array::F64(items) => items.iter().map(|f| AnyValue::Double(*f)).collect(),
136+
Array::String(items) => {
137+
items.iter().map(|s| AnyValue::String(s.clone())).collect()
138+
}
139+
};
140+
AnyValue::ListAny(Box::new(v))
141+
}
142+
}
143+
}
144+
}
145+
124146
impl<T: Into<AnyValue>> FromIterator<T> for AnyValue {
125147
/// Creates an [`AnyValue::ListAny`] value from a sequence of `Into<AnyValue>` values.
126148
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {

0 commit comments

Comments
 (0)