Skip to content
3 changes: 3 additions & 0 deletions opentelemetry-sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## vNext

### Added
- Added `Resource::get_ref(&self, key: &Key) -> Option<&Value>` to allow retrieving a reference to a resource value without cloning.

## 0.31.0

Released 2025-Sep-25
Expand Down
40 changes: 20 additions & 20 deletions opentelemetry-sdk/src/logs/logger_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -424,44 +424,44 @@ mod tests {
assert_eq!(
processor
.resource()
.get(&Key::from_static_str(resource_key))
.get_ref(&Key::from_static_str(resource_key))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have requested for some additional information on the need of this new method here.

Irrespective of that, I am fine adding the new method. One comment - unless really needed, let's keep all existing usage unmodified in this PR. If needed, I'd prefer to have a follow up PR updating all usages to the new method. This helps keep PRs short and easy to see that this is purely additive change.

.map(|v| v.to_string()),
expect.map(|s| s.to_string())
);

assert_eq!(
exporter
.resource()
.get(&Key::from_static_str(resource_key))
.get_ref(&Key::from_static_str(resource_key))
.map(|v| v.to_string()),
expect.map(|s| s.to_string())
);
};
let assert_telemetry_resource =
|processor: &TestProcessorForResource, exporter: &TestExporterForResource| {
assert_eq!(
processor.resource().get(&TELEMETRY_SDK_LANGUAGE.into()),
Some(Value::from("rust"))
processor.resource().get_ref(&TELEMETRY_SDK_LANGUAGE.into()),
Some(&Value::from("rust"))
);
assert_eq!(
processor.resource().get(&TELEMETRY_SDK_NAME.into()),
Some(Value::from("opentelemetry"))
processor.resource().get_ref(&TELEMETRY_SDK_NAME.into()),
Some(&Value::from("opentelemetry"))
);
assert_eq!(
processor.resource().get(&TELEMETRY_SDK_VERSION.into()),
Some(Value::from(env!("CARGO_PKG_VERSION")))
processor.resource().get_ref(&TELEMETRY_SDK_VERSION.into()),
Some(&Value::from(env!("CARGO_PKG_VERSION")))
);
assert_eq!(
exporter.resource().get(&TELEMETRY_SDK_LANGUAGE.into()),
Some(Value::from("rust"))
exporter.resource().get_ref(&TELEMETRY_SDK_LANGUAGE.into()),
Some(&Value::from("rust"))
);
assert_eq!(
exporter.resource().get(&TELEMETRY_SDK_NAME.into()),
Some(Value::from("opentelemetry"))
exporter.resource().get_ref(&TELEMETRY_SDK_NAME.into()),
Some(&Value::from("opentelemetry"))
);
assert_eq!(
exporter.resource().get(&TELEMETRY_SDK_VERSION.into()),
Some(Value::from(env!("CARGO_PKG_VERSION")))
exporter.resource().get_ref(&TELEMETRY_SDK_VERSION.into()),
Some(&Value::from(env!("CARGO_PKG_VERSION")))
);
};

Expand Down Expand Up @@ -872,16 +872,16 @@ mod tests {
let resource = builder.resource.unwrap();

assert_eq!(
resource.get(&Key::from_static_str("key1")),
Some(Value::from("value1"))
resource.get_ref(&Key::from_static_str("key1")),
Some(&Value::from("value1"))
);
assert_eq!(
resource.get(&Key::from_static_str("key2")),
Some(Value::from("value2"))
resource.get_ref(&Key::from_static_str("key2")),
Some(&Value::from("value2"))
);
assert_eq!(
resource.get(&Key::from_static_str("key3")),
Some(Value::from("value3"))
resource.get_ref(&Key::from_static_str("key3")),
Some(&Value::from("value3"))
);
assert_eq!(resource.schema_url(), Some("http://example.com"));
}
Expand Down
26 changes: 13 additions & 13 deletions opentelemetry-sdk/src/metrics/meter_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ mod tests {
assert_eq!(
provider.inner.pipes.0[0]
.resource
.get(&Key::from_static_str(resource_key))
.get_ref(&Key::from_static_str(resource_key))
.map(|v| v.to_string()),
expect.map(|s| s.to_string())
);
Expand All @@ -438,20 +438,20 @@ mod tests {
assert_eq!(
provider.inner.pipes.0[0]
.resource
.get(&TELEMETRY_SDK_LANGUAGE.into()),
Some(Value::from("rust"))
.get_ref(&TELEMETRY_SDK_LANGUAGE.into()),
Some(&Value::from("rust"))
);
assert_eq!(
provider.inner.pipes.0[0]
.resource
.get(&TELEMETRY_SDK_NAME.into()),
Some(Value::from("opentelemetry"))
.get_ref(&TELEMETRY_SDK_NAME.into()),
Some(&Value::from("opentelemetry"))
);
assert_eq!(
provider.inner.pipes.0[0]
.resource
.get(&TELEMETRY_SDK_VERSION.into()),
Some(Value::from(env!("CARGO_PKG_VERSION")))
.get_ref(&TELEMETRY_SDK_VERSION.into()),
Some(&Value::from(env!("CARGO_PKG_VERSION")))
);
};

Expand Down Expand Up @@ -716,16 +716,16 @@ mod tests {
let resource = builder.resource.unwrap();

assert_eq!(
resource.get(&Key::from_static_str("key1")),
Some(Value::from("value1"))
resource.get_ref(&Key::from_static_str("key1")),
Some(&Value::from("value1"))
);
assert_eq!(
resource.get(&Key::from_static_str("key2")),
Some(Value::from("value2"))
resource.get_ref(&Key::from_static_str("key2")),
Some(&Value::from("value2"))
);
assert_eq!(
resource.get(&Key::from_static_str("key3")),
Some(Value::from("value3"))
resource.get_ref(&Key::from_static_str("key3")),
Some(&Value::from("value3"))
);
assert_eq!(resource.schema_url(), Some("http://example.com"));
}
Expand Down
16 changes: 8 additions & 8 deletions opentelemetry-sdk/src/resource/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,15 @@ mod tests {
// Ensure no env var set
let no_env = SdkProvidedResourceDetector.detect();
assert_eq!(
no_env.get(&Key::from_static_str(crate::resource::SERVICE_NAME)),
Some(Value::from("unknown_service")),
no_env.get_ref(&Key::from_static_str(crate::resource::SERVICE_NAME)),
Some(&Value::from("unknown_service")),
);

temp_env::with_var(OTEL_SERVICE_NAME, Some("test service"), || {
let with_service = SdkProvidedResourceDetector.detect();
assert_eq!(
with_service.get(&Key::from_static_str(crate::resource::SERVICE_NAME)),
Some(Value::from("test service")),
with_service.get_ref(&Key::from_static_str(crate::resource::SERVICE_NAME)),
Some(&Value::from("test service")),
)
});

Expand All @@ -156,8 +156,8 @@ mod tests {
|| {
let with_service = SdkProvidedResourceDetector.detect();
assert_eq!(
with_service.get(&Key::from_static_str(crate::resource::SERVICE_NAME)),
Some(Value::from("test service1")),
with_service.get_ref(&Key::from_static_str(crate::resource::SERVICE_NAME)),
Some(&Value::from("test service1")),
)
},
);
Expand All @@ -171,8 +171,8 @@ mod tests {
|| {
let with_service = SdkProvidedResourceDetector.detect();
assert_eq!(
with_service.get(&Key::from_static_str(crate::resource::SERVICE_NAME)),
Some(Value::from("test service"))
with_service.get_ref(&Key::from_static_str(crate::resource::SERVICE_NAME)),
Some(&Value::from("test service"))
);
},
);
Expand Down
5 changes: 5 additions & 0 deletions opentelemetry-sdk/src/resource/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,11 @@ impl Resource {
pub fn get(&self, key: &Key) -> Option<Value> {
self.inner.attrs.get(key).cloned()
}

/// Returns a reference to the value for the resource associated with the given key without cloning.
pub fn get_ref(&self, key: &Key) -> Option<&Value> {
self.inner.attrs.get(key)
}
}

/// An iterator over the entries of a `Resource`.
Expand Down
29 changes: 16 additions & 13 deletions opentelemetry-sdk/src/trace/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,7 @@ mod tests {
provider
.config()
.resource
.get(&Key::from_static_str(resource_key))
.get_ref(&Key::from_static_str(resource_key))
.map(|v| v.to_string()),
expect.map(|s| s.to_string())
);
Expand All @@ -587,19 +587,22 @@ mod tests {
provider
.config()
.resource
.get(&TELEMETRY_SDK_LANGUAGE.into()),
Some(Value::from("rust"))
.get_ref(&TELEMETRY_SDK_LANGUAGE.into()),
Some(&Value::from("rust"))
);
assert_eq!(
provider.config().resource.get(&TELEMETRY_SDK_NAME.into()),
Some(Value::from("opentelemetry"))
provider
.config()
.resource
.get_ref(&TELEMETRY_SDK_NAME.into()),
Some(&Value::from("opentelemetry"))
);
assert_eq!(
provider
.config()
.resource
.get(&TELEMETRY_SDK_VERSION.into()),
Some(Value::from(env!("CARGO_PKG_VERSION")))
.get_ref(&TELEMETRY_SDK_VERSION.into()),
Some(&Value::from(env!("CARGO_PKG_VERSION")))
);
};

Expand Down Expand Up @@ -759,16 +762,16 @@ mod tests {
.into_owned();

assert_eq!(
resource.get(&Key::from_static_str("key1")),
Some(Value::from("value1"))
resource.get_ref(&Key::from_static_str("key1")),
Some(&Value::from("value1"))
);
assert_eq!(
resource.get(&Key::from_static_str("key2")),
Some(Value::from("value2"))
resource.get_ref(&Key::from_static_str("key2")),
Some(&Value::from("value2"))
);
assert_eq!(
resource.get(&Key::from_static_str("key3")),
Some(Value::from("value3"))
resource.get_ref(&Key::from_static_str("key3")),
Some(&Value::from("value3"))
);
assert_eq!(resource.schema_url(), Some("http://example.com"));
}
Expand Down
4 changes: 2 additions & 2 deletions opentelemetry-sdk/src/trace/span_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1361,8 +1361,8 @@ mod tests {
exported_resource
.as_ref()
.unwrap()
.get(&Key::new("service.name")),
Some(Value::from("test_service"))
.get_ref(&Key::new("service.name")),
Some(&Value::from("test_service"))
);
}

Expand Down
2 changes: 1 addition & 1 deletion stress/src/logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl LogExporter for NoopExporter {

fn set_resource(&mut self, res: &Resource) {
self.service_name = res
.get(&Key::from_static_str("service.name"))
.get_ref(&Key::from_static_str("service.name"))
.map(|v| v.to_string());
}
}
Expand Down