From 6e94c27784d92c28ee5e2d68e27893b9d6ed73b9 Mon Sep 17 00:00:00 2001 From: hlts2 Date: Thu, 1 Aug 2024 17:18:32 +0900 Subject: [PATCH] feat: update interface Signed-off-by: hlts2 --- rust/libs/observability/src/config.rs | 14 ++++++++++++-- rust/libs/observability/src/observability.rs | 16 ++++++++-------- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/rust/libs/observability/src/config.rs b/rust/libs/observability/src/config.rs index be5525d730..5ad71cd52c 100644 --- a/rust/libs/observability/src/config.rs +++ b/rust/libs/observability/src/config.rs @@ -61,8 +61,8 @@ impl Config { self } - pub fn attribute(mut self, key: String, value: String) -> Self { - self.attributes.insert(key, value); + pub fn attribute(mut self, key: &str, value: &str) -> Self { + self.attributes.insert(key.to_string(), value.to_string()); self } @@ -106,6 +106,11 @@ impl Tracer { self.enabled = enabled; self } + + pub fn endpoint(mut self, endpoint: &str) -> Self { + self.endpoint = endpoint.to_string(); + self + } } impl Default for Tracer { @@ -129,6 +134,11 @@ impl Meter { self } + pub fn endpoint(mut self, endpoint: &str) -> Self { + self.endpoint = endpoint.to_string(); + self + } + pub fn export_duration(mut self, dur: Duration) -> Self { self.export_duration = dur; self diff --git a/rust/libs/observability/src/observability.rs b/rust/libs/observability/src/observability.rs index 98fe2ebebb..c1f3328f1d 100644 --- a/rust/libs/observability/src/observability.rs +++ b/rust/libs/observability/src/observability.rs @@ -13,8 +13,6 @@ // See the License for the specific language governing permissions and // limitations under the License. // -use std::sync::Arc; - use anyhow::{Ok, Result}; use opentelemetry::global::{self, shutdown_tracer_provider}; use opentelemetry_otlp::WithExportConfig; @@ -25,19 +23,21 @@ use opentelemetry_sdk::{runtime, Resource}; use crate::config::Config; +pub const SERVICE_NAME: &str = opentelemetry_semantic_conventions::resource::SERVICE_NAME; + pub trait Observability { - fn build(&mut self) -> Result<()>; + fn build(self) -> Result; fn shutdown(&mut self) -> Result<()>; } pub struct ObservabilityImpl { - config: Arc, + config: Config, meter_provider: Option, tracer_provider: Option, } impl ObservabilityImpl { - fn new(cfg: Arc) -> ObservabilityImpl { + pub fn new(cfg: Config) -> ObservabilityImpl { ObservabilityImpl { config: cfg, meter_provider: None, @@ -51,9 +51,9 @@ impl ObservabilityImpl { } impl Observability for ObservabilityImpl { - fn build(&mut self) -> Result<()> { + fn build(mut self) -> Result { if !self.config.enabled { - return Ok(()); + return Ok(self); } if self.config.meter.enabled { @@ -92,7 +92,7 @@ impl Observability for ObservabilityImpl { global::set_text_map_propagator(TraceContextPropagator::new()); global::set_tracer_provider(tracer.provider().unwrap()); } - Ok(()) + Ok(self) } fn shutdown(&mut self) -> Result<()> {