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

Fill in version information #962

Merged
merged 6 commits into from
Feb 9, 2022
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
10 changes: 8 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down
12 changes: 1 addition & 11 deletions cmd/mimir/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shall we change cortex to mimir here? It would change cortex_build_info to mimir_build_info, and this needs to be documented in CHANGELOG. I don't see this metric used anywhere in our jsonnet or internal tooling. (Can be separate PR too)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that makes a lot of sense given that this is no longer cortex build information. Probably best to do it in another PR though since I'm unaware of what the plans are for metric renaming.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general we plan to keep metrics as-is. However in this specific case, I think it makes sense to rename. But we can discuss that separately.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Opened #1022

prometheus.MustRegister(configHash)
Expand Down
78 changes: 78 additions & 0 deletions pkg/util/version/info.go
Original file line number Diff line number Diff line change
@@ -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)
}