diff --git a/Makefile b/Makefile index 3eb92c7..80fe620 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,20 @@ pkgs = $(shell go list ./...) PREFIX ?= $(shell pwd) BIN_DIR ?= $(shell pwd) DOCKER_IMAGE_NAME ?= ncabatoff/process-exporter -TAG_VERSION ?= $(shell git describe --tags --abbrev=0) + +BRANCH ?= $(shell git rev-parse --abbrev-ref HEAD) +BUILDDATE ?= $(shell date --iso-8601=seconds) +BUILDUSER ?= $(shell whoami)@$(shell hostname) +REVISION ?= $(shell git rev-parse HEAD) +TAG_VERSION ?= $(shell git describe --tags --abbrev=0) + +VERSION_LDFLAGS := \ + -X github.com/prometheus/common/version.Branch=$(BRANCH) \ + -X github.com/prometheus/common/version.BuildDate=$(BUILDDATE) \ + -X github.com/prometheus/common/version.BuildUser=$(BUILDUSER) \ + -X github.com/prometheus/common/version.Revision=$(REVISION) \ + -X main.version=$(TAG_VERSION) + SMOKE_TEST = -config.path packaging/conf/all.yaml -once-to-stdout-delay 1s |grep -q 'namedprocess_namegroup_memory_bytes{groupname="process-exporte",memtype="virtual"}' all: format vet test build smoke @@ -26,7 +39,7 @@ vet: build: @echo ">> building code" - cd cmd/process-exporter; CGO_ENABLED=0 go build -ldflags "-X main.version=$(TAG_VERSION)" -o ../../process-exporter -a -tags netgo + cd cmd/process-exporter; CGO_ENABLED=0 go build -ldflags "$(VERSION_LDFLAGS)" -o ../../process-exporter -a -tags netgo smoke: @echo ">> smoke testing process-exporter" diff --git a/cmd/process-exporter/main.go b/cmd/process-exporter/main.go index d4eb09f..b38c477 100644 --- a/cmd/process-exporter/main.go +++ b/cmd/process-exporter/main.go @@ -17,6 +17,7 @@ import ( "github.com/ncabatoff/process-exporter/proc" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promhttp" + promVersion "github.com/prometheus/common/version" ) // Version is set at build time use ldflags. @@ -276,6 +277,11 @@ func (nmr *nameMapperRegex) MatchAndName(nacl common.ProcAttributes) (bool, stri return false, "" } +func init() { + promVersion.Version = version + prometheus.MustRegister(promVersion.NewCollector("process_exporter")) +} + func main() { var ( listenAddress = flag.String("web.listen-address", ":9256", @@ -310,7 +316,7 @@ func main() { flag.Parse() if *showVersion { - fmt.Printf("process-exporter version %s\n", version) + fmt.Printf("%s\n", promVersion.Print("process-exporter")) os.Exit(0) } diff --git a/go.mod b/go.mod index 2493201..299182c 100644 --- a/go.mod +++ b/go.mod @@ -8,6 +8,7 @@ require ( github.com/ncabatoff/go-seq v0.0.0-20180805175032-b08ef85ed833 github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect github.com/prometheus/client_golang v1.8.0 + github.com/prometheus/common v0.14.0 github.com/prometheus/procfs v0.2.0 gopkg.in/check.v1 v1.0.0-20200902074654-038fdea0a05b gopkg.in/yaml.v2 v2.3.0