Skip to content

Commit a95e681

Browse files
committed
cont..
1 parent 2bfde32 commit a95e681

File tree

8 files changed

+70
-94
lines changed

8 files changed

+70
-94
lines changed

opentelemetry-proto/src/transform/logs.rs

+9-12
Original file line numberDiff line numberDiff line change
@@ -89,20 +89,14 @@ pub mod tonic {
8989
LogRecord {
9090
time_unix_nano: log_record.timestamp.map(to_nanos).unwrap_or_default(),
9191
observed_time_unix_nano: to_nanos(log_record.observed_timestamp.unwrap()),
92-
severity_number: severity_number.into(),
93-
severity_text: log_record.severity_text.map(Into::into).unwrap_or_default(),
94-
body: log_record.body.map(Into::into),
9592
attributes: {
9693
let mut attributes: Vec<KeyValue> = log_record
97-
.attributes
98-
.iter()
99-
.filter_map(|kv| {
100-
kv.as_ref().map(|(k, v)| KeyValue {
101-
key: k.to_string(),
102-
value: Some(AnyValue {
103-
value: Some(v.clone().into()),
104-
}),
105-
})
94+
.attributes_iter()
95+
.map(|kv| KeyValue {
96+
key: kv.0.to_string(),
97+
value: Some(AnyValue {
98+
value: Some(kv.1.clone().into()),
99+
}),
106100
})
107101
.collect();
108102

@@ -116,6 +110,9 @@ pub mod tonic {
116110
}
117111
attributes
118112
},
113+
severity_number: severity_number.into(),
114+
severity_text: log_record.severity_text.map(Into::into).unwrap_or_default(),
115+
body: log_record.body.map(Into::into),
119116
dropped_attributes_count: 0,
120117
flags: trace_context
121118
.map(|ctx| {

opentelemetry-sdk/Cargo.toml

+4-4
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ name = "log"
9898
harness = false
9999
required-features = ["logs"]
100100

101-
[[bench]]
102-
name = "growable_array"
103-
harness = false
104-
required-features = ["logs"]
101+
#[[bench]]
102+
#name = "growable_array"
103+
#harness = false
104+
#required-features = ["logs"]

opentelemetry-sdk/benches/growable_array.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
/*
12
use criterion::{criterion_group, criterion_main, Criterion};
23
use opentelemetry::logs::AnyValue;
34
use opentelemetry::Key;
4-
use opentelemetry_sdk::logs::GrowableArray;
5+
use opentelemetry_sdk::logs::growable_array::GrowableArray;
56
67
#[derive(Clone, Debug, PartialEq)]
78
pub struct KeyValuePair(Key, AnyValue);
@@ -95,3 +96,4 @@ criterion_group!(
9596
vec_iteration_benchmark
9697
);
9798
criterion_main!(benches);
99+
*/

opentelemetry-sdk/src/logs/growable_array.rs

+9-52
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const DEFAULT_INITIAL_VEC_CAPACITY: usize = 5;
77

88
#[derive(Debug, Clone, PartialEq)]
99
/// A hybrid vector that starts with a fixed-size array and grows dynamically with a vector.
10-
pub struct GrowableArray<
10+
pub(crate) struct GrowableArray<
1111
T: Default + Clone + PartialEq,
1212
const MAX_STACK_CAPACITY: usize = DEFAULT_MAX_STACK_CAPACITY,
1313
const INITIAL_VEC_CAPACITY: usize = DEFAULT_INITIAL_VEC_CAPACITY,
@@ -39,12 +39,12 @@ impl<
3939
> GrowableArray<T, MAX_STACK_CAPACITY, INITIAL_VEC_CAPACITY>
4040
{
4141
/// Creates a new `GrowableArray` with the default initial capacity.
42-
pub fn new() -> Self {
42+
pub(crate) fn new() -> Self {
4343
Self::default()
4444
}
4545

4646
/// Pushes a value into the `GrowableArray`.
47-
pub fn push(&mut self, value: T) {
47+
pub(crate) fn push(&mut self, value: T) {
4848
if self.count < MAX_STACK_CAPACITY {
4949
self.initial[self.count] = value;
5050
self.count += 1;
@@ -58,7 +58,7 @@ impl<
5858
}
5959

6060
/// Gets a reference to the value at the specified index.
61-
pub fn get(&self, index: usize) -> Option<&T> {
61+
pub(crate) fn get(&self, index: usize) -> Option<&T> {
6262
if index < self.count {
6363
Some(&self.initial[index])
6464
} else if let Some(ref additional) = self.additional {
@@ -69,12 +69,12 @@ impl<
6969
}
7070

7171
/// Returns the number of elements in the `GrowableArray`.
72-
pub fn len(&self) -> usize {
72+
pub(crate) fn len(&self) -> usize {
7373
self.count + self.additional.as_ref().map_or(0, Vec::len)
7474
}
7575

7676
/// Returns an iterator over the elements in the `GrowableArray`.
77-
pub fn iter(&self) -> GrowableArrayIter<'_, T, MAX_STACK_CAPACITY> {
77+
pub(crate) fn iter(&self) -> GrowableArrayIter<'_, T, MAX_STACK_CAPACITY> {
7878
if self.additional.is_none() || self.additional.as_ref().unwrap().is_empty() {
7979
GrowableArrayIter::StackOnly {
8080
iter: self.initial.iter().take(self.count),
@@ -86,37 +86,6 @@ impl<
8686
}
8787
}
8888
}
89-
90-
/// Checks if the `GrowableArray` contains the specified value.
91-
pub fn contains(&self, value: &T) -> bool {
92-
self.initial[..self.count].contains(value)
93-
|| self
94-
.additional
95-
.as_ref()
96-
.map_or(false, |vec| vec.contains(value))
97-
}
98-
99-
/// Maps each element to a new `GrowableArray` using the provided function.
100-
pub fn map<U: Default + Clone + PartialEq, F>(
101-
&self,
102-
mut f: F,
103-
) -> GrowableArray<U, MAX_STACK_CAPACITY>
104-
where
105-
F: FnMut(&T) -> U,
106-
{
107-
let mut new_vec = GrowableArray::<U, MAX_STACK_CAPACITY>::new();
108-
109-
for i in 0..self.count {
110-
new_vec.push(f(&self.initial[i]));
111-
}
112-
if let Some(ref additional) = self.additional {
113-
for value in additional {
114-
new_vec.push(f(value));
115-
}
116-
}
117-
118-
new_vec
119-
}
12089
}
12190

12291
// Implement `IntoIterator` for `GrowableArray`
@@ -142,7 +111,8 @@ impl<T: Default + Clone + PartialEq, const INITIAL_CAPACITY: usize> IntoIterator
142111

143112
#[derive(Debug)]
144113
/// Iterator for consuming a `GrowableArray`.
145-
pub enum GrowableArrayIntoIter<T: Default + Clone + PartialEq, const INITIAL_CAPACITY: usize> {
114+
pub(crate) enum GrowableArrayIntoIter<T: Default + Clone + PartialEq, const INITIAL_CAPACITY: usize>
115+
{
146116
/// stackonly
147117
StackOnly {
148118
/// iter
@@ -196,7 +166,7 @@ impl<'a, T: Default + Clone + PartialEq + 'a, const INITIAL_CAPACITY: usize> Int
196166

197167
#[derive(Debug)]
198168
/// Iterator for referencing elements in a `GrowableArray`.
199-
pub enum GrowableArrayIter<'a, T: Default, const INITIAL_CAPACITY: usize> {
169+
pub(crate) enum GrowableArrayIter<'a, T: Default, const INITIAL_CAPACITY: usize> {
200170
/// stackonly
201171
StackOnly {
202172
/// iter
@@ -319,17 +289,4 @@ mod tests {
319289
assert_eq!(iter.next(), Some(KeyValuePair(key2, value2)));
320290
assert_eq!(iter.next(), None);
321291
}
322-
323-
#[test]
324-
fn test_contains() {
325-
let mut collection = GrowableArray::<i32>::new();
326-
for i in 0..10 {
327-
collection.push(i);
328-
}
329-
assert!(collection.contains(&5));
330-
assert!(!collection.contains(&15));
331-
332-
collection.push(15);
333-
assert!(collection.contains(&15));
334-
}
335292
}

opentelemetry-sdk/src/logs/log_processor.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ mod tests {
509509
BatchLogProcessor, OTEL_BLRP_EXPORT_TIMEOUT, OTEL_BLRP_MAX_EXPORT_BATCH_SIZE,
510510
OTEL_BLRP_MAX_QUEUE_SIZE, OTEL_BLRP_SCHEDULE_DELAY,
511511
};
512-
use crate::logs::AttributesGrowableArray;
512+
use crate::logs::record::AttributesGrowableArray;
513513
use crate::testing::logs::InMemoryLogsExporterBuilder;
514514
use crate::{
515515
export::logs::{LogData, LogExporter},

opentelemetry-sdk/src/logs/mod.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
//! # OpenTelemetry Log SDK
22
3-
mod growable_array;
3+
pub(crate) mod growable_array;
44
mod log_emitter;
55
mod log_processor;
6-
mod record;
6+
pub(crate) mod record;
77

8-
pub use growable_array::{GrowableArray, GrowableArrayIntoIter, GrowableArrayIter};
98
pub use log_emitter::{Builder, Logger, LoggerProvider};
109
pub use log_processor::{
1110
BatchConfig, BatchConfigBuilder, BatchLogProcessor, BatchLogProcessorBuilder, LogProcessor,
1211
SimpleLogProcessor,
1312
};
14-
pub use record::{AttributesGrowableArray, LogRecord, TraceContext};
13+
pub use record::{LogRecord, TraceContext};
1514

1615
#[cfg(all(test, feature = "testing"))]
1716
mod tests {

opentelemetry-sdk/src/logs/record.rs

+26-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::growable_array::GrowableArray;
1+
use crate::logs::growable_array::GrowableArray;
22
use opentelemetry::{
33
logs::{AnyValue, Severity},
44
trace::{SpanContext, SpanId, TraceFlags, TraceId},
@@ -12,7 +12,7 @@ use std::{borrow::Cow, time::SystemTime};
1212
const PREALLOCATED_ATTRIBUTE_CAPACITY: usize = 8;
1313

1414
/// A vector of `Option<(Key, AnyValue)>` with default capacity.
15-
pub type AttributesGrowableArray =
15+
pub(crate) type AttributesGrowableArray =
1616
GrowableArray<Option<(Key, AnyValue)>, PREALLOCATED_ATTRIBUTE_CAPACITY>;
1717

1818
#[derive(Debug, Default, Clone, PartialEq)]
@@ -44,7 +44,7 @@ pub struct LogRecord {
4444
pub body: Option<AnyValue>,
4545

4646
/// Additional attributes associated with this record
47-
pub attributes: AttributesGrowableArray,
47+
pub(crate) attributes: AttributesGrowableArray,
4848
}
4949

5050
impl opentelemetry::logs::LogRecord for LogRecord {
@@ -103,6 +103,29 @@ impl opentelemetry::logs::LogRecord for LogRecord {
103103
}
104104
}
105105

106+
impl LogRecord {
107+
/// Provides an iterator over the attributes.
108+
pub fn attributes_iter(&self) -> impl Iterator<Item = &(Key, AnyValue)> {
109+
self.attributes.iter().filter_map(|opt| opt.as_ref())
110+
}
111+
112+
/// Returns the number of attributes in the `LogRecord`.
113+
pub fn attributes_len(&self) -> usize {
114+
self.attributes.len()
115+
}
116+
117+
/// Checks if the `LogRecord` contains the specified attribute.
118+
pub fn contains_attribute(&self, key: &Key, value: &AnyValue) -> bool {
119+
self.attributes.iter().any(|opt| {
120+
if let Some((k, v)) = opt {
121+
k == key && v == value
122+
} else {
123+
false
124+
}
125+
})
126+
}
127+
}
128+
106129
/// TraceContext stores the trace context for logs that have an associated
107130
/// span.
108131
#[derive(Debug, Clone, PartialEq)]

opentelemetry-stdout/src/logs/transform.rs

+15-17
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use crate::common::{
44
as_human_readable, as_opt_human_readable, as_opt_unix_nano, as_unix_nano, AttributeSet,
55
KeyValue, Resource, Scope, Value,
66
};
7-
use opentelemetry_sdk::logs::AttributesGrowableArray;
87
use serde::Serialize;
98

109
/// Transformed logs data that can be serialized.
@@ -108,6 +107,21 @@ struct LogRecord {
108107
impl From<opentelemetry_sdk::export::logs::LogData> for LogRecord {
109108
fn from(value: opentelemetry_sdk::export::logs::LogData) -> Self {
110109
LogRecord {
110+
attributes: {
111+
let mut attributes = value
112+
.record
113+
.attributes_iter()
114+
.map(|(k, v)| KeyValue::from((k.clone(), v.clone()))) // Map each pair to a KeyValue
115+
.collect::<Vec<KeyValue>>(); // Collect into a Vec<KeyValue>s
116+
117+
if let Some(event_name) = &value.record.event_name {
118+
attributes.push(KeyValue::from((
119+
"name".into(),
120+
opentelemetry::Value::String(event_name.clone().into()),
121+
)));
122+
}
123+
attributes
124+
},
111125
trace_id: value
112126
.record
113127
.trace_context
@@ -132,22 +146,6 @@ impl From<opentelemetry_sdk::export::logs::LogData> for LogRecord {
132146
.severity_number
133147
.map(|u| u as u32)
134148
.unwrap_or_default(),
135-
attributes: {
136-
let mut attributes = value
137-
.record
138-
.attributes
139-
.iter()
140-
.filter_map(|kv| kv.clone().map(|(k, v)| (k, v).into())) // Filter out None values and convert to KeyValue
141-
.collect::<Vec<_>>();
142-
143-
if let Some(event_name) = &value.record.event_name {
144-
attributes.push(KeyValue::from((
145-
"name".into(),
146-
opentelemetry::Value::String(event_name.clone().into()),
147-
)));
148-
}
149-
attributes
150-
},
151149
dropped_attributes_count: 0,
152150
severity_text: value.record.severity_text,
153151
body: value.record.body.map(|a| a.into()),

0 commit comments

Comments
 (0)