diff --git a/Makefile b/Makefile index ee895f8f8..12d8e68b3 100644 --- a/Makefile +++ b/Makefile @@ -17,16 +17,30 @@ TEST_ARTIFACTS = prometheus prometheus.build search_index include Makefile.INCLUDE +REV := $(shell git rev-parse --short HEAD) +BRANCH := $(shell git rev-parse --abbrev-ref HEAD) +HOSTNAME := $(shell hostname) +BUILD_DATE := $(shell date) +BUILDFLAGS := -ldflags \ + " -X main.buildVersion $(REV)\ + -X main.buildBranch $(BRANCH)\ + -X main.buildUser $(USER)@$(HOSTNAME)\ + -X main.buildDate '$(BUILD_DATE)'\ + -X main.goVersion '$(GO_VERSION)'\ + -X main.leveldbVersion '$(LEVELDB_VERSION)'\ + -X main.protobufVersion '$(PROTOCOL_BUFFERS_VERSION)'\ + -X main.snappyVersion '$(SNAPPY_VERSION)'" + all: test advice: go tool vet . binary: build - go build -o prometheus.build + go build $(BUILDFLAGS) -o prometheus.build build: preparation model web - go build . + go build $(BUILDFLAGS) . clean: $(MAKE) -C build clean diff --git a/build_info.go b/build_info.go new file mode 100644 index 000000000..0231a8387 --- /dev/null +++ b/build_info.go @@ -0,0 +1,38 @@ +package main + +import ( + "text/template" +) + +// Build information. Populated by Makefile. +var ( + buildVersion string + buildBranch string + buildUser string + buildDate string + goVersion string + leveldbVersion string + protobufVersion string + snappyVersion string +) + +var BuildInfo = map[string]string{ + "version": buildVersion, + "branch": buildBranch, + "user": buildUser, + "date": buildDate, + "go_version": goVersion, + "leveldb_version": leveldbVersion, + "protobuf_version": protobufVersion, + "snappy_version": snappyVersion, +} + +var versionInfoTmpl = template.Must(template.New("version").Parse( + `prometheus, version {{.version}} ({{.branch}}) + build user: {{.user}} + build date: {{.date}} + go version: {{.go_version}} + leveldb version: {{.leveldb_version}} + protobuf version: {{.protobuf_version}} + snappy version: {{.snappy_version}} +`)) diff --git a/main.go b/main.go index 85bbee144..a1cb15c0b 100644 --- a/main.go +++ b/main.go @@ -34,6 +34,7 @@ import ( var ( _ = fmt.Sprintf("") + printVersion = flag.Bool("version", false, "print version information") configFile = flag.String("configFile", "prometheus.conf", "Prometheus configuration file name.") metricsStoragePath = flag.String("metricsStoragePath", "/tmp/metrics", "Base path for metrics storage.") scrapeResultsQueueCapacity = flag.Int("scrapeResultsQueueCapacity", 4096, "The size of the scrape results queue.") @@ -45,6 +46,12 @@ var ( func main() { flag.Parse() + + if *printVersion { + versionInfoTmpl.Execute(os.Stdout, BuildInfo) + os.Exit(0) + } + conf, err := config.LoadFromFile(*configFile) if err != nil { log.Fatalf("Error loading configuration from %s: %v", *configFile, err)