Skip to content

Commit

Permalink
Convert to go slog and add logging parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
DRuggeri committed Jan 6, 2025
1 parent a961854 commit bf7bdc2
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 440 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ Several flags are available to customize how the exporter works. Note that none
**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.

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

Expand Down
9 changes: 5 additions & 4 deletions collectors/clients_collector.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package collectors

import (
"github.com/DRuggeri/netgear_client"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/log"
"log/slog"

Check failure on line 4 in collectors/clients_collector.go

View workflow job for this annotation

GitHub Actions / Test (1.18.x, ubuntu-latest)

package log/slog is not in GOROOT (/opt/hostedtoolcache/go/1.18.10/x64/src/log/slog)
"strconv"
"time"

"github.com/DRuggeri/netgear_client"
"github.com/prometheus/client_golang/prometheus"
)

type ClientCollector struct {
Expand Down Expand Up @@ -119,7 +120,7 @@ func (c *ClientCollector) Collect(ch chan<- prometheus.Metric) {
errorMetric := float64(0)
clients, err := c.client.GetAttachDevice()
if err != nil {
log.Errorf("Error while collecting client statistics: %v", err)
slog.Error("error while collecting client statistics: %v", slog.String("error", err.Error()))
errorMetric = float64(1)
c.scrapeErrorsTotalMetric.Inc()
} else {
Expand Down
11 changes: 6 additions & 5 deletions collectors/system_info_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package collectors

import (
"fmt"
"github.com/DRuggeri/netgear_client"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/log"
"log/slog"
"strconv"
"strings"
"time"

"github.com/DRuggeri/netgear_client"
"github.com/prometheus/client_golang/prometheus"
)

type SystemInfo struct {
Expand Down Expand Up @@ -107,7 +108,7 @@ func (c *SystemInfo) Collect(ch chan<- prometheus.Metric) {
errorMetric := float64(0)
stats, err := c.client.GetSystemInfo()
if err != nil {
log.Errorf("Error while collecting system info: %v", err)
slog.Error("error while collecting system info: %v", slog.String("error", err.Error()))
errorMetric = float64(1)
c.scrapeErrorsTotalMetric.Inc()
} else {
Expand All @@ -121,7 +122,7 @@ func (c *SystemInfo) Collect(ch chan<- prometheus.Metric) {
c.metrics[name].Set(metric)
c.metrics[name].Collect(ch)
} else {
log.Warnf("System info stat named '%s' missing from results!", name)
slog.Warn(fmt.Sprintf("system info stat named '%s' missing from results!", name))
}
}
}
Expand Down
11 changes: 6 additions & 5 deletions collectors/traffic_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package collectors

import (
"fmt"
"github.com/DRuggeri/netgear_client"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/log"
"log/slog"
"strconv"
"strings"
"time"

"github.com/DRuggeri/netgear_client"
"github.com/prometheus/client_golang/prometheus"
)

type TrafficCollector struct {
Expand Down Expand Up @@ -123,7 +124,7 @@ func (c *TrafficCollector) Collect(ch chan<- prometheus.Metric) {
errorMetric := float64(0)
stats, err := c.client.GetTrafficMeterStatistics()
if err != nil {
log.Errorf("Error while collecting traffic statistics: %v", err)
slog.Error("error while collecting traffic statistics", slog.String("error", err.Error()))
errorMetric = float64(1)
c.trafficScrapeErrorsTotalMetric.Inc()
} else {
Expand All @@ -146,7 +147,7 @@ func (c *TrafficCollector) Collect(ch chan<- prometheus.Metric) {
c.metrics[name].Set(metric)
c.metrics[name].Collect(ch)
} else {
log.Warnf("Traffic stat named '%s' missing from results!", name)
slog.Warn(fmt.Sprintf("traffic stat named '%s' missing from results!", name))
}
}
}
Expand Down
23 changes: 18 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
module github.com/DRuggeri/netgear_exporter

go 1.14
go 1.23

require (
github.com/DRuggeri/netgear_client v0.0.0-20190218174711-5c01ae5fd546
github.com/prometheus/client_golang v1.9.0
github.com/prometheus/common v0.15.0
gopkg.in/alecthomas/kingpin.v2 v2.2.6
github.com/DRuggeri/netgear_client v0.0.0-20230219193432-22cf2da4d7d4
github.com/alecthomas/kingpin v2.2.6+incompatible
github.com/prometheus/client_golang v1.20.5
)

require (
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 // indirect
github.com/alecthomas/units v0.0.0-20240927000941-0f3dac36c52b // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/klauspost/compress v1.17.11 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.61.0 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
golang.org/x/sys v0.29.0 // indirect
google.golang.org/protobuf v1.36.1 // indirect
)
Loading

0 comments on commit bf7bdc2

Please sign in to comment.