Skip to content

Commit

Permalink
allow setting repo url and provider at runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
mellowagain committed Nov 15, 2023
1 parent 4cfd2f3 commit cc7bbc5
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 26 deletions.
2 changes: 0 additions & 2 deletions autometrics-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
};
Expand Down
28 changes: 5 additions & 23 deletions autometrics/src/labels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

Expand All @@ -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.
Expand Down
44 changes: 44 additions & 0 deletions autometrics/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ pub struct AutometricsSettings {
#[cfg(any(prometheus_exporter, prometheus, prometheus_client))]
pub(crate) histogram_buckets: Vec<f64>,
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)]
Expand Down Expand Up @@ -75,6 +77,8 @@ impl AutometricsSettings {
#[derive(Debug, Default)]
pub struct AutometricsSettingsBuilder {
pub(crate) service_name: Option<String>,
pub(crate) repo_url: Option<String>,
pub(crate) repo_provider: Option<String>,
#[cfg(any(prometheus_exporter, prometheus, prometheus_client))]
pub(crate) histogram_buckets: Option<Vec<f64>>,
#[cfg(any(prometheus, opentelemetry))]
Expand Down Expand Up @@ -111,6 +115,16 @@ impl AutometricsSettingsBuilder {
self
}

pub fn repo_url(mut self, repo_url: impl Into<String>) -> Self {
self.repo_url = Some(repo_url.into());
self
}

pub fn repo_provider(mut self, repo_provider: impl Into<String>) -> 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`].
Expand Down Expand Up @@ -184,6 +198,11 @@ impl AutometricsSettingsBuilder {
.unwrap_or_else(|| <prometheus_client::registry::Registry>::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
Expand All @@ -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)]
Expand All @@ -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)]
Expand Down
2 changes: 1 addition & 1 deletion autometrics/src/tracker/prometheus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down

0 comments on commit cc7bbc5

Please sign in to comment.