Skip to content

Commit

Permalink
Merge pull request #455 from bboreham/build-tags
Browse files Browse the repository at this point in the history
version: add Go build tags to output
  • Loading branch information
roidelapluie authored Mar 6, 2023
2 parents d7d98af + 56d83af commit 6a5f4db
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
5 changes: 4 additions & 1 deletion version/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func NewCollector(program string) prometheus.Collector {
"goversion": GoVersion,
"goos": GoOS,
"goarch": GoArch,
"tags": getTags(),
},
},
func() float64 { return 1 },
Expand All @@ -66,6 +67,7 @@ var versionInfoTmpl = `
build date: {{.buildDate}}
go version: {{.goVersion}}
platform: {{.platform}}
tags: {{.tags}}
`

// Print returns version information.
Expand All @@ -79,6 +81,7 @@ func Print(program string) string {
"buildDate": BuildDate,
"goVersion": GoVersion,
"platform": GoOS + "/" + GoArch,
"tags": getTags(),
}
t := template.Must(template.New("version").Parse(versionInfoTmpl))

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

// BuildContext returns goVersion, platform, buildUser and buildDate information.
func BuildContext() string {
return fmt.Sprintf("(go=%s, platform=%s, user=%s, date=%s)", GoVersion, GoOS+"/"+GoArch, BuildUser, BuildDate)
return fmt.Sprintf("(go=%s, platform=%s, user=%s, date=%s, tags=%s)", GoVersion, GoOS+"/"+GoArch, BuildUser, BuildDate, getTags())
}
4 changes: 4 additions & 0 deletions version/info_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@ package version
func getRevision() string {
return Revision
}

func getTags() string {
return "unknown" // Not available prior to Go 1.18
}
19 changes: 14 additions & 5 deletions version/info_go118.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package version
import "runtime/debug"

var computedRevision string
var computedTags string

func getRevision() string {
if Revision != "" {
Expand All @@ -27,19 +28,24 @@ func getRevision() string {
return computedRevision
}

func getTags() string {
return computedTags
}

func init() {
computedRevision = computeRevision()
computedRevision, computedTags = computeRevision()
}

func computeRevision() string {
func computeRevision() (string, string) {
var (
rev = "unknown"
tags = "unknown"
modified bool
)

buildInfo, ok := debug.ReadBuildInfo()
if !ok {
return rev
return rev, tags
}
for _, v := range buildInfo.Settings {
if v.Key == "vcs.revision" {
Expand All @@ -50,9 +56,12 @@ func computeRevision() string {
modified = true
}
}
if v.Key == "-tags" {
tags = v.Value
}
}
if modified {
return rev + "-modified"
return rev + "-modified", tags
}
return rev
return rev, tags
}

0 comments on commit 6a5f4db

Please sign in to comment.