Skip to content

Commit

Permalink
feat: replace zerolog with slog (#3146)
Browse files Browse the repository at this point in the history
* feat: replace zerolog with slog

* ci: use the slog linter

* refactor: improve logging when recovering
  • Loading branch information
cgrinds authored Sep 12, 2024
1 parent 7b0c965 commit 675d361
Show file tree
Hide file tree
Showing 26 changed files with 196 additions and 187 deletions.
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ linters:
- prealloc
- reassign
- revive
- sloglint
- staticcheck
- stylecheck
- tenv
Expand Down
2 changes: 0 additions & 2 deletions cmd/admin/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"errors"
"fmt"
"github.com/netapp/harvest/v2/pkg/conf"
"github.com/netapp/harvest/v2/pkg/logging"
"github.com/netapp/harvest/v2/pkg/util"
"github.com/rs/zerolog"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -111,7 +110,6 @@ func (a *Admin) APISD(w http.ResponseWriter, r *http.Request) {

func (a *Admin) setupLogger() {
zerolog.SetGlobalLevel(zerolog.InfoLevel)
zerolog.ErrorStackMarshaler = logging.MarshalStack //nolint:reassign

a.logger = zerolog.New(zerolog.ConsoleWriter{Out: os.Stderr}).
With().Caller().Timestamp().Logger()
Expand Down
2 changes: 1 addition & 1 deletion cmd/collectors/restperf/plugins/nic/nic.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (n *Nic) Init() error {

timeout, _ := time.ParseDuration(rest.DefaultTimeout)
if n.client, err = rest.New(conf.ZapiPoller(n.ParentParams), timeout, n.Auth); err != nil {
n.Logger.Error().Stack().Err(err).Msg("connecting")
n.Logger.Error().Err(err).Msg("connecting")
return err
}

Expand Down
5 changes: 4 additions & 1 deletion cmd/poller/collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package collector

import (
"errors"
"fmt"
"github.com/netapp/harvest/v2/pkg/auth"
"github.com/netapp/harvest/v2/pkg/conf"
"github.com/netapp/harvest/v2/pkg/logging"
Expand All @@ -26,6 +27,7 @@ import (
"math"
"math/rand"
"reflect"
"runtime/debug"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -304,7 +306,8 @@ func (c *AbstractCollector) Start(wg *sync.WaitGroup) {
defer wg.Done()
defer func() {
if r := recover(); r != nil {
c.Logger.Error().Stack().Err(errs.New(errs.ErrPanic, "")).Any("err", r).Msg("Collector panicked")
err := fmt.Sprintf("%+v\n", r)
c.Logger.Error().Str("err", err).Bytes("stack", debug.Stack()).Msg("Collector panicked")
}
}()

Expand Down
9 changes: 0 additions & 9 deletions cmd/poller/poller.go
Original file line number Diff line number Diff line change
Expand Up @@ -1404,14 +1404,5 @@ func init() {

// start poller, if fails try to write to syslog
func main() {
// don't recover if a goroutine has panicked, instead
// log as much as possible
defer func() {
if r := recover(); r != nil {
logger.Error().Stack().Any("err", r).Msg("Poller panicked")
logger.Fatal().Msg(`(main) terminating abnormally, tip: run in foreground mode (with "--loglevel 0") to debug`)
}
}()

cobra.CheckErr(pollerCmd.Execute())
}
46 changes: 24 additions & 22 deletions integration/certer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/Netapp/harvest-automation/certer/models"
"github.com/Netapp/harvest-automation/test/utils"
"github.com/carlmjohnson/requests"
"github.com/rs/zerolog/log"
"log/slog"
"net/http"
"os"
"os/exec"
Expand Down Expand Up @@ -64,15 +64,15 @@ func printRequired(name string) {
}

func begin() {
log.Info().Str("ip", ip).Msg("Create certificates for ip")
slog.Info("Create certificates for ip", slog.String("ip", ip))

// Get admin SVM
fetchAdminSVM()

// Query for existing CA
certificates, err := fetchCA()
if err != nil {
log.Error().Err(err).Send()
slog.Error("", slog.Any("err", err))
return
}

Expand All @@ -84,41 +84,42 @@ func begin() {
// Create private key and certificate signing request (CSR)
csr, err := ensureOpenSSLInstalled()
if err != nil {
log.Error().Err(err).Send()
slog.Error("", slog.Any("err", err))
return
}

// Delete existing
if certificates.NumRecords > 0 {
log.Info().
Int("num", certificates.NumRecords).
Str("common_name", commonName).
Msg("Deleting matching certificates")
slog.Info("Deleting matching certificates",
slog.Int("num", certificates.NumRecords),
slog.String("common_name", commonName))

err := deleteCertificates(certificates)
if err != nil {
log.Error().Err(err).Msg("failed to delete certificates")
slog.Error("failed to delete certificates", slog.Any("err", err))
return
}
}

// Create a root CA certificate that will be used to sign certificate requests for the user account(s)
err = createRootCA()
if err != nil {
log.Error().Err(err).Msg("failed")
slog.Error("failed", slog.Any("err", err))
return
}

// Sign the locally created certificate with the root CA generated above
err = signCSR(csr)
if err != nil {
log.Error().Err(err).Msg("failed")
slog.Error("failed", slog.Any("err", err))
return
}

// Add certificate auth to this ONTAP user
err = addCertificateAuthToHarvestUser()
if err != nil {
log.Error().Err(err).Send()
slog.Error("", slog.Any("err", err))

}

fmt.Printf("Success! Test with:\n")
Expand All @@ -130,15 +131,16 @@ func begin() {
func sleep(s string) {
duration, err := time.ParseDuration(s)
if err != nil {
log.Error().Err(err).Msg("failed to sleep")
slog.Error("failed to sleep", slog.Any("err", err))
}
log.Info().Str("sleep", s).Msg("sleep")
slog.Info("sleep", slog.String("sleep", s))
time.Sleep(duration)
}

func curlServer() {
if _, err := os.Stat(local(".crt")); errors.Is(err, os.ErrNotExist) {
log.Panic().Str("crt", local(".crt")).Msg("does not exist")
slog.Error("does not exist", slog.String("crt", local(".crt")))
os.Exit(1)
}

for range 60 {
Expand All @@ -147,7 +149,7 @@ func curlServer() {
fmt.Sprintf("https://%s/api/cluster?fields=version", ip))
output, err := command.CombinedOutput()
if err != nil {
log.Error().Err(err).Str("output", string(output)).Msg("failed to exec curl")
slog.Error("failed to exec curl", slog.Any("err", err), slog.String("output", string(output)))
} else {
fmt.Println(string(output))
return
Expand All @@ -159,7 +161,7 @@ func curlServer() {
func certsAreFresh(certificates models.Certificates) bool {
cert := certificates.Records[0]
date := cert.ExpiryTime.Format("2006-01-02")
log.Info().Str("expire", date).Msg("Certificates are fresh. Done")
slog.Info("Certificates are fresh. Done", slog.String("expire", date))
return cert.ExpiryTime.After(time.Now().Add(8 * time.Hour))
}

Expand Down Expand Up @@ -271,7 +273,7 @@ func ensureOpenSSLInstalled() (string, error) {
if err != nil {
return "", fmt.Errorf("err=%w output=%s", err, output)
}
log.Debug().Str("output", string(output)).Msg("created private key")
slog.Debug("created private key", slog.String("output", string(output)))
// openssl req -days 3650 -sha256 -new -nodes -key cert/u2.key -subj /CN=harvest -out u2.csr

command = exec.Command("openssl", "req", "-days", "3650", "-sha256", "-new", "-nodes", "-key", privateKey,
Expand All @@ -281,8 +283,8 @@ func ensureOpenSSLInstalled() (string, error) {
return "", fmt.Errorf("error creating csr err=%w output=%s", err, output)
}

log.Debug().Str("output", string(output)).Msg("created csr")
log.Info().Str("privateKey", privateKey).Msg("Created private key and certificate signing request (CSR)")
slog.Debug("created csr", slog.String("output", string(output)))
slog.Info("Created private key and certificate signing request (CSR)", slog.String("privateKey", privateKey))

data, err := os.ReadFile(csr)
if err != nil {
Expand All @@ -308,7 +310,7 @@ func createRootCA() error {
if err != nil {
return fmt.Errorf("failed to create root CA err=%w", err)
}
log.Info().Msg("Created Root CA")
slog.Info("Created Root CA")
return nil
}

Expand All @@ -321,7 +323,7 @@ func fetchAdminSVM() {
ToJSON(&svmResp).
Fetch(context.Background())
if err != nil {
log.Error().Err(err).Msg("failed to fetch admin SVM")
slog.Error("failed to fetch admin SVM", slog.Any("err", err))
return
}
adminSVM = svmResp.Records[0].Vserver
Expand Down
8 changes: 4 additions & 4 deletions integration/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ replace github.com/netapp/harvest/v2 => ../
require (
github.com/carlmjohnson/requests v0.23.5
github.com/netapp/harvest/v2 v2.0.0-20240618123731-00a4fa826af5
github.com/rs/zerolog v1.33.0
github.com/tidwall/gjson v1.17.3
golang.org/x/text v0.17.0
golang.org/x/text v0.18.0
)

require (
Expand All @@ -28,7 +27,8 @@ require (
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/shirou/gopsutil/v4 v4.24.7 // indirect
github.com/rs/zerolog v1.33.0 // indirect
github.com/shirou/gopsutil/v4 v4.24.8 // indirect
github.com/shoenig/go-m1cpu v0.1.6 // indirect
github.com/spf13/cobra v1.8.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
Expand All @@ -38,7 +38,7 @@ require (
github.com/tklauser/numcpus v0.8.0 // indirect
github.com/yusufpapurcu/wmi v1.2.4 // indirect
golang.org/x/net v0.26.0 // indirect
golang.org/x/sys v0.24.0 // indirect
golang.org/x/sys v0.25.0 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
12 changes: 6 additions & 6 deletions integration/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8=
github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/shirou/gopsutil/v4 v4.24.7 h1:V9UGTK4gQ8HvcnPKf6Zt3XHyQq/peaekfxpJ2HSocJk=
github.com/shirou/gopsutil/v4 v4.24.7/go.mod h1:0uW/073rP7FYLOkvxolUQM5rMOLTNmRXnFKafpb71rw=
github.com/shirou/gopsutil/v4 v4.24.8 h1:pVQjIenQkIhqO81mwTaXjTzOMT7d3TZkf43PlVFHENI=
github.com/shirou/gopsutil/v4 v4.24.8/go.mod h1:wE0OrJtj4dG+hYkxqDH3QiBICdKSf04/npcvLLc/oRg=
github.com/shoenig/go-m1cpu v0.1.6 h1:nxdKQNcEB6vzgA2E2bvzKIYRuNj7XNJ4S/aRSwKzFtM=
github.com/shoenig/go-m1cpu v0.1.6/go.mod h1:1JJMcUBvfNwpq05QDQVAnx3gUHr9IYF7GNg9SUEw2VQ=
github.com/shoenig/test v0.6.4 h1:kVTaSd7WLz5WZ2IaoM0RSzRsUD+m8wRR+5qvntpn4LU=
Expand Down Expand Up @@ -82,10 +82,10 @@ golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg=
golang.org/x/sys v0.24.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc=
golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34=
golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.18.0 h1:XvMDiNzPAl0jr17s6W9lcaIhGUfUORdGCNsuLmPG224=
golang.org/x/text v0.18.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
Expand Down
9 changes: 7 additions & 2 deletions integration/test/copy_logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/Netapp/harvest-automation/test/docker"
"github.com/Netapp/harvest-automation/test/installer"
"github.com/Netapp/harvest-automation/test/utils"
"github.com/rs/zerolog/log"
"log/slog"
"os/exec"
"testing"
)
Expand All @@ -26,7 +26,12 @@ func TestCopyLogs(t *testing.T) {
dest := harvestLogDir + "/" + containerShortID + ".log"
err = docker.StoreContainerLog(containerShortID, dest)
if err != nil {
log.Error().Err(err).Str("id", containerShortID).Str("dest", dest).Msg("Unable to copy logs")
slog.Error(
"Unable to copy logs",
slog.Any("err", err),
slog.String("id", containerShortID),
slog.String("dest", dest),
)
}
}
}
Expand Down
30 changes: 21 additions & 9 deletions integration/test/counter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/netapp/harvest/v2/pkg/tree/node"
"github.com/netapp/harvest/v2/pkg/util"
"github.com/netapp/harvest/v2/third_party/go-version"
"github.com/rs/zerolog/log"
"log/slog"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -46,29 +46,39 @@ func TestCounters(t *testing.T) {
utils.SkipIfMissing(t, utils.Regression)
_, err := conf.LoadHarvestConfig(installer.HarvestConfigFile)
if err != nil {
log.Fatal().Err(err).Msg("Unable to load harvest config")
slog.Error("Unable to load harvest config", slog.Any("err", err))
os.Exit(1)
}

pollerName := "dc1"
if poller, err = conf.PollerNamed(pollerName); err != nil {
log.Fatal().Err(err).Str("poller", pollerName).Send()
slog.Error("", slog.Any("err", err), slog.String("poller", pollerName))
os.Exit(1)
}
if poller.Addr == "" {
log.Fatal().Str("poller", pollerName).Msg("Address is empty")
slog.Error("Address is empty", slog.String("poller", pollerName))
os.Exit(1)
}
timeout, _ := time.ParseDuration(rest2.DefaultTimeout)

if client, err = rest2.New(poller, timeout, auth.NewCredentials(poller, logging.Get())); err != nil {
log.Fatal().Err(err).Str("poller", pollerName).Msg("error creating new client")
slog.Error(
"error creating new client",
slog.Any("err", err),
slog.String("poller", pollerName),
)
os.Exit(1)
}

if err = client.Init(5); err != nil {
log.Fatal().Err(err).Msg("client init failed")
slog.Error("client init failed", slog.Any("err", err))
os.Exit(1)
}

restCounters := processRestCounters(client)
if err = invokeRestCall(client, restCounters); err != nil {
log.Error().Err(err).Msg("rest call failed")
slog.Error("rest call failed", slog.Any("err", err))
os.Exit(1)
}

}
Expand Down Expand Up @@ -109,7 +119,8 @@ func visitRestTemplates(dir string, client *rest2.Client, eachTemp func(path str
result := make(map[string][]counterData)
err := filepath.Walk(dir, func(path string, _ os.FileInfo, err error) error {
if err != nil {
log.Fatal().Err(err).Msg("failed to read directory:")
slog.Error("failed to read directory:", slog.Any("err", err))
os.Exit(1)
}
ext := filepath.Ext(path)
if ext != ".yaml" {
Expand All @@ -131,7 +142,8 @@ func visitRestTemplates(dir string, client *rest2.Client, eachTemp func(path str
})

if err != nil {
log.Fatal().Err(err).Str("dir", dir).Msg("failed to walk directory: %s")
slog.Error("failed to walk directory", slog.Any("err", err), slog.String("dir", dir))
os.Exit(1)
}

return result
Expand Down
Loading

0 comments on commit 675d361

Please sign in to comment.