diff --git a/Makefile b/Makefile index 67c3f63206e..f8a00323497 100644 --- a/Makefile +++ b/Makefile @@ -178,7 +178,13 @@ LATEST_BUILD_IMAGE_TAG ?= trafficdump-b1d000267 # as it currently disallows TTY devices. This value needs to be overridden # in any custom cloudbuild.yaml files TTY := --tty -GO_FLAGS := -ldflags "-X main.Branch=$(GIT_BRANCH) -X main.Revision=$(GIT_REVISION) -X main.Version=$(VERSION) -extldflags \"-static\" -s -w" -tags netgo +MIMIR_VERSION := github.com/grafana/mimir/pkg/util/version + +GO_FLAGS := -ldflags "\ + -X $(MIMIR_VERSION).Branch=$(GIT_BRANCH) \ + -X $(MIMIR_VERSION).Revision=$(GIT_REVISION) \ + -X $(MIMIR_VERSION).Version=$(VERSION) \ + -extldflags \"-static\" -s -w" -tags netgo ifeq ($(BUILD_IN_CONTAINER),true) @@ -344,7 +350,7 @@ check-makefiles: format-makefiles .PHONY: format-makefiles format-makefiles: ## Format all Makefiles. format-makefiles: $(MAKEFILES) - sed -i -e 's/^\(\t*\) /\1\t/g' -e 's/^\(\t*\) /\1/' -- $? + $(SED) -i -e 's/^\(\t*\) /\1\t/g' -e 's/^\(\t*\) /\1/' -- $? clean: $(SUDO) docker rmi $(IMAGE_NAMES) >/dev/null 2>&1 || true diff --git a/cmd/mimir/main.go b/cmd/mimir/main.go index d4389d71051..191a706b8a7 100644 --- a/cmd/mimir/main.go +++ b/cmd/mimir/main.go @@ -21,19 +21,12 @@ import ( "github.com/grafana/dskit/flagext" "github.com/pkg/errors" "github.com/prometheus/client_golang/prometheus" - "github.com/prometheus/common/version" "github.com/weaveworks/common/tracing" "gopkg.in/yaml.v2" "github.com/grafana/mimir/pkg/mimir" util_log "github.com/grafana/mimir/pkg/util/log" -) - -// Version is set via build flag -ldflags -X main.Version -var ( - Version string - Branch string - Revision string + "github.com/grafana/mimir/pkg/util/version" ) // configHash exposes information about the loaded config @@ -46,9 +39,6 @@ var configHash *prometheus.GaugeVec = prometheus.NewGaugeVec( ) func init() { - version.Version = Version - version.Branch = Branch - version.Revision = Revision prometheus.MustRegister(version.NewCollector("mimir")) prometheus.MustRegister(version.NewCollector("cortex")) prometheus.MustRegister(configHash) diff --git a/pkg/util/version/info.go b/pkg/util/version/info.go new file mode 100644 index 00000000000..d78179b2360 --- /dev/null +++ b/pkg/util/version/info.go @@ -0,0 +1,78 @@ +// SPDX-License-Identifier: AGPL-3.0-only +// Provenance-includes-location: https://github.com/prometheus/common/blob/main/version/info.go +// Provenance-includes-license: Apache-2.0 +// Provenance-includes-copyright: The Prometheus Authors. + +package version + +import ( + "bytes" + "fmt" + "runtime" + "strings" + "text/template" + + "github.com/prometheus/client_golang/prometheus" +) + +// Build information. Populated at build-time. +// Note: Removed BuildUser and BuildDate for reproducible builds +var ( + Version string = "unknown" + Revision string = "unknown" + Branch string = "unknown" + GoVersion = runtime.Version() +) + +// NewCollector returns a collector that exports metrics about current version +// information. +func NewCollector(program string) prometheus.Collector { + return prometheus.NewGaugeFunc( + prometheus.GaugeOpts{ + Namespace: program, + Name: "build_info", + Help: fmt.Sprintf( + "A metric with a constant '1' value labeled by version, revision, branch, and goversion from which %s was built.", + program, + ), + ConstLabels: prometheus.Labels{ + "version": Version, + "revision": Revision, + "branch": Branch, + "goversion": GoVersion, + }, + }, + func() float64 { return 1 }, + ) +} + +// versionInfoTmpl contains the template used by Info. +var versionInfoTmpl = ` +{{.program}}, version {{.version}} (branch: {{.branch}}, revision: {{.revision}}) + go version: {{.goVersion}} + platform: {{.platform}} +` + +// Print returns version information. +func Print(program string) string { + m := map[string]string{ + "program": program, + "version": Version, + "revision": Revision, + "branch": Branch, + "goVersion": GoVersion, + "platform": runtime.GOOS + "/" + runtime.GOARCH, + } + t := template.Must(template.New("version").Parse(versionInfoTmpl)) + + var buf bytes.Buffer + if err := t.ExecuteTemplate(&buf, "version", m); err != nil { + panic(err) + } + return strings.TrimSpace(buf.String()) +} + +// Info returns version, branch and revision information. +func Info() string { + return fmt.Sprintf("(version=%s, branch=%s, revision=%s)", Version, Branch, Revision) +}