Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Logs SDK] Modify LogRecord to use Vector instead of OrderMap for attributes #1142

Merged
merged 4 commits into from
Jul 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions opentelemetry-api/src/logs/record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub struct LogRecord {
pub body: Option<AnyValue>,

/// Additional attributes associated with this record
pub attributes: Option<OrderMap<Key, AnyValue>>,
pub attributes: Option<Vec<(Key, AnyValue)>>,
}

impl LogRecord {
Expand Down Expand Up @@ -326,8 +326,9 @@ impl LogRecordBuilder {
}
}

/// Assign attributes, overriding previously set attributes
pub fn with_attributes(self, attributes: OrderMap<Key, AnyValue>) -> Self {
/// Assign attributes.
/// The SDK doesn't carry on any deduplication on these attributes.
pub fn with_attributes(self, attributes: Vec<(Key, AnyValue)>) -> Self {
TommyCpp marked this conversation as resolved.
Show resolved Hide resolved
Self {
record: LogRecord {
attributes: Some(attributes),
Expand All @@ -336,18 +337,18 @@ 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<K, V>(mut self, key: K, value: V) -> Self
where
K: Into<Key>,
V: Into<AnyValue>,
{
if let Some(ref mut map) = self.record.attributes {
map.insert(key.into(), value.into());
if let Some(ref mut vec) = self.record.attributes {
vec.push((key.into(), value.into()));
} else {
let mut map = OrderMap::with_capacity(1);
map.insert(key.into(), value.into());
self.record.attributes = Some(map);
let vec = vec![(key.into(), value.into())];
self.record.attributes = Some(vec);
}

self
Expand Down
52 changes: 23 additions & 29 deletions opentelemetry-appender-tracing/src/layer.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use opentelemetry_api::{
logs::{AnyValue, LogRecord, Logger, LoggerProvider, Severity},
Key, OrderMap,
};
use std::vec;

use opentelemetry_api::logs::{LogRecord, Logger, LoggerProvider, Severity};

use tracing_subscriber::Layer;

Expand All @@ -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.insert(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 = OrderMap::with_capacity(1);
map.insert(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.insert(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: OrderMap<Key, AnyValue> = OrderMap::with_capacity(1);
map.insert(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.insert(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 = OrderMap::with_capacity(1);
map.insert(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.insert(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 = OrderMap::with_capacity(1);
map.insert(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.insert(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 = OrderMap::with_capacity(1);
map.insert(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);
}
}

Expand Down
12 changes: 6 additions & 6 deletions opentelemetry-user-events-logs/src/logs/exporter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,22 +107,22 @@ impl UserEventsExporter {
fn add_attributes_to_event(
&self,
eb: &mut EventBuilder,
attribs: &mut dyn Iterator<Item = (&Key, &AnyValue)>,
attribs: &mut dyn Iterator<Item = &(Key, AnyValue)>,
) {
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);
Expand Down Expand Up @@ -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) => {
Expand Down