Skip to content

Commit

Permalink
Switch from gometalinter to golangci-lint
Browse files Browse the repository at this point in the history
Signed-off-by: Mario Trangoni <mjtrangoni@gmail.com>
  • Loading branch information
mjtrangoni committed Nov 9, 2019
1 parent d5ce508 commit 63a8d84
Show file tree
Hide file tree
Showing 8 changed files with 218 additions and 86 deletions.
83 changes: 83 additions & 0 deletions .golanci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
linters-settings:
govet:
check-shadowing: true
golint:
min-confidence: 0
gocyclo:
min-complexity: 20
gocognit:
min-complexity: 30
maligned:
suggest-new: true
dupl:
threshold: 100
goconst:
min-len: 2
min-occurrences: 2
depguard:
list-type: blacklist
packages:
# logging is allowed only by logutils.Log, logrus
# is allowed to use only in logutils package
- github.com/sirupsen/logrus
misspell:
locale: US
ignore-words:
- lustre
lll:
line-length: 120
goimports:
local-prefixes: github.com/golangci/golangci-lint
gocritic:
enabled-tags:
- performance
- style
- experimental
disabled-checks:
- wrapperFunc
- hugeParam
funlen:
lines: 300
statements: 50
wsl:
# If true append is only allowed to be cuddled if appending value is
# matching variables, fields or types on line above. Default is true.
strict-append: true
# Allow calls and assignments to be cuddled as long as the lines have any
# matching variables, fields or types. Default is true.
allow-assign-and-call: true
# Allow multiline assignments to be cuddled. Default is true.
allow-multiline-assign: true
# Allow case blocks to end with a whitespace.
allow-case-traling-whitespace: true
# Allow declarations (var) to be cuddled.
allow-cuddle-declarations: true

linters:
enable-all: true
disable:
- gochecknoinits
- gochecknoglobals
- goimports

run:
skip-dirs:
- test/testdata_etc

issues:
exclude-rules:
- text: "type `typedDesc` is unused"
linters:
- unused
- text: "consider giving a name to these results"
linters:
- gocritic
- text: "Subprocess launched with function call as argument or cmd arguments"
linters:
- gosec

# golangci.com configuration
# https://github.com/golangci/golangci/wiki/Configuration
service:
prepare:
- echo "here I can run custom commands, but no preparation needed for this repo"
8 changes: 0 additions & 8 deletions .gometalinter.json

This file was deleted.

38 changes: 25 additions & 13 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,54 +15,66 @@ GO ?= GO15VENDOREXPERIMENT=1 go
GOPATH := $(firstword $(subst :, ,$(shell $(GO) env GOPATH)))
PROMU ?= $(GOPATH)/bin/promu
GODEP ?= $(GOPATH)/bin/dep
GOLINTER ?= $(GOPATH)/bin/gometalinter
GOLINTER ?= $(GOPATH)/bin/golangci-lint
pkgs = $(shell $(GO) list ./... | grep -v /vendor/)
TARGET ?= pacemaker_exporter

PREFIX ?= $(shell pwd)
BIN_DIR ?= $(shell pwd)

all: depcheck format vet gometalinter build test
.PHONY: all
all: clean depcheck format vet golangci build test

.PHONY: test
test:
@echo ">> running tests"
@$(GO) test -short $(pkgs)
@$(GO) test -v $(pkgs)

.PHONY: format
format:
@echo ">> formatting code"
@$(GO) fmt $(pkgs)

gometalinter: $(GOLINTER)
.PHONY: vet
vet:
@echo ">> vetting code"
@$(GO) vet $(pkgs)

.PHONY: golangci $(GOLINTER)
golangci: $(GOLINTER)
@echo ">> linting code"
@$(GOLINTER) --install > /dev/null
@$(GOLINTER) --config=./.gometalinter.json ./...
@$(GOLINTER) run --config ./.golanci.yml

.PHONY: build
build: $(PROMU) depcheck
@echo ">> building binaries"
@$(PROMU) build --prefix $(PREFIX)

.PHONY: clean
clean:
@echo ">> Cleaning up"
@echo ">> cleaning up"
@find . -type f -name '*~' -exec rm -fv {} \;
@$(RM) $(TARGET)

.PHONY: depcheck
depcheck: $(GODEP)
@echo ">> ensure vendoring"
@$(GODEP) ensure

$(GOPATH)/bin/dep dep:
.PHONY: dep $(GODEP)
$(GODEP) dep:
@GOOS=$(shell uname -s | tr A-Z a-z) \
GOARCH=$(subst x86_64,amd64,$(patsubst i%86,386,$(shell uname -m))) \
$(GO) get -u github.com/golang/dep/cmd/dep

$(GOPATH)/bin/promu promu:
.PHONY: promu
$(PROMU) promu:
@GOOS=$(shell uname -s | tr A-Z a-z) \
GOARCH=$(subst x86_64,amd64,$(patsubst i%86,386,$(shell uname -m))) \
$(GO) get -u github.com/prometheus/promu

$(GOPATH)/bin/gometalinter lint:
.PHONY: golangci-lint lint
$(GOPATH)/bin/golangci-lint lint:
@GOOS=$(shell uname -s | tr A-Z a-z) \
GOARCH=$(subst x86_64,amd64,$(patsubst i%86,386,$(shell uname -m))) \
$(GO) get -u github.com/alecthomas/gometalinter

.PHONY: all format vet build test promu clean $(GOPATH)/bin/promu $(GOPATH)/bin/gometalinter lint $(GOPATH)/bin/dep dep depcheck
$(GO) get -u github.com/golangci/golangci-lint/cmd/golangci-lint
15 changes: 13 additions & 2 deletions collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ var (

const (
defaultEnabled = true
//defaultDisabled = false
// defaultDisabled = false
)

var (
Expand Down Expand Up @@ -79,28 +79,35 @@ type PacemakerCollector struct {
// NewPacemakerCollector creates a new PacemakerCollector
func NewPacemakerCollector(filters ...string) (*PacemakerCollector, error) {
f := make(map[string]bool)

for _, filter := range filters {
enabled, exist := collectorState[filter]
if !exist {
return nil, fmt.Errorf("missing collector: %s", filter)
}

if !*enabled {
return nil, fmt.Errorf("disabled collector: %s", filter)
}

f[filter] = true
}

collectors := make(map[string]Collector)

for key, enabled := range collectorState {
if *enabled {
collector, err := factories[key]()
if err != nil {
return nil, err
}

if len(f) == 0 || f[key] {
collectors[key] = collector
}
}
}

return &PacemakerCollector{Collectors: collectors}, nil
}

Expand All @@ -114,23 +121,27 @@ func (n PacemakerCollector) Describe(ch chan<- *prometheus.Desc) {
func (n PacemakerCollector) Collect(ch chan<- prometheus.Metric) {
wg := sync.WaitGroup{}
wg.Add(len(n.Collectors))

for name, c := range n.Collectors {
go func(name string, c Collector) {
execute(name, c, ch)
wg.Done()
}(name, c)
}

wg.Wait()
}

func execute(name string, c Collector, ch chan<- prometheus.Metric) {
var success float64

begin := time.Now()
err := c.Update(ch)
duration := time.Since(begin)
var success float64

if err != nil {
log.Errorf("ERROR: %s collector failed after %fs: %s", name, duration.Seconds(), err)

success = 0
} else {
log.Debugf("OK: %s collector succeeded after %fs.", name, duration.Seconds())
Expand Down
5 changes: 4 additions & 1 deletion collector/crm_mon.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ import (
)

var (
crmMonElemEnabled = kingpin.Flag("collector.crm_mon.elements-enabled", "Pacemaker `crm_mon` XML elements that will be exported.").Default("summary,nodes,node_attributes,clones,resources,resources_group,failures,bans").String()
crmMonElemEnabled = kingpin.Flag("collector.crm_mon.elements-enabled",
"Pacemaker `crm_mon` XML elements that will be exported.").Default(
"summary,nodes,node_attributes,clones,resources,resources_group,failures,bans").String()
)

type crmMonCollector struct {
Expand Down Expand Up @@ -362,5 +364,6 @@ func (c *crmMonCollector) Update(ch chan<- prometheus.Metric) error {
if err != nil {
return fmt.Errorf("couldn't get crm_mon information: %s", err)
}

return nil
}
Loading

0 comments on commit 63a8d84

Please sign in to comment.