Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add revision to version string #3147

Merged
merged 2 commits into from
Jul 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
bin
*.wasm
.git
*.wasm
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# base installs required dependencies and runs go mod download to cache dependencies
#
FROM --platform=${BUILDPLATFORM} docker.io/golang:1.20-alpine AS base
RUN apk --update --no-cache add bash build-base curl
RUN apk --update --no-cache add bash build-base curl git

#
# build creates all needed binaries
Expand Down
11 changes: 11 additions & 0 deletions cmd/dendrite/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
30 changes: 28 additions & 2 deletions internal/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package internal

import (
"fmt"
"runtime/debug"
"strings"
)

Expand All @@ -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 {
Expand All @@ -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
}
}
}
Loading