From f72fb9ec549696966ca917e6bdb3c4f4a87ff3c0 Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Wed, 5 Jul 2023 15:06:59 -0700 Subject: [PATCH 1/4] use vector for attributes --- opentelemetry-api/src/logs/record.rs | 10 +++--- opentelemetry-appender-tracing/src/layer.rs | 32 +++++++++---------- .../src/logs/exporter.rs | 10 +++--- 3 files changed, 26 insertions(+), 26 deletions(-) diff --git a/opentelemetry-api/src/logs/record.rs b/opentelemetry-api/src/logs/record.rs index 6cd47c25bb..0e3319e664 100644 --- a/opentelemetry-api/src/logs/record.rs +++ b/opentelemetry-api/src/logs/record.rs @@ -27,7 +27,7 @@ pub struct LogRecord { pub body: Option, /// Additional attributes associated with this record - pub attributes: Option>, + pub attributes: Option>, } impl LogRecord { @@ -327,7 +327,7 @@ impl LogRecordBuilder { } /// Assign attributes, overriding previously set attributes - pub fn with_attributes(self, attributes: OrderMap) -> Self { + pub fn with_attributes(self, attributes: Vec<(Key, AnyValue)>) -> Self { Self { record: LogRecord { attributes: Some(attributes), @@ -343,10 +343,10 @@ impl LogRecordBuilder { V: Into, { if let Some(ref mut map) = self.record.attributes { - map.insert(key.into(), value.into()); + map.push((key.into(), value.into())); } else { - let mut map = OrderMap::with_capacity(1); - map.insert(key.into(), value.into()); + let mut map = Vec::with_capacity(1); + map.push((key.into(), value.into())); self.record.attributes = Some(map); } diff --git a/opentelemetry-appender-tracing/src/layer.rs b/opentelemetry-appender-tracing/src/layer.rs index 1719a5223c..b9b49dab2c 100644 --- a/opentelemetry-appender-tracing/src/layer.rs +++ b/opentelemetry-appender-tracing/src/layer.rs @@ -1,6 +1,6 @@ use opentelemetry_api::{ logs::{AnyValue, LogRecord, Logger, LoggerProvider, Severity}, - Key, OrderMap, + Key, }; use tracing_subscriber::Layer; @@ -17,50 +17,50 @@ impl<'a> tracing::field::Visit for EventVisitor<'a> { if field.name() == "message" { self.log_record.body = Some(format!("{value:?}").into()); } else if let Some(ref mut map) = self.log_record.attributes { - map.insert(field.name().into(), format!("{value:?}").into()); + map.push((field.name().into(), format!("{value:?}").into())); } else { - let mut map = OrderMap::with_capacity(1); - map.insert(field.name().into(), format!("{value:?}").into()); + let mut map = Vec::with_capacity(1); + map.push((field.name().into(), format!("{value:?}").into())); self.log_record.attributes = Some(map); } } fn record_str(&mut self, field: &tracing_core::Field, value: &str) { if let Some(ref mut map) = self.log_record.attributes { - map.insert(field.name().into(), value.to_owned().into()); + map.push((field.name().into(), value.to_owned().into())); } else { - let mut map: OrderMap = OrderMap::with_capacity(1); - map.insert(field.name().into(), value.to_owned().into()); + let mut map: Vec<(Key, AnyValue)> = Vec::with_capacity(1); + map.push((field.name().into(), value.to_owned().into())); self.log_record.attributes = Some(map); } } fn record_bool(&mut self, field: &tracing_core::Field, value: bool) { if let Some(ref mut map) = self.log_record.attributes { - map.insert(field.name().into(), value.into()); + map.push((field.name().into(), value.into())); } else { - let mut map = OrderMap::with_capacity(1); - map.insert(field.name().into(), value.into()); + let mut map = Vec::with_capacity(1); + map.push((field.name().into(), value.into())); self.log_record.attributes = Some(map); } } fn record_f64(&mut self, field: &tracing::field::Field, value: f64) { if let Some(ref mut map) = self.log_record.attributes { - map.insert(field.name().into(), value.into()); + map.push((field.name().into(), value.into())); } else { - let mut map = OrderMap::with_capacity(1); - map.insert(field.name().into(), value.into()); + let mut map = Vec::with_capacity(1); + map.push((field.name().into(), value.into())); self.log_record.attributes = Some(map); } } fn record_i64(&mut self, field: &tracing::field::Field, value: i64) { if let Some(ref mut map) = self.log_record.attributes { - map.insert(field.name().into(), value.into()); + map.push((field.name().into(), value.into())); } else { - let mut map = OrderMap::with_capacity(1); - map.insert(field.name().into(), value.into()); + let mut map = Vec::with_capacity(1); + map.push((field.name().into(), value.into())); self.log_record.attributes = Some(map); } } diff --git a/opentelemetry-user-events-logs/src/logs/exporter.rs b/opentelemetry-user-events-logs/src/logs/exporter.rs index 354b1830c4..1825b4e8a5 100644 --- a/opentelemetry-user-events-logs/src/logs/exporter.rs +++ b/opentelemetry-user-events-logs/src/logs/exporter.rs @@ -107,22 +107,22 @@ impl UserEventsExporter { fn add_attributes_to_event( &self, eb: &mut EventBuilder, - attribs: &mut dyn Iterator, + attribs: &mut dyn Iterator, ) { for attrib in attribs { if attrib.0.to_string() == EVENT_ID || attrib.0.to_string() == EVENT_NAME { continue; } let field_name = &attrib.0.to_string(); - match attrib.1 { + match attrib.1.to_owned() { AnyValue::Boolean(b) => { - eb.add_value(field_name, *b, FieldFormat::Boolean, 0); + eb.add_value(field_name, b, FieldFormat::Boolean, 0); } AnyValue::Int(i) => { - eb.add_value(field_name, *i, FieldFormat::SignedInt, 0); + eb.add_value(field_name, i, FieldFormat::SignedInt, 0); } AnyValue::Double(f) => { - eb.add_value(field_name, *f, FieldFormat::Float, 0); + eb.add_value(field_name, f, FieldFormat::Float, 0); } AnyValue::String(s) => { eb.add_str(field_name, &s.to_string(), FieldFormat::Default, 0); From edd61b165466dfdbefb6f5030f5681f6f525b87c Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Thu, 6 Jul 2023 13:18:36 -0700 Subject: [PATCH 2/4] lint errors --- opentelemetry-api/src/logs/record.rs | 9 ++-- opentelemetry-appender-tracing/src/layer.rs | 52 ++++++++----------- .../src/logs/exporter.rs | 2 +- 3 files changed, 28 insertions(+), 35 deletions(-) diff --git a/opentelemetry-api/src/logs/record.rs b/opentelemetry-api/src/logs/record.rs index 0e3319e664..dfdb72e92c 100644 --- a/opentelemetry-api/src/logs/record.rs +++ b/opentelemetry-api/src/logs/record.rs @@ -342,12 +342,11 @@ impl LogRecordBuilder { K: Into, V: Into, { - if let Some(ref mut map) = self.record.attributes { - map.push((key.into(), value.into())); + if let Some(ref mut vec) = self.record.attributes { + vec.push((key.into(), value.into())); } else { - let mut map = Vec::with_capacity(1); - map.push((key.into(), value.into())); - self.record.attributes = Some(map); + let vec = vec![(key.into(), value.into())]; + self.record.attributes = Some(vec); } self diff --git a/opentelemetry-appender-tracing/src/layer.rs b/opentelemetry-appender-tracing/src/layer.rs index b9b49dab2c..14ff32a49d 100644 --- a/opentelemetry-appender-tracing/src/layer.rs +++ b/opentelemetry-appender-tracing/src/layer.rs @@ -1,7 +1,6 @@ -use opentelemetry_api::{ - logs::{AnyValue, LogRecord, Logger, LoggerProvider, Severity}, - Key, -}; +use std::vec; + +use opentelemetry_api::logs::{LogRecord, Logger, LoggerProvider, Severity}; use tracing_subscriber::Layer; @@ -16,52 +15,47 @@ impl<'a> tracing::field::Visit for EventVisitor<'a> { fn record_debug(&mut self, field: &tracing::field::Field, value: &dyn std::fmt::Debug) { if field.name() == "message" { self.log_record.body = Some(format!("{value:?}").into()); - } else if let Some(ref mut map) = self.log_record.attributes { - map.push((field.name().into(), format!("{value:?}").into())); + } else if let Some(ref mut vec) = self.log_record.attributes { + vec.push((field.name().into(), format!("{value:?}").into())); } else { - let mut map = Vec::with_capacity(1); - map.push((field.name().into(), format!("{value:?}").into())); - self.log_record.attributes = Some(map); + let vec = vec![(field.name().into(), format!("{value:?}").into())]; + self.log_record.attributes = Some(vec); } } fn record_str(&mut self, field: &tracing_core::Field, value: &str) { - if let Some(ref mut map) = self.log_record.attributes { - map.push((field.name().into(), value.to_owned().into())); + if let Some(ref mut vec) = self.log_record.attributes { + vec.push((field.name().into(), value.to_owned().into())); } else { - let mut map: Vec<(Key, AnyValue)> = Vec::with_capacity(1); - map.push((field.name().into(), value.to_owned().into())); - self.log_record.attributes = Some(map); + let vec = vec![(field.name().into(), value.to_owned().into())]; + self.log_record.attributes = Some(vec); } } fn record_bool(&mut self, field: &tracing_core::Field, value: bool) { - if let Some(ref mut map) = self.log_record.attributes { - map.push((field.name().into(), value.into())); + if let Some(ref mut vec) = self.log_record.attributes { + vec.push((field.name().into(), value.into())); } else { - let mut map = Vec::with_capacity(1); - map.push((field.name().into(), value.into())); - self.log_record.attributes = Some(map); + let vec = vec![(field.name().into(), value.into())]; + self.log_record.attributes = Some(vec); } } fn record_f64(&mut self, field: &tracing::field::Field, value: f64) { - if let Some(ref mut map) = self.log_record.attributes { - map.push((field.name().into(), value.into())); + if let Some(ref mut vec) = self.log_record.attributes { + vec.push((field.name().into(), value.into())); } else { - let mut map = Vec::with_capacity(1); - map.push((field.name().into(), value.into())); - self.log_record.attributes = Some(map); + let vec = vec![(field.name().into(), value.into())]; + self.log_record.attributes = Some(vec); } } fn record_i64(&mut self, field: &tracing::field::Field, value: i64) { - if let Some(ref mut map) = self.log_record.attributes { - map.push((field.name().into(), value.into())); + if let Some(ref mut vec) = self.log_record.attributes { + vec.push((field.name().into(), value.into())); } else { - let mut map = Vec::with_capacity(1); - map.push((field.name().into(), value.into())); - self.log_record.attributes = Some(map); + let vec = vec![(field.name().into(), value.into())]; + self.log_record.attributes = Some(vec); } } diff --git a/opentelemetry-user-events-logs/src/logs/exporter.rs b/opentelemetry-user-events-logs/src/logs/exporter.rs index 1825b4e8a5..a7853bbe58 100644 --- a/opentelemetry-user-events-logs/src/logs/exporter.rs +++ b/opentelemetry-user-events-logs/src/logs/exporter.rs @@ -221,7 +221,7 @@ impl UserEventsExporter { let (mut event_id, mut event_name) = (0, ""); let mut event_count = 0; if log_data.record.attributes.is_some() { - for (k, v) in log_data.record.attributes.as_ref().unwrap().into_iter() { + for (k, v) in log_data.record.attributes.as_ref().unwrap().iter() { if k.as_str() == EVENT_ID { event_id = match v { AnyValue::Int(value) => { From 6093b5bee8b5eb50aa360c9747de3c032e2d08c8 Mon Sep 17 00:00:00 2001 From: Lalit Date: Mon, 10 Jul 2023 00:20:30 -0700 Subject: [PATCH 3/4] add comment --- opentelemetry-api/src/logs/record.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/opentelemetry-api/src/logs/record.rs b/opentelemetry-api/src/logs/record.rs index dfdb72e92c..fb19c51bd0 100644 --- a/opentelemetry-api/src/logs/record.rs +++ b/opentelemetry-api/src/logs/record.rs @@ -326,7 +326,8 @@ impl LogRecordBuilder { } } - /// Assign attributes, overriding previously set attributes + /// Assign attributes. + /// The SDK doesn't carry on any deduplication on these attributes. pub fn with_attributes(self, attributes: Vec<(Key, AnyValue)>) -> Self { Self { record: LogRecord { @@ -336,7 +337,8 @@ impl LogRecordBuilder { } } - /// Set a single attribute for this record + /// Set a single attribute for this record. + /// The SDK doesn't carry on any deduplication on these attributes. pub fn with_attribute(mut self, key: K, value: V) -> Self where K: Into, From 5554e1940766df0657da5cb64805bb6c4dd53e5e Mon Sep 17 00:00:00 2001 From: Lalit Date: Mon, 10 Jul 2023 09:10:59 -0700 Subject: [PATCH 4/4] fix lint --- opentelemetry-api/src/logs/record.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/opentelemetry-api/src/logs/record.rs b/opentelemetry-api/src/logs/record.rs index fb19c51bd0..c98b3a7581 100644 --- a/opentelemetry-api/src/logs/record.rs +++ b/opentelemetry-api/src/logs/record.rs @@ -326,7 +326,7 @@ impl LogRecordBuilder { } } - /// Assign attributes. + /// Assign attributes. /// The SDK doesn't carry on any deduplication on these attributes. pub fn with_attributes(self, attributes: Vec<(Key, AnyValue)>) -> Self { Self {