Skip to content

Commit

Permalink
Merge branch 'main' into utpilla/Use-slices-instead-references-to-vecs
Browse files Browse the repository at this point in the history
  • Loading branch information
cijothomas committed May 10, 2024
2 parents 3da3853 + 6d50e5f commit ba607a3
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 19 deletions.
62 changes: 49 additions & 13 deletions opentelemetry-sdk/src/attributes/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ use std::{
use opentelemetry::{Array, Key, KeyValue, Value};
use ordered_float::OrderedFloat;

use crate::Resource;

#[derive(Clone, Debug)]
struct HashKeyValue(KeyValue);

Expand Down Expand Up @@ -87,17 +85,6 @@ impl From<&[KeyValue]> for AttributeSet {
}
}

impl From<&Resource> for AttributeSet {
fn from(values: &Resource) -> Self {
let vec = values
.iter()
.map(|(key, value)| HashKeyValue(KeyValue::new(key.clone(), value.clone())))
.collect::<Vec<_>>();

AttributeSet::new(vec)
}
}

fn calculate_hash(values: &[HashKeyValue]) -> u64 {
let mut hasher = DefaultHasher::new();
values.iter().fold(&mut hasher, |mut hasher, item| {
Expand Down Expand Up @@ -146,3 +133,52 @@ impl Hash for AttributeSet {
state.write_u64(self.1)
}
}

#[cfg(test)]
mod tests {
use std::hash::DefaultHasher;
use std::hash::{Hash, Hasher};

use crate::attributes::set::HashKeyValue;
use opentelemetry::KeyValue;

#[test]
fn equality_kv_float() {
let kv1 = HashKeyValue(KeyValue::new("key", 1.0));
let kv2 = HashKeyValue(KeyValue::new("key", 1.0));
assert_eq!(kv1, kv2);

let kv1 = HashKeyValue(KeyValue::new("key", 1.0));
let kv2 = HashKeyValue(KeyValue::new("key", 1.01));
assert_ne!(kv1, kv2);

let kv1 = HashKeyValue(KeyValue::new("key", std::f64::NAN));
let kv2 = HashKeyValue(KeyValue::new("key", std::f64::NAN));
assert_eq!(kv1, kv2);

let kv1 = HashKeyValue(KeyValue::new("key", std::f64::INFINITY));
let kv2 = HashKeyValue(KeyValue::new("key", std::f64::INFINITY));
assert_eq!(kv1, kv2);
}

#[test]
fn hash_kv_float() {
let kv1 = HashKeyValue(KeyValue::new("key", 1.0));
let kv2 = HashKeyValue(KeyValue::new("key", 1.0));
assert_eq!(hash_helper(&kv1), hash_helper(&kv2));

let kv1 = HashKeyValue(KeyValue::new("key", std::f64::NAN));
let kv2 = HashKeyValue(KeyValue::new("key", std::f64::NAN));
assert_eq!(hash_helper(&kv1), hash_helper(&kv2));

let kv1 = HashKeyValue(KeyValue::new("key", std::f64::INFINITY));
let kv2 = HashKeyValue(KeyValue::new("key", std::f64::INFINITY));
assert_eq!(hash_helper(&kv1), hash_helper(&kv2));
}

fn hash_helper<T: Hash>(item: &T) -> u64 {
let mut hasher = DefaultHasher::new();
item.hash(&mut hasher);
hasher.finish()
}
}
5 changes: 2 additions & 3 deletions opentelemetry-stdout/src/logs/transform.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use std::{borrow::Cow, collections::HashMap, time::SystemTime};

use crate::common::{
as_human_readable, as_opt_human_readable, as_opt_unix_nano, as_unix_nano, KeyValue, Resource,
Scope, Value,
as_human_readable, as_opt_human_readable, as_opt_unix_nano, as_unix_nano, AttributeSet,
KeyValue, Resource, Scope, Value,
};
use opentelemetry_sdk::AttributeSet;
use serde::Serialize;

/// Transformed logs data that can be serialized.
Expand Down
3 changes: 1 addition & 2 deletions opentelemetry-stdout/src/trace/transform.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::common::{as_human_readable, as_unix_nano, KeyValue, Resource, Scope};
use opentelemetry_sdk::AttributeSet;
use crate::common::{as_human_readable, as_unix_nano, AttributeSet, KeyValue, Resource, Scope};
use serde::{Serialize, Serializer};
use std::{borrow::Cow, collections::HashMap, time::SystemTime};

Expand Down
3 changes: 2 additions & 1 deletion opentelemetry/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@

### Changed

- Deprecate `versioned_logger()` in favor of `logger_builder()` [1567](https://github.com/open-telemetry/opentelemetry-rust/pull/1567).
- **BREAKING** Moving LogRecord implementation to the SDK. [1702](https://github.com/open-telemetry/opentelemetry-rust/pull/1702).
- Relocated `LogRecord` struct to SDK.
- Introduced the `LogRecord` trait in the API for populating log records. This trait is implemented by the SDK.
This is the breaking change for the authors of Log Appenders. Refer to the [opentelemetry-appender-tracing](https://github.com/open-telemetry/opentelemetry-rust/tree/main/opentelemetry-appender-tracing) for more details.

- Deprecate `versioned_logger()` in favor of `logger_builder()` [1567](https://github.com/open-telemetry/opentelemetry-rust/pull/1567).

Before:

```rust
Expand Down

0 comments on commit ba607a3

Please sign in to comment.