Repository: prometheus/blackbox_exporter
Pull request #404 (2019-01-07 09:42:51)

*: update Makefile.common with new staticcheck

brian-brazil: :+1: 

Patch:
diff --git a/Makefile.common b/Makefile.common
index 741579e..fff85f9 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,17 +64,30 @@ PROMU        := $(FIRST_GOPATH)/bin/promu
 STATICCHECK  := $(FIRST_GOPATH)/bin/staticcheck
 pkgs          = ./...
 
-GO_VERSION        ?= $(shell $(GO) version)
-GO_BUILD_PLATFORM ?= $(subst /,-,$(lastword $(GO_VERSION)))
+ifeq (arm, $(GOHOSTARCH))
+	GOHOSTARM ?= $(shell GOARM= $(GO) env GOARM)
+	GO_BUILD_PLATFORM ?= $(GOHOSTOS)-$(GOHOSTARCH)v$(GOHOSTARM)
+else
+	GO_BUILD_PLATFORM ?= $(GOHOSTOS)-$(GOHOSTARCH)
+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
 
@@ -110,12 +125,12 @@ 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:
 	@echo ">> formatting code"
-	GO111MODULE=$(GO111MODULE) $(GO) fmt $(GOOPTS) $(pkgs)
+	GO111MODULE=$(GO111MODULE) $(GO) fmt $(pkgs)
 
 .PHONY: common-vet
 common-vet:
@@ -125,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
@@ -140,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/
@@ -175,30 +195,20 @@ common-docker-tag-latest:
 promu: $(PROMU)
 
 $(PROMU):
-	curl -s -L $(PROMU_URL) | tar -xvz -C /tmp
-	mkdir -v -p $(FIRST_GOPATH)/bin
-	cp -v /tmp/promu-$(PROMU_VERSION).$(GO_BUILD_PLATFORM)/promu $(PROMU)
+	$(eval PROMU_TMP := $(shell mktemp -d))
+	curl -s -L $(PROMU_URL) | tar -xvzf - -C $(PROMU_TMP)
+	mkdir -p $(FIRST_GOPATH)/bin
+	cp $(PROMU_TMP)/promu-$(PROMU_VERSION).$(GO_BUILD_PLATFORM)/promu $(FIRST_GOPATH)/bin/promu
+	rm -r $(PROMU_TMP)
 
 .PHONY: proto
 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/config/config.go b/config/config.go
index 2bd5d94..ea92778 100644
--- a/config/config.go
+++ b/config/config.go
@@ -27,11 +27,11 @@ func (sc *SafeConfig) ReloadConfig(confFile string) (err error) {
 
 	yamlFile, err := ioutil.ReadFile(confFile)
 	if err != nil {
-		return fmt.Errorf("Error reading config file: %s", err)
+		return fmt.Errorf("error reading config file: %s", err)
 	}
 
 	if err := yaml.UnmarshalStrict(yamlFile, c); err != nil {
-		return fmt.Errorf("Error parsing config file: %s", err)
+		return fmt.Errorf("error parsing config file: %s", err)
 	}
 
 	sc.Lock()
@@ -145,7 +145,7 @@ func (s *DNSProbe) UnmarshalYAML(unmarshal func(interface{}) error) error {
 		return err
 	}
 	if s.QueryName == "" {
-		return errors.New("Query name must be set for DNS module")
+		return errors.New("query name must be set for DNS module")
 	}
 	return nil
 }
diff --git a/main.go b/main.go
index 58779fc..9103682 100644
--- a/main.go
+++ b/main.go
@@ -146,8 +146,6 @@ func probeHandler(w http.ResponseWriter, r *http.Request, c *config.Config, logg
 
 type scrapeLogger struct {
 	next         log.Logger
-	module       string
-	target       string
 	buffer       bytes.Buffer
 	bufferLogger log.Logger
 }
diff --git a/prober/dns.go b/prober/dns.go
index c5078f6..471331c 100644
--- a/prober/dns.go
+++ b/prober/dns.go
@@ -170,7 +170,7 @@ func ProbeDNS(ctx context.Context, target string, module config.Module, registry
 
 	level.Info(logger).Log("msg", "Making DNS query", "target", target, "dial_protocol", dialProtocol, "query", module.DNS.QueryName, "type", qt)
 	timeoutDeadline, _ := ctx.Deadline()
-	client.Timeout = timeoutDeadline.Sub(time.Now())
+	client.Timeout = time.Until(timeoutDeadline)
 	response, _, err := client.Exchange(msg, target)
 	if err != nil {
 		level.Error(logger).Log("msg", "Error while sending a DNS query", "err", err)
diff --git a/prober/http.go b/prober/http.go
index 542f312..ed58b96 100644
--- a/prober/http.go
+++ b/prober/http.go
@@ -238,7 +238,7 @@ func ProbeHTTP(ctx context.Context, target string, module config.Module, registr
 		redirects = len(via)
 		if redirects > 10 || httpConfig.NoFollowRedirects {
 			level.Info(logger).Log("msg", "Not following redirect")
-			return errors.New("Don't follow redirects")
+			return errors.New("don't follow redirects")
 		}
 		return nil
 	}
diff --git a/prober/icmp.go b/prober/icmp.go
index 2e01265..330ae9d 100644
--- a/prober/icmp.go
+++ b/prober/icmp.go
@@ -61,9 +61,6 @@ func ProbeICMP(ctx context.Context, target string, module config.Module, registr
 
 	registry.MustRegister(durationGaugeVec)
 
-	timeoutDeadline, _ := ctx.Deadline()
-	deadline := time.Now().Add(timeoutDeadline.Sub(time.Now()))
-
 	ip, lookupTime, err := chooseProtocol(module.ICMP.IPProtocol, module.ICMP.IPProtocolFallback, target, registry, logger)
 	if err != nil {
 		level.Warn(logger).Log("msg", "Error resolving address", "err", err)
@@ -165,6 +162,7 @@ func ProbeICMP(ctx context.Context, target string, module config.Module, registr
 	}
 
 	rb := make([]byte, 65536)
+	deadline, _ := ctx.Deadline()
 	if err := socket.SetReadDeadline(deadline); err != nil {
 		level.Error(logger).Log("msg", "Error setting socket deadline", "err", err)
 		return
@@ -188,7 +186,7 @@ func ProbeICMP(ctx context.Context, target string, module config.Module, registr
 			rb[2] = 0
 			rb[3] = 0
 		}
-		if bytes.Compare(rb[:n], wb) == 0 {
+		if bytes.Equal(rb[:n], wb) {
 			durationGaugeVec.WithLabelValues("rtt").Add(time.Since(rttStart).Seconds())
 			level.Info(logger).Log("msg", "Found matching reply packet")
 			return true
diff --git a/prober/tcp.go b/prober/tcp.go
index 5bc13db..f0ad879 100644
--- a/prober/tcp.go
+++ b/prober/tcp.go
@@ -54,7 +54,7 @@ func dialTCP(ctx context.Context, target string, module config.Module, registry
 		srcIP := net.ParseIP(module.TCP.SourceIPAddress)
 		if srcIP == nil {
 			level.Error(logger).Log("msg", "Error parsing source ip address", "srcIP", module.TCP.SourceIPAddress)
-			return nil, fmt.Errorf("Error parsing source ip address: %s", module.TCP.SourceIPAddress)
+			return nil, fmt.Errorf("error parsing source ip address: %s", module.TCP.SourceIPAddress)
 		}
 		level.Info(logger).Log("msg", "Using local address", "srcIP", srcIP)
 		dialer.LocalAddr = &net.TCPAddr{IP: srcIP}
diff --git a/prober/utils.go b/prober/utils.go
index 323868b..12e3ad3 100644
--- a/prober/utils.go
+++ b/prober/utils.go
@@ -49,7 +49,7 @@ func chooseProtocol(IPProtocol string, fallbackIPProtocol bool, target string, r
 
 	ip, err = net.ResolveIPAddr(IPProtocol, target)
 	if err != nil {
-		if fallbackIPProtocol == false {
+		if !fallbackIPProtocol {
 			level.Error(logger).Log("msg", "Resolution with IP protocol failed (fallback_ip_protocol is false): err", err)
 		} else {
 			level.Warn(logger).Log("msg", "Resolution with IP protocol failed, attempting fallback protocol", "fallback_protocol", fallbackProtocol, "err", err)


Test patch:
diff --git a/config/config_test.go b/config/config_test.go
index a586be7..94b0248 100644
--- a/config/config_test.go
+++ b/config/config_test.go
@@ -28,15 +28,15 @@ func TestLoadBadConfigs(t *testing.T) {
 	}{
 		{
 			ConfigFile:    "testdata/blackbox-bad.yml",
-			ExpectedError: "Error parsing config file: yaml: unmarshal errors:\n  line 50: field invalid_extra_field not found in type config.plain",
+			ExpectedError: "error parsing config file: yaml: unmarshal errors:\n  line 50: field invalid_extra_field not found in type config.plain",
 		},
 		{
 			ConfigFile:    "testdata/blackbox-bad2.yml",
-			ExpectedError: "Error parsing config file: at most one of bearer_token & bearer_token_file must be configured",
+			ExpectedError: "error parsing config file: at most one of bearer_token & bearer_token_file must be configured",
 		},
 		{
 			ConfigFile:    "testdata/invalid-dns-module.yml",
-			ExpectedError: "Error parsing config file: Query name must be set for DNS module",
+			ExpectedError: "error parsing config file: query name must be set for DNS module",
 		},
 	}
 	for i, test := range tests {