Skip to content

Commit

Permalink
Improve linting (#897)
Browse files Browse the repository at this point in the history
* Enable more golangci-lint checks.
* Cleanup various linter issues.

Signed-off-by: SuperQ <[email protected]>
  • Loading branch information
SuperQ authored Jun 23, 2023
1 parent 4a3227b commit 562ae90
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 21 deletions.
29 changes: 26 additions & 3 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
# Run only staticcheck for now. Additional linters will be enabled one-by-one.
linters:
enable:
- staticcheck
disable-all: true
- misspell
- revive
disable:
# Disable soon to deprecated[1] linters that lead to false
# positives when build tags disable certain files[2]
# 1: https://github.com/golangci/golangci-lint/issues/1841
# 2: https://github.com/prometheus/node_exporter/issues/1545
- deadcode
- unused
- structcheck
- varcheck

issues:
exclude-rules:
- path: _test.go
linters:
- errcheck
- govet

linters-settings:
errcheck:
exclude-functions:
# Used in HTTP handlers, any error is handled by the server itself.
- (net/http.ResponseWriter).Write
# Never check for logger errors.
- (github.com/go-kit/log.Logger).Log
22 changes: 9 additions & 13 deletions collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ type internalMetrics struct {
snmpRetries prometheus.Counter
}

type collector struct {
type Collector struct {
ctx context.Context
target string
auth *config.Auth
Expand Down Expand Up @@ -391,18 +391,18 @@ func newInternalMetrics(reg prometheus.Registerer) internalMetrics {
}
}

func New(ctx context.Context, target string, auth *config.Auth, module *config.Module, logger log.Logger, reg prometheus.Registerer) *collector {
func New(ctx context.Context, target string, auth *config.Auth, module *config.Module, logger log.Logger, reg prometheus.Registerer) *Collector {
internalMetrics := newInternalMetrics(reg)
return &collector{ctx: ctx, target: target, auth: auth, module: module, logger: logger, metrics: internalMetrics}
return &Collector{ctx: ctx, target: target, auth: auth, module: module, logger: logger, metrics: internalMetrics}
}

// Describe implements Prometheus.Collector.
func (c collector) Describe(ch chan<- *prometheus.Desc) {
func (c Collector) Describe(ch chan<- *prometheus.Desc) {
ch <- prometheus.NewDesc("dummy", "dummy", nil, nil)
}

// Collect implements Prometheus.Collector.
func (c collector) Collect(ch chan<- prometheus.Metric) {
func (c Collector) Collect(ch chan<- prometheus.Metric) {
start := time.Now()
results, err := ScrapeTarget(c.ctx, c.target, c.auth, c.module, c.logger, c.metrics)
if err != nil {
Expand Down Expand Up @@ -465,9 +465,8 @@ func getPduValue(pdu *gosnmp.SnmpPDU) float64 {
if *wrapCounters {
// Wrap by 2^53.
return float64(gosnmp.ToBigInt(pdu.Value).Uint64() % float64Mantissa)
} else {
return float64(gosnmp.ToBigInt(pdu.Value).Uint64())
}
return float64(gosnmp.ToBigInt(pdu.Value).Uint64())
case gosnmp.OpaqueFloat:
return float64(pdu.Value.(float32))
case gosnmp.OpaqueDouble:
Expand Down Expand Up @@ -530,7 +529,6 @@ func pduToSamples(indexOids []int, pdu *gosnmp.SnmpPDU, metric *config.Metric, o
labels := indexesToLabels(indexOids, metric, oidToPdu, metrics)

value := getPduValue(pdu)
t := prometheus.UntypedValue

labelnames := make([]string, 0, len(labels)+1)
labelvalues := make([]string, 0, len(labels)+1)
Expand All @@ -539,6 +537,7 @@ func pduToSamples(indexOids []int, pdu *gosnmp.SnmpPDU, metric *config.Metric, o
labelvalues = append(labelvalues, v)
}

var t prometheus.ValueType
switch metric.Type {
case "counter":
t = prometheus.CounterValue
Expand Down Expand Up @@ -827,9 +826,8 @@ func indexOidsAsString(indexOids []int, typ string, fixedSize int, implied bool,
}
if len(parts) == 0 {
return "", subOid, indexOids
} else {
return fmt.Sprintf("0x%X", string(parts)), subOid, indexOids
}
return fmt.Sprintf("0x%X", string(parts)), subOid, indexOids
case "DisplayString":
var subOid []int
length := fixedSize
Expand Down Expand Up @@ -867,12 +865,10 @@ func indexOidsAsString(indexOids []int, typ string, fixedSize int, implied bool,
value, ok := enumValues[subOid[0]]
if ok {
return value, subOid, indexOids
} else {
return fmt.Sprintf("%d", subOid[0]), subOid, indexOids
}
return fmt.Sprintf("%d", subOid[0]), subOid, indexOids
default:
panic(fmt.Sprintf("Unknown index type %s", typ))
return "", nil, nil
}
}

Expand Down
6 changes: 3 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func LoadFile(filename string) (*Config, error) {
}

var (
defaultRetries int = 3
defaultRetries = 3

DefaultAuth = Auth{
Community: "public",
Expand Down Expand Up @@ -69,8 +69,8 @@ var (

// Config for the snmp_exporter.
type Config struct {
Auths map[string]*Auth `yaml:"auths",omitempty"`
Modules map[string]*Module `yaml:"modules",omitempty"`
Auths map[string]*Auth `yaml:"auths,omitempty"`
Modules map[string]*Module `yaml:"modules,omitempty"`
Version int `yaml:"version"`
}

Expand Down
3 changes: 1 addition & 2 deletions generator/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,8 @@ func getMetricNode(oid string, node *Node, nameToNode map[string]*Node) (*Node,
_, ok = metricType(n.Type)
if ok && metricAccess(n.Access) && len(n.Indexes) == 0 {
return n, oidScalar
} else {
return n, oidSubtree
}
return n, oidSubtree
}

// Unknown OID/name, search Node tree for longest match.
Expand Down

0 comments on commit 562ae90

Please sign in to comment.