Skip to content

Commit 14faca3

Browse files
authored
Resource.get modified to only need a reference (#2552)
1 parent 4f472a8 commit 14faca3

File tree

8 files changed

+28
-26
lines changed

8 files changed

+28
-26
lines changed

opentelemetry-sdk/CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,8 @@ re-introduced in a future date, along with the ability to change the cardinality
269269
limit.
270270

271271
- *Breaking* Removed unused `opentelemetry_sdk::Error` enum.
272+
- *Breaking* Resource.get() modified to require reference to Key instead of owned.
273+
Replace `get(Key::from_static_str("key"))` with `get(&Key::from_static_str("key"))`
272274
- *Breaking* (Affects custom Exporter authors only) Moved `ExportError` trait from `opentelemetry::export::ExportError` to `opentelemetry_sdk::ExportError`
273275
- *Breaking (Affects custom SpanExporter, SpanProcessor authors only)*: Rename namespaces for Span exporter structs/traits
274276
before:

opentelemetry-sdk/src/logs/log_emitter.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -403,22 +403,22 @@ mod tests {
403403
assert_eq!(
404404
provider
405405
.resource()
406-
.get(Key::from_static_str(resource_key))
406+
.get(&Key::from_static_str(resource_key))
407407
.map(|v| v.to_string()),
408408
expect.map(|s| s.to_string())
409409
);
410410
};
411411
let assert_telemetry_resource = |provider: &super::LoggerProvider| {
412412
assert_eq!(
413-
provider.resource().get(TELEMETRY_SDK_LANGUAGE.into()),
413+
provider.resource().get(&TELEMETRY_SDK_LANGUAGE.into()),
414414
Some(Value::from("rust"))
415415
);
416416
assert_eq!(
417-
provider.resource().get(TELEMETRY_SDK_NAME.into()),
417+
provider.resource().get(&TELEMETRY_SDK_NAME.into()),
418418
Some(Value::from("opentelemetry"))
419419
);
420420
assert_eq!(
421-
provider.resource().get(TELEMETRY_SDK_VERSION.into()),
421+
provider.resource().get(&TELEMETRY_SDK_VERSION.into()),
422422
Some(Value::from(env!("CARGO_PKG_VERSION")))
423423
);
424424
};

opentelemetry-sdk/src/metrics/meter_provider.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ mod tests {
319319
assert_eq!(
320320
provider.inner.pipes.0[0]
321321
.resource
322-
.get(Key::from_static_str(resource_key))
322+
.get(&Key::from_static_str(resource_key))
323323
.map(|v| v.to_string()),
324324
expect.map(|s| s.to_string())
325325
);
@@ -328,19 +328,19 @@ mod tests {
328328
assert_eq!(
329329
provider.inner.pipes.0[0]
330330
.resource
331-
.get(TELEMETRY_SDK_LANGUAGE.into()),
331+
.get(&TELEMETRY_SDK_LANGUAGE.into()),
332332
Some(Value::from("rust"))
333333
);
334334
assert_eq!(
335335
provider.inner.pipes.0[0]
336336
.resource
337-
.get(TELEMETRY_SDK_NAME.into()),
337+
.get(&TELEMETRY_SDK_NAME.into()),
338338
Some(Value::from("opentelemetry"))
339339
);
340340
assert_eq!(
341341
provider.inner.pipes.0[0]
342342
.resource
343-
.get(TELEMETRY_SDK_VERSION.into()),
343+
.get(&TELEMETRY_SDK_VERSION.into()),
344344
Some(Value::from(env!("CARGO_PKG_VERSION")))
345345
);
346346
};

opentelemetry-sdk/src/resource/env.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ impl ResourceDetector for SdkProvidedResourceDetector {
8484
.or_else(|| {
8585
EnvResourceDetector::new()
8686
.detect()
87-
.get(Key::new(super::SERVICE_NAME))
87+
.get(&Key::new(super::SERVICE_NAME))
8888
})
8989
.unwrap_or_else(|| "unknown_service".into()),
9090
)])
@@ -138,14 +138,14 @@ mod tests {
138138
// Ensure no env var set
139139
let no_env = SdkProvidedResourceDetector.detect();
140140
assert_eq!(
141-
no_env.get(Key::from_static_str(crate::resource::SERVICE_NAME)),
141+
no_env.get(&Key::from_static_str(crate::resource::SERVICE_NAME)),
142142
Some(Value::from("unknown_service")),
143143
);
144144

145145
temp_env::with_var(OTEL_SERVICE_NAME, Some("test service"), || {
146146
let with_service = SdkProvidedResourceDetector.detect();
147147
assert_eq!(
148-
with_service.get(Key::from_static_str(crate::resource::SERVICE_NAME)),
148+
with_service.get(&Key::from_static_str(crate::resource::SERVICE_NAME)),
149149
Some(Value::from("test service")),
150150
)
151151
});
@@ -156,7 +156,7 @@ mod tests {
156156
|| {
157157
let with_service = SdkProvidedResourceDetector.detect();
158158
assert_eq!(
159-
with_service.get(Key::from_static_str(crate::resource::SERVICE_NAME)),
159+
with_service.get(&Key::from_static_str(crate::resource::SERVICE_NAME)),
160160
Some(Value::from("test service1")),
161161
)
162162
},
@@ -171,7 +171,7 @@ mod tests {
171171
|| {
172172
let with_service = SdkProvidedResourceDetector.detect();
173173
assert_eq!(
174-
with_service.get(Key::from_static_str(crate::resource::SERVICE_NAME)),
174+
with_service.get(&Key::from_static_str(crate::resource::SERVICE_NAME)),
175175
Some(Value::from("test service"))
176176
);
177177
},

opentelemetry-sdk/src/resource/mod.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,11 @@ pub struct Resource {
5454
impl Resource {
5555
/// Creates a [ResourceBuilder] that allows you to configure multiple aspects of the Resource.
5656
///
57-
/// This [ResourceBuilder] will always include the following [ResourceDetector]s:
57+
/// This [ResourceBuilder] will include the following [ResourceDetector]s:
5858
/// - [SdkProvidedResourceDetector]
5959
/// - [TelemetryResourceDetector]
6060
/// - [EnvResourceDetector]
61+
/// If you'd like to start from an empty resource, use [Resource::builder_empty].
6162
pub fn builder() -> ResourceBuilder {
6263
ResourceBuilder {
6364
resource: Self::from_detectors(&[
@@ -138,8 +139,6 @@ impl Resource {
138139
}
139140

140141
/// Create a new `Resource` from resource detectors.
141-
///
142-
/// timeout will be applied to each detector.
143142
fn from_detectors(detectors: &[Box<dyn ResourceDetector>]) -> Self {
144143
let mut resource = Resource::empty();
145144
for detector in detectors {
@@ -227,8 +226,8 @@ impl Resource {
227226
}
228227

229228
/// Retrieve the value from resource associate with given key.
230-
pub fn get(&self, key: Key) -> Option<Value> {
231-
self.inner.attrs.get(&key).cloned()
229+
pub fn get(&self, key: &Key) -> Option<Value> {
230+
self.inner.attrs.get(key).cloned()
232231
}
233232
}
234233

@@ -260,8 +259,6 @@ impl<'a> IntoIterator for &'a Resource {
260259
pub trait ResourceDetector {
261260
/// detect returns an initialized Resource based on gathered information.
262261
///
263-
/// timeout is used in case the detection operation takes too much time.
264-
///
265262
/// If source information to construct a Resource is inaccessible, an empty Resource should be returned
266263
///
267264
/// If source information to construct a Resource is invalid, for example,

opentelemetry-sdk/src/trace/provider.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,7 @@ mod tests {
552552
provider
553553
.config()
554554
.resource
555-
.get(Key::from_static_str(resource_key))
555+
.get(&Key::from_static_str(resource_key))
556556
.map(|v| v.to_string()),
557557
expect.map(|s| s.to_string())
558558
);
@@ -562,15 +562,18 @@ mod tests {
562562
provider
563563
.config()
564564
.resource
565-
.get(TELEMETRY_SDK_LANGUAGE.into()),
565+
.get(&TELEMETRY_SDK_LANGUAGE.into()),
566566
Some(Value::from("rust"))
567567
);
568568
assert_eq!(
569-
provider.config().resource.get(TELEMETRY_SDK_NAME.into()),
569+
provider.config().resource.get(&TELEMETRY_SDK_NAME.into()),
570570
Some(Value::from("opentelemetry"))
571571
);
572572
assert_eq!(
573-
provider.config().resource.get(TELEMETRY_SDK_VERSION.into()),
573+
provider
574+
.config()
575+
.resource
576+
.get(&TELEMETRY_SDK_VERSION.into()),
574577
Some(Value::from(env!("CARGO_PKG_VERSION")))
575578
);
576579
};

opentelemetry-sdk/src/trace/span_processor.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1222,7 +1222,7 @@ mod tests {
12221222
exported_resource
12231223
.as_ref()
12241224
.unwrap()
1225-
.get(Key::new("service.name")),
1225+
.get(&Key::new("service.name")),
12261226
Some(Value::from("test_service"))
12271227
);
12281228
}

opentelemetry-zipkin/src/exporter/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ impl ZipkinPipelineBuilder {
116116
} else {
117117
let service_name = SdkProvidedResourceDetector
118118
.detect()
119-
.get(semcov::resource::SERVICE_NAME.into())
119+
.get(&semcov::resource::SERVICE_NAME.into())
120120
.unwrap()
121121
.to_string();
122122
(

0 commit comments

Comments
 (0)