Skip to content

Commit 6d50e5f

Browse files
cijothomasTommyCpp
andauthored
Remove SDK's attributeset usage in stdout, add tests (#1730)
Co-authored-by: Zhongyang Wu <zhongyang.wu@outlook.com>
1 parent 6da455b commit 6d50e5f

File tree

3 files changed

+52
-18
lines changed

3 files changed

+52
-18
lines changed

opentelemetry-sdk/src/attributes/set.rs

+49-13
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ use std::{
88
use opentelemetry::{Array, Key, KeyValue, Value};
99
use ordered_float::OrderedFloat;
1010

11-
use crate::Resource;
12-
1311
#[derive(Clone, Debug)]
1412
struct HashKeyValue(KeyValue);
1513

@@ -87,17 +85,6 @@ impl From<&[KeyValue]> for AttributeSet {
8785
}
8886
}
8987

90-
impl From<&Resource> for AttributeSet {
91-
fn from(values: &Resource) -> Self {
92-
let vec = values
93-
.iter()
94-
.map(|(key, value)| HashKeyValue(KeyValue::new(key.clone(), value.clone())))
95-
.collect::<Vec<_>>();
96-
97-
AttributeSet::new(vec)
98-
}
99-
}
100-
10188
fn calculate_hash(values: &[HashKeyValue]) -> u64 {
10289
let mut hasher = DefaultHasher::new();
10390
values.iter().fold(&mut hasher, |mut hasher, item| {
@@ -146,3 +133,52 @@ impl Hash for AttributeSet {
146133
state.write_u64(self.1)
147134
}
148135
}
136+
137+
#[cfg(test)]
138+
mod tests {
139+
use std::hash::DefaultHasher;
140+
use std::hash::{Hash, Hasher};
141+
142+
use crate::attributes::set::HashKeyValue;
143+
use opentelemetry::KeyValue;
144+
145+
#[test]
146+
fn equality_kv_float() {
147+
let kv1 = HashKeyValue(KeyValue::new("key", 1.0));
148+
let kv2 = HashKeyValue(KeyValue::new("key", 1.0));
149+
assert_eq!(kv1, kv2);
150+
151+
let kv1 = HashKeyValue(KeyValue::new("key", 1.0));
152+
let kv2 = HashKeyValue(KeyValue::new("key", 1.01));
153+
assert_ne!(kv1, kv2);
154+
155+
let kv1 = HashKeyValue(KeyValue::new("key", std::f64::NAN));
156+
let kv2 = HashKeyValue(KeyValue::new("key", std::f64::NAN));
157+
assert_eq!(kv1, kv2);
158+
159+
let kv1 = HashKeyValue(KeyValue::new("key", std::f64::INFINITY));
160+
let kv2 = HashKeyValue(KeyValue::new("key", std::f64::INFINITY));
161+
assert_eq!(kv1, kv2);
162+
}
163+
164+
#[test]
165+
fn hash_kv_float() {
166+
let kv1 = HashKeyValue(KeyValue::new("key", 1.0));
167+
let kv2 = HashKeyValue(KeyValue::new("key", 1.0));
168+
assert_eq!(hash_helper(&kv1), hash_helper(&kv2));
169+
170+
let kv1 = HashKeyValue(KeyValue::new("key", std::f64::NAN));
171+
let kv2 = HashKeyValue(KeyValue::new("key", std::f64::NAN));
172+
assert_eq!(hash_helper(&kv1), hash_helper(&kv2));
173+
174+
let kv1 = HashKeyValue(KeyValue::new("key", std::f64::INFINITY));
175+
let kv2 = HashKeyValue(KeyValue::new("key", std::f64::INFINITY));
176+
assert_eq!(hash_helper(&kv1), hash_helper(&kv2));
177+
}
178+
179+
fn hash_helper<T: Hash>(item: &T) -> u64 {
180+
let mut hasher = DefaultHasher::new();
181+
item.hash(&mut hasher);
182+
hasher.finish()
183+
}
184+
}

opentelemetry-stdout/src/logs/transform.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
use std::{borrow::Cow, collections::HashMap, time::SystemTime};
22

33
use crate::common::{
4-
as_human_readable, as_opt_human_readable, as_opt_unix_nano, as_unix_nano, KeyValue, Resource,
5-
Scope, Value,
4+
as_human_readable, as_opt_human_readable, as_opt_unix_nano, as_unix_nano, AttributeSet,
5+
KeyValue, Resource, Scope, Value,
66
};
7-
use opentelemetry_sdk::AttributeSet;
87
use serde::Serialize;
98

109
/// Transformed logs data that can be serialized.

opentelemetry-stdout/src/trace/transform.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
use crate::common::{as_human_readable, as_unix_nano, KeyValue, Resource, Scope};
2-
use opentelemetry_sdk::AttributeSet;
1+
use crate::common::{as_human_readable, as_unix_nano, AttributeSet, KeyValue, Resource, Scope};
32
use serde::{Serialize, Serializer};
43
use std::{borrow::Cow, collections::HashMap, time::SystemTime};
54

0 commit comments

Comments
 (0)