Merge pull request #902 from jimmidyson/git-version

Expose git revision as well as version in version info, add Makefile
This commit is contained in:
Vish Kannan 2015-10-08 11:42:12 -07:00
commit 64702e7202
10 changed files with 111 additions and 9 deletions

38
Makefile Normal file
View File

@ -0,0 +1,38 @@
# Copyright 2015 Google Inc. All rights reserved.
#
# 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
GO := godep go
pkgs = $(shell $(GO) list ./...)
all: format build test
test:
@echo ">> running tests"
@$(GO) test -short $(pkgs)
format:
@echo ">> formatting code"
@$(GO) fmt $(pkgs)
vet:
@echo ">> vetting code"
@$(GO) vet $(pkgs)
build:
@echo ">> building binaries"
@./build/build.sh
docker:
@docker build -t cadvisor:$(shell git rev-parse --short HEAD) -f deploy/Dockerfile .
.PHONY: all format build test vet docker

42
build/build.sh Executable file
View File

@ -0,0 +1,42 @@
#!/usr/bin/env bash
# Copyright 2015 Google Inc. All rights reserved.
#
# 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
set -e
repo_path="github.com/google/cadvisor"
version=$( cat version/VERSION )
revision=$( git rev-parse --short HEAD 2> /dev/null || echo 'unknown' )
branch=$( git rev-parse --abbrev-ref HEAD 2> /dev/null || echo 'unknown' )
host=$( hostname -f )
build_date=$( date +%Y%m%d-%H:%M:%S )
go_version=$( go version | sed -e 's/^[^0-9.]*\([0-9.]*\).*/\1/' )
if [ "$(go env GOOS)" = "windows" ]; then
ext=".exe"
fi
ldflags="
-X ${repo_path}/version.Version=${version}
-X ${repo_path}/version.Revision=${revision}
-X ${repo_path}/version.Branch=${branch}
-X ${repo_path}/version.BuildUser=${USER}@${host}
-X ${repo_path}/version.BuildDate=${build_date}
-X ${repo_path}/version.GoVersion=${go_version}"
echo " > cadvisor"
godep go build -ldflags "${ldflags}" -o cadvisor${ext} ${repo_path}
exit 0

View File

@ -54,7 +54,7 @@ func main() {
flag.Parse() flag.Parse()
if *versionFlag { if *versionFlag {
fmt.Printf("cAdvisor version %s\n", version.VERSION) fmt.Printf("cAdvisor version %s (%s)\n", version.Info["version"], version.Info["revision"])
os.Exit(0) os.Exit(0)
} }
@ -91,7 +91,7 @@ func main() {
// Install signal handler. // Install signal handler.
installSignalHandler(containerManager) installSignalHandler(containerManager)
glog.Infof("Starting cAdvisor version: %q on port %d", version.VERSION, *argPort) glog.Infof("Starting cAdvisor version: %s-%s on port %d", version.Info["version"], version.Info["revision"], *argPort)
addr := fmt.Sprintf("%s:%d", *argIp, *argPort) addr := fmt.Sprintf("%s:%d", *argIp, *argPort)
glog.Fatal(http.ListenAndServe(addr, nil)) glog.Fatal(http.ListenAndServe(addr, nil))

View File

@ -179,6 +179,8 @@ type VersionInfo struct {
// cAdvisor version. // cAdvisor version.
CadvisorVersion string `json:"cadvisor_version"` CadvisorVersion string `json:"cadvisor_version"`
// cAdvisor git revision.
CadvisorRevision string `json:"cadvisor_revision"`
} }
type MachineInfoFactory interface { type MachineInfoFactory interface {

View File

@ -122,7 +122,8 @@ func getVersionInfo() (*info.VersionInfo, error) {
KernelVersion: kernel_version, KernelVersion: kernel_version,
ContainerOsVersion: container_os, ContainerOsVersion: container_os,
DockerVersion: docker_version, DockerVersion: docker_version,
CadvisorVersion: version.VERSION, CadvisorVersion: version.Info["version"],
CadvisorRevision: version.Info["revision"],
}, nil }, nil
} }

View File

@ -453,7 +453,7 @@ func NewPrometheusCollector(infoProvider infoProvider) *PrometheusCollector {
} }
var ( var (
versionInfoDesc = prometheus.NewDesc("cadvisor_version_info", "A metric with a constant '1' value labeled by kernel version, OS version, docker version & cadvisor version.", []string{"kernelVersion", "osVersion", "dockerVersion", "cadvisorVersion"}, nil) versionInfoDesc = prometheus.NewDesc("cadvisor_version_info", "A metric with a constant '1' value labeled by kernel version, OS version, docker version, cadvisor version & cadvisor revision.", []string{"kernelVersion", "osVersion", "dockerVersion", "cadvisorVersion", "cadvisorRevision"}, nil)
machineInfoCoresDesc = prometheus.NewDesc("machine_cpu_cores", "Number of CPU cores on the machine.", nil, nil) machineInfoCoresDesc = prometheus.NewDesc("machine_cpu_cores", "Number of CPU cores on the machine.", nil, nil)
machineInfoMemoryDesc = prometheus.NewDesc("machine_memory_bytes", "Amount of memory installed on the machine.", nil, nil) machineInfoMemoryDesc = prometheus.NewDesc("machine_memory_bytes", "Amount of memory installed on the machine.", nil, nil)
) )
@ -534,7 +534,7 @@ func (c *PrometheusCollector) collectVersionInfo(ch chan<- prometheus.Metric) {
glog.Warningf("Couldn't get version info: %s", err) glog.Warningf("Couldn't get version info: %s", err)
return return
} }
ch <- prometheus.MustNewConstMetric(versionInfoDesc, prometheus.GaugeValue, 1, []string{versionInfo.KernelVersion, versionInfo.ContainerOsVersion, versionInfo.DockerVersion, versionInfo.CadvisorVersion}...) ch <- prometheus.MustNewConstMetric(versionInfoDesc, prometheus.GaugeValue, 1, []string{versionInfo.KernelVersion, versionInfo.ContainerOsVersion, versionInfo.DockerVersion, versionInfo.CadvisorVersion, versionInfo.CadvisorRevision}...)
} }
func (c *PrometheusCollector) collectMachineInfo(ch chan<- prometheus.Metric) { func (c *PrometheusCollector) collectMachineInfo(ch chan<- prometheus.Metric) {

View File

@ -35,6 +35,7 @@ func (p testSubcontainersInfoProvider) GetVersionInfo() (*info.VersionInfo, erro
ContainerOsVersion: "Fedora 22 (Twenty Two)", ContainerOsVersion: "Fedora 22 (Twenty Two)",
DockerVersion: "1.8.1", DockerVersion: "1.8.1",
CadvisorVersion: "0.16.0", CadvisorVersion: "0.16.0",
CadvisorRevision: "abcdef",
}, nil }, nil
} }
@ -170,7 +171,7 @@ func TestPrometheusCollector(t *testing.T) {
// (https://github.com/prometheus/client_golang/issues/58), we simply compare // (https://github.com/prometheus/client_golang/issues/58), we simply compare
// verbatim text-format metrics outputs, but ignore certain metric lines // verbatim text-format metrics outputs, but ignore certain metric lines
// whose value depends on the current time or local circumstances. // whose value depends on the current time or local circumstances.
includeRe := regexp.MustCompile("^(# HELP |# TYPE |)container_") includeRe := regexp.MustCompile("^(?:(?:# HELP |# TYPE)container_|cadvisor_version_info{)")
ignoreRe := regexp.MustCompile("^container_last_seen{") ignoreRe := regexp.MustCompile("^container_last_seen{")
for i, want := range wantLines { for i, want := range wantLines {
if !includeRe.MatchString(want) || ignoreRe.MatchString(want) { if !includeRe.MatchString(want) || ignoreRe.MatchString(want) {

View File

@ -1,6 +1,6 @@
# HELP cadvisor_version_info A metric with a constant '1' value labeled by kernel version, OS version, docker version & cadvisor version. # HELP cadvisor_version_info A metric with a constant '1' value labeled by kernel version, OS version, docker version & cadvisor version.
# TYPE cadvisor_version_info gauge # TYPE cadvisor_version_info gauge
cadvisor_version_info{cadvisorVersion="0.16.0",dockerVersion="1.8.1",kernelVersion="4.1.6-200.fc22.x86_64",osVersion="Fedora 22 (Twenty Two)"} 1 cadvisor_version_info{cadvisorRevision="abcdef",cadvisorVersion="0.16.0",dockerVersion="1.8.1",kernelVersion="4.1.6-200.fc22.x86_64",osVersion="Fedora 22 (Twenty Two)"} 1
# HELP container_cpu_system_seconds_total Cumulative system cpu time consumed in seconds. # HELP container_cpu_system_seconds_total Cumulative system cpu time consumed in seconds.
# TYPE container_cpu_system_seconds_total counter # TYPE container_cpu_system_seconds_total counter
container_cpu_system_seconds_total{id="testcontainer",image="test",name="testcontaineralias"} 7e-09 container_cpu_system_seconds_total{id="testcontainer",image="test",name="testcontaineralias"} 7e-09

1
version/VERSION Normal file
View File

@ -0,0 +1 @@
0.18.0

View File

@ -14,5 +14,22 @@
package version package version
// Version of cAdvisor. // Build information. Populated at build-time.
const VERSION = "0.18.0" var (
Version string
Revision string
Branch string
BuildUser string
BuildDate string
GoVersion string
)
// Info provides the iterable version information.
var Info = map[string]string{
"version": Version,
"revision": Revision,
"branch": Branch,
"buildUser": BuildUser,
"buildDate": BuildDate,
"goVersion": GoVersion,
}