From 5b6c04976099344b802d30ca285e9788d6aff0f7 Mon Sep 17 00:00:00 2001 From: Julien Pivotto Date: Sat, 16 Apr 2022 00:53:50 +0200 Subject: [PATCH] go118: Get VCS info from debug.BuildInfo Signed-off-by: Julien Pivotto --- version/info.go | 6 ++--- version/info_default.go | 21 +++++++++++++++ version/info_go118.go | 58 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+), 3 deletions(-) create mode 100644 version/info_default.go create mode 100644 version/info_go118.go diff --git a/version/info.go b/version/info.go index 3e2a7ee5..39bcfba6 100644 --- a/version/info.go +++ b/version/info.go @@ -46,7 +46,7 @@ func NewCollector(program string) prometheus.Collector { ), ConstLabels: prometheus.Labels{ "version": Version, - "revision": Revision, + "revision": getRevision(), "branch": Branch, "goversion": GoVersion, }, @@ -69,7 +69,7 @@ func Print(program string) string { m := map[string]string{ "program": program, "version": Version, - "revision": Revision, + "revision": getRevision(), "branch": Branch, "buildUser": BuildUser, "buildDate": BuildDate, @@ -87,7 +87,7 @@ func Print(program string) string { // Info returns version, branch and revision information. func Info() string { - return fmt.Sprintf("(version=%s, branch=%s, revision=%s)", Version, Branch, Revision) + return fmt.Sprintf("(version=%s, branch=%s, revision=%s)", Version, Branch, getRevision()) } // BuildContext returns goVersion, buildUser and buildDate information. diff --git a/version/info_default.go b/version/info_default.go new file mode 100644 index 00000000..2ab0be00 --- /dev/null +++ b/version/info_default.go @@ -0,0 +1,21 @@ +// Copyright 2022 The Prometheus Authors +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//go:build !go1.18 +// +build !go1.18 + +package version + +func getRevision() string { + return Revision +} diff --git a/version/info_go118.go b/version/info_go118.go new file mode 100644 index 00000000..bed0f499 --- /dev/null +++ b/version/info_go118.go @@ -0,0 +1,58 @@ +// Copyright 2022 The Prometheus Authors +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//go:build go1.18 +// +build go1.18 + +package version + +import "runtime/debug" + +var computedRevision string + +func getRevision() string { + if Revision != "" { + return Revision + } + return computedRevision +} + +func init() { + computedRevision = computeRevision() +} + +func computeRevision() string { + var ( + rev = "unknown" + modified bool + ) + + buildInfo, ok := debug.ReadBuildInfo() + if !ok { + return rev + } + for _, v := range buildInfo.Settings { + if v.Key == "vcs.revision" { + rev = v.Value + } + if v.Key == "vcs.modified" { + if v.Value == "true" { + modified = true + } + } + } + if modified { + return rev + "-modified" + } + return rev +}