Skip to content

Commit

Permalink
✨ improve logging (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
charlie-haley authored Feb 2, 2023
1 parent f28a4a4 commit 111d317
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 27 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ GLOBAL OPTIONS:
--password value Password for your Omada user. [$OMADA_PASS]
--port value Port on which to expose the Prometheus metrics. (default: "9202") [$OMADA_PORT]
--site value Omada site to scrape metrics from. (default: "Default") [$OMADA_SITE]
--log-level value Application log level. (default: "error") [$LOG_LEVEL]
--timeout value Timeout when making requests to the Omada Controller. (default: 15) [$OMADA_REQUEST_TIMEOUT]
--insecure Whether to skip verifying the SSL certificate on the controller. (default: false) [$OMADA_INSECURE]
--disable-go-collector Disable Go collector metrics. (default: true) [$OMADA_DISABLE_GO_COLLECTOR]
Expand All @@ -95,6 +96,7 @@ OMADA_INSECURE | Whether to skip verifying the SSL certificate on the
OMADA_REQUEST_TIMEOUT | Timeout when making requests to the Omada Controller. (default: 15)
OMADA_DISABLE_GO_COLLECTOR | Disable Go collector metrics. (default: true)
OMADA_DISABLE_PROCESS_COLLECTOR | Disable process collector metrics. (default: true)
LOG_LEVEL | Application log level. (default: "error")

### Helm
```
Expand Down
15 changes: 12 additions & 3 deletions cmd/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import (
"github.com/charlie-haley/omada_exporter/pkg/config"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
log "github.com/sirupsen/logrus"
zerolog "github.com/rs/zerolog"
log "github.com/rs/zerolog/log"
"github.com/urfave/cli/v2"
)

Expand All @@ -34,6 +35,7 @@ func Run() {
&cli.StringFlag{Destination: &conf.Password, Required: true, Name: "password", Value: "", Usage: "Password for your Omada user.", EnvVars: []string{"OMADA_PASS"}},
&cli.StringFlag{Destination: &conf.Port, Name: "port", Value: "9202", Usage: "Port on which to expose the Prometheus metrics.", EnvVars: []string{"OMADA_PORT"}},
&cli.StringFlag{Destination: &conf.Site, Name: "site", Value: "Default", Usage: "Omada site to scrape metrics from.", EnvVars: []string{"OMADA_SITE"}},
&cli.StringFlag{Destination: &conf.LogLevel, Name: "log-level", Value: "error", Usage: "Application log level.", EnvVars: []string{"LOG_LEVEL"}},
&cli.IntFlag{Destination: &conf.Timeout, Name: "timeout", Value: 15, Usage: "Timeout when making requests to the Omada Controller.", EnvVars: []string{"OMADA_REQUEST_TIMEOUT"}},
&cli.BoolFlag{Destination: &conf.Insecure, Name: "insecure", Value: false, Usage: "Whether to skip verifying the SSL certificate on the controller.", EnvVars: []string{"OMADA_INSECURE"}},
&cli.BoolFlag{Destination: &conf.GoCollectorDisabled, Name: "disable-go-collector", Value: true, Usage: "Disable Go collector metrics.", EnvVars: []string{"OMADA_DISABLE_GO_COLLECTOR"}},
Expand All @@ -51,12 +53,19 @@ func Run() {

err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
log.Fatal().Err(err).Msg("App failed to run")
os.Exit(1)
}
}

func run(c *cli.Context) error {
// set log level
level, err := zerolog.ParseLevel(conf.LogLevel)
if err != nil {
return err
}
zerolog.SetGlobalLevel(level)

if conf.GoCollectorDisabled {
// remove Go collector
prometheus.Unregister(prometheus.NewGoCollector())
Expand Down Expand Up @@ -84,7 +93,7 @@ func run(c *cli.Context) error {
prometheus.MustRegister(collector.NewDeviceCollector(client))
prometheus.MustRegister(collector.NewPortCollector(client))

log.Info(fmt.Sprintf("listening on :%s", conf.Port))
log.Info().Msg(fmt.Sprintf("listening on :%s", conf.Port))
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte(`<html>
<head>
Expand Down
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ go 1.19
require (
github.com/prometheus/client_golang v1.9.0
github.com/rs/zerolog v1.28.0
github.com/sirupsen/logrus v1.8.1
github.com/urfave/cli/v2 v2.3.0
)

Expand All @@ -22,7 +21,6 @@ require (
github.com/prometheus/procfs v0.2.0 // indirect
github.com/russross/blackfriday/v2 v2.0.1 // indirect
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
github.com/stretchr/testify v1.7.0 // indirect
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6 // indirect
google.golang.org/protobuf v1.26.0 // indirect
)
8 changes: 0 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSY
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/dustin/go-humanize v0.0.0-20171111073723-bb3d318650d4/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
Expand Down Expand Up @@ -255,8 +254,6 @@ github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeV
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88=
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM=
Expand All @@ -271,8 +268,6 @@ github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
Expand Down Expand Up @@ -345,7 +340,6 @@ golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20190502145724-3ef323f4f1fd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190726091711-fc99dfbffb4e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190826190057-c7b8b68b1456/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191220142924-d4481acd189f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200106162015-b016eb3dc98e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down Expand Up @@ -421,8 +415,6 @@ gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.5/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
honnef.co/go/tools v0.0.0-20180728063816-88497007e858/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
Expand Down
7 changes: 4 additions & 3 deletions pkg/api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"io"
"net/http"

log "github.com/sirupsen/logrus"
log "github.com/rs/zerolog/log"
)

// gets clients by switch mac address
Expand Down Expand Up @@ -40,10 +40,10 @@ func (c *Client) getClientsWithFilters(filtersEnabled bool, mac string) ([]Netwo
return nil, err
}
if !loggedIn {
log.Info(fmt.Sprintf("not logged in, logging in with user: %s", c.Config.Username))
log.Info().Msg(fmt.Sprintf("not logged in, logging in with user: %s", c.Config.Username))
err := c.Login()
if err != nil || c.token == "" {
log.Error(fmt.Sprintf("Failed to login: %s", err))
log.Error().Err(err).Msg("Failed to login")
return nil, err
}
}
Expand Down Expand Up @@ -75,6 +75,7 @@ func (c *Client) getClientsWithFilters(filtersEnabled bool, mac string) ([]Netwo
if err != nil {
return nil, err
}
log.Debug().Bytes("data", body).Msg("Received data from clients endpoint")

clientdata := clientResponse{}
err = json.Unmarshal(body, &clientdata)
Expand Down
7 changes: 4 additions & 3 deletions pkg/api/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"io"
"net/http"

log "github.com/sirupsen/logrus"
log "github.com/rs/zerolog/log"
)

func (c *Client) GetController() (*Controller, error) {
Expand All @@ -15,10 +15,10 @@ func (c *Client) GetController() (*Controller, error) {
return nil, err
}
if !loggedIn {
log.Info(fmt.Errorf("not logged in, logging in with user: %s", c.Config.Username))
log.Info().Msg(fmt.Sprintf("not logged in, logging in with user: %s", c.Config.Username))
err := c.Login()
if err != nil || c.token == "" {
log.Error(fmt.Errorf("failed to login: %s", err))
log.Error().Err(err).Msg("failed to login")
return nil, err
}
}
Expand All @@ -40,6 +40,7 @@ func (c *Client) GetController() (*Controller, error) {
if err != nil {
return nil, err
}
log.Debug().Bytes("data", body).Msg("Received data from controllerStatus endpoint")

controllerData := controllerResponse{}
err = json.Unmarshal(body, &controllerData)
Expand Down
7 changes: 4 additions & 3 deletions pkg/api/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"io"
"net/http"

log "github.com/sirupsen/logrus"
log "github.com/rs/zerolog/log"
)

func (c *Client) GetDevices() ([]Device, error) {
Expand All @@ -15,10 +15,10 @@ func (c *Client) GetDevices() ([]Device, error) {
return nil, err
}
if !loggedIn {
log.Info(fmt.Errorf("not logged in, logging in with user: %s", c.Config.Username))
log.Info().Msg(fmt.Sprintf("not logged in, logging in with user: %s", c.Config.Username))
err := c.Login()
if err != nil || c.token == "" {
log.Error(fmt.Errorf("failed to login: %s", err))
log.Error().Err(err).Msg("failed to login")
return nil, err
}
}
Expand All @@ -40,6 +40,7 @@ func (c *Client) GetDevices() ([]Device, error) {
if err != nil {
return nil, err
}
log.Debug().Bytes("data", body).Msg("Received data from devices endpoint")

devicedata := deviceResponse{}
err = json.Unmarshal(body, &devicedata)
Expand Down
7 changes: 4 additions & 3 deletions pkg/api/port.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"io"
"net/http"

log "github.com/sirupsen/logrus"
log "github.com/rs/zerolog/log"
)

func (c *Client) GetPorts(switchMac string) ([]Port, error) {
Expand All @@ -15,10 +15,10 @@ func (c *Client) GetPorts(switchMac string) ([]Port, error) {
return nil, err
}
if !loggedIn {
log.Info(fmt.Errorf("not logged in, logging in with user: %s", c.Config.Username))
log.Info().Msg(fmt.Sprintf("not logged in, logging in with user: %s", c.Config.Username))
err := c.Login()
if err != nil || c.token == "" {
log.Error(fmt.Errorf("failed to login: %s", err))
log.Error().Err(err).Msg("failed to login")
return nil, err
}
}
Expand All @@ -40,6 +40,7 @@ func (c *Client) GetPorts(switchMac string) ([]Port, error) {
if err != nil {
return nil, err
}
log.Debug().Bytes("data", body).Msg("Received data from ports endpoint")

portdata := portResponse{}
err = json.Unmarshal(body, &portdata)
Expand Down
4 changes: 2 additions & 2 deletions pkg/api/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"io"
"net/http"

log "github.com/sirupsen/logrus"
log "github.com/rs/zerolog/log"
)

// there's no nice way of fetching the site ID from the `Viewer` role
Expand All @@ -17,7 +17,7 @@ func (c *Client) getSiteId(name string) (*string, error) {
return nil, err
}
if !loggedIn {
log.Info(fmt.Errorf("not logged in, logging in with user: %s", c.Config.Username))
log.Info().Msg(fmt.Sprintf("not logged in, logging in with user: %s", c.Config.Username))
err := c.Login()
if err != nil || c.token == "" {
return nil, fmt.Errorf("failed to login: %s", err)
Expand Down
1 change: 1 addition & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ type Config struct {
Password string
Port string
Site string
LogLevel string
Timeout int
Insecure bool
GoCollectorDisabled bool
Expand Down

0 comments on commit 111d317

Please sign in to comment.