From 2c062e5bf9137f933d10a90bc391b51d28560809 Mon Sep 17 00:00:00 2001 From: Galorhallen Date: Wed, 5 Jan 2022 20:38:33 +0100 Subject: [PATCH] Woring on logging --- config/configuration.go | 15 ++++++++------- go.mod | 3 ++- go.sum | 3 +++ internal/metrics/metrics.go | 5 ++--- internal/pihole/client.go | 25 ++++++++++++++++++------- internal/server/server.go | 13 ++++++++----- main.go | 10 +++++++++- 7 files changed, 50 insertions(+), 24 deletions(-) diff --git a/config/configuration.go b/config/configuration.go index 7c03318..b829926 100644 --- a/config/configuration.go +++ b/config/configuration.go @@ -4,11 +4,12 @@ import ( "context" "errors" "fmt" - "log" "reflect" "strings" "time" + log "github.com/sirupsen/logrus" + "github.com/heetch/confita" "github.com/heetch/confita/backend" "github.com/heetch/confita/backend/env" @@ -172,25 +173,25 @@ func (c Config) PIHoleLoginURL() string { func (c EnvConfig) show() { val := reflect.ValueOf(&c).Elem() - log.Println("------------------------------------") - log.Println("- PI-Hole exporter configuration -") - log.Println("------------------------------------") + log.Info("------------------------------------") + log.Info("- PI-Hole exporter configuration -") + log.Info("------------------------------------") for i := 0; i < val.NumField(); i++ { valueField := val.Field(i) typeField := val.Type().Field(i) // Do not print password or api token but do print the authentication method if typeField.Name != "PIHolePassword" && typeField.Name != "PIHoleApiToken" { - log.Println(fmt.Sprintf("%s : %v", typeField.Name, valueField.Interface())) + log.Info(fmt.Sprintf("%s : %v", typeField.Name, valueField.Interface())) } else { showAuthenticationMethod(typeField.Name, valueField.Len()) } } - log.Println("------------------------------------") + log.Info("------------------------------------") } func showAuthenticationMethod(name string, length int) { if length > 0 { - log.Println(fmt.Sprintf("Pi-Hole Authentication Method : %s", name)) + log.Info(fmt.Sprintf("Pi-Hole Authentication Method : %s", name)) } } diff --git a/go.mod b/go.mod index 0d00d2a..bfdf246 100644 --- a/go.mod +++ b/go.mod @@ -5,8 +5,9 @@ go 1.15 require ( github.com/heetch/confita v0.10.0 github.com/prometheus/client_golang v1.11.0 + github.com/sirupsen/logrus v1.8.1 github.com/stretchr/testify v1.7.0 github.com/xonvanetta/shutdown v0.0.3 golang.org/x/net v0.0.0-20200625001655-4c5254603344 - golang.org/x/sync v0.0.0-20210220032951-036812b2e83c + golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect ) diff --git a/go.sum b/go.sum index 7164e67..ced6b40 100644 --- a/go.sum +++ b/go.sum @@ -195,6 +195,8 @@ github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg 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/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.1 h1:2vfRuCMp5sSVIDSqO8oNnWJq7mPa6KVP3iPIwFBuy8A= @@ -260,6 +262,7 @@ golang.org/x/sys v0.0.0-20190403152447-81d4e9dc473e/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190508220229-2d0786266e9c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/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= golang.org/x/sys v0.0.0-20200615200032-f1bc736245b1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= diff --git a/internal/metrics/metrics.go b/internal/metrics/metrics.go index 3a50a3a..c824713 100644 --- a/internal/metrics/metrics.go +++ b/internal/metrics/metrics.go @@ -1,9 +1,8 @@ package metrics import ( - "log" - "github.com/prometheus/client_golang/prometheus" + log "github.com/sirupsen/logrus" ) var ( @@ -201,5 +200,5 @@ func Init() { func initMetric(name string, metric *prometheus.GaugeVec) { prometheus.MustRegister(metric) - log.Printf("New Prometheus metric registered: %s", name) + log.Info("New Prometheus metric registered: ", name) } diff --git a/internal/pihole/client.go b/internal/pihole/client.go index f453a62..022be6b 100644 --- a/internal/pihole/client.go +++ b/internal/pihole/client.go @@ -4,7 +4,7 @@ import ( "encoding/json" "fmt" "io/ioutil" - "log" + "net" "net/http" "net/url" "os" @@ -12,6 +12,8 @@ import ( "strings" "time" + log "github.com/sirupsen/logrus" + "github.com/eko/pihole-exporter/config" "github.com/eko/pihole-exporter/internal/metrics" ) @@ -54,11 +56,18 @@ type Client struct { func NewClient(config *config.Config) *Client { err := config.Validate() if err != nil { - log.Print(err) + log.Error(err) os.Exit(1) } - fmt.Printf("Creating client with config %s\n", config) + log.Info("Creating client with config ", config) + + netTransport := &http.Transport{ + Dial: (&net.Dialer{ + Timeout: 5 * time.Second, + }).Dial, + TLSHandshakeTimeout: 5 * time.Second, + } return &Client{ config: config, @@ -66,6 +75,8 @@ func NewClient(config *config.Config) *Client { CheckRedirect: func(req *http.Request, via []*http.Request) error { return http.ErrUseLastResponse }, + Timeout: 10 * time.Second, + Transport: netTransport, }, Status: make(chan *ClientChannel, 1), } @@ -76,11 +87,11 @@ func (c *Client) String() string { } func (c *Client) CollectMetricsAsync(writer http.ResponseWriter, request *http.Request) { - log.Printf("Collecting from %s", c.config.PIHoleHostname) + log.Infof("Collecting from %s", c.config.PIHoleHostname) if stats, err := c.getStatistics(); err == nil { c.setMetrics(stats) c.Status <- &ClientChannel{Status: MetricsCollectionSuccess, Err: nil} - log.Printf("New tick of statistics from %s: %s", c.config.PIHoleHostname, stats) + log.Infof("New tick of statistics from %s: %s", c.config.PIHoleHostname, stats) } else { c.Status <- &ClientChannel{Status: MetricsCollectionError, Err: err} } @@ -92,7 +103,7 @@ func (c *Client) CollectMetrics(writer http.ResponseWriter, request *http.Reques return err } c.setMetrics(stats) - log.Printf("New tick of statistics from %s: %s", c.config.PIHoleHostname, stats) + log.Infof("New tick of statistics from %s: %s", c.config.PIHoleHostname, stats) return nil } @@ -157,7 +168,7 @@ func (c *Client) getPHPSessionID() (sessionID string) { resp, err := c.httpClient.Do(req) if err != nil { - log.Printf("An error has occured during login to PI-Hole: %v", err) + log.Error("An error has occured during login to PI-Hole: %v", err) } for _, cookie := range resp.Cookies() { diff --git a/internal/server/server.go b/internal/server/server.go index 3c5efd4..dadf250 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -2,7 +2,6 @@ package server import ( "fmt" - "log" "net/http" "strconv" "strings" @@ -10,6 +9,7 @@ import ( "github.com/eko/pihole-exporter/internal/pihole" "github.com/prometheus/client_golang/prometheus/promhttp" + log "github.com/sirupsen/logrus" "golang.org/x/net/context" ) @@ -32,14 +32,17 @@ func NewServer(port uint16, clients []*pihole.Client) *Server { } mux.HandleFunc("/metrics", func(writer http.ResponseWriter, request *http.Request) { - log.Printf("request.Header: %v\n", request.Header) - + log.Debug("request.Header: %v\n", request.Header) + for _, client := range clients { go client.CollectMetricsAsync(writer, request) } for _, client := range clients { - log.Printf("Received %s from %s\n", <-client.Status, client.GetHostname()) + status := <-client.Status + if status.Status == pihole.MetricsCollectionError { + log.Error("Received %s from %s\n", <-client.Status, client.GetHostname()) + } } promhttp.Handler().ServeHTTP(writer, request) @@ -76,7 +79,7 @@ func (s *Server) handleMetrics(clients []*pihole.Client) http.HandlerFunc { for _, client := range clients { if err := client.CollectMetrics(writer, request); err != nil { errors = append(errors, err.Error()) - fmt.Printf("Error %s\n", err) + fmt.Errorf("Error %s\n", err) } } diff --git a/main.go b/main.go index 8bee1a8..6e20ef9 100644 --- a/main.go +++ b/main.go @@ -2,7 +2,10 @@ package main import ( "fmt" - "log" + + "time" + + log "github.com/sirupsen/logrus" "github.com/eko/pihole-exporter/config" "github.com/eko/pihole-exporter/internal/metrics" @@ -12,6 +15,11 @@ import ( ) func main() { + log.SetFormatter(&log.TextFormatter{ + DisableColors: false, + FullTimestamp: true, + TimestampFormat: time.RFC3339, + }) envConf, clientConfigs, err := config.Load() if err != nil { log.Fatal(err.Error())