Skip to content

Commit

Permalink
*: update Makefile.common with new staticcheck (#372)
Browse files Browse the repository at this point in the history
Signed-off-by: Simon Pasquier <[email protected]>
  • Loading branch information
simonpasquier authored Jan 7, 2019
1 parent b4cd74c commit 43f7ba1
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 37 deletions.
56 changes: 33 additions & 23 deletions Makefile.common
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand All @@ -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/
Expand Down Expand Up @@ -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)
Expand Down
14 changes: 7 additions & 7 deletions collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func ScrapeTarget(target string, config *config.Module) ([]gosnmp.SnmpPDU, error
snmp.Target = host
p, err := strconv.Atoi(port)
if err != nil {
return nil, fmt.Errorf("Error converting port number to int for target %s: %s", target, err)
return nil, fmt.Errorf("error converting port number to int for target %s: %s", target, err)
}
snmp.Port = uint16(p)
}
Expand All @@ -106,7 +106,7 @@ func ScrapeTarget(target string, config *config.Module) ([]gosnmp.SnmpPDU, error
// Do the actual walk.
err := snmp.Connect()
if err != nil {
return nil, fmt.Errorf("Error connecting to target %s: %s", target, err)
return nil, fmt.Errorf("error connecting to target %s: %s", target, err)
}
defer snmp.Conn.Close()

Expand All @@ -127,7 +127,7 @@ func ScrapeTarget(target string, config *config.Module) ([]gosnmp.SnmpPDU, error
getStart := time.Now()
packet, err := snmp.Get(getOids[:oids])
if err != nil {
return nil, fmt.Errorf("Error getting target %s: %s", snmp.Target, err)
return nil, fmt.Errorf("error getting target %s: %s", snmp.Target, err)
}
log.Debugf("Get of %d OIDs completed in %s", oids, time.Since(getStart))
// SNMPv1 will return packet error for unsupported OIDs.
Expand All @@ -139,7 +139,7 @@ func ScrapeTarget(target string, config *config.Module) ([]gosnmp.SnmpPDU, error
// Response received with errors.
// TODO: "stringify" gosnmp errors instead of showing error code.
if packet.Error != gosnmp.NoError {
return nil, fmt.Errorf("Error reported by target %s: Error Status %d", snmp.Target, packet.Error)
return nil, fmt.Errorf("error reported by target %s: Error Status %d", snmp.Target, packet.Error)
}
for _, v := range packet.Variables {
if v.Type == gosnmp.NoSuchObject || v.Type == gosnmp.NoSuchInstance {
Expand All @@ -161,7 +161,7 @@ func ScrapeTarget(target string, config *config.Module) ([]gosnmp.SnmpPDU, error
pdus, err = snmp.BulkWalkAll(subtree)
}
if err != nil {
return nil, fmt.Errorf("Error walking target %s: %s", snmp.Target, err)
return nil, fmt.Errorf("error walking target %s: %s", snmp.Target, err)
}
log.Debugf("Walk of target %q subtree %q completed in %s", snmp.Target, subtree, time.Since(walkStart))

Expand Down Expand Up @@ -388,7 +388,7 @@ func pduToSamples(indexOids []int, pdu *gosnmp.SnmpPDU, metric *config.Metric, o
t, value, labelvalues...)
if err != nil {
sample = prometheus.NewInvalidMetric(prometheus.NewDesc("snmp_error", "Error calling NewConstMetric", nil, nil),
fmt.Errorf("Error for metric %s with labels %v from indexOids %v: %v", metric.Name, labelvalues, indexOids, err))
fmt.Errorf("error for metric %s with labels %v from indexOids %v: %v", metric.Name, labelvalues, indexOids, err))
}

return []prometheus.Metric{sample}
Expand All @@ -413,7 +413,7 @@ func applyRegexExtracts(metric *config.Metric, pduValue string, labelnames, labe
prometheus.GaugeValue, v, labelvalues...)
if err != nil {
newMetric = prometheus.NewInvalidMetric(prometheus.NewDesc("snmp_error", "Error calling NewConstMetric for regex_extract", nil, nil),
fmt.Errorf("Error for metric %s with labels %v: %v", metric.Name+name, labelvalues, err))
fmt.Errorf("error for metric %s with labels %v: %v", metric.Name+name, labelvalues, err))
}
results = append(results, newMetric)
break
Expand Down
12 changes: 6 additions & 6 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,26 +93,26 @@ func (c *Module) UnmarshalYAML(unmarshal func(interface{}) error) error {
switch wp.Auth.SecurityLevel {
case "authPriv":
if wp.Auth.PrivPassword == "" {
return fmt.Errorf("Priv password is missing, required for SNMPv3 with priv.")
return fmt.Errorf("priv password is missing, required for SNMPv3 with priv")
}
if wp.Auth.PrivProtocol != "DES" && wp.Auth.PrivProtocol != "AES" {
return fmt.Errorf("Priv protocol must be DES or AES.")
return fmt.Errorf("priv protocol must be DES or AES")
}
fallthrough
case "authNoPriv":
if wp.Auth.Password == "" {
return fmt.Errorf("Auth password is missing, required for SNMPv3 with auth.")
return fmt.Errorf("auth password is missing, required for SNMPv3 with auth")
}
if wp.Auth.AuthProtocol != "MD5" && wp.Auth.AuthProtocol != "SHA" {
return fmt.Errorf("Auth protocol must be SHA or MD5.")
return fmt.Errorf("auth protocol must be SHA or MD5")
}
fallthrough
case "noAuthNoPriv":
if wp.Auth.Username == "" {
return fmt.Errorf("Auth username is missing, required for SNMPv3")
return fmt.Errorf("auth username is missing, required for SNMPv3")
}
default:
return fmt.Errorf("Security level must be one of authPriv, authNoPriv or noAuthNoPriv")
return fmt.Errorf("security level must be one of authPriv, authNoPriv or noAuthNoPriv")
}
}
return nil
Expand Down
2 changes: 1 addition & 1 deletion generator/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (c *MetricOverrides) UnmarshalYAML(unmarshal func(interface{}) error) error
// Ensure type for override is valid if one is defined.
typ, ok := metricType(c.Type)
if c.Type != "" && (!ok || typ != c.Type) {
return fmt.Errorf("Invalid metric type override '%s'", c.Type)
return fmt.Errorf("invalid metric type override '%s'", c.Type)
}

return nil
Expand Down

0 comments on commit 43f7ba1

Please sign in to comment.