Makefile: add checkmetrics target, use in CI (#797)

This commit is contained in:
Matt Layher 2018-02-13 12:04:03 -05:00 committed by Ben Kochie
parent 3de2542d21
commit dcb31670d6
3 changed files with 36 additions and 4 deletions

View file

@ -16,6 +16,7 @@ GOPATH := $(firstword $(subst :, ,$(shell $(GO) env GOPATH)))
GOARCH := $(shell $(GO) env GOARCH)
GOHOSTARCH := $(shell $(GO) env GOHOSTARCH)
PROMTOOL ?= $(GOPATH)/bin/promtool
PROMU ?= $(GOPATH)/bin/promu
STATICCHECK ?= $(GOPATH)/bin/staticcheck
pkgs = $(shell $(GO) list ./... | grep -v /vendor/)
@ -48,6 +49,12 @@ else
test-e2e := skip-test-e2e
endif
ifeq ($(MACH), ppc64le)
e2e-out = collector/fixtures/e2e-ppc64le-output.txt
else
e2e-out = collector/fixtures/e2e-output.txt
endif
# 64bit -> 32bit mapping for cross-checking. At least for amd64/386, the 64bit CPU can execute 32bit code but not the other way around, so we don't support cross-testing upwards.
cross-test = skip-test-32bit
define goarch_pair
@ -65,7 +72,7 @@ $(eval $(call goarch_pair,arm64,arm))
$(eval $(call goarch_pair,mips64,mips))
$(eval $(call goarch_pair,mips64el,mipsel))
all: format vet staticcheck build test $(cross-test) $(test-e2e)
all: format vet staticcheck checkmetrics build test $(cross-test) $(test-e2e)
style:
@echo ">> checking code style"
@ -93,6 +100,10 @@ test-e2e: build collector/fixtures/sys/.unpacked
skip-test-e2e:
@echo ">> SKIP running end-to-end tests on $(OS_detected)"
checkmetrics: $(PROMTOOL)
@echo ">> checking metrics for correctness"
./checkmetrics.sh $(PROMTOOL) $(e2e-out)
format:
@echo ">> formatting code"
@$(GO) fmt $(pkgs)
@ -124,6 +135,9 @@ test-docker:
@echo ">> testing docker image"
./test_image.sh "$(DOCKER_IMAGE_NAME):$(DOCKER_IMAGE_TAG)" 9100
$(GOPATH)/bin/promtool promtool:
@GOOS= GOARCH= $(GO) get -u github.com/prometheus/prometheus/cmd/promtool
$(GOPATH)/bin/promu promu:
@GOOS= GOARCH= $(GO) get -u github.com/prometheus/promu
@ -131,10 +145,10 @@ $(GOPATH)/bin/staticcheck:
@GOOS= GOARCH= $(GO) get -u honnef.co/go/tools/cmd/staticcheck
.PHONY: all style format build test test-e2e vet tarball docker promu staticcheck
.PHONY: all style format build test test-e2e vet tarball docker promtool promu staticcheck checkmetrics
# Declaring the binaries at their default locations as PHONY targets is a hack
# to ensure the latest version is downloaded on every make execution.
# If this is not desired, copy/symlink these binaries to a different path and
# set the respective environment variables.
.PHONY: $(GOPATH)/bin/promu $(GOPATH)/bin/staticcheck
.PHONY: $(GOPATH)/bin/promtool $(GOPATH)/bin/promu $(GOPATH)/bin/staticcheck

18
checkmetrics.sh Executable file
View file

@ -0,0 +1,18 @@
#!/usr/bin/env bash
if [[ ( -z "$1" ) || ( -z "$2" ) ]]; then
echo "usage: ./checkmetrics.sh /usr/bin/promtool e2e-output.txt"
exit 1
fi
# Only check node_exporter's metrics, as the Prometheus Go client currently
# exposes a metric with a unit of microseconds. Once that is fixed, remove
# this filter.
lint=$($1 check metrics < $2 2>&1 | grep "node_")
if [[ ! -z $lint ]]; then
echo -e "Some Prometheus metrics do not follow best practices:\n"
echo "$lint"
exit 1
fi

View file

@ -148,4 +148,4 @@ get "127.0.0.1:${port}/metrics" | grep -E -v "${skip_re}" > "${tmpdir}/e2e-outpu
diff -u \
"${fixture}" \
"${tmpdir}/e2e-output.txt"
"${tmpdir}/e2e-output.txt"