diff --git a/opentelemetry-sdk/CHANGELOG.md b/opentelemetry-sdk/CHANGELOG.md index f2a11be59f..3d8f486f36 100644 --- a/opentelemetry-sdk/CHANGELOG.md +++ b/opentelemetry-sdk/CHANGELOG.md @@ -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 diff --git a/opentelemetry-sdk/src/logs/logger_provider.rs b/opentelemetry-sdk/src/logs/logger_provider.rs index e7de152807..02908d0373 100644 --- a/opentelemetry-sdk/src/logs/logger_provider.rs +++ b/opentelemetry-sdk/src/logs/logger_provider.rs @@ -424,7 +424,7 @@ mod tests { assert_eq!( processor .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()) ); @@ -432,7 +432,7 @@ mod tests { 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()) ); @@ -440,28 +440,28 @@ mod tests { 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"))) ); }; @@ -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")); } diff --git a/opentelemetry-sdk/src/metrics/meter_provider.rs b/opentelemetry-sdk/src/metrics/meter_provider.rs index 5b5b4f8f16..a88f033001 100644 --- a/opentelemetry-sdk/src/metrics/meter_provider.rs +++ b/opentelemetry-sdk/src/metrics/meter_provider.rs @@ -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()) ); @@ -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"))) ); }; @@ -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")); } diff --git a/opentelemetry-sdk/src/resource/env.rs b/opentelemetry-sdk/src/resource/env.rs index b4ea198c37..79d29991af 100644 --- a/opentelemetry-sdk/src/resource/env.rs +++ b/opentelemetry-sdk/src/resource/env.rs @@ -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")), ) }); @@ -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")), ) }, ); @@ -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")) ); }, ); diff --git a/opentelemetry-sdk/src/resource/mod.rs b/opentelemetry-sdk/src/resource/mod.rs index 054a472ea3..d7d6d6e2bb 100644 --- a/opentelemetry-sdk/src/resource/mod.rs +++ b/opentelemetry-sdk/src/resource/mod.rs @@ -228,6 +228,11 @@ impl Resource { pub fn get(&self, key: &Key) -> Option { 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`. diff --git a/opentelemetry-sdk/src/trace/provider.rs b/opentelemetry-sdk/src/trace/provider.rs index 2b05f89aea..6f3d12f9f6 100644 --- a/opentelemetry-sdk/src/trace/provider.rs +++ b/opentelemetry-sdk/src/trace/provider.rs @@ -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()) ); @@ -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"))) ); }; @@ -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")); } diff --git a/opentelemetry-sdk/src/trace/span_processor.rs b/opentelemetry-sdk/src/trace/span_processor.rs index 0ebe80f885..2fd56a5246 100644 --- a/opentelemetry-sdk/src/trace/span_processor.rs +++ b/opentelemetry-sdk/src/trace/span_processor.rs @@ -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")) ); } diff --git a/stress/src/logs.rs b/stress/src/logs.rs index 88ec1ee5af..ac743dcdfe 100644 --- a/stress/src/logs.rs +++ b/stress/src/logs.rs @@ -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()); } }