|
| 1 | +use opentelemetry::KeyValue; |
| 2 | + |
| 3 | +use super::{Resource, ResourceDetector}; |
| 4 | + |
| 5 | +/// Builder to allow easy composition of a Resource |
| 6 | +#[derive(Debug, Default)] |
| 7 | +pub struct ResourceBuilder { |
| 8 | + resource: Resource, |
| 9 | +} |
| 10 | + |
| 11 | +impl ResourceBuilder { |
| 12 | + /// Create ResourceBuilder with an empty [Resource]. |
| 13 | + pub fn new_empty() -> Self { |
| 14 | + ResourceBuilder { |
| 15 | + resource: Resource::empty(), |
| 16 | + } |
| 17 | + } |
| 18 | + |
| 19 | + /// Create ResourceBuilder with a default [Resource]. |
| 20 | + pub fn new_default() -> Self { |
| 21 | + ResourceBuilder { |
| 22 | + resource: Resource::default(), |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + /// Add a single [ResourceDetector] to your resource. |
| 27 | + pub fn with_detector(self, detector: Box<dyn ResourceDetector>) -> Self { |
| 28 | + self.with_detectors(vec![detector]) |
| 29 | + } |
| 30 | + |
| 31 | + /// Add multiple [ResourceDetector] to your resource. |
| 32 | + pub fn with_detectors(mut self, detectors: Vec<Box<dyn ResourceDetector>>) -> Self { |
| 33 | + self.resource = self.resource.merge(&Resource::from_detectors(detectors)); |
| 34 | + self |
| 35 | + } |
| 36 | + |
| 37 | + /// Add a [KeyValue] to the resource. |
| 38 | + pub fn with_key_value(self, kv: KeyValue) -> Self { |
| 39 | + self.with_key_values(vec![kv]) |
| 40 | + } |
| 41 | + |
| 42 | + /// Add multiple [KeyValue]s to the resource. |
| 43 | + pub fn with_key_values<T: IntoIterator<Item = KeyValue>>(mut self, kvs: T) -> Self { |
| 44 | + self.resource = self.resource.merge(&Resource::new(kvs)); |
| 45 | + self |
| 46 | + } |
| 47 | + |
| 48 | + /// Create a [Resource] with the options provided to the [ResourceBuilder]. |
| 49 | + pub fn build(self) -> Resource { |
| 50 | + self.resource |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +#[cfg(test)] |
| 55 | +mod tests { |
| 56 | + use opentelemetry::KeyValue; |
| 57 | + |
| 58 | + use crate::resource::EnvResourceDetector; |
| 59 | + |
| 60 | + use super::*; |
| 61 | + |
| 62 | + #[test] |
| 63 | + fn detect_resource() { |
| 64 | + temp_env::with_vars( |
| 65 | + [ |
| 66 | + ( |
| 67 | + "OTEL_RESOURCE_ATTRIBUTES", |
| 68 | + Some("key=value, k = v , a= x, a=z"), |
| 69 | + ), |
| 70 | + ("IRRELEVANT", Some("20200810")), |
| 71 | + ], |
| 72 | + || { |
| 73 | + let resource = Resource::builder() |
| 74 | + .with_detector(Box::new(EnvResourceDetector::new())) |
| 75 | + .with_key_value(KeyValue::new("test1", "test_value")) |
| 76 | + .with_key_values(vec![ |
| 77 | + KeyValue::new("test1", "test_value1"), |
| 78 | + KeyValue::new("test2", "test_value2"), |
| 79 | + ]) |
| 80 | + .build(); |
| 81 | + |
| 82 | + assert_eq!( |
| 83 | + resource, |
| 84 | + Resource::new(vec![ |
| 85 | + KeyValue::new("key", "value"), |
| 86 | + KeyValue::new("test1", "test_value1"), |
| 87 | + KeyValue::new("test2", "test_value2"), |
| 88 | + KeyValue::new("k", "v"), |
| 89 | + KeyValue::new("a", "x"), |
| 90 | + KeyValue::new("a", "z"), |
| 91 | + ]) |
| 92 | + ) |
| 93 | + }, |
| 94 | + ) |
| 95 | + } |
| 96 | +} |
0 commit comments