Thanks to visit codestin.com
Credit goes to docs.autometrics.dev

Rust
Adding version information

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:

LabelEnvironment VariablesDefaultDescription
versionAUTOMETRICS_VERSION or CARGO_PKG_VERSIONCARGO_PKG_VERSION (set by cargo by default)The version of your app
commitAUTOMETRICS_COMMIT or VERGEN_GIT_COMMIT""The Git SHA-1 commit hash of your app
branchAUTOMETRICS_BRANCH or VERGEN_GIT_BRANCH""The Git branch of your app
autometrics.versionNone1.0.0The version of the Autometrics spec your app is targeting. You cannot modify this value, it is hardcoded.
service.nameAUTOMETRICS_SERVICE_NAME, OTEL_SERVICE_NAME or CARGO_PKG_NAMECARGO_PKG_NAME (set by cargo by default)Name of the service
repository.urlAUTOMETRICS_REPOSITORY_URL or CARGO_PKG_REPOSITORYCARGO_PKG_REPOSITORY (set by cargo by default)Repository url where the source code of your app is available
repository.providerAUTOMETRICS_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");
}