improve usage of Resource

* Extract into reusable function (we'll need this later)
* Merge the values we want to override over the defaults, instead of
  dropping the defaults completely
* Don't unnecessarily allocate (the `vec![]` usage is gone)
This commit is contained in:
Charles Hall 2024-05-29 14:34:55 -07:00
parent c64a474954
commit 94fda7c875
No known key found for this signature in database
GPG key ID: 7B8E0645816E07CF

View file

@ -36,12 +36,8 @@ pub(crate) fn init(config: &Config) -> Result<Guard, error::Observability> {
let tracer = opentelemetry_otlp::new_pipeline()
.tracing()
.with_trace_config(
opentelemetry_sdk::trace::config().with_resource(
Resource::new(vec![KeyValue::new(
"service.name",
env!("CARGO_PKG_NAME"),
)]),
),
opentelemetry_sdk::trace::config()
.with_resource(standard_resource()),
)
.with_exporter(opentelemetry_otlp::new_exporter().tonic())
.install_batch(opentelemetry_sdk::runtime::Tokio)?;
@ -81,3 +77,11 @@ pub(crate) fn init(config: &Config) -> Result<Guard, error::Observability> {
flame_guard,
})
}
/// Construct the standard [`Resource`] value to use for this service
fn standard_resource() -> Resource {
Resource::default().merge(&Resource::new([KeyValue::new(
"service.name",
env!("CARGO_PKG_NAME"),
)]))
}