Skip to content

Commit f21a5db

Browse files
committed
Properly include source version in the binary and expose in metrics
Due to golang being annoying the VCS version isn't recorded in debug.ReadBuildInfo when using `go build`. If you have too much time, see: - golang/go#29228 - golang/go#50603
1 parent 51239b0 commit f21a5db

File tree

3 files changed

+30
-13
lines changed

3 files changed

+30
-13
lines changed

rolling-shutter/Makefile

+5-5
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ EXECUTABLE ?= ${BINDIR}/rolling-shutter
1010
GOPATH ?= $(${GO} env GOPATH)
1111

1212
build:
13-
${GO} build ${GOFLAGS} -o ${EXECUTABLE}
13+
${GO} build ${GOFLAGS} -ldflags="-X github.com/shutter-network/rolling-shutter/rolling-shutter/cmd/shversion.version=$(shell git describe --always --dirty)" -o ${EXECUTABLE}
1414

1515
shcryptowasm: wasm
1616
echo "The 'shcryptowasm' target is deprecated, use 'wasm' instead."
@@ -106,10 +106,10 @@ install-asdf-plugins:
106106
../tools/asdf-install-plugins.sh
107107

108108
install-asdf: install-asdf-plugins
109-
# Various packages installed by asdf internally also use make and don't like if an external make has already
110-
# tinkered with then environment.
111-
# Therefore we unset MAKELEVEL here.
112-
env -u MAKELEVEL asdf install
109+
@# Various packages installed by asdf internally also use make and don't like if an external make has already
110+
@# tinkered with then environment.
111+
@# Therefore we unset MAKELEVEL here.
112+
@env -u MAKELEVEL asdf install
113113

114114
lint:
115115
golangci-lint run --tests

rolling-shutter/cmd/shversion/shversion.go

+10-6
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,19 @@ import (
88
"runtime/debug"
99
)
1010

11+
// This gets set via ldflags when building via the Makefile
1112
var version string
1213

1314
// Version returns shuttermint's version string.
1415
func Version() string {
16+
var raceinfo string
17+
if raceDetectorEnabled {
18+
raceinfo = ", race detector enabled"
19+
}
20+
return fmt.Sprintf("%s (%s, %s-%s%s)", VersionShort(), runtime.Version(), runtime.GOOS, runtime.GOARCH, raceinfo)
21+
}
22+
23+
func VersionShort() string {
1524
if version == "" {
1625
info, ok := debug.ReadBuildInfo()
1726
if ok {
@@ -26,10 +35,5 @@ func Version() string {
2635
}
2736
}
2837
}
29-
30-
var raceinfo string
31-
if raceDetectorEnabled {
32-
raceinfo = ", race detector enabled"
33-
}
34-
return fmt.Sprintf("%s (%s, %s-%s%s)", version, runtime.Version(), runtime.GOOS, runtime.GOARCH, raceinfo)
38+
return version
3539
}

rolling-shutter/medley/metricsserver/metricsserver.go

+15-2
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,28 @@ import (
77
"time"
88

99
"github.com/prometheus/client_golang/prometheus"
10-
"github.com/prometheus/client_golang/prometheus/collectors"
1110
"github.com/prometheus/client_golang/prometheus/promhttp"
1211
"github.com/rs/zerolog/log"
1312

13+
"github.com/shutter-network/rolling-shutter/rolling-shutter/cmd/shversion"
1414
"github.com/shutter-network/rolling-shutter/rolling-shutter/medley/service"
1515
)
1616

17+
var metricsGoBuildInfo = prometheus.NewGauge(
18+
prometheus.GaugeOpts{
19+
Namespace: "go",
20+
Subsystem: "build",
21+
Name: "info",
22+
Help: "A metric with a constant '1' value labeled by version from which the binary was built.",
23+
ConstLabels: prometheus.Labels{
24+
"version": shversion.VersionShort(),
25+
},
26+
},
27+
)
28+
1729
func init() {
18-
prometheus.MustRegister(collectors.NewBuildInfoCollector())
30+
metricsGoBuildInfo.Set(1)
31+
prometheus.MustRegister(metricsGoBuildInfo)
1932
}
2033

2134
type MetricsServer struct {

0 commit comments

Comments
 (0)