Merge pull request #1614 from sdurrheimer/common-version

Make version informations consistent between prometheus components
This commit is contained in:
Fabian Reinartz 2016-05-06 10:58:24 +02:00
commit baa3e19fa0
11 changed files with 198 additions and 188 deletions

View file

@ -9,11 +9,11 @@ build:
path: ./cmd/promtool
flags: -a -tags netgo
ldflags: |
-X {{repoPath}}/version.Version={{.Version}}
-X {{repoPath}}/version.Revision={{.Revision}}
-X {{repoPath}}/version.Branch={{.Branch}}
-X {{repoPath}}/version.BuildUser={{user}}@{{host}}
-X {{repoPath}}/version.BuildDate={{date "20060102-15:04:05"}}
-X {{repoPath}}/vendor/github.com/prometheus/common/version.Version={{.Version}}
-X {{repoPath}}/vendor/github.com/prometheus/common/version.Revision={{.Revision}}
-X {{repoPath}}/vendor/github.com/prometheus/common/version.Branch={{.Branch}}
-X {{repoPath}}/vendor/github.com/prometheus/common/version.BuildUser={{user}}@{{host}}
-X {{repoPath}}/vendor/github.com/prometheus/common/version.BuildDate={{date "20060102-15:04:05"}}
tarball:
files:
- consoles

View file

@ -15,21 +15,17 @@
package main
import (
"bytes"
"flag"
"fmt"
_ "net/http/pprof" // Comment this line to disable pprof endpoint.
"os"
"os/signal"
"strings"
"syscall"
"text/template"
"time"
"github.com/prometheus/common/log"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/log"
"github.com/prometheus/common/version"
"github.com/prometheus/prometheus/config"
"github.com/prometheus/prometheus/notifier"
"github.com/prometheus/prometheus/promql"
@ -38,7 +34,6 @@ import (
"github.com/prometheus/prometheus/storage"
"github.com/prometheus/prometheus/storage/local"
"github.com/prometheus/prometheus/storage/remote"
"github.com/prometheus/prometheus/version"
"github.com/prometheus/prometheus/web"
)
@ -59,6 +54,10 @@ var (
})
)
func init() {
prometheus.MustRegister(version.NewCollector("prometheus"))
}
// Main manages the startup and shutdown lifecycle of the entire Prometheus server.
func Main() int {
if err := parse(os.Args[1:]); err != nil {
@ -66,11 +65,14 @@ func Main() int {
return 2
}
printVersion()
if cfg.printVersion {
fmt.Fprintln(os.Stdout, version.Print("prometheus"))
return 0
}
log.Infoln("Starting prometheus", version.Info())
log.Infoln("Build context", version.BuildContext())
var reloadables []Reloadable
var (
@ -108,7 +110,16 @@ func Main() int {
Birth: time.Now(),
}
webHandler := web.New(memStorage, queryEngine, ruleManager, status, &cfg.web)
version := &web.PrometheusVersion{
Version: version.Version,
Revision: version.Revision,
Branch: version.Branch,
BuildUser: version.BuildUser,
BuildDate: version.BuildDate,
GoVersion: version.GoVersion,
}
webHandler := web.New(memStorage, queryEngine, ruleManager, status, version, &cfg.web)
reloadables = append(reloadables, status, targetManager, ruleManager, webHandler, notifier)
@ -221,20 +232,3 @@ func reloadConfig(filename string, rls ...Reloadable) (success bool) {
}
return success
}
var versionInfoTmpl = `
prometheus, version {{.version}} (branch: {{.branch}}, revision: {{.revision}})
build user: {{.buildUser}}
build date: {{.buildDate}}
go version: {{.goVersion}}
`
func printVersion() {
t := template.Must(template.New("version").Parse(versionInfoTmpl))
var buf bytes.Buffer
if err := t.ExecuteTemplate(&buf, "version", version.Map); err != nil {
panic(err)
}
fmt.Fprintln(os.Stdout, strings.TrimSpace(buf.String()))
}

View file

@ -14,18 +14,16 @@
package main
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"text/template"
"github.com/prometheus/common/version"
"github.com/prometheus/prometheus/config"
"github.com/prometheus/prometheus/promql"
"github.com/prometheus/prometheus/util/cli"
"github.com/prometheus/prometheus/version"
)
// CheckConfigCmd validates configuration files.
@ -184,22 +182,9 @@ func checkRules(t cli.Term, filename string) (int, error) {
return len(rules), nil
}
var versionInfoTmpl = `
prometheus, version {{.version}} (branch: {{.branch}}, revision: {{.revision}})
build user: {{.buildUser}}
build date: {{.buildDate}}
go version: {{.goVersion}}
`
// VersionCmd prints the binaries version information.
func VersionCmd(t cli.Term, _ ...string) int {
tmpl := template.Must(template.New("version").Parse(versionInfoTmpl))
var buf bytes.Buffer
if err := tmpl.ExecuteTemplate(&buf, "version", version.Map); err != nil {
panic(err)
}
fmt.Fprintln(t.Out(), strings.TrimSpace(buf.String()))
fmt.Fprintln(os.Stdout, version.Print("promtool"))
return 0
}

View file

@ -14,16 +14,12 @@
package main
import (
"bytes"
"fmt"
"os"
"strings"
"text/template"
"github.com/prometheus/prometheus/util/cli"
"github.com/prometheus/prometheus/version"
"github.com/prometheus/common/version"
"github.com/prometheus/prometheus/storage/local"
"github.com/prometheus/prometheus/util/cli"
)
// DumpHeadsCmd dumps metadata of a heads.db file.
@ -39,22 +35,9 @@ func DumpHeadsCmd(t cli.Term, args ...string) int {
return 0
}
var versionInfoTmpl = `
prometheus, version {{.version}} (branch: {{.branch}}, revision: {{.revision}})
build user: {{.buildUser}}
build date: {{.buildDate}}
go version: {{.goVersion}}
`
// VersionCmd prints the binaries version information.
func VersionCmd(t cli.Term, _ ...string) int {
tmpl := template.Must(template.New("version").Parse(versionInfoTmpl))
var buf bytes.Buffer
if err := tmpl.ExecuteTemplate(&buf, "version", version.Map); err != nil {
panic(err)
}
fmt.Fprintln(t.Out(), strings.TrimSpace(buf.String()))
fmt.Fprintln(os.Stdout, version.Print("storagetool"))
return 0
}

89
vendor/github.com/prometheus/common/version/info.go generated vendored Normal file
View file

@ -0,0 +1,89 @@
// Copyright 2016 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.
package version
import (
"bytes"
"fmt"
"runtime"
"strings"
"text/template"
"github.com/prometheus/client_golang/prometheus"
)
// Build information. Populated at build-time.
var (
Version string
Revision string
Branch string
BuildUser string
BuildDate string
GoVersion = runtime.Version()
)
// NewCollector returns a collector which exports metrics about current version information.
func NewCollector(program string) *prometheus.GaugeVec {
buildInfo := prometheus.NewGaugeVec(
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,
),
},
[]string{"version", "revision", "branch", "goversion"},
)
buildInfo.WithLabelValues(Version, Revision, Branch, GoVersion).Set(1)
return buildInfo
}
// versionInfoTmpl contains the template used by Info.
var versionInfoTmpl = `
{{.program}}, version {{.version}} (branch: {{.branch}}, revision: {{.revision}})
build user: {{.buildUser}}
build date: {{.buildDate}}
go version: {{.goVersion}}
`
// Print returns version information.
func Print(program string) string {
m := map[string]string{
"program": program,
"version": Version,
"revision": Revision,
"branch": Branch,
"buildUser": BuildUser,
"buildDate": BuildDate,
"goVersion": GoVersion,
}
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)
}
// BuildContext returns goVersion, buildUser and buildDate information.
func BuildContext() string {
return fmt.Sprintf("(go=%s, user=%s, date=%s)", GoVersion, BuildUser, BuildDate)
}

6
vendor/vendor.json vendored
View file

@ -185,6 +185,12 @@
"revision": "14ca1097bbe21584194c15e391a9dab95ad42a59",
"revisionTime": "2016-01-25T23:57:51+01:00"
},
{
"checksumSHA1": "91KYK0SpvkaMJJA2+BcxbVnyRO0=",
"path": "github.com/prometheus/common/version",
"revision": "dd586c1c5abb0be59e60f942c22af711a2008cb4",
"revisionTime": "2016-05-03T22:05:32Z"
},
{
"path": "github.com/prometheus/procfs",
"revision": "c91d8eefde16bd047416409eb56353ea84a186e4",

View file

@ -1,53 +0,0 @@
// Copyright 2015 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.
package version
import (
"runtime"
"github.com/prometheus/client_golang/prometheus"
)
// Build information. Populated at build-time.
var (
Version string
Revision string
Branch string
BuildUser string
BuildDate string
GoVersion = runtime.Version()
)
// Map provides the iterable version information.
var Map = map[string]string{
"version": Version,
"revision": Revision,
"branch": Branch,
"buildUser": BuildUser,
"buildDate": BuildDate,
"goVersion": GoVersion,
}
func init() {
buildInfo := prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "prometheus_build_info",
Help: "A metric with a constant '1' value labeled by version, revision, branch, and goversion from which Prometheus was built.",
},
[]string{"version", "revision", "branch", "goversion"},
)
buildInfo.WithLabelValues(Version, Revision, Branch, GoVersion).Set(1)
prometheus.MustRegister(buildInfo)
}

File diff suppressed because one or more lines are too long

View file

@ -15,12 +15,30 @@
<h2 id="buildinformation">Build Information</h2>
<table class="table table-condensed table-bordered table-striped table-hover">
<tbody>
{{range $key, $value := .Info}}
<tr>
<th scope="row">{{$key}}</th>
<td>{{$value}}</td>
<th scope="row">Version</th>
<td>{{.Version.Version}}</td>
</tr>
<tr>
<th scope="row">Revision</th>
<td>{{.Version.Revision}}</td>
</tr>
<tr>
<th scope="row">Branch</th>
<td>{{.Version.Branch}}</td>
</tr>
<tr>
<th scope="row">BuildUser</th>
<td>{{.Version.BuildUser}}</td>
</tr>
<tr>
<th scope="row">BuildDate</th>
<td>{{.Version.BuildDate}}</td>
</tr>
<tr>
<th scope="row">GoVersion</th>
<td>{{.Version.GoVersion}}</td>
</tr>
{{end}}
</tbody>
</table>

View file

@ -44,7 +44,6 @@ import (
"github.com/prometheus/prometheus/storage/local"
"github.com/prometheus/prometheus/template"
"github.com/prometheus/prometheus/util/httputil"
"github.com/prometheus/prometheus/version"
"github.com/prometheus/prometheus/web/api/legacy"
"github.com/prometheus/prometheus/web/api/v1"
"github.com/prometheus/prometheus/web/ui"
@ -67,6 +66,7 @@ type Handler struct {
reloadCh chan struct{}
options *Options
statusInfo *PrometheusStatus
versionInfo *PrometheusVersion
externalLabels model.LabelSet
mtx sync.RWMutex
@ -110,6 +110,16 @@ func (s *PrometheusStatus) ApplyConfig(conf *config.Config) bool {
return true
}
// PrometheusVersion contains various build information about Prometheus
type PrometheusVersion struct {
Version string `json:"version"`
Revision string `json:"revision"`
Branch string `json:"branch"`
BuildUser string `json:"buildUser"`
BuildDate string `json:"buildDate"`
GoVersion string `json:"goVersion"`
}
// Options for the web Handler.
type Options struct {
ListenAddress string
@ -123,7 +133,7 @@ type Options struct {
}
// New initializes a new web Handler.
func New(st local.Storage, qe *promql.Engine, rm *rules.Manager, status *PrometheusStatus, o *Options) *Handler {
func New(st local.Storage, qe *promql.Engine, rm *rules.Manager, status *PrometheusStatus, version *PrometheusVersion, o *Options) *Handler {
router := route.New()
h := &Handler{
@ -133,6 +143,7 @@ func New(st local.Storage, qe *promql.Engine, rm *rules.Manager, status *Prometh
reloadCh: make(chan struct{}),
options: o,
statusInfo: status,
versionInfo: version,
ruleManager: rm,
queryEngine: qe,
@ -315,17 +326,17 @@ func (h *Handler) status(w http.ResponseWriter, r *http.Request) {
defer h.statusInfo.mu.RUnlock()
h.executeTemplate(w, "status.html", struct {
Status *PrometheusStatus
Info map[string]string
Status *PrometheusStatus
Version *PrometheusVersion
}{
Status: h.statusInfo,
Info: version.Map,
Status: h.statusInfo,
Version: h.versionInfo,
})
}
func (h *Handler) version(w http.ResponseWriter, r *http.Request) {
dec := json.NewEncoder(w)
if err := dec.Encode(version.Map); err != nil {
if err := dec.Encode(h.versionInfo); err != nil {
http.Error(w, fmt.Sprintf("error encoding JSON: %s", err), http.StatusInternalServerError)
}
}