From 070e4b2e17750941e38ebb4037d67da3a05a6788 Mon Sep 17 00:00:00 2001 From: Ben Kochie Date: Fri, 4 Jan 2019 16:58:53 +0100 Subject: [PATCH] Update Makefile.common (#1220) * Update Makefile.common Update to new staticcheck method[0]. [0]: https://github.com/prometheus/prometheus/pull/5057 Signed-off-by: Ben Kochie * Fix staticcheck errors. Signed-off-by: Ben Kochie --- Makefile.common | 40 +++++++++++++++++++++------------------- collector/ntp.go | 2 +- collector/supervisord.go | 10 +++++----- collector/textfile.go | 2 +- collector/zfs_linux.go | 14 +++++++------- 5 files changed, 35 insertions(+), 33 deletions(-) diff --git a/Makefile.common b/Makefile.common index f26a7887..fff85f92 100644 --- a/Makefile.common +++ b/Makefile.common @@ -29,6 +29,8 @@ GO ?= go GOFMT ?= $(GO)fmt FIRST_GOPATH := $(firstword $(subst :, ,$(shell $(GO) env GOPATH))) GOOPTS ?= +GOHOSTOS ?= $(shell $(GO) env GOHOSTOS) +GOHOSTARCH ?= $(shell $(GO) env GOHOSTARCH) GO_VERSION ?= $(shell $(GO) version) GO_VERSION_NUMBER ?= $(word 3, $(GO_VERSION)) @@ -62,8 +64,6 @@ PROMU := $(FIRST_GOPATH)/bin/promu STATICCHECK := $(FIRST_GOPATH)/bin/staticcheck pkgs = ./... -GOHOSTOS ?= $(shell $(GO) env GOHOSTOS) -GOHOSTARCH ?= $(shell $(GO) env GOHOSTARCH) ifeq (arm, $(GOHOSTARCH)) GOHOSTARM ?= $(shell GOARM= $(GO) env GOARM) GO_BUILD_PLATFORM ?= $(GOHOSTOS)-$(GOHOSTARCH)v$(GOHOSTARM) @@ -73,12 +73,21 @@ endif PROMU_VERSION ?= 0.2.0 PROMU_URL := https://github.com/prometheus/promu/releases/download/v$(PROMU_VERSION)/promu-$(PROMU_VERSION).$(GO_BUILD_PLATFORM).tar.gz +STATICCHECK_VERSION ?= 2019.1 +STATICCHECK_URL := https://github.com/dominikh/go-tools/releases/download/$(STATICCHECK_VERSION)/staticcheck_$(GOHOSTOS)_$(GOHOSTARCH) PREFIX ?= $(shell pwd) BIN_DIR ?= $(shell pwd) DOCKER_IMAGE_TAG ?= $(subst /,-,$(shell git rev-parse --abbrev-ref HEAD)) DOCKER_REPO ?= prom +ifeq ($(GOHOSTARCH),amd64) + ifeq ($(GOHOSTOS),$(filter $(GOHOSTOS),linux freebsd darwin windows)) + # Only supported on amd64 + test-flags := -race + endif +endif + .PHONY: all all: precheck style staticcheck unused build test @@ -116,7 +125,7 @@ common-test-short: .PHONY: common-test common-test: @echo ">> running all tests" - GO111MODULE=$(GO111MODULE) $(GO) test -race $(GOOPTS) $(pkgs) + GO111MODULE=$(GO111MODULE) $(GO) test $(test-flags) $(GOOPTS) $(pkgs) .PHONY: common-format common-format: @@ -131,8 +140,12 @@ common-vet: .PHONY: common-staticcheck common-staticcheck: $(STATICCHECK) @echo ">> running staticcheck" + chmod +x $(STATICCHECK) ifdef GO111MODULE - GO111MODULE=$(GO111MODULE) $(STATICCHECK) -ignore "$(STATICCHECK_IGNORE)" -checks "SA*" $(pkgs) +# 'go list' needs to be executed before staticcheck to prepopulate the modules cache. +# Otherwise staticcheck might fail randomly for some reason not yet explained. + GO111MODULE=$(GO111MODULE) $(GO) list -e -compiled -test=true -export=false -deps=true -find=false -tags= -- ./... > /dev/null + GO111MODULE=$(GO111MODULE) $(STATICCHECK) -ignore "$(STATICCHECK_IGNORE)" $(pkgs) else $(STATICCHECK) -ignore "$(STATICCHECK_IGNORE)" $(pkgs) endif @@ -146,8 +159,9 @@ else ifdef GO111MODULE @echo ">> running check for unused/missing packages in go.mod" GO111MODULE=$(GO111MODULE) $(GO) mod tidy +ifeq (,$(wildcard vendor)) @git diff --exit-code -- go.sum go.mod -ifneq (,$(wildcard vendor)) +else @echo ">> running check for unused packages in vendor/" GO111MODULE=$(GO111MODULE) $(GO) mod vendor @git diff --exit-code -- go.sum go.mod vendor/ @@ -192,21 +206,9 @@ proto: @echo ">> generating code from proto files" @./scripts/genproto.sh -.PHONY: $(STATICCHECK) $(STATICCHECK): -ifdef GO111MODULE -# Get staticcheck from a temporary directory to avoid modifying the local go.{mod,sum}. -# See https://github.com/golang/go/issues/27643. -# For now, we are using the next branch of staticcheck because master isn't compatible yet with Go modules. - tmpModule=$$(mktemp -d 2>&1) && \ - mkdir -p $${tmpModule}/staticcheck && \ - cd "$${tmpModule}"/staticcheck && \ - GO111MODULE=on $(GO) mod init example.com/staticcheck && \ - GO111MODULE=on GOOS= GOARCH= $(GO) get -u honnef.co/go/tools/cmd/staticcheck@next && \ - rm -rf $${tmpModule}; -else - GOOS= GOARCH= GO111MODULE=off $(GO) get -u honnef.co/go/tools/cmd/staticcheck -endif + mkdir -p $(FIRST_GOPATH)/bin + curl -s -L $(STATICCHECK_URL) > $(STATICCHECK) ifdef GOVENDOR .PHONY: $(GOVENDOR) diff --git a/collector/ntp.go b/collector/ntp.go index e545a1d6..9b3c5a27 100644 --- a/collector/ntp.go +++ b/collector/ntp.go @@ -68,7 +68,7 @@ func NewNtpCollector() (Collector, error) { } if *ntpOffsetTolerance < 0 { - return nil, fmt.Errorf("Offset tolerance must be non-negative") + return nil, fmt.Errorf("offset tolerance must be non-negative") } return &ntpCollector{ diff --git a/collector/supervisord.go b/collector/supervisord.go index eb6720b8..0e325407 100644 --- a/collector/supervisord.go +++ b/collector/supervisord.go @@ -76,14 +76,14 @@ func NewSupervisordCollector() (Collector, error) { func (c *supervisordCollector) isRunning(state int) bool { // http://supervisord.org/subprocess.html#process-states const ( - STOPPED = 0 + // STOPPED = 0 STARTING = 10 RUNNING = 20 - BACKOFF = 30 + // BACKOFF = 30 STOPPING = 40 - EXITED = 100 - FATAL = 200 - UNKNOWN = 1000 + // EXITED = 100 + // FATAL = 200 + // UNKNOWN = 1000 ) switch state { case STARTING, RUNNING, STOPPING: diff --git a/collector/textfile.go b/collector/textfile.go index ddd88b85..c80de98d 100644 --- a/collector/textfile.go +++ b/collector/textfile.go @@ -95,7 +95,7 @@ func convertMetricFamily(metricFamily *dto.MetricFamily, ch chan<- prometheus.Me break } } - if present == false { + if !present { names = append(names, k) values = append(values, "") } diff --git a/collector/zfs_linux.go b/collector/zfs_linux.go index ae80650b..a964d502 100644 --- a/collector/zfs_linux.go +++ b/collector/zfs_linux.go @@ -29,14 +29,14 @@ import ( // constants from https://github.com/zfsonlinux/zfs/blob/master/lib/libspl/include/sys/kstat.h // kept as strings for comparison thus avoiding conversion to int const ( - kstatDataChar = "0" - kstatDataInt32 = "1" - kstatDataUint32 = "2" - kstatDataInt64 = "3" + // kstatDataChar = "0" + // kstatDataInt32 = "1" + // kstatDataUint32 = "2" + // kstatDataInt64 = "3" kstatDataUint64 = "4" - kstatDataLong = "5" - kstatDataUlong = "6" - kstatDataString = "7" + // kstatDataLong = "5" + // kstatDataUlong = "6" + // kstatDataString = "7" ) func (c *zfsCollector) openProcFile(path string) (*os.File, error) {