diff --git a/autometrics-macros/src/lib.rs b/autometrics-macros/src/lib.rs index bc29e6a..c90f911 100644 --- a/autometrics-macros/src/lib.rs +++ b/autometrics-macros/src/lib.rs @@ -308,8 +308,6 @@ fn instrument_function( option_env!("AUTOMETRICS_VERSION").or(option_env!("CARGO_PKG_VERSION")).unwrap_or_default(), option_env!("AUTOMETRICS_COMMIT").or(option_env!("VERGEN_GIT_SHA")).unwrap_or_default(), option_env!("AUTOMETRICS_BRANCH").or(option_env!("VERGEN_GIT_BRANCH")).unwrap_or_default(), - option_env!("AUTOMETRICS_REPOSITORY_URL").or(option_env!("CARGO_PKG_REPOSITORY")).unwrap_or_default(), - option_env!("AUTOMETRICS_REPOSITORY_PROVIDER").unwrap_or_default(), )); AutometricsTracker::start(#gauge_labels) }; diff --git a/autometrics/src/labels.rs b/autometrics/src/labels.rs index 89d8925..a3cb2e4 100644 --- a/autometrics/src/labels.rs +++ b/autometrics/src/labels.rs @@ -21,19 +21,15 @@ pub struct BuildInfoLabels { } impl BuildInfoLabels { - pub fn new(version: &'static str, commit: &'static str, branch: &'static str, repo_url: &'static str, mut repo_provider: &'static str) -> Self { - if repo_provider.is_empty() { - repo_provider = Self::determinate_repo_provider_from_url(repo_url); - } - + pub fn new(version: &'static str, commit: &'static str, branch: &'static str) -> Self { Self { version, commit, branch, service_name: &get_settings().service_name, - repo_url, - repo_provider, - autometrics_version: AUTOMETRICS_SPEC_TARGET + repo_url: &get_settings().repo_url, + repo_provider: &get_settings().repo_provider, + autometrics_version: AUTOMETRICS_SPEC_TARGET, } } @@ -45,23 +41,9 @@ impl BuildInfoLabels { (SERVICE_NAME_KEY, self.service_name), (REPO_URL_KEY, self.repo_url), (REPO_PROVIDER_KEY, self.repo_provider), - (AUTOMETRICS_VERSION_KEY, self.autometrics_version) + (AUTOMETRICS_VERSION_KEY, self.autometrics_version), ] } - - fn determinate_repo_provider_from_url(url: &'static str) -> &'static str { - let lowered = url.to_lowercase(); - - if lowered.contains("github.com") { - "github" - } else if lowered.contains("gitlab.com") { - "gitlab" - } else if lowered.contains("bitbucket.org") { - "bitbucket" - } else { - "" - } - } } /// These are the labels used for the `function.calls` metric. diff --git a/autometrics/src/settings.rs b/autometrics/src/settings.rs index eb45efa..e042d05 100644 --- a/autometrics/src/settings.rs +++ b/autometrics/src/settings.rs @@ -34,6 +34,8 @@ pub struct AutometricsSettings { #[cfg(any(prometheus_exporter, prometheus, prometheus_client))] pub(crate) histogram_buckets: Vec, pub(crate) service_name: String, + pub(crate) repo_url: String, + pub(crate) repo_provider: String, #[cfg(any(prometheus, opentelemetry))] pub(crate) prometheus_registry: prometheus::Registry, #[cfg(prometheus_client)] @@ -75,6 +77,8 @@ impl AutometricsSettings { #[derive(Debug, Default)] pub struct AutometricsSettingsBuilder { pub(crate) service_name: Option, + pub(crate) repo_url: Option, + pub(crate) repo_provider: Option, #[cfg(any(prometheus_exporter, prometheus, prometheus_client))] pub(crate) histogram_buckets: Option>, #[cfg(any(prometheus, opentelemetry))] @@ -111,6 +115,16 @@ impl AutometricsSettingsBuilder { self } + pub fn repo_url(mut self, repo_url: impl Into) -> Self { + self.repo_url = Some(repo_url.into()); + self + } + + pub fn repo_provider(mut self, repo_provider: impl Into) -> Self { + self.repo_provider = Some(repo_provider.into()); + self + } + /// Configure the [`prometheus::Registry`] that will be used to collect metrics when using /// either the `prometheus` or `opentelemetry` backends. If none is set, it will use /// the [`prometheus::default_registry`]. @@ -184,6 +198,11 @@ impl AutometricsSettingsBuilder { .unwrap_or_else(|| ::default()), ); + let repo_url = self + .repo_url + .or_else(|| env::var("AUTOMETRICS_REPOSITORY_URL").ok()) + .unwrap_or_else(|| env!("CARGO_PKG_REPOSITORY").to_string()); + AutometricsSettings { #[cfg(any(prometheus_exporter, prometheus, prometheus_client))] histogram_buckets: self @@ -194,6 +213,15 @@ impl AutometricsSettingsBuilder { .or_else(|| env::var("AUTOMETRICS_SERVICE_NAME").ok()) .or_else(|| env::var("OTEL_SERVICE_NAME").ok()) .unwrap_or_else(|| env!("CARGO_PKG_NAME").to_string()), + repo_provider: self + .repo_provider + .or_else(|| env::var("AUTOMETRICS_REPOSITORY_PROVIDER").ok()) + .or_else(|| { + AutometricsSettingsBuilder::determinate_repo_provider_from_url(Some(&repo_url)) + .map(|s| s.to_string()) + }) + .unwrap_or_default(), + repo_url, #[cfg(prometheus_client)] prometheus_client_registry, #[cfg(prometheus_client)] @@ -204,6 +232,22 @@ impl AutometricsSettingsBuilder { .unwrap_or_else(|| prometheus::default_registry().clone()), } } + + fn determinate_repo_provider_from_url(url: Option<&str>) -> Option<&'static str> { + url.and_then(|url| { + let lowered = url.to_lowercase(); + + if lowered.contains("github.com") { + Some("github") + } else if lowered.contains("gitlab.com") { + Some("gitlab") + } else if lowered.contains("bitbucket.org") { + Some("bitbucket") + } else { + None + } + }) + } } #[derive(Debug, Error)] diff --git a/autometrics/src/tracker/prometheus.rs b/autometrics/src/tracker/prometheus.rs index 30b9bf2..3c7e356 100644 --- a/autometrics/src/tracker/prometheus.rs +++ b/autometrics/src/tracker/prometheus.rs @@ -148,7 +148,7 @@ impl TrackMetrics for PrometheusTracker { build_info_labels.service_name, build_info_labels.repo_url, build_info_labels.repo_provider, - AUTOMETRICS_SPEC_TARGET + build_info_labels.autometrics_version, ]) .set(1); });