Skip to content

Commit

Permalink
feat: update interface
Browse files Browse the repository at this point in the history
Signed-off-by: hlts2 <[email protected]>
  • Loading branch information
hlts2 committed Aug 1, 2024
1 parent 3e34f59 commit 6e94c27
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
14 changes: 12 additions & 2 deletions rust/libs/observability/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand Down
16 changes: 8 additions & 8 deletions rust/libs/observability/src/observability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<ObservabilityImpl, anyhow::Error>;
fn shutdown(&mut self) -> Result<()>;
}

pub struct ObservabilityImpl {
config: Arc<Config>,
config: Config,
meter_provider: Option<SdkMeterProvider>,
tracer_provider: Option<TracerProvider>,
}

impl ObservabilityImpl {
fn new(cfg: Arc<Config>) -> ObservabilityImpl {
pub fn new(cfg: Config) -> ObservabilityImpl {
ObservabilityImpl {
config: cfg,
meter_provider: None,
Expand All @@ -51,9 +51,9 @@ impl ObservabilityImpl {
}

impl Observability for ObservabilityImpl {
fn build(&mut self) -> Result<()> {
fn build(mut self) -> Result<ObservabilityImpl, anyhow::Error> {
if !self.config.enabled {
return Ok(());
return Ok(self);
}

if self.config.meter.enabled {
Expand Down Expand Up @@ -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<()> {
Expand Down

0 comments on commit 6e94c27

Please sign in to comment.