version: add more data to prometheus metric

This commit is contained in:
2023-07-08 14:45:55 -07:00
parent cca1240a65
commit 7550d1597c
3 changed files with 49 additions and 26 deletions

View File

@@ -21,9 +21,10 @@ var gitModified bool
var info Info
type Info struct {
Version string `json:",omitempty"`
GitRev string `json:",omitempty"`
BuildTime string `json:",omitempty"`
Version string `json:",omitempty"`
GitRev string `json:",omitempty"`
GitRevShort string `json:",omitempty"`
BuildTime string `json:",omitempty"`
}
func init() {
@@ -46,13 +47,18 @@ func init() {
// logger.Setup().Info("build info", "k", h.Key, "v", h.Value)
switch h.Key {
case "vcs.time":
buildTime = h.Value
if len(buildTime) == 0 {
buildTime = h.Value
}
info.BuildTime = h.Value
case "vcs.revision":
// https://blog.carlmjohnson.net/post/2023/golang-git-hash-how-to/
// todo: use BuildInfo.Main.Version if revision is empty
gitVersion = h.Value
info.GitRev = h.Value
// if gitVersion != h.Value {
// logger.Setup().Warn("gitVersion and info.GitRev differs", "gitVersion", gitVersion, "gitRev", info.GitRev)
// }
gitVersion = h.Value
case "vcs.modified":
if h.Value == "true" {
gitModified = true
@@ -61,6 +67,12 @@ func init() {
}
}
info.GitRevShort = info.GitRev
if len(info.GitRevShort) > 7 {
info.GitRevShort = info.GitRevShort[:7]
}
info.Version = VERSION
Version()
@@ -84,11 +96,23 @@ func RegisterMetric(registry prometheus.Registerer) {
Name: "build_info",
Help: "Build information",
},
[]string{"version", "build"},
[]string{
"version",
"buildtime",
"gittime",
"git",
},
)
registry.MustRegister(buildInfo)
info := VersionInfo()
buildInfo.WithLabelValues(fmt.Sprintf("%s/%s", info.Version, info.GitRev), info.BuildTime).Set(1)
buildInfo.WithLabelValues(
fmt.Sprintf("%s/%s",
info.Version, info.GitRevShort,
),
buildTime,
info.BuildTime,
info.GitRev,
).Set(1)
}
var v string