Skip to content

Commit 1f1a4fe

Browse files
authored
[Logs SDK] Modify LogRecord to use Vector instead of OrderMap for attributes (#1142)
* use vector for attributes * lint errors * add comment * fix lint
1 parent 2d5d544 commit 1f1a4fe

File tree

3 files changed

+39
-44
lines changed

3 files changed

+39
-44
lines changed

opentelemetry-api/src/logs/record.rs

+10-9
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub struct LogRecord {
2727
pub body: Option<AnyValue>,
2828

2929
/// Additional attributes associated with this record
30-
pub attributes: Option<OrderMap<Key, AnyValue>>,
30+
pub attributes: Option<Vec<(Key, AnyValue)>>,
3131
}
3232

3333
impl LogRecord {
@@ -326,8 +326,9 @@ impl LogRecordBuilder {
326326
}
327327
}
328328

329-
/// Assign attributes, overriding previously set attributes
330-
pub fn with_attributes(self, attributes: OrderMap<Key, AnyValue>) -> Self {
329+
/// Assign attributes.
330+
/// The SDK doesn't carry on any deduplication on these attributes.
331+
pub fn with_attributes(self, attributes: Vec<(Key, AnyValue)>) -> Self {
331332
Self {
332333
record: LogRecord {
333334
attributes: Some(attributes),
@@ -336,18 +337,18 @@ impl LogRecordBuilder {
336337
}
337338
}
338339

339-
/// Set a single attribute for this record
340+
/// Set a single attribute for this record.
341+
/// The SDK doesn't carry on any deduplication on these attributes.
340342
pub fn with_attribute<K, V>(mut self, key: K, value: V) -> Self
341343
where
342344
K: Into<Key>,
343345
V: Into<AnyValue>,
344346
{
345-
if let Some(ref mut map) = self.record.attributes {
346-
map.insert(key.into(), value.into());
347+
if let Some(ref mut vec) = self.record.attributes {
348+
vec.push((key.into(), value.into()));
347349
} else {
348-
let mut map = OrderMap::with_capacity(1);
349-
map.insert(key.into(), value.into());
350-
self.record.attributes = Some(map);
350+
let vec = vec![(key.into(), value.into())];
351+
self.record.attributes = Some(vec);
351352
}
352353

353354
self

opentelemetry-appender-tracing/src/layer.rs

+23-29
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
use opentelemetry_api::{
2-
logs::{AnyValue, LogRecord, Logger, LoggerProvider, Severity},
3-
Key, OrderMap,
4-
};
1+
use std::vec;
2+
3+
use opentelemetry_api::logs::{LogRecord, Logger, LoggerProvider, Severity};
54

65
use tracing_subscriber::Layer;
76

@@ -16,52 +15,47 @@ impl<'a> tracing::field::Visit for EventVisitor<'a> {
1615
fn record_debug(&mut self, field: &tracing::field::Field, value: &dyn std::fmt::Debug) {
1716
if field.name() == "message" {
1817
self.log_record.body = Some(format!("{value:?}").into());
19-
} else if let Some(ref mut map) = self.log_record.attributes {
20-
map.insert(field.name().into(), format!("{value:?}").into());
18+
} else if let Some(ref mut vec) = self.log_record.attributes {
19+
vec.push((field.name().into(), format!("{value:?}").into()));
2120
} else {
22-
let mut map = OrderMap::with_capacity(1);
23-
map.insert(field.name().into(), format!("{value:?}").into());
24-
self.log_record.attributes = Some(map);
21+
let vec = vec![(field.name().into(), format!("{value:?}").into())];
22+
self.log_record.attributes = Some(vec);
2523
}
2624
}
2725

2826
fn record_str(&mut self, field: &tracing_core::Field, value: &str) {
29-
if let Some(ref mut map) = self.log_record.attributes {
30-
map.insert(field.name().into(), value.to_owned().into());
27+
if let Some(ref mut vec) = self.log_record.attributes {
28+
vec.push((field.name().into(), value.to_owned().into()));
3129
} else {
32-
let mut map: OrderMap<Key, AnyValue> = OrderMap::with_capacity(1);
33-
map.insert(field.name().into(), value.to_owned().into());
34-
self.log_record.attributes = Some(map);
30+
let vec = vec![(field.name().into(), value.to_owned().into())];
31+
self.log_record.attributes = Some(vec);
3532
}
3633
}
3734

3835
fn record_bool(&mut self, field: &tracing_core::Field, value: bool) {
39-
if let Some(ref mut map) = self.log_record.attributes {
40-
map.insert(field.name().into(), value.into());
36+
if let Some(ref mut vec) = self.log_record.attributes {
37+
vec.push((field.name().into(), value.into()));
4138
} else {
42-
let mut map = OrderMap::with_capacity(1);
43-
map.insert(field.name().into(), value.into());
44-
self.log_record.attributes = Some(map);
39+
let vec = vec![(field.name().into(), value.into())];
40+
self.log_record.attributes = Some(vec);
4541
}
4642
}
4743

4844
fn record_f64(&mut self, field: &tracing::field::Field, value: f64) {
49-
if let Some(ref mut map) = self.log_record.attributes {
50-
map.insert(field.name().into(), value.into());
45+
if let Some(ref mut vec) = self.log_record.attributes {
46+
vec.push((field.name().into(), value.into()));
5147
} else {
52-
let mut map = OrderMap::with_capacity(1);
53-
map.insert(field.name().into(), value.into());
54-
self.log_record.attributes = Some(map);
48+
let vec = vec![(field.name().into(), value.into())];
49+
self.log_record.attributes = Some(vec);
5550
}
5651
}
5752

5853
fn record_i64(&mut self, field: &tracing::field::Field, value: i64) {
59-
if let Some(ref mut map) = self.log_record.attributes {
60-
map.insert(field.name().into(), value.into());
54+
if let Some(ref mut vec) = self.log_record.attributes {
55+
vec.push((field.name().into(), value.into()));
6156
} else {
62-
let mut map = OrderMap::with_capacity(1);
63-
map.insert(field.name().into(), value.into());
64-
self.log_record.attributes = Some(map);
57+
let vec = vec![(field.name().into(), value.into())];
58+
self.log_record.attributes = Some(vec);
6559
}
6660
}
6761

opentelemetry-user-events-logs/src/logs/exporter.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -107,22 +107,22 @@ impl UserEventsExporter {
107107
fn add_attributes_to_event(
108108
&self,
109109
eb: &mut EventBuilder,
110-
attribs: &mut dyn Iterator<Item = (&Key, &AnyValue)>,
110+
attribs: &mut dyn Iterator<Item = &(Key, AnyValue)>,
111111
) {
112112
for attrib in attribs {
113113
if attrib.0.to_string() == EVENT_ID || attrib.0.to_string() == EVENT_NAME {
114114
continue;
115115
}
116116
let field_name = &attrib.0.to_string();
117-
match attrib.1 {
117+
match attrib.1.to_owned() {
118118
AnyValue::Boolean(b) => {
119-
eb.add_value(field_name, *b, FieldFormat::Boolean, 0);
119+
eb.add_value(field_name, b, FieldFormat::Boolean, 0);
120120
}
121121
AnyValue::Int(i) => {
122-
eb.add_value(field_name, *i, FieldFormat::SignedInt, 0);
122+
eb.add_value(field_name, i, FieldFormat::SignedInt, 0);
123123
}
124124
AnyValue::Double(f) => {
125-
eb.add_value(field_name, *f, FieldFormat::Float, 0);
125+
eb.add_value(field_name, f, FieldFormat::Float, 0);
126126
}
127127
AnyValue::String(s) => {
128128
eb.add_str(field_name, &s.to_string(), FieldFormat::Default, 0);
@@ -221,7 +221,7 @@ impl UserEventsExporter {
221221
let (mut event_id, mut event_name) = (0, "");
222222
let mut event_count = 0;
223223
if log_data.record.attributes.is_some() {
224-
for (k, v) in log_data.record.attributes.as_ref().unwrap().into_iter() {
224+
for (k, v) in log_data.record.attributes.as_ref().unwrap().iter() {
225225
if k.as_str() == EVENT_ID {
226226
event_id = match v {
227227
AnyValue::Int(value) => {

0 commit comments

Comments
 (0)