diff --git a/CHANGELOG.md b/CHANGELOG.md index d9f8212..28595eb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Fixed missing feature flag for `opentelemetry-otlp` when autometrics feature `otel-push-exporter` is enabled +### Autometrics 1.0 compliance + +The [Autometrics specification v1.0.0](https://github.com/autometrics-dev/autometrics-shared/blob/main/specs/autometrics_v1.0.0.md) has been released. +The following changes are made in order to bring the release in compliance with the spec: + +- Added `repository_url` and `repository_provider` as `build_info` labels. They can be + configured either through environment variables or automatically based on your `Cargo.toml` + `package.repository` value (only GitHub, GitLab and BitBucket) (#152) + ## [0.6.0](https://github.com/autometrics-dev/autometrics-rs/releases/tag/v0.5.0) - 2023-08-08 ### Added diff --git a/autometrics-macros/src/lib.rs b/autometrics-macros/src/lib.rs index c90f911..bc29e6a 100644 --- a/autometrics-macros/src/lib.rs +++ b/autometrics-macros/src/lib.rs @@ -308,6 +308,8 @@ 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/constants.rs b/autometrics/src/constants.rs index 77acae6..26f49b2 100644 --- a/autometrics/src/constants.rs +++ b/autometrics/src/constants.rs @@ -37,3 +37,7 @@ pub const COMMIT_KEY: &str = "commit"; pub const BRANCH_KEY: &str = "branch"; pub const SERVICE_NAME_KEY: &str = "service.name"; pub const SERVICE_NAME_KEY_PROMETHEUS: &str = "service_name"; +pub const REPO_URL_KEY: &str = "repository.url"; +pub const REPO_URL_KEY_PROMETHEUS: &str = "repository_url"; +pub const REPO_PROVIDER_KEY: &str = "repository.provider"; +pub const REPO_PROVIDER_KEY_PROMETHEUS: &str = "repository_provider"; diff --git a/autometrics/src/labels.rs b/autometrics/src/labels.rs index 4cc79f7..f242a9c 100644 --- a/autometrics/src/labels.rs +++ b/autometrics/src/labels.rs @@ -15,15 +15,23 @@ pub struct BuildInfoLabels { pub(crate) commit: &'static str, pub(crate) version: &'static str, pub(crate) service_name: &'static str, + pub(crate) repo_url: &'static str, + pub(crate) repo_provider: &'static str, } impl BuildInfoLabels { - pub fn new(version: &'static str, commit: &'static str, branch: &'static str) -> Self { + 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); + } + Self { version, commit, branch, service_name: &get_settings().service_name, + repo_url, + repo_provider } } @@ -33,8 +41,24 @@ impl BuildInfoLabels { (VERSION_KEY, self.version), (BRANCH_KEY, self.branch), (SERVICE_NAME_KEY, self.service_name), + (REPO_URL_KEY, self.repo_url), + (REPO_PROVIDER_KEY, self.repo_provider) ] } + + 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/tracker/prometheus.rs b/autometrics/src/tracker/prometheus.rs index dd3d796..87d9218 100644 --- a/autometrics/src/tracker/prometheus.rs +++ b/autometrics/src/tracker/prometheus.rs @@ -73,7 +73,9 @@ static BUILD_INFO: Lazy = Lazy::new(|| { COMMIT_KEY, VERSION_KEY, BRANCH_KEY, - SERVICE_NAME_KEY_PROMETHEUS + SERVICE_NAME_KEY_PROMETHEUS, + REPO_URL_KEY_PROMETHEUS, + REPO_PROVIDER_KEY_PROMETHEUS, ], get_settings().prometheus_registry.clone() ) @@ -143,6 +145,8 @@ impl TrackMetrics for PrometheusTracker { build_info_labels.version, build_info_labels.branch, build_info_labels.service_name, + build_info_labels.repo_url, + build_info_labels.repo_provider ]) .set(1); });