|
| 1 | +// Copyright 2016 The Prometheus Authors |
| 2 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +// you may not use this file except in compliance with the License. |
| 4 | +// You may obtain a copy of the License at |
| 5 | +// |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | + |
| 14 | +package version |
| 15 | + |
| 16 | +import ( |
| 17 | + "bytes" |
| 18 | + "fmt" |
| 19 | + "runtime" |
| 20 | + "runtime/debug" |
| 21 | + "strings" |
| 22 | + "text/template" |
| 23 | + |
| 24 | + "github.com/prometheus/client_golang/prometheus" |
| 25 | +) |
| 26 | + |
| 27 | +// Build information. Populated at build-time. |
| 28 | +var ( |
| 29 | + Version string |
| 30 | + Revision string |
| 31 | + Branch string |
| 32 | + BuildUser string |
| 33 | + BuildDate string |
| 34 | + GoVersion = runtime.Version() |
| 35 | + GoOS = runtime.GOOS |
| 36 | + GoArch = runtime.GOARCH |
| 37 | +) |
| 38 | + |
| 39 | +var ( |
| 40 | + computedRevision string |
| 41 | + computedTags string |
| 42 | +) |
| 43 | + |
| 44 | +func getRevision() string { |
| 45 | + if Revision != "" { |
| 46 | + return Revision |
| 47 | + } |
| 48 | + return computedRevision |
| 49 | +} |
| 50 | + |
| 51 | +func getTags() string { |
| 52 | + return computedTags |
| 53 | +} |
| 54 | + |
| 55 | +func init() { |
| 56 | + computedRevision, computedTags = computeRevision() |
| 57 | +} |
| 58 | + |
| 59 | +func computeRevision() (string, string) { |
| 60 | + var ( |
| 61 | + rev = "unknown" |
| 62 | + tags = "unknown" |
| 63 | + modified bool |
| 64 | + ) |
| 65 | + |
| 66 | + buildInfo, ok := debug.ReadBuildInfo() |
| 67 | + if !ok { |
| 68 | + return rev, tags |
| 69 | + } |
| 70 | + for _, v := range buildInfo.Settings { |
| 71 | + if v.Key == "vcs.revision" { |
| 72 | + rev = v.Value |
| 73 | + } |
| 74 | + if v.Key == "vcs.modified" { |
| 75 | + if v.Value == "true" { |
| 76 | + modified = true |
| 77 | + } |
| 78 | + } |
| 79 | + if v.Key == "-tags" { |
| 80 | + tags = v.Value |
| 81 | + } |
| 82 | + } |
| 83 | + if modified { |
| 84 | + return rev + "-modified", tags |
| 85 | + } |
| 86 | + return rev, tags |
| 87 | +} |
| 88 | + |
| 89 | +// NewCollector returns a collector that exports metrics about current version |
| 90 | +// information. |
| 91 | +func NewCollector(program string) prometheus.Collector { |
| 92 | + return prometheus.NewGaugeFunc( |
| 93 | + prometheus.GaugeOpts{ |
| 94 | + Namespace: program, |
| 95 | + Name: "build_info", |
| 96 | + Help: fmt.Sprintf( |
| 97 | + "A metric with a constant '1' value labeled by version, revision, branch, goversion from which %s was built, and the goos and goarch for the build.", |
| 98 | + program, |
| 99 | + ), |
| 100 | + ConstLabels: prometheus.Labels{ |
| 101 | + "version": Version, |
| 102 | + "revision": getRevision(), |
| 103 | + "branch": Branch, |
| 104 | + "goversion": GoVersion, |
| 105 | + "goos": GoOS, |
| 106 | + "goarch": GoArch, |
| 107 | + "tags": getTags(), |
| 108 | + }, |
| 109 | + }, |
| 110 | + func() float64 { return 1 }, |
| 111 | + ) |
| 112 | +} |
| 113 | + |
| 114 | +// versionInfoTmpl contains the template used by Info. |
| 115 | +var versionInfoTmpl = ` |
| 116 | +{{.program}}, version {{.version}} (branch: {{.branch}}, revision: {{.revision}}) |
| 117 | + build user: {{.buildUser}} |
| 118 | + build date: {{.buildDate}} |
| 119 | + go version: {{.goVersion}} |
| 120 | + platform: {{.platform}} |
| 121 | + tags: {{.tags}} |
| 122 | +` |
| 123 | + |
| 124 | +// Print returns version information. |
| 125 | +func Print(program string) string { |
| 126 | + m := map[string]string{ |
| 127 | + "program": program, |
| 128 | + "version": Version, |
| 129 | + "revision": getRevision(), |
| 130 | + "branch": Branch, |
| 131 | + "buildUser": BuildUser, |
| 132 | + "buildDate": BuildDate, |
| 133 | + "goVersion": GoVersion, |
| 134 | + "platform": GoOS + "/" + GoArch, |
| 135 | + "tags": getTags(), |
| 136 | + } |
| 137 | + t := template.Must(template.New("version").Parse(versionInfoTmpl)) |
| 138 | + |
| 139 | + var buf bytes.Buffer |
| 140 | + if err := t.ExecuteTemplate(&buf, "version", m); err != nil { |
| 141 | + panic(err) |
| 142 | + } |
| 143 | + return strings.TrimSpace(buf.String()) |
| 144 | +} |
| 145 | + |
| 146 | +// Info returns version, branch and revision information. |
| 147 | +func Info() string { |
| 148 | + return fmt.Sprintf("(version=%s, branch=%s, revision=%s)", Version, Branch, getRevision()) |
| 149 | +} |
| 150 | + |
| 151 | +// BuildContext returns goVersion, platform, buildUser and buildDate information. |
| 152 | +func BuildContext() string { |
| 153 | + return fmt.Sprintf("(go=%s, platform=%s, user=%s, date=%s, tags=%s)", GoVersion, GoOS+"/"+GoArch, BuildUser, BuildDate, getTags()) |
| 154 | +} |
0 commit comments