Try to get the vcs.revision when building the version string

This commit is contained in:
Till Faelligen 2023-07-10 09:56:11 +02:00
parent 69b2069dea
commit c796f20d1c
No known key found for this signature in database
GPG key ID: ACCDC9606D472758
2 changed files with 39 additions and 2 deletions

View file

@ -26,6 +26,7 @@ import (
"github.com/matrix-org/dendrite/setup/jetstream"
"github.com/matrix-org/dendrite/setup/process"
"github.com/matrix-org/gomatrixserverlib/fclient"
"github.com/prometheus/client_golang/prometheus"
"github.com/sirupsen/logrus"
"github.com/matrix-org/dendrite/appservice"
@ -187,6 +188,16 @@ func main() {
}
}
upCounter := prometheus.NewCounter(prometheus.CounterOpts{
Namespace: "dendrite",
Name: "up",
ConstLabels: map[string]string{
"version": internal.VersionString(),
},
})
upCounter.Add(1)
prometheus.MustRegister(upCounter)
// Expose the matrix APIs directly rather than putting them under a /api path.
go func() {
basepkg.SetupAndServeHTTP(processCtx, cfg, routers, httpAddr, nil, nil)

View file

@ -2,6 +2,7 @@ package internal
import (
"fmt"
"runtime/debug"
"strings"
)
@ -19,6 +20,8 @@ const (
VersionMinor = 13
VersionPatch = 1
VersionTag = "" // example: "rc1"
gitRevLen = 7 // 7 matches the displayed characters on github.com
)
func VersionString() string {
@ -37,7 +40,30 @@ func init() {
if branch != "" {
parts = append(parts, branch)
}
defer func() {
if len(parts) > 0 {
version += "+" + strings.Join(parts, ".")
}
}()
// Try to get the revision Dendrite was build from.
// If we can't, e.g. Dendrite wasn't built (go run) or no VCS version is present,
// we just use the provided version above.
info, ok := debug.ReadBuildInfo()
if !ok {
return
}
for _, setting := range info.Settings {
if setting.Key == "vcs.revision" {
revLen := len(setting.Value)
if revLen >= gitRevLen {
parts = append(parts, setting.Value[:gitRevLen])
} else {
parts = append(parts, setting.Value[:revLen])
}
break
}
}
}