Skip to content

Commit

Permalink
feat: add ResourceBuilder for creating Resources
Browse files Browse the repository at this point in the history
  • Loading branch information
pitoniak32 committed Nov 22, 2024
1 parent 0428dd5 commit b0bbed5
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 0 deletions.
96 changes: 96 additions & 0 deletions opentelemetry-sdk/src/resource/builder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
use opentelemetry::KeyValue;

use super::{Resource, ResourceDetector};

/// Builder to allow easy composition of a Resource
#[derive(Debug, Default)]
pub struct ResourceBuilder {
resource: Resource,
}

impl ResourceBuilder {
/// Create ResourceBuilder with an empty [Resource].
pub fn new_empty() -> Self {
ResourceBuilder {
resource: Resource::empty(),
}
}

/// Create ResourceBuilder with a default [Resource].
pub fn new_default() -> Self {
ResourceBuilder {
resource: Resource::default(),
}
}

Check warning on line 24 in opentelemetry-sdk/src/resource/builder.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-sdk/src/resource/builder.rs#L20-L24

Added lines #L20 - L24 were not covered by tests

/// Add a single [ResourceDetector] to your resource.
pub fn with_detector(self, detector: Box<dyn ResourceDetector>) -> Self {
self.with_detectors(vec![detector])
}

/// Add multiple [ResourceDetector] to your resource.
pub fn with_detectors(mut self, detectors: Vec<Box<dyn ResourceDetector>>) -> Self {
self.resource = self.resource.merge(&Resource::from_detectors(detectors));
self
}

/// Add a [KeyValue] to the resource.
pub fn with_key_value(self, kv: KeyValue) -> Self {
self.with_key_values(vec![kv])
}

/// Add multiple [KeyValue]s to the resource.
pub fn with_key_values<T: IntoIterator<Item = KeyValue>>(mut self, kvs: T) -> Self {
self.resource = self.resource.merge(&Resource::new(kvs));
self
}

/// Create a [Resource] with the options provided to the [ResourceBuilder].
pub fn build(self) -> Resource {
self.resource
}
}

#[cfg(test)]
mod tests {
use opentelemetry::KeyValue;

use crate::resource::EnvResourceDetector;

use super::*;

#[test]
fn detect_resource() {
temp_env::with_vars(
[
(
"OTEL_RESOURCE_ATTRIBUTES",
Some("key=value, k = v , a= x, a=z"),
),
("IRRELEVANT", Some("20200810")),
],
|| {
let resource = Resource::builder()
.with_detector(Box::new(EnvResourceDetector::new()))
.with_key_value(KeyValue::new("test1", "test_value"))
.with_key_values(vec![
KeyValue::new("test1", "test_value1"),
KeyValue::new("test2", "test_value2"),
])
.build();

assert_eq!(
resource,
Resource::new(vec![
KeyValue::new("key", "value"),
KeyValue::new("test1", "test_value1"),
KeyValue::new("test2", "test_value2"),
KeyValue::new("k", "v"),
KeyValue::new("a", "x"),
KeyValue::new("a", "z"),
])
)
},
)
}
}
19 changes: 19 additions & 0 deletions opentelemetry-sdk/src/resource/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
//!
//! The OS and Process resource detectors are packaged separately in the
//! [`opentelemetry-resource-detector` crate](https://github.com/open-telemetry/opentelemetry-rust-contrib/tree/main/opentelemetry-resource-detectors).
mod builder;
mod env;
mod telemetry;

Expand All @@ -36,6 +37,8 @@ use std::collections::{hash_map, HashMap};
use std::ops::Deref;
use std::sync::Arc;

use self::builder::ResourceBuilder;

/// Inner structure of `Resource` holding the actual data.
/// This structure is designed to be shared among `Resource` instances via `Arc`.
#[derive(Debug, Clone, PartialEq)]
Expand All @@ -62,6 +65,22 @@ impl Default for Resource {
}

impl Resource {
/// Creates a Builder that allows you to configure multiple aspects of the Resource.
///
/// If you want to start from a [Resource::default()] see [Resource::builder_default()].
///
/// Starts with a [Resource::empty()].
pub fn builder() -> ResourceBuilder {
ResourceBuilder::new_empty()
}

/// Creates a Builder that allows you to configure multiple aspects of the Resource.
///
/// Starts with a [Resource::default()].
pub fn builder_default() -> ResourceBuilder {
ResourceBuilder::new_empty()
}

Check warning on line 82 in opentelemetry-sdk/src/resource/mod.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-sdk/src/resource/mod.rs#L80-L82

Added lines #L80 - L82 were not covered by tests

/// Creates an empty resource.
/// This is the basic constructor that initializes a resource with no attributes and no schema URL.
pub fn empty() -> Self {
Expand Down

0 comments on commit b0bbed5

Please sign in to comment.