Skip to content

Commit

Permalink
[autometrics 1.0 compliance] add repo url and provider (#152)
Browse files Browse the repository at this point in the history
* add repo url and provider

* update changelog

* coulda lso add the pr number ngl

* fix wrong cardinality

* unintended change my bad
  • Loading branch information
mellowagain authored Nov 9, 2023
1 parent 10e9c77 commit b887363
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 2 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions autometrics-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
};
Expand Down
4 changes: 4 additions & 0 deletions autometrics/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
26 changes: 25 additions & 1 deletion autometrics/src/labels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand All @@ -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.
Expand Down
6 changes: 5 additions & 1 deletion autometrics/src/tracker/prometheus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ static BUILD_INFO: Lazy<IntGaugeVec> = 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()
)
Expand Down Expand Up @@ -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);
});
Expand Down

0 comments on commit b887363

Please sign in to comment.