Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix various golangci-lint nags #98

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -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
46 changes: 20 additions & 26 deletions collector/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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 {
Expand All @@ -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{
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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])
}
Expand Down Expand Up @@ -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]
}
Expand Down Expand Up @@ -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)
Expand All @@ -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:])
Expand Down
6 changes: 4 additions & 2 deletions collector/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand Down
10 changes: 5 additions & 5 deletions collector/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -22,7 +23,7 @@ var (
infos descMap
)

/*Exporter entry point to squid exporter */
// Exporter entry point to Squid exporter.
type Exporter struct {
client SquidClient

Expand All @@ -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 {
Expand Down Expand Up @@ -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()

Expand Down
5 changes: 2 additions & 3 deletions collector/service_times.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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(
Expand Down
9 changes: 5 additions & 4 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config

import (
"errors"
"flag"
"fmt"
"log"
Expand Down Expand Up @@ -42,7 +43,7 @@ type Labels struct {
Values []string
}

/*Config configurations for exporter */
// Config configurations for exporter.
type Config struct {
ListenAddress string
WebConfigPath string
Expand All @@ -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{}

Expand Down Expand Up @@ -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])
Expand Down
4 changes: 2 additions & 2 deletions helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 5 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@ 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"
versioncollector "github.com/prometheus/client_golang/prometheus/collectors/version"
"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() {
Expand Down Expand Up @@ -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
},
Expand Down
6 changes: 3 additions & 3 deletions types/types.go
Original file line number Diff line number Diff line change
@@ -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