Skip to content

Commit

Permalink
feat: add endpint configuration to root configuration
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 6e94c27 commit e32bf43
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 24 deletions.
1 change: 1 addition & 0 deletions rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions rust/libs/observability/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ opentelemetry-semantic-conventions = { version = "0.16.0"}
scopeguard = { version = "1.2.0"}
paste = {version = "1.0.15"}
anyhow = { version = "1.0.86"}
url = { version = "2.5.2"}
27 changes: 9 additions & 18 deletions rust/libs/observability/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use opentelemetry_sdk::{self, Resource};
#[derive(Clone, Debug)]
pub struct Config {
pub enabled: bool,
pub endpoint: String,
pub attributes: HashMap<String, String>,
pub tracer: Tracer,
pub meter: Meter,
Expand All @@ -30,13 +31,11 @@ pub struct Config {
#[derive(Clone, Debug)]
pub struct Tracer {
pub enabled: bool,
pub endpoint: String,
}

#[derive(Clone, Debug)]
pub struct Meter {
pub enabled: bool,
pub endpoint: String,
pub export_duration: Duration,
pub export_timeout_duration: Duration,
}
Expand All @@ -45,6 +44,7 @@ impl Config {
pub fn new() -> Self {
Self {
enabled: false,
endpoint: "".to_string(),
attributes: HashMap::new(),
tracer: Tracer::default(),
meter: Meter::default(),
Expand All @@ -56,6 +56,11 @@ impl Config {
self
}

pub fn endpoint(mut self, endpoint: &str) -> Self {
self.endpoint = endpoint.to_string();
self
}

pub fn attributes(mut self, attrs: HashMap<String, String>) -> Self {
self.attributes = attrs;
self
Expand Down Expand Up @@ -96,21 +101,13 @@ impl From<&Config> for Resource {

impl Tracer {
pub fn new() -> Self {
Self {
enabled: false,
endpoint: "".to_string(),
}
Self { enabled: false }
}

pub fn enabled(mut self, enabled: bool) -> Self {
self.enabled = enabled;
self
}

pub fn endpoint(mut self, endpoint: &str) -> Self {
self.endpoint = endpoint.to_string();
self
}
}

impl Default for Tracer {
Expand All @@ -123,9 +120,8 @@ impl Meter {
pub fn new() -> Self {
Self {
enabled: false,
endpoint: "".to_string(),
export_duration: Duration::from_secs(1),
export_timeout_duration: Duration::from_secs(10),
export_timeout_duration: Duration::from_secs(5),
}
}

Expand All @@ -134,11 +130,6 @@ 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
17 changes: 11 additions & 6 deletions rust/libs/observability/src/observability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use opentelemetry_sdk::metrics::SdkMeterProvider;
use opentelemetry_sdk::propagation::TraceContextPropagator;
use opentelemetry_sdk::trace::{self, TracerProvider};
use opentelemetry_sdk::{runtime, Resource};
use url::Url;

use crate::config::Config;

Expand Down Expand Up @@ -65,9 +66,11 @@ impl Observability for ObservabilityImpl {
.with_resource(Resource::from(self.config()))
.with_timeout(self.config.meter.export_timeout_duration)
.with_exporter(
opentelemetry_otlp::new_exporter()
.tonic()
.with_endpoint(self.config.meter.endpoint.as_str()),
opentelemetry_otlp::new_exporter().tonic().with_endpoint(
Url::parse(self.config.endpoint.as_str())?
.join("/v1/metrics")?
.as_str(),
),
)
.build()?;
self.meter_provider = Some(provider.clone());
Expand All @@ -78,9 +81,11 @@ impl Observability for ObservabilityImpl {
let tracer = opentelemetry_otlp::new_pipeline()
.tracing()
.with_exporter(
opentelemetry_otlp::new_exporter()
.tonic()
.with_endpoint(self.config.tracer.endpoint.as_str()),
opentelemetry_otlp::new_exporter().tonic().with_endpoint(
Url::parse(self.config.endpoint.as_str())?
.join("/v1/traces")?
.as_str(),
),
)
.with_trace_config(
trace::config()
Expand Down

0 comments on commit e32bf43

Please sign in to comment.