Skip to content

Commit 6fe0067

Browse files
Merge pull request #25 from williamchanrico/add_golangci_lint
Add golangci-lint and its fixes
2 parents cf63890 + 89e51e7 commit 6fe0067

File tree

25 files changed

+583
-372
lines changed

25 files changed

+583
-372
lines changed

.github/workflows/test.yml

+17
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,20 @@ jobs:
2525
2626
- name: Unit tests
2727
run: go test -race -v ./...
28+
29+
golangci-lint:
30+
permissions:
31+
checks: write
32+
contents: read
33+
pull-requests: write
34+
name: golangci-lint
35+
runs-on: ubuntu-latest
36+
steps:
37+
- uses: actions/checkout@v2
38+
39+
- uses: reviewdog/action-golangci-lint@v2
40+
with:
41+
github_token: ${{ secrets.github_token }}
42+
level: 'warning'
43+
reporter: github-pr-review
44+
go_version: '1.17'

.golangci.yaml

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# This file contains all available configuration options
2+
# with their default values.
3+
4+
# options for analysis running
5+
run:
6+
# timeout for analysis, e.g. 30s, 5m, default is 1m
7+
timeout: 30s
8+
9+
# exit code when at least one issue was found, default is 1
10+
issues-exit-code: 1
11+
12+
# include test files or not, default is true
13+
tests: true
14+
15+
# default is true. Enables skipping of directories:
16+
# vendor$, third_party$, testdata$, examples$, Godeps$, builtin$
17+
skip-dirs-use-default: true
18+
19+
linters:
20+
enable-all: true
21+
disable:
22+
- gci
23+
- wsl
24+
- testpackage
25+
- gochecknoinits
26+
- gochecknoglobals
27+
- gomnd
28+
- funlen
29+
- paralleltest
30+
- gofumpt
31+
- ireturn
32+
- scopelint
33+
34+
# all available settings of specific linters
35+
linters-settings:
36+
enable-all: true
37+
38+
staticcheck:
39+
# Select the Go version to target. The default is '1.13'.
40+
go: "1.17"
41+
# https://staticcheck.io/docs/options#checks
42+
checks: [ "all" ]
43+
44+
stylecheck:
45+
# Select the Go version to target. The default is '1.13'.
46+
go: "1.17"
47+
# https://staticcheck.io/docs/options#checks
48+
checks: [ "all", "-ST1000", "-ST1003", "-ST1016", "-ST1020", "-ST1021", "-ST1022" ]
49+
# https://staticcheck.io/docs/options#dot_import_whitelist
50+
dot-import-whitelist:
51+
- fmt
52+
# https://staticcheck.io/docs/options#initialisms
53+
initialisms: [ "ACL", "API", "ASCII", "CPU", "CSS", "DNS", "EOF", "GUID", "HTML", "HTTP", "HTTPS", "ID", "IP", "JSON", "QPS", "RAM", "RPC", "SLA", "SMTP", "SQL", "SSH", "TCP", "TLS", "TTL", "UDP", "UI", "GID", "UID", "UUID", "URI", "URL", "UTF8", "VM", "XML", "XMPP", "XSRF", "XSS" ]
54+
# https://staticcheck.io/docs/options#http_status_code_whitelist
55+
http-status-code-whitelist: [ "200", "400", "404", "500" ]
56+
57+
tagliatelle:
58+
# check the struck tag name case
59+
case:
60+
# use the struct field name to check the name of the struct tag
61+
use-field-name: true
62+
rules:
63+
# any struct tag type can be used.
64+
# support string case: `camel`, `pascal`, `kebab`, `snake`, `goCamel`, `goPascal`, `goKebab`, `goSnake`, `upper`, `lower`
65+
json: snake
66+
yaml: camel
67+
xml: camel
68+
bson: camel
69+
avro: snake
70+
mapstructure: kebab
71+
72+
lll:
73+
# max line length, lines longer will be reported. Default is 120.
74+
# '\t' is counted as 1 character by default, and can be changed with the tab-width option
75+
line-length: 180
76+
# tab width in spaces. Default to 1.
77+
tab-width: 1
78+
79+
issues:
80+
# Excluding configuration per-path, per-linter, per-text and per-source
81+
exclude-rules:
82+
# Exclude some staticcheck messages
83+
- linters:
84+
- godox
85+
text: "TODO:"
86+
87+
# Independently from option `exclude` we use default exclude patterns,
88+
# it can be disabled by this option. To list all
89+
# excluded by default patterns execute `golangci-lint run --help`.
90+
# Default value for this option is true.
91+
exclude-use-default: false

Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ go-build-linux: ## Build the app for linux platforms exclude: "arm64" CGO errors
6868

6969
.PHONY: go-test
7070
go-test: ## Run go test
71+
golangci-lint run
7172
go test -v ./...
7273

7374
# DOCKER TASKS

cmd/planet-exporter/internal/internal.go

+20-18
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@ package internal
1616

1717
import (
1818
"context"
19+
"errors"
1920
"fmt"
2021
"net/http"
22+
"net/http/pprof"
2123
"os"
2224
"os/signal"
2325
"syscall"
2426
"time"
2527

26-
"net/http/pprof"
27-
2828
"planet-exporter/collector"
2929
taskdarkstat "planet-exporter/collector/task/darkstat"
3030
taskebpf "planet-exporter/collector/task/ebpf"
@@ -38,8 +38,8 @@ import (
3838
log "github.com/sirupsen/logrus"
3939
)
4040

41-
// Config contains main service config options
42-
type Config struct {
41+
// Config contains main service config options.
42+
type Config struct { // nolint:maligned
4343
// Main config
4444
ListenAddress string
4545
LogLevel string
@@ -63,23 +63,23 @@ type Config struct {
6363
TaskSocketstatEnabled bool
6464
}
6565

66-
// Service contains main service dependency
66+
// Service contains main service dependency.
6767
type Service struct {
6868
Config Config
6969

7070
// Collector is prometheus collector that is registered
7171
Collector *collector.PlanetCollector
7272
}
7373

74-
// New service
74+
// New service.
7575
func New(config Config, collector *collector.PlanetCollector) Service {
7676
return Service{
7777
Config: config,
7878
Collector: collector,
7979
}
8080
}
8181

82-
// Run main service
82+
// Run main service.
8383
func (s Service) Run(ctx context.Context) error {
8484
ctx, cancel := context.WithCancel(ctx)
8585
defer cancel()
@@ -88,14 +88,14 @@ func (s Service) Run(ctx context.Context) error {
8888
log.Infof("Set task ticker duration to %v", s.Config.TaskInterval)
8989
interval, err := time.ParseDuration(s.Config.TaskInterval)
9090
if err != nil {
91-
return err
91+
return fmt.Errorf("error parsing interval duration: %w", err)
9292
}
9393
go s.collect(ctx, interval)
9494

95-
r := prometheus.NewRegistry()
96-
r.MustRegister(version.NewCollector("planet_exporter"))
97-
if err := r.Register(s.Collector); err != nil {
98-
return fmt.Errorf("Failed to register planet collector: %v", err)
95+
promRegistry := prometheus.NewRegistry()
96+
promRegistry.MustRegister(version.NewCollector("planet_exporter"))
97+
if err := promRegistry.Register(s.Collector); err != nil {
98+
return fmt.Errorf("failed to register planet collector: %w", err)
9999
}
100100

101101
handler := http.NewServeMux()
@@ -113,8 +113,8 @@ func (s Service) Run(ctx context.Context) error {
113113
}
114114
})
115115
handler.Handle("/metrics", promhttp.HandlerFor(
116-
prometheus.Gatherers{r},
117-
promhttp.HandlerOpts{
116+
prometheus.Gatherers{promRegistry},
117+
promhttp.HandlerOpts{ // nolint:exhaustivestruct
118118
ErrorHandling: promhttp.ContinueOnError,
119119
},
120120
))
@@ -139,18 +139,20 @@ func (s Service) Run(ctx context.Context) error {
139139
}()
140140

141141
log.Infof("Start HTTP server on %v", s.Config.ListenAddress)
142-
if err := httpServer.Serve(s.Config.ListenAddress); err != http.ErrServerClosed {
143-
return err
142+
if err := httpServer.Serve(s.Config.ListenAddress); !errors.Is(err, http.ErrServerClosed) {
143+
return fmt.Errorf("error on HTTP server: %w", err)
144144
}
145145

146146
<-stopChan
147147

148148
return nil
149149
}
150150

151-
// collect periodically runs all collector tasks that are expensive to compute on-the-fly
151+
// collect periodically runs all collector tasks that are expensive to compute on-the-fly.
152152
func (s Service) collect(ctx context.Context, interval time.Duration) {
153-
inventoryTicker := time.NewTicker(interval * 25)
153+
const inventoryTickerIntervalSeconds = 25
154+
155+
inventoryTicker := time.NewTicker(interval * inventoryTickerIntervalSeconds)
154156
defaultTicker := time.NewTicker(interval)
155157
defer inventoryTicker.Stop()
156158
defer defaultTicker.Stop()

cmd/planet-exporter/main.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,13 @@ import (
2626
log "github.com/sirupsen/logrus"
2727
)
2828

29-
var (
30-
version string
31-
showVersionAndExit bool
32-
)
29+
var version string
3330

3431
func main() {
3532
var config internal.Config
3633

34+
var showVersionAndExit bool
35+
3736
// Main
3837
flag.StringVar(&config.ListenAddress, "listen-address", "0.0.0.0:19100", "Address to which exporter will bind its HTTP interface")
3938
flag.StringVar(&config.LogLevel, "log-level", "info", "Log level")
@@ -59,11 +58,11 @@ func main() {
5958
flag.Parse()
6059

6160
if showVersionAndExit {
62-
fmt.Printf("planet-exporter %v\n", version)
61+
fmt.Println("planet-exporter", version) // nolint:forbidigo
6362
os.Exit(0)
6463
}
6564

66-
log.SetFormatter(&log.TextFormatter{
65+
log.SetFormatter(&log.TextFormatter{ // nolint:exhaustivestruct
6766
DisableColors: config.LogDisableColors,
6867
DisableTimestamp: config.LogDisableTimestamp,
6968
FullTimestamp: true,
@@ -88,7 +87,8 @@ func main() {
8887
log.Info("Initialize main service")
8988
svc := internal.New(config, collector)
9089
if err := svc.Run(ctx); err != nil {
91-
log.Fatalf("Main service exit with error: %v", err)
90+
log.Errorf("Main service exit with error: %v", err)
91+
os.Exit(1)
9292
}
9393

9494
log.Info("Main service exit successfully")

0 commit comments

Comments
 (0)