Adding version information to metrics in Rust
Autometrics makes it easy to spot versions and commits that introduce errors or latency.
It produces a build_info
metric and uses labels to expose various metadata related to your app to Prometheus.
build_info{branch="main",commit="4cfd2f3b26224fa82daf4ba68fe36e188f3153ff",version="1.0.0",service_name="api",repo_url="https://github.com/autometrics-dev/autometrics-rs",repo_provider="github",autometrics_version="1.0.0"} 1
Labels are exposed and can be set as environment variables at compile or run time:
Label | Environment Variables | Default | Description |
---|---|---|---|
version | AUTOMETRICS_VERSION or CARGO_PKG_VERSION | CARGO_PKG_VERSION (set by cargo by default) | The version of your app |
commit | AUTOMETRICS_COMMIT or VERGEN_GIT_COMMIT | "" | The Git SHA-1 commit hash of your app |
branch | AUTOMETRICS_BRANCH or VERGEN_GIT_BRANCH | "" | The Git branch of your app |
autometrics.version | None | 1.0.0 | The version of the Autometrics spec your app is targeting. You cannot modify this value, it is hardcoded. |
service.name | AUTOMETRICS_SERVICE_NAME , OTEL_SERVICE_NAME or CARGO_PKG_NAME | CARGO_PKG_NAME (set by cargo by default) | Name of the service |
repository.url | AUTOMETRICS_REPOSITORY_URL or CARGO_PKG_REPOSITORY | CARGO_PKG_REPOSITORY (set by cargo by default) | Repository url where the source code of your app is available |
repository.provider | AUTOMETRICS_REPOSITORY_PROVIDER | "" | Repository provider for `repository.url`, e.g `github` or `gitlab` |
Using vergen
to set the git
details
You can use the vergen (opens in a new tab) crate to set the git
details that Autometrics can then pick up.
Add vergen
to your Cargo.toml
file
Cargo.toml
[build-dependencies]
vergen = { version = "8.1", features = ["git", "gitcl"] }
Add the following snippet to your build.rs
file
build.rs
fn main() {
vergen::EmitBuilder::builder()
.git_sha(true)
.git_branch()
.emit()
.expect("Unable to generate build info");
}