diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 0000000..5385090 --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,18 @@ +linters: + enable: + - errorlint + - goimports + - misspell + - perfsprint + - testifylint + - usestdlibvars + +issues: + exclude-rules: + - path: _test.go + linters: + - errcheck + +linters-settings: + goimports: + local-prefixes: github.com/boynux/squid-exporter diff --git a/collector/client.go b/collector/client.go index 4dd6da2..7a1ef46 100644 --- a/collector/client.go +++ b/collector/client.go @@ -15,7 +15,7 @@ import ( "github.com/boynux/squid-exporter/types" ) -/*CacheObjectClient holds information about squid manager */ +// CacheObjectClient holds information about Squid manager. type CacheObjectClient struct { ch connectionHandler basicAuthString string @@ -31,7 +31,7 @@ type connectionHandlerImpl struct { port int } -/*SquidClient provides functionality to fetch squid metrics */ +// SquidClient provides functionality to fetch Squid metrics. type SquidClient interface { GetCounters() (types.Counters, error) GetServiceTimes() (types.Counters, error) @@ -45,9 +45,9 @@ const ( func buildBasicAuthString(login string, password string) string { if len(login) == 0 { return "" - } else { - return base64.StdEncoding.EncodeToString([]byte(login + ":" + password)) } + + return base64.StdEncoding.EncodeToString([]byte(login + ":" + password)) } type CacheObjectRequest struct { @@ -58,7 +58,7 @@ type CacheObjectRequest struct { Headers []string } -/*NewCacheObjectClient initializes a new cache client */ +// NewCacheObjectClient initializes a new cache client. func NewCacheObjectClient(cor *CacheObjectRequest) *CacheObjectClient { return &CacheObjectClient{ &connectionHandlerImpl{ @@ -82,8 +82,8 @@ func (c *CacheObjectClient) readFromSquid(endpoint string) (*bufio.Reader, error return nil, err } - if r.StatusCode != 200 { - return nil, fmt.Errorf("Non success code %d while fetching metrics", r.StatusCode) + if r.StatusCode != http.StatusOK { + return nil, fmt.Errorf("non-success code %d while fetching metrics", r.StatusCode) } return bufio.NewReader(r.Body), err @@ -106,13 +106,13 @@ func readLines(reader *bufio.Reader, lines chan<- string) { close(lines) } -/*GetCounters fetches counters from squid cache manager */ +// GetCounters fetches counters from Squid cache manager. func (c *CacheObjectClient) GetCounters() (types.Counters, error) { var counters types.Counters reader, err := c.readFromSquid("counters") if err != nil { - return nil, fmt.Errorf("error getting counters: %v", err) + return nil, fmt.Errorf("error getting counters: %w", err) } lines := make(chan string) @@ -130,39 +130,36 @@ func (c *CacheObjectClient) GetCounters() (types.Counters, error) { return counters, err } -/*GetServiceTimes fetches service times from squid cache manager */ +// GetServiceTimes fetches service times from Squid cache manager. func (c *CacheObjectClient) GetServiceTimes() (types.Counters, error) { var serviceTimes types.Counters reader, err := c.readFromSquid("service_times") if err != nil { - return nil, fmt.Errorf("error getting service times: %v", err) + return nil, fmt.Errorf("error getting service times: %w", err) } lines := make(chan string) go readLines(reader, lines) for line := range lines { - s, err := decodeServiceTimeStrings(line) - if err != nil { + if s, err := decodeServiceTimeStrings(line); err != nil { log.Println(err) - } else { - if s.Key != "" { - serviceTimes = append(serviceTimes, s) - } + } else if s.Key != "" { + serviceTimes = append(serviceTimes, s) } } return serviceTimes, err } -/*GetInfos fetches info from squid cache manager */ +// GetInfos fetches info from Squid cache manager. func (c *CacheObjectClient) GetInfos() (types.Counters, error) { var infos types.Counters reader, err := c.readFromSquid("info") if err != nil { - return nil, fmt.Errorf("error getting info: %v", err) + return nil, fmt.Errorf("error getting info: %w", err) } lines := make(chan string) @@ -179,8 +176,7 @@ func (c *CacheObjectClient) GetInfos() (types.Counters, error) { } else { if len(dis.VarLabels) > 0 { if dis.VarLabels[0].Key == "5min" { - var infoAvg5 types.Counter - var infoAvg60 types.Counter + var infoAvg5, infoAvg60 types.Counter infoAvg5.Key = dis.Key + "_" + dis.VarLabels[0].Key infoAvg60.Key = dis.Key + "_" + dis.VarLabels[1].Key @@ -193,7 +189,6 @@ func (c *CacheObjectClient) GetInfos() (types.Counters, error) { infoAvg60.Value = value infos = append(infos, infoAvg60) } - } else { infoVarLabels.VarLabels = append(infoVarLabels.VarLabels, dis.VarLabels[0]) } @@ -303,7 +298,7 @@ func decodeInfoStrings(line string) (types.Counter, error) { // metrics with value as string need to save as label, format like "Squid Object Cache: Version 6.1" (the 3 first metrics) if key == "Squid_Object_Cache" || key == "Build_Info" || key == "Service_Name" { if key == "Squid_Object_Cache" { // To clarify that the value is the squid version. - key = key + "_Version" + key += "_Version" if slices := strings.Split(value, " "); len(slices) > 0 { value = slices[1] } @@ -344,10 +339,9 @@ func decodeInfoStrings(line string) (types.Counter, error) { infoAvgCounter.VarLabels = append(infoAvgCounter.VarLabels, infoAvg5mVarLabel, infoAvg60mVarLabel) return infoAvgCounter, nil - } else { - value = slices[0] } + value = slices[0] } value = strings.Replace(value, "%", "", -1) @@ -359,7 +353,7 @@ func decodeInfoStrings(line string) (types.Counter, error) { } } else { // this catch the last 4 metrics format like "value metricName" - lineTrimed := strings.TrimSpace(line[:]) + lineTrimed := strings.TrimSpace(line) if idx := strings.Index(lineTrimed, " "); idx >= 0 { key := strings.TrimSpace(lineTrimed[idx+1:]) diff --git a/collector/client_test.go b/collector/client_test.go index 55406bf..520f4d9 100644 --- a/collector/client_test.go +++ b/collector/client_test.go @@ -4,8 +4,10 @@ import ( "net" "testing" - "github.com/boynux/squid-exporter/types" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "github.com/boynux/squid-exporter/types" ) type mockConnectionHandler struct { @@ -74,7 +76,7 @@ func TestDecodeMetricStrings(t *testing.T) { c, err := tc.d(tc.s) if tc.e != "" { - assert.EqualError(t, err, tc.e) + require.EqualError(t, err, tc.e) } assert.Equal(t, tc.c, c) } diff --git a/collector/metrics.go b/collector/metrics.go index 21aa66f..4077cc0 100644 --- a/collector/metrics.go +++ b/collector/metrics.go @@ -4,8 +4,9 @@ import ( "log" "time" - "github.com/boynux/squid-exporter/config" "github.com/prometheus/client_golang/prometheus" + + "github.com/boynux/squid-exporter/config" ) type descMap map[string]*prometheus.Desc @@ -22,7 +23,7 @@ var ( infos descMap ) -/*Exporter entry point to squid exporter */ +// Exporter entry point to Squid exporter. type Exporter struct { client SquidClient @@ -42,7 +43,7 @@ type CollectorConfig struct { Headers []string } -/*New initializes a new exporter */ +// New initializes a new exporter. func New(c *CollectorConfig) *Exporter { counters = generateSquidCounters(c.Labels.Keys) if ExtractServiceTimes { @@ -90,10 +91,9 @@ func (e *Exporter) Describe(ch chan<- *prometheus.Desc) { for _, v := range infos { ch <- v } - } -/*Collect fetches metrics from squid manager and pushes them to promethus */ +// Collect fetches metrics from Squid manager and expose them to Prometheus. func (e *Exporter) Collect(c chan<- prometheus.Metric) { insts, err := e.client.GetCounters() diff --git a/collector/service_times.go b/collector/service_times.go index 6d058e7..92328db 100644 --- a/collector/service_times.go +++ b/collector/service_times.go @@ -119,8 +119,7 @@ func generateSquidServiceTimes(labels []string) descMap { for i := range squidServiceTimess { serviceTime := squidServiceTimess[i] - var key string - var name string + var key, name string if serviceTime.Counter != "" { key = fmt.Sprintf("%s_%s_%s", serviceTime.Section, serviceTime.Counter, serviceTime.Suffix) @@ -129,7 +128,7 @@ func generateSquidServiceTimes(labels []string) descMap { } else { key = fmt.Sprintf("%s_%s", serviceTime.Section, serviceTime.Suffix) name = prometheus.BuildFQName(namespace, strings.Replace(serviceTime.Section, ".", "_", -1), - fmt.Sprintf("%s", serviceTime.Suffix)) + serviceTime.Suffix) } serviceTimes[key] = prometheus.NewDesc( diff --git a/config/config.go b/config/config.go index ba17a3d..dfaee47 100644 --- a/config/config.go +++ b/config/config.go @@ -1,6 +1,7 @@ package config import ( + "errors" "flag" "fmt" "log" @@ -42,7 +43,7 @@ type Labels struct { Values []string } -/*Config configurations for exporter */ +// Config configurations for exporter. type Config struct { ListenAddress string WebConfigPath string @@ -59,7 +60,7 @@ type Config struct { UseProxyHeader bool } -/*NewConfig creates a new config object from command line args */ +// NewConfig creates a new config object from command line args. func NewConfig() *Config { c := &Config{} @@ -146,12 +147,12 @@ func (l *Labels) Set(value string) error { args := strings.Split(value, "=") if len(args) != 2 || len(args[1]) < 1 { - return fmt.Errorf("Label must be in 'key=value' format") + return errors.New("label must be in 'key=value' format") } for _, key := range l.Keys { if key == args[0] { - return fmt.Errorf("Labels must be distinct, found duplicated key %s", args[0]) + return fmt.Errorf("labels must be distinct; found duplicate key %q", args[0]) } } l.Keys = append(l.Keys, args[0]) diff --git a/helpers.go b/helpers.go index 85e19b5..413e694 100644 --- a/helpers.go +++ b/helpers.go @@ -6,9 +6,9 @@ import ( "strconv" "strings" - "github.com/boynux/squid-exporter/config" - proxyproto "github.com/pires/go-proxyproto" + + "github.com/boynux/squid-exporter/config" ) func createProxyHeader(cfg *config.Config) string { diff --git a/main.go b/main.go index d284dda..3fa268e 100644 --- a/main.go +++ b/main.go @@ -8,8 +8,6 @@ import ( "strconv" "strings" - "github.com/boynux/squid-exporter/collector" - "github.com/boynux/squid-exporter/config" kitlog "github.com/go-kit/log" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/collectors" @@ -17,6 +15,9 @@ import ( "github.com/prometheus/client_golang/prometheus/promhttp" "github.com/prometheus/common/version" "github.com/prometheus/exporter-toolkit/web" + + "github.com/boynux/squid-exporter/collector" + "github.com/boynux/squid-exporter/config" ) func init() { @@ -53,11 +54,11 @@ func main() { PidFn: func() (int, error) { content, err := os.ReadFile(cfg.Pidfile) if err != nil { - return 0, fmt.Errorf("can't read pid file %q: %s", cfg.Pidfile, err) + return 0, fmt.Errorf("cannot read pid file %q: %w", cfg.Pidfile, err) } value, err := strconv.Atoi(strings.TrimSpace(string(content))) if err != nil { - return 0, fmt.Errorf("can't parse pid file %q: %s", cfg.Pidfile, err) + return 0, fmt.Errorf("cannot parse pid file %q: %w", cfg.Pidfile, err) } return value, nil }, diff --git a/types/types.go b/types/types.go index fbf8712..35186f5 100644 --- a/types/types.go +++ b/types/types.go @@ -1,17 +1,17 @@ package types -/*VarLabel maps key value prometheus labes*/ +// VarLabel maps key value Prometheus labels. type VarLabel struct { Key string Value string } -/*Counter maps a squid conters */ +// Counter maps a Squid counter. type Counter struct { Key string Value float64 VarLabels []VarLabel } -/*Counters is a list of multiple squid counters */ +// Counters is a list of multiple Squid counters. type Counters []Counter