Skip to content

Commit

Permalink
doc(resource): update docs to be accurate with current behavior (#2568)
Browse files Browse the repository at this point in the history
  • Loading branch information
pitoniak32 authored Jan 27, 2025
1 parent 18834f5 commit 6fa9ae2
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions opentelemetry-sdk/src/resource/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,7 @@ impl Resource {

/// Create a new `Resource` from key value pairs.
///
/// Values are de-duplicated by key, and the first key-value pair with a non-empty string value
/// will be retained
/// Values are de-duplicated by key, and the last key-value pair will be retained
pub(crate) fn new<T: IntoIterator<Item = KeyValue>>(kvs: T) -> Self {
let mut attrs = HashMap::new();
for kv in kvs {
Expand Down Expand Up @@ -331,18 +330,24 @@ mod tests {

use super::*;

#[test]
fn new_resource() {
let args_with_dupe_keys = [KeyValue::new("a", ""), KeyValue::new("a", "final")];

let mut expected_attrs = HashMap::new();
expected_attrs.insert(Key::new("a"), Value::from("final"));
#[rstest]
#[case([KeyValue::new("a", ""), KeyValue::new("a", "final")], [(Key::new("a"), Value::from("final"))])]
#[case([KeyValue::new("a", "final"), KeyValue::new("a", "")], [(Key::new("a"), Value::from(""))])]
fn new_resource(
#[case] given_attributes: [KeyValue; 2],
#[case] expected_attrs: [(Key, Value); 1],
) {
// Arrange
let expected = HashMap::from_iter(expected_attrs.into_iter());

// Act
let resource = Resource::builder_empty()
.with_attributes(args_with_dupe_keys)
.with_attributes(given_attributes)
.build();
let resource_inner = Arc::try_unwrap(resource.inner).expect("Failed to unwrap Arc");
assert_eq!(resource_inner.attrs, expected_attrs);

// Assert
assert_eq!(resource_inner.attrs, expected);
assert_eq!(resource_inner.schema_url, None);
}

Expand Down

0 comments on commit 6fa9ae2

Please sign in to comment.