Skip to content

Commit bf7bdc2

Browse files
committed
Convert to go slog and add logging parameters
1 parent a961854 commit bf7bdc2

File tree

7 files changed

+120
-440
lines changed

7 files changed

+120
-440
lines changed

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ Several flags are available to customize how the exporter works. Note that none
5252
**NOTE**: This exporter MUST have the password set in the NETGEAR_EXPORTER_PASSWORD environment variable. If it is not set, it will fail to start with a warning message.
5353

5454
```
55+
usage: netgear_exporter [<flags>]
56+
5557
Flags:
5658
-h, --help Show context-sensitive help (also try --help-long and --help-man).
5759
--url="https://www.routerlogin.com"
@@ -74,10 +76,9 @@ Flags:
7476
certificate, any intermediates, and the CA's certificate ($NETGEAR_EXPORTER_WEB_TLS_CERTFILE)
7577
--web.tls.key_file=WEB.TLS.KEY_FILE
7678
Path to a file that contains the TLS private key (PEM format) ($NETGEAR_EXPORTER_WEB_TLS_KEYFILE)
77-
--printMetrics Print the metrics this exporter exposes and exits. Default: false ($NETGEAR_EXPORTER_PRINT_METRICS)
78-
--log.level="info" Only log messages with the given severity or above. Valid levels: [debug, info, warn, error, fatal]
79-
--log.format="logger:stderr"
80-
Set the log target and format. Example: "logger:syslog?appname=bob&local=7" or "logger:stdout?json=true"
79+
--printMetrics Prints the metrics this exporter exposes and exits. Default: false ($NETGEAR_EXPORTER_PRINT_METRICS)
80+
--log.level="info" Minimum log level for messages. One of error, warn, info, or debug. Default: info ($NETGEAR_EXPORTER_LOG_LEVEL)
81+
--log.json Format log lines as JSON. Default: false ($NETGEAR_EXPORTER_LOG_JSON)
8182
--version Show application version.
8283
```
8384

collectors/clients_collector.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package collectors
22

33
import (
4-
"github.com/DRuggeri/netgear_client"
5-
"github.com/prometheus/client_golang/prometheus"
6-
"github.com/prometheus/common/log"
4+
"log/slog"
75
"strconv"
86
"time"
7+
8+
"github.com/DRuggeri/netgear_client"
9+
"github.com/prometheus/client_golang/prometheus"
910
)
1011

1112
type ClientCollector struct {
@@ -119,7 +120,7 @@ func (c *ClientCollector) Collect(ch chan<- prometheus.Metric) {
119120
errorMetric := float64(0)
120121
clients, err := c.client.GetAttachDevice()
121122
if err != nil {
122-
log.Errorf("Error while collecting client statistics: %v", err)
123+
slog.Error("error while collecting client statistics: %v", slog.String("error", err.Error()))
123124
errorMetric = float64(1)
124125
c.scrapeErrorsTotalMetric.Inc()
125126
} else {

collectors/system_info_collector.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ package collectors
22

33
import (
44
"fmt"
5-
"github.com/DRuggeri/netgear_client"
6-
"github.com/prometheus/client_golang/prometheus"
7-
"github.com/prometheus/common/log"
5+
"log/slog"
86
"strconv"
97
"strings"
108
"time"
9+
10+
"github.com/DRuggeri/netgear_client"
11+
"github.com/prometheus/client_golang/prometheus"
1112
)
1213

1314
type SystemInfo struct {
@@ -107,7 +108,7 @@ func (c *SystemInfo) Collect(ch chan<- prometheus.Metric) {
107108
errorMetric := float64(0)
108109
stats, err := c.client.GetSystemInfo()
109110
if err != nil {
110-
log.Errorf("Error while collecting system info: %v", err)
111+
slog.Error("error while collecting system info: %v", slog.String("error", err.Error()))
111112
errorMetric = float64(1)
112113
c.scrapeErrorsTotalMetric.Inc()
113114
} else {
@@ -121,7 +122,7 @@ func (c *SystemInfo) Collect(ch chan<- prometheus.Metric) {
121122
c.metrics[name].Set(metric)
122123
c.metrics[name].Collect(ch)
123124
} else {
124-
log.Warnf("System info stat named '%s' missing from results!", name)
125+
slog.Warn(fmt.Sprintf("system info stat named '%s' missing from results!", name))
125126
}
126127
}
127128
}

collectors/traffic_collector.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ package collectors
22

33
import (
44
"fmt"
5-
"github.com/DRuggeri/netgear_client"
6-
"github.com/prometheus/client_golang/prometheus"
7-
"github.com/prometheus/common/log"
5+
"log/slog"
86
"strconv"
97
"strings"
108
"time"
9+
10+
"github.com/DRuggeri/netgear_client"
11+
"github.com/prometheus/client_golang/prometheus"
1112
)
1213

1314
type TrafficCollector struct {
@@ -123,7 +124,7 @@ func (c *TrafficCollector) Collect(ch chan<- prometheus.Metric) {
123124
errorMetric := float64(0)
124125
stats, err := c.client.GetTrafficMeterStatistics()
125126
if err != nil {
126-
log.Errorf("Error while collecting traffic statistics: %v", err)
127+
slog.Error("error while collecting traffic statistics", slog.String("error", err.Error()))
127128
errorMetric = float64(1)
128129
c.trafficScrapeErrorsTotalMetric.Inc()
129130
} else {
@@ -146,7 +147,7 @@ func (c *TrafficCollector) Collect(ch chan<- prometheus.Metric) {
146147
c.metrics[name].Set(metric)
147148
c.metrics[name].Collect(ch)
148149
} else {
149-
log.Warnf("Traffic stat named '%s' missing from results!", name)
150+
slog.Warn(fmt.Sprintf("traffic stat named '%s' missing from results!", name))
150151
}
151152
}
152153
}

go.mod

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,23 @@
11
module github.com/DRuggeri/netgear_exporter
22

3-
go 1.14
3+
go 1.23
44

55
require (
6-
github.com/DRuggeri/netgear_client v0.0.0-20190218174711-5c01ae5fd546
7-
github.com/prometheus/client_golang v1.9.0
8-
github.com/prometheus/common v0.15.0
9-
gopkg.in/alecthomas/kingpin.v2 v2.2.6
6+
github.com/DRuggeri/netgear_client v0.0.0-20230219193432-22cf2da4d7d4
7+
github.com/alecthomas/kingpin v2.2.6+incompatible
8+
github.com/prometheus/client_golang v1.20.5
9+
)
10+
11+
require (
12+
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 // indirect
13+
github.com/alecthomas/units v0.0.0-20240927000941-0f3dac36c52b // indirect
14+
github.com/beorn7/perks v1.0.1 // indirect
15+
github.com/cespare/xxhash/v2 v2.3.0 // indirect
16+
github.com/klauspost/compress v1.17.11 // indirect
17+
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
18+
github.com/prometheus/client_model v0.6.1 // indirect
19+
github.com/prometheus/common v0.61.0 // indirect
20+
github.com/prometheus/procfs v0.15.1 // indirect
21+
golang.org/x/sys v0.29.0 // indirect
22+
google.golang.org/protobuf v1.36.1 // indirect
1023
)

0 commit comments

Comments
 (0)