Go
Adding version information

Adding version information to metrics in Go

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 the version, commit and branch information of your app to Prometheus.

Example:

build_info{branch="main",commit="5cc2aa5447907fbf0a7ddd",version="1.0.0"} 1

Usage

To add version information to your metrics, simply fill the autometrics.BuildInfo parameter to the autometrics.Init() call:

main.go
autometrics.Init(
    nil,
    autometrics.DefBuckets,
    autometrics.BuildInfo{
        Version: "<version_here>",
        Commit:  "<git_commit_hash_here",
        Branch:  "<git_branch_here>",
    },
    nil,
)

autometrics.BuildInfo parameter accepts each fields as strings which you can set as you wish or use something like ldflags to set them at build time.

Using ldflags to add version information at build time

Declare variables to hold the version information

main.go
var (
    Version = "development"
    Commit  = "n/a"
    Branch  string
)

Fill autometrics.BuildInfo in your autometrics.Init() call

main.go
autometrics.Init(
    nil,
    autometrics.DefBuckets,
    autometrics.BuildInfo{
        Version: Version,
        Commit:  Commit,
        Branch:  Branch,
    },
    nil,
)

Build your app with ldflags

Now you can build your Go app using ldflags to set the version, commit and branch information.

Here's an example that uses git tags, commit hash and branch name:

Shell
go build -ldflags "-X main.Version=$(git describe --tags) -X main.Commit=$(git rev-parse HEAD) -X main.Branch=$(git rev-parse --abbrev-ref HEAD)"