diff options
author | Till <2353100+S7evinK@users.noreply.github.com> | 2023-07-11 13:56:25 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-07-11 13:56:25 +0200 |
commit | 99f94fc73513ca9a9eccd859ce61487f7379a7b1 (patch) | |
tree | 49656aa527bea1ba31d063a4203f5a44d508c0f0 /internal | |
parent | 69b2069dea160faff6b2b13bb3d660037f12649d (diff) |
Add revision to version string (#3147)
Since the removal of `build.sh`, we don't include any information about
the revision Dendrite was build from. Since go1.18, the revision a
binary was build from is automatically included, so we can try to get
that instead.
This also adds a `dendrite_up` metric showing the current version
(`dendrite_up{version="0.13.1+c796f20"} 1`)
Closes #2993
Diffstat (limited to 'internal')
-rw-r--r-- | internal/version.go | 30 |
1 files changed, 28 insertions, 2 deletions
diff --git a/internal/version.go b/internal/version.go index c42b2039..eedc3327 100644 --- a/internal/version.go +++ b/internal/version.go @@ -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) } - if len(parts) > 0 { - version += "+" + strings.Join(parts, ".") + + 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 + } } } |