Woring on logging

This commit is contained in:
Galorhallen 2022-01-05 20:38:33 +01:00
parent 7009c705bb
commit 2c062e5bf9
7 changed files with 50 additions and 24 deletions

View file

@ -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))
}
}

3
go.mod
View file

@ -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
)

3
go.sum
View file

@ -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=

View file

@ -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)
}

View file

@ -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() {

View file

@ -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)
}
}

10
main.go
View file

@ -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())