Skip to content

Commit 6c2a134

Browse files
committed
Go modernize
Signed-off-by: SuperQ <[email protected]>
1 parent af0ff8e commit 6c2a134

File tree

5 files changed

+19
-27
lines changed

5 files changed

+19
-27
lines changed

collector/collector.go

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ var combinedTypeMapping = map[string]map[int]string{
6363

6464
func oidToList(oid string) []int {
6565
result := []int{}
66-
for _, x := range strings.Split(oid, ".") {
66+
for x := range strings.SplitSeq(oid, ".") {
6767
o, _ := strconv.Atoi(x)
6868
result = append(result, o)
6969
}
@@ -118,10 +118,7 @@ func ScrapeTarget(snmp scraper.SNMPScraper, target string, auth *config.Auth, mo
118118
maxOids = 1
119119
}
120120
for len(getOids) > 0 {
121-
oids := len(getOids)
122-
if oids > maxOids {
123-
oids = maxOids
124-
}
121+
oids := min(len(getOids), maxOids)
125122

126123
packet, err := snmp.Get(getOids[:oids])
127124
if err != nil {
@@ -355,7 +352,7 @@ func (c Collector) collect(ch chan<- prometheus.Metric, logger *slog.Logger, cli
355352
g.MaxRepetitions = module.WalkParams.MaxRepetitions
356353
g.UseUnconnectedUDPSocket = module.WalkParams.UseUnconnectedUDPSocket
357354
if module.WalkParams.AllowNonIncreasingOIDs {
358-
g.AppOpts = map[string]interface{}{
355+
g.AppOpts = map[string]any{
359356
"c": true,
360357
}
361358
}
@@ -423,10 +420,7 @@ func (c Collector) collect(ch chan<- prometheus.Metric, logger *slog.Logger, cli
423420
// Collect implements Prometheus.Collector.
424421
func (c Collector) Collect(ch chan<- prometheus.Metric) {
425422
wg := sync.WaitGroup{}
426-
workerCount := c.concurrency
427-
if workerCount < 1 {
428-
workerCount = 1
429-
}
423+
workerCount := max(c.concurrency, 1)
430424
ctx, cancel := context.WithCancel(c.ctx)
431425
defer cancel()
432426
workerChan := make(chan *NamedModule)
@@ -764,7 +758,7 @@ func enumAsStateSet(metric *config.Metric, value int, labelnames, labelvalues []
764758
return results
765759
}
766760

767-
func bits(metric *config.Metric, value interface{}, labelnames, labelvalues []string) []prometheus.Metric {
761+
func bits(metric *config.Metric, value any, labelnames, labelvalues []string) []prometheus.Metric {
768762
bytes, ok := value.([]byte)
769763
if !ok {
770764
return []prometheus.Metric{prometheus.NewInvalidMetric(prometheus.NewDesc("snmp_error", "BITS type was not a BISTRING on the wire.", nil, nil),
@@ -936,7 +930,7 @@ func indexOidsAsString(indexOids []int, typ string, fixedSize int, implied bool,
936930
return strings.Join(parts, "."), subOid, indexOids
937931
case "InetAddressIPv6":
938932
subOid, indexOids := splitOid(indexOids, 16)
939-
parts := make([]interface{}, 16)
933+
parts := make([]any, 16)
940934
for i, o := range subOid {
941935
parts[i] = o
942936
}

config/config.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ type Module struct {
138138
Filters []DynamicFilter `yaml:"filters,omitempty"`
139139
}
140140

141-
func (c *Module) UnmarshalYAML(unmarshal func(interface{}) error) error {
141+
func (c *Module) UnmarshalYAML(unmarshal func(any) error) error {
142142
*c = DefaultModule
143143
type plain Module
144144
return unmarshal((*plain)(c))
@@ -273,7 +273,7 @@ var (
273273
)
274274

275275
// MarshalYAML implements the yaml.Marshaler interface.
276-
func (s Secret) MarshalYAML() (interface{}, error) {
276+
func (s Secret) MarshalYAML() (any, error) {
277277
if DoNotHideSecrets {
278278
return string(s), nil
279279
}
@@ -283,7 +283,7 @@ func (s Secret) MarshalYAML() (interface{}, error) {
283283
return nil, nil
284284
}
285285

286-
func (c *Auth) UnmarshalYAML(unmarshal func(interface{}) error) error {
286+
func (c *Auth) UnmarshalYAML(unmarshal func(any) error) error {
287287
*c = DefaultAuth
288288
type plain Auth
289289
if err := unmarshal((*plain)(c)); err != nil {
@@ -327,7 +327,7 @@ type RegexpExtract struct {
327327
Regex Regexp `yaml:"regex"`
328328
}
329329

330-
func (c *RegexpExtract) UnmarshalYAML(unmarshal func(interface{}) error) error {
330+
func (c *RegexpExtract) UnmarshalYAML(unmarshal func(any) error) error {
331331
*c = DefaultRegexpExtract
332332
type plain RegexpExtract
333333
return unmarshal((*plain)(c))
@@ -339,15 +339,15 @@ type Regexp struct {
339339
}
340340

341341
// MarshalYAML implements the yaml.Marshaler interface.
342-
func (re Regexp) MarshalYAML() (interface{}, error) {
342+
func (re Regexp) MarshalYAML() (any, error) {
343343
if re.Regexp != nil {
344344
return re.String(), nil
345345
}
346346
return nil, nil
347347
}
348348

349349
// UnmarshalYAML implements the yaml.Unmarshaler interface.
350-
func (re *Regexp) UnmarshalYAML(unmarshal func(interface{}) error) error {
350+
func (re *Regexp) UnmarshalYAML(unmarshal func(any) error) error {
351351
var s string
352352
if err := unmarshal(&s); err != nil {
353353
return err

generator/config.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ type MetricOverrides struct {
3939
}
4040

4141
// UnmarshalYAML implements the yaml.Unmarshaler interface.
42-
func (c *MetricOverrides) UnmarshalYAML(unmarshal func(interface{}) error) error {
42+
func (c *MetricOverrides) UnmarshalYAML(unmarshal func(any) error) error {
4343
type plain MetricOverrides
4444
if err := unmarshal((*plain)(c)); err != nil {
4545
return err
@@ -62,7 +62,7 @@ type ModuleConfig struct {
6262
}
6363

6464
// UnmarshalYAML implements the yaml.Unmarshaler interface.
65-
func (c *ModuleConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
65+
func (c *ModuleConfig) UnmarshalYAML(unmarshal func(any) error) error {
6666
type plain ModuleConfig
6767
if err := unmarshal((*plain)(c)); err != nil {
6868
return err

generator/tree.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"fmt"
1818
"log/slog"
1919
"regexp"
20+
"slices"
2021
"sort"
2122
"strconv"
2223
"strings"
@@ -466,12 +467,9 @@ func generateConfigModule(cfg *ModuleConfig, node *Node, nameToNode map[string]*
466467

467468
// If lookup label is used as source index in another lookup,
468469
// we need to add this new label as another index.
469-
for _, sourceIndex := range requiredAsIndex {
470-
if sourceIndex == l.Labelname {
471-
idx := &config.Index{Labelname: l.Labelname, Type: l.Type}
472-
metric.Indexes = append(metric.Indexes, idx)
473-
break
474-
}
470+
if slices.Contains(requiredAsIndex, l.Labelname) {
471+
idx := &config.Index{Labelname: l.Labelname, Type: l.Type}
472+
metric.Indexes = append(metric.Indexes, idx)
475473
}
476474

477475
// Make sure we walk the lookup OID(s).

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ func handler(w http.ResponseWriter, r *http.Request, logger *slog.Logger, export
132132
uniqueM := make(map[string]bool)
133133
var modules []string
134134
for _, qm := range queryModule {
135-
for _, m := range strings.Split(qm, ",") {
135+
for m := range strings.SplitSeq(qm, ",") {
136136
if m == "" {
137137
continue
138138
}

0 commit comments

Comments
 (0)