opentelemetry_sdk/resource/
mod.rs

1//! Representations of entities producing telemetry.
2//!
3//! A [Resource] is an immutable representation of the entity producing
4//! telemetry as attributes. For example, a process producing telemetry that is
5//! running in a container on Kubernetes has a Pod name, it is in a namespace
6//! and possibly is part of a Deployment which also has a name. All three of
7//! these attributes can be included in the `Resource`. Note that there are
8//! certain ["standard attributes"] that have prescribed meanings.
9//!
10//! ["standard attributes"]: https://github.com/open-telemetry/opentelemetry-specification/blob/v1.9.0/specification/resource/semantic_conventions/README.md
11//!
12//! # Resource detectors
13//!
14//! [`ResourceDetector`]s are used to detect resource from runtime or
15//! environmental variables. The following are provided by default with this
16//! SDK.
17//!
18//! - [`EnvResourceDetector`] - detect resource from environmental variables.
19//! - [`TelemetryResourceDetector`] - detect telemetry SDK's information.
20//!
21//! The OS and Process resource detectors are packaged separately in the
22//! [`opentelemetry-resource-detector` crate](https://github.com/open-telemetry/opentelemetry-rust-contrib/tree/main/opentelemetry-resource-detectors).
23mod env;
24mod telemetry;
25
26mod attributes;
27pub(crate) use attributes::*;
28
29pub use env::EnvResourceDetector;
30pub use env::SdkProvidedResourceDetector;
31pub use telemetry::TelemetryResourceDetector;
32
33use opentelemetry::{Key, KeyValue, Value};
34use std::borrow::Cow;
35use std::collections::{hash_map, HashMap};
36use std::ops::Deref;
37use std::sync::Arc;
38
39/// Inner structure of `Resource` holding the actual data.
40/// This structure is designed to be shared among `Resource` instances via `Arc`.
41#[derive(Debug, Clone, PartialEq)]
42struct ResourceInner {
43    attrs: HashMap<Key, Value>,
44    schema_url: Option<Cow<'static, str>>,
45}
46
47/// An immutable representation of the entity producing telemetry as attributes.
48/// Utilizes `Arc` for efficient sharing and cloning.
49#[derive(Clone, Debug, PartialEq)]
50pub struct Resource {
51    inner: Arc<ResourceInner>,
52}
53
54impl Resource {
55    /// Creates a [ResourceBuilder] that allows you to configure multiple aspects of the Resource.
56    ///
57    /// This [ResourceBuilder] will include the following [ResourceDetector]s:
58    /// - [SdkProvidedResourceDetector]
59    /// - [TelemetryResourceDetector]
60    /// - [EnvResourceDetector]
61    ///   If you'd like to start from an empty resource, use [Resource::builder_empty].
62    pub fn builder() -> ResourceBuilder {
63        ResourceBuilder {
64            resource: Self::from_detectors(&[
65                Box::new(SdkProvidedResourceDetector),
66                Box::new(TelemetryResourceDetector),
67                Box::new(EnvResourceDetector::new()),
68            ]),
69        }
70    }
71
72    /// Creates a [ResourceBuilder] that allows you to configure multiple aspects of the Resource.
73    ///
74    /// This [ResourceBuilder] will not include any attributes or [ResourceDetector]s by default.
75    pub fn builder_empty() -> ResourceBuilder {
76        ResourceBuilder {
77            resource: Resource::empty(),
78        }
79    }
80
81    /// Creates an empty resource.
82    /// This is the basic constructor that initializes a resource with no attributes and no schema URL.
83    pub(crate) fn empty() -> Self {
84        Resource {
85            inner: Arc::new(ResourceInner {
86                attrs: HashMap::new(),
87                schema_url: None,
88            }),
89        }
90    }
91
92    /// Create a new `Resource` from key value pairs.
93    ///
94    /// Values are de-duplicated by key, and the last key-value pair will be retained
95    pub(crate) fn new<T: IntoIterator<Item = KeyValue>>(kvs: T) -> Self {
96        let mut attrs = HashMap::new();
97        for kv in kvs {
98            attrs.insert(kv.key, kv.value);
99        }
100
101        Resource {
102            inner: Arc::new(ResourceInner {
103                attrs,
104                schema_url: None,
105            }),
106        }
107    }
108
109    /// Create a new `Resource` from a key value pairs and [schema url].
110    ///
111    /// Values are de-duplicated by key, and the first key-value pair with a non-empty string value
112    /// will be retained.
113    ///
114    /// schema_url must be a valid URL using HTTP or HTTPS protocol.
115    ///
116    /// [schema url]: https://github.com/open-telemetry/opentelemetry-specification/blob/v1.9.0/specification/schemas/overview.md#schema-url
117    fn from_schema_url<KV, S>(kvs: KV, schema_url: S) -> Self
118    where
119        KV: IntoIterator<Item = KeyValue>,
120        S: Into<Cow<'static, str>>,
121    {
122        let schema_url_str = schema_url.into();
123        let normalized_schema_url = if schema_url_str.is_empty() {
124            None
125        } else {
126            Some(schema_url_str)
127        };
128        let mut attrs = HashMap::new();
129        for kv in kvs {
130            attrs.insert(kv.key, kv.value);
131        }
132        Resource {
133            inner: Arc::new(ResourceInner {
134                attrs,
135                schema_url: normalized_schema_url,
136            }),
137        }
138    }
139
140    /// Create a new `Resource` from resource detectors.
141    fn from_detectors(detectors: &[Box<dyn ResourceDetector>]) -> Self {
142        let mut resource = Resource::empty();
143        for detector in detectors {
144            let detected_res = detector.detect();
145            // This call ensures that if the Arc is not uniquely owned,
146            // the data is cloned before modification, preserving safety.
147            // If the Arc is uniquely owned, it simply returns a mutable reference to the data.
148            let inner = Arc::make_mut(&mut resource.inner);
149            for (key, value) in detected_res.into_iter() {
150                inner.attrs.insert(Key::new(key.clone()), value.clone());
151            }
152        }
153
154        resource
155    }
156
157    /// Create a new `Resource` by combining two resources.
158    ///
159    /// ### Key value pairs
160    /// Keys from the `other` resource have priority over keys from this resource, even if the
161    /// updated value is empty.
162    ///
163    /// ### [Schema url]
164    /// If both of the resource are not empty. Schema url is determined by the following rules, in order:
165    /// 1. If this resource has a schema url, it will be used.
166    /// 2. If this resource does not have a schema url, and the other resource has a schema url, it will be used.
167    /// 3. If both resources have a schema url and it's the same, it will be used.
168    /// 4. If both resources have a schema url and it's different, the schema url will be empty.
169    /// 5. If both resources do not have a schema url, the schema url will be empty.
170    ///
171    /// [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() {
174            return other.clone();
175        }
176        if other.is_empty() {
177            return self.clone();
178        }
179        let mut combined_attrs = self.inner.attrs.clone();
180        for (k, v) in other.inner.attrs.iter() {
181            combined_attrs.insert(k.clone(), v.clone());
182        }
183
184        // Resolve the schema URL according to the precedence rules
185        let combined_schema_url = match (&self.inner.schema_url, &other.inner.schema_url) {
186            // If both resources have a schema URL and it's the same, use it
187            (Some(url1), Some(url2)) if url1 == url2 => Some(url1.clone()),
188            // If both resources have a schema URL but they are not the same, the schema URL will be empty
189            (Some(_), Some(_)) => None,
190            // If this resource does not have a schema URL, and the other resource has a schema URL, it will be used
191            (None, Some(url)) => Some(url.clone()),
192            // If this resource has a schema URL, it will be used (covers case 1 and any other cases where `self` has a schema URL)
193            (Some(url), _) => Some(url.clone()),
194            // If both resources do not have a schema URL, the schema URL will be empty
195            (None, None) => None,
196        };
197        Resource {
198            inner: Arc::new(ResourceInner {
199                attrs: combined_attrs,
200                schema_url: combined_schema_url,
201            }),
202        }
203    }
204
205    /// Return the [schema url] of the resource. If the resource does not have a schema url, return `None`.
206    ///
207    /// [schema url]: https://github.com/open-telemetry/opentelemetry-specification/blob/v1.9.0/specification/schemas/overview.md#schema-url
208    pub fn schema_url(&self) -> Option<&str> {
209        self.inner.schema_url.as_ref().map(|s| s.as_ref())
210    }
211
212    /// Returns the number of attributes for this resource
213    pub fn len(&self) -> usize {
214        self.inner.attrs.len()
215    }
216
217    /// Returns `true` if the resource contains no attributes.
218    pub fn is_empty(&self) -> bool {
219        self.inner.attrs.is_empty()
220    }
221
222    /// Gets an iterator over the attributes of this resource.
223    pub fn iter(&self) -> Iter<'_> {
224        Iter(self.inner.attrs.iter())
225    }
226
227    /// Retrieve the value from resource associate with given key.
228    pub fn get(&self, key: &Key) -> Option<Value> {
229        self.inner.attrs.get(key).cloned()
230    }
231}
232
233/// An iterator over the entries of a `Resource`.
234#[derive(Debug)]
235pub struct Iter<'a>(hash_map::Iter<'a, Key, Value>);
236
237impl<'a> Iterator for Iter<'a> {
238    type Item = (&'a Key, &'a Value);
239
240    fn next(&mut self) -> Option<Self::Item> {
241        self.0.next()
242    }
243}
244
245impl<'a> IntoIterator for &'a Resource {
246    type Item = (&'a Key, &'a Value);
247    type IntoIter = Iter<'a>;
248
249    fn into_iter(self) -> Self::IntoIter {
250        Iter(self.inner.attrs.iter())
251    }
252}
253
254/// ResourceDetector detects OpenTelemetry resource information
255///
256/// Implementations of this trait can be passed to
257/// the [`ResourceBuilder::with_detectors`] function to generate a Resource from the merged information.
258pub trait ResourceDetector {
259    /// detect returns an initialized Resource based on gathered information.
260    ///
261    /// If source information to construct a Resource is inaccessible, an empty Resource should be returned
262    ///
263    /// If source information to construct a Resource is invalid, for example,
264    /// missing required values. an empty Resource should be returned.
265    fn detect(&self) -> Resource;
266}
267
268/// Builder for [Resource]
269#[derive(Debug)]
270pub struct ResourceBuilder {
271    resource: Resource,
272}
273
274impl ResourceBuilder {
275    /// Add a single [ResourceDetector] to your resource.
276    pub fn with_detector(self, detector: Box<dyn ResourceDetector>) -> Self {
277        self.with_detectors(&[detector])
278    }
279
280    /// Add multiple [ResourceDetector]s to your resource.
281    pub fn with_detectors(mut self, detectors: &[Box<dyn ResourceDetector>]) -> Self {
282        self.resource = self.resource.merge(&Resource::from_detectors(detectors));
283        self
284    }
285
286    /// Add a [KeyValue] to the resource.
287    pub fn with_attribute(self, kv: KeyValue) -> Self {
288        self.with_attributes([kv])
289    }
290
291    /// Add multiple [KeyValue]s to the resource.
292    pub fn with_attributes<T: IntoIterator<Item = KeyValue>>(mut self, kvs: T) -> Self {
293        self.resource = self.resource.merge(&Resource::new(kvs));
294        self
295    }
296
297    /// Add `service.name` resource attribute.
298    pub fn with_service_name(self, name: impl Into<Value>) -> Self {
299        self.with_attribute(KeyValue::new(SERVICE_NAME, name.into()))
300    }
301
302    /// This will merge the provided `schema_url` with the current state of the Resource being built. It
303    /// will use the following rules to determine which `schema_url` should be used.
304    ///
305    /// ### [Schema url]
306    /// Schema url is determined by the following rules, in order:
307    /// 1. If the current builder resource doesn't have a `schema_url`, the provided `schema_url` will be used.
308    /// 2. If the current builder resource has a `schema_url`, and the provided `schema_url` is different from the builder resource, `schema_url` will be empty.
309    /// 3. If the provided `schema_url` is the same as the current builder resource, it will be used.
310    ///
311    /// [Schema url]: https://github.com/open-telemetry/opentelemetry-specification/blob/v1.9.0/specification/schemas/overview.md#schema-url
312    pub fn with_schema_url<KV, S>(mut self, attributes: KV, schema_url: S) -> Self
313    where
314        KV: IntoIterator<Item = KeyValue>,
315        S: Into<Cow<'static, str>>,
316    {
317        self.resource = Resource::from_schema_url(attributes, schema_url).merge(&self.resource);
318        self
319    }
320
321    /// Create a [Resource] with the options provided to the [ResourceBuilder].
322    pub fn build(self) -> Resource {
323        self.resource
324    }
325}
326
327#[cfg(test)]
328mod tests {
329    use rstest::rstest;
330
331    use super::*;
332
333    #[rstest]
334    #[case([KeyValue::new("a", ""), KeyValue::new("a", "final")], [(Key::new("a"), Value::from("final"))])]
335    #[case([KeyValue::new("a", "final"), KeyValue::new("a", "")], [(Key::new("a"), Value::from(""))])]
336    fn new_resource(
337        #[case] given_attributes: [KeyValue; 2],
338        #[case] expected_attrs: [(Key, Value); 1],
339    ) {
340        // Arrange
341        let expected = HashMap::from_iter(expected_attrs.into_iter());
342
343        // Act
344        let resource = Resource::builder_empty()
345            .with_attributes(given_attributes)
346            .build();
347        let resource_inner = Arc::try_unwrap(resource.inner).expect("Failed to unwrap Arc");
348
349        // Assert
350        assert_eq!(resource_inner.attrs, expected);
351        assert_eq!(resource_inner.schema_url, None);
352    }
353
354    #[test]
355    fn merge_resource_key_value_pairs() {
356        let resource_a = Resource::builder_empty()
357            .with_attributes([
358                KeyValue::new("a", ""),
359                KeyValue::new("b", "b-value"),
360                KeyValue::new("d", "d-value"),
361            ])
362            .build();
363
364        let resource_b = Resource::builder_empty()
365            .with_attributes([
366                KeyValue::new("a", "a-value"),
367                KeyValue::new("c", "c-value"),
368                KeyValue::new("d", ""),
369            ])
370            .build();
371
372        let mut expected_attrs = HashMap::new();
373        expected_attrs.insert(Key::new("a"), Value::from("a-value"));
374        expected_attrs.insert(Key::new("b"), Value::from("b-value"));
375        expected_attrs.insert(Key::new("c"), Value::from("c-value"));
376        expected_attrs.insert(Key::new("d"), Value::from(""));
377
378        let expected_resource = Resource {
379            inner: Arc::new(ResourceInner {
380                attrs: expected_attrs,
381                schema_url: None, // Assuming schema_url handling if needed
382            }),
383        };
384
385        assert_eq!(resource_a.merge(&resource_b), expected_resource);
386    }
387
388    #[rstest]
389    #[case(Some("http://schema/a"), None, Some("http://schema/a"))]
390    #[case(Some("http://schema/a"), Some("http://schema/b"), None)]
391    #[case(None, Some("http://schema/b"), Some("http://schema/b"))]
392    #[case(
393        Some("http://schema/a"),
394        Some("http://schema/a"),
395        Some("http://schema/a")
396    )]
397    #[case(None, None, None)]
398    fn merge_resource_schema_url(
399        #[case] schema_url_a: Option<&'static str>,
400        #[case] schema_url_b: Option<&'static str>,
401        #[case] expected_schema_url: Option<&'static str>,
402    ) {
403        let resource_a =
404            Resource::from_schema_url([KeyValue::new("key", "")], schema_url_a.unwrap_or(""));
405        let resource_b =
406            Resource::from_schema_url([KeyValue::new("key", "")], schema_url_b.unwrap_or(""));
407
408        let merged_resource = resource_a.merge(&resource_b);
409        let result_schema_url = merged_resource.schema_url();
410
411        assert_eq!(
412            result_schema_url.map(|s| s as &str),
413            expected_schema_url,
414            "Merging schema_url_a {:?} with schema_url_b {:?} did not yield expected result {:?}",
415            schema_url_a,
416            schema_url_b,
417            expected_schema_url
418        );
419    }
420
421    #[rstest]
422    #[case(vec![], vec![KeyValue::new("key", "b")], "http://schema/a", None)]
423    #[case(vec![KeyValue::new("key", "a")], vec![KeyValue::new("key", "b")], "http://schema/a", Some("http://schema/a"))]
424    fn merge_resource_with_missing_attribtes(
425        #[case] key_values_a: Vec<KeyValue>,
426        #[case] key_values_b: Vec<KeyValue>,
427        #[case] schema_url: &'static str,
428        #[case] expected_schema_url: Option<&'static str>,
429    ) {
430        let resource = Resource::from_schema_url(key_values_a, schema_url);
431        let other_resource = Resource::builder_empty()
432            .with_attributes(key_values_b)
433            .build();
434
435        assert_eq!(
436            resource.merge(&other_resource).schema_url(),
437            expected_schema_url
438        );
439    }
440
441    #[test]
442    fn detect_resource() {
443        temp_env::with_vars(
444            [
445                (
446                    "OTEL_RESOURCE_ATTRIBUTES",
447                    Some("key=value, k = v , a= x, a=z"),
448                ),
449                ("IRRELEVANT", Some("20200810")),
450            ],
451            || {
452                let detector = EnvResourceDetector::new();
453                let resource = Resource::from_detectors(&[Box::new(detector)]);
454                assert_eq!(
455                    resource,
456                    Resource::builder_empty()
457                        .with_attributes([
458                            KeyValue::new("key", "value"),
459                            KeyValue::new("k", "v"),
460                            KeyValue::new("a", "x"),
461                            KeyValue::new("a", "z"),
462                        ])
463                        .build()
464                )
465            },
466        )
467    }
468
469    #[rstest]
470    #[case(Some("http://schema/a"), Some("http://schema/b"), None)]
471    #[case(None, Some("http://schema/b"), Some("http://schema/b"))]
472    #[case(
473        Some("http://schema/a"),
474        Some("http://schema/a"),
475        Some("http://schema/a")
476    )]
477    fn builder_with_schema_url(
478        #[case] schema_url_a: Option<&'static str>,
479        #[case] schema_url_b: Option<&'static str>,
480        #[case] expected_schema_url: Option<&'static str>,
481    ) {
482        let base_builder = if let Some(url) = schema_url_a {
483            ResourceBuilder {
484                resource: Resource::from_schema_url(vec![KeyValue::new("key", "")], url),
485            }
486        } else {
487            ResourceBuilder {
488                resource: Resource::empty(),
489            }
490        };
491
492        let resource = base_builder
493            .with_schema_url(
494                vec![KeyValue::new("key", "")],
495                schema_url_b.expect("should always be Some for this test"),
496            )
497            .build();
498
499        assert_eq!(
500            resource.schema_url().map(|s| s as &str),
501            expected_schema_url,
502            "Merging schema_url_a {:?} with schema_url_b {:?} did not yield expected result {:?}",
503            schema_url_a,
504            schema_url_b,
505            expected_schema_url
506        );
507    }
508
509    #[test]
510    fn builder_detect_resource() {
511        temp_env::with_vars(
512            [
513                (
514                    "OTEL_RESOURCE_ATTRIBUTES",
515                    Some("key=value, k = v , a= x, a=z"),
516                ),
517                ("IRRELEVANT", Some("20200810")),
518            ],
519            || {
520                let resource = Resource::builder_empty()
521                    .with_detector(Box::new(EnvResourceDetector::new()))
522                    .with_service_name("testing_service")
523                    .with_attribute(KeyValue::new("test1", "test_value"))
524                    .with_attributes([
525                        KeyValue::new("test1", "test_value1"),
526                        KeyValue::new("test2", "test_value2"),
527                    ])
528                    .build();
529
530                assert_eq!(
531                    resource,
532                    Resource::builder_empty()
533                        .with_attributes([
534                            KeyValue::new("key", "value"),
535                            KeyValue::new("test1", "test_value1"),
536                            KeyValue::new("test2", "test_value2"),
537                            KeyValue::new(SERVICE_NAME, "testing_service"),
538                            KeyValue::new("k", "v"),
539                            KeyValue::new("a", "x"),
540                            KeyValue::new("a", "z"),
541                        ])
542                        .build()
543                )
544            },
545        )
546    }
547}