-
Notifications
You must be signed in to change notification settings - Fork 444
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat/add resource builder #2322
base: main
Are you sure you want to change the base?
Changes from all commits
8adc454
2f57428
aa3cd6e
7beb6bf
e501d4f
bee950f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,108 @@ | ||||||
use std::time::Duration; | ||||||
|
||||||
use opentelemetry::{KeyValue, Value}; | ||||||
|
||||||
use super::{Resource, ResourceDetector, SERVICE_NAME}; | ||||||
|
||||||
/// 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 { | ||||||
pitoniak32 marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
This makes it easier to reason about. Since on |
||||||
ResourceBuilder { | ||||||
resource: Resource::empty(), | ||||||
} | ||||||
} | ||||||
|
||||||
/// Create ResourceBuilder with a default [Resource]. | ||||||
pub fn new_default() -> Self { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Probably should be done as an |
||||||
ResourceBuilder { | ||||||
resource: Resource::default(), | ||||||
} | ||||||
} | ||||||
|
||||||
/// 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(Duration::from_secs(0), detectors)); | ||||||
self | ||||||
} | ||||||
|
||||||
/// Add a [KeyValue] to the resource. | ||||||
pub fn with_key_value(self, kv: KeyValue) -> Self { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. interesting why this does not need There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It might be since the with_detectors consumes the builder and its not a |
||||||
self.with_key_values(vec![kv]) | ||||||
} | ||||||
|
||||||
/// Add an [Attribute] to your resource for "service.name" | ||||||
pub fn with_service_name<V>(self, service_name: V) -> Self | ||||||
where | ||||||
V: Into<Value>, | ||||||
{ | ||||||
self.with_key_value(KeyValue::new(SERVICE_NAME, service_name)) | ||||||
} | ||||||
|
||||||
/// 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"), | ||||||
]) | ||||||
) | ||||||
}, | ||||||
) | ||||||
} | ||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wondering if we can do
with_servicename_resource("metrics-advanced-example")
? This will be the most common use case.Or just
Resource::builder().with_servicename("metrics-advanced-example")......build()
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like that, I was using the go implementation for Resource builder this weekend and they had something similar to this and it felt pretty nice