Skip to content

Commit 3713f72

Browse files
committed
Calls to MeterProviderBuilder::with_resource are now additive
1 parent ac69af6 commit 3713f72

File tree

3 files changed

+40
-5
lines changed

3 files changed

+40
-5
lines changed

opentelemetry-sdk/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
## vNext
44

5-
- *Breaking* Make `force_flush()` in `PushMetricExporter` synchronous
5+
- *Breaking*: Calls to `MeterProviderBuilder::with_resource` are now additive ([#2324](https://github.com/open-telemetry/opentelemetry-rust/issues/2324)).
6+
- *Breaking*: Make `force_flush()` in `PushMetricExporter` synchronous
67

78
## 0.28.0
89

opentelemetry-sdk/src/metrics/meter_provider.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,10 +232,16 @@ impl MeterProviderBuilder {
232232
/// with all [Meter]s the [MeterProvider] will create.
233233
///
234234
/// By default, if this option is not used, the default [Resource] will be used.
235+
/// Note that the calls to this method are additive, each call merges the provided
236+
/// resource with the previous one.
235237
///
236238
/// [Meter]: opentelemetry::metrics::Meter
237239
pub fn with_resource(mut self, resource: Resource) -> Self {
238-
self.resource = Some(resource);
240+
self.resource = match self.resource {
241+
Some(existing) => Some(existing.merge(&resource)),
242+
None => Some(resource),
243+
};
244+
239245
self
240246
}
241247

@@ -331,6 +337,7 @@ mod tests {
331337
use opentelemetry::{global, InstrumentationScope};
332338
use opentelemetry::{Key, KeyValue, Value};
333339
use std::env;
340+
use crate::metrics::SdkMeterProvider;
334341

335342
#[test]
336343
fn test_meter_provider_resource() {
@@ -553,4 +560,20 @@ mod tests {
553560

554561
assert_eq!(provider.inner.meters.lock().unwrap().len(), 5);
555562
}
563+
564+
#[test]
565+
fn with_resource_multiple_calls_ensure_additive() {
566+
let builder = SdkMeterProvider::builder()
567+
.with_resource(Resource::new(vec![KeyValue::new("key1", "value1")]))
568+
.with_resource(Resource::new(vec![KeyValue::new("key2", "value2")]))
569+
.with_resource(Resource::builder_empty().with_schema_url(vec![], "http://example.com").build())
570+
.with_resource(Resource::new(vec![KeyValue::new("key3", "value3")]));
571+
572+
let resource = builder.resource.unwrap();
573+
574+
assert_eq!(resource.get(&Key::from_static_str("key1")), Some(Value::from("value1")));
575+
assert_eq!(resource.get(&Key::from_static_str("key2")), Some(Value::from("value2")));
576+
assert_eq!(resource.get(&Key::from_static_str("key3")), Some(Value::from("value3")));
577+
assert_eq!(resource.schema_url(), Some("http://example.com"));
578+
}
556579
}

opentelemetry-sdk/src/resource/mod.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,11 @@ impl Resource {
169169
/// 5. If both resources do not have a schema url, the schema url will be empty.
170170
///
171171
/// [Schema url]: https://github.com/open-telemetry/opentelemetry-specification/blob/v1.9.0/specification/schemas/overview.md#schema-url
172-
fn merge<T: Deref<Target = Self>>(&self, other: T) -> Self {
173-
if self.is_empty() {
172+
pub(crate) fn merge<T: Deref<Target = Self>>(&self, other: T) -> Self {
173+
if self.is_empty() && self.schema_url().is_none() {
174174
return other.clone();
175175
}
176-
if other.is_empty() {
176+
if other.is_empty() && other.schema_url().is_none() {
177177
return self.clone();
178178
}
179179
let mut combined_attrs = self.inner.attrs.clone();
@@ -544,4 +544,15 @@ mod tests {
544544
},
545545
)
546546
}
547+
548+
#[test]
549+
fn with_schema_url_for_empty_attributes() {
550+
let resource = Resource::builder_empty()
551+
.with_schema_url(vec![], "http://schema/a")
552+
.build();
553+
assert_eq!(resource.schema_url(), Some("http://schema/a"));
554+
555+
let resource = Resource::builder_empty().build().merge(&resource);
556+
assert_eq!(resource.schema_url(), Some("http://schema/a"));
557+
}
547558
}

0 commit comments

Comments
 (0)