Skip to content

Commit 56d83af

Browse files
committed
version: add Go build tags to output
Build tags allow different implementations of code sections to be included in the binary, so can be important to understand the exact behaviour expected at runtime. This change includes build tags at the end of the `BuildContext` string, and as an extra label on the info metric. Signed-off-by: Bryan Boreham <[email protected]>
1 parent d9a31c0 commit 56d83af

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

version/info.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ func NewCollector(program string) prometheus.Collector {
5353
"goversion": GoVersion,
5454
"goos": GoOS,
5555
"goarch": GoArch,
56+
"tags": getTags(),
5657
},
5758
},
5859
func() float64 { return 1 },
@@ -66,6 +67,7 @@ var versionInfoTmpl = `
6667
build date: {{.buildDate}}
6768
go version: {{.goVersion}}
6869
platform: {{.platform}}
70+
tags: {{.tags}}
6971
`
7072

7173
// Print returns version information.
@@ -79,6 +81,7 @@ func Print(program string) string {
7981
"buildDate": BuildDate,
8082
"goVersion": GoVersion,
8183
"platform": GoOS + "/" + GoArch,
84+
"tags": getTags(),
8285
}
8386
t := template.Must(template.New("version").Parse(versionInfoTmpl))
8487

@@ -96,5 +99,5 @@ func Info() string {
9699

97100
// BuildContext returns goVersion, platform, buildUser and buildDate information.
98101
func BuildContext() string {
99-
return fmt.Sprintf("(go=%s, platform=%s, user=%s, date=%s)", GoVersion, GoOS+"/"+GoArch, BuildUser, BuildDate)
102+
return fmt.Sprintf("(go=%s, platform=%s, user=%s, date=%s, tags=%s)", GoVersion, GoOS+"/"+GoArch, BuildUser, BuildDate, getTags())
100103
}

version/info_default.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,7 @@ package version
1919
func getRevision() string {
2020
return Revision
2121
}
22+
23+
func getTags() string {
24+
return "unknown" // Not available prior to Go 1.18
25+
}

version/info_go118.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package version
1919
import "runtime/debug"
2020

2121
var computedRevision string
22+
var computedTags string
2223

2324
func getRevision() string {
2425
if Revision != "" {
@@ -27,19 +28,24 @@ func getRevision() string {
2728
return computedRevision
2829
}
2930

31+
func getTags() string {
32+
return computedTags
33+
}
34+
3035
func init() {
31-
computedRevision = computeRevision()
36+
computedRevision, computedTags = computeRevision()
3237
}
3338

34-
func computeRevision() string {
39+
func computeRevision() (string, string) {
3540
var (
3641
rev = "unknown"
42+
tags = "unknown"
3743
modified bool
3844
)
3945

4046
buildInfo, ok := debug.ReadBuildInfo()
4147
if !ok {
42-
return rev
48+
return rev, tags
4349
}
4450
for _, v := range buildInfo.Settings {
4551
if v.Key == "vcs.revision" {
@@ -50,9 +56,12 @@ func computeRevision() string {
5056
modified = true
5157
}
5258
}
59+
if v.Key == "-tags" {
60+
tags = v.Value
61+
}
5362
}
5463
if modified {
55-
return rev + "-modified"
64+
return rev + "-modified", tags
5665
}
57-
return rev
66+
return rev, tags
5867
}

0 commit comments

Comments
 (0)