Skip to content

Commit

Permalink
refactor: improve slog error logging
Browse files Browse the repository at this point in the history
  • Loading branch information
cgrinds committed Oct 7, 2024
1 parent 3fafe47 commit 563871f
Show file tree
Hide file tree
Showing 90 changed files with 576 additions and 508 deletions.
13 changes: 7 additions & 6 deletions cmd/admin/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"errors"
"fmt"
"github.com/netapp/harvest/v2/pkg/conf"
"github.com/netapp/harvest/v2/pkg/slogx"
"github.com/netapp/harvest/v2/pkg/util"
"github.com/spf13/cobra"
"github.com/zekroTJA/timedmap/v2"
Expand Down Expand Up @@ -55,7 +56,7 @@ func (a *Admin) startServer() {
defer cancel()

if err := server.Shutdown(ctx); err != nil {
a.logger.Error("Could not gracefully shutdown the admin node", slog.Any("err", err))
a.logger.Error("Could not gracefully shutdown the admin node", slogx.Err(err))
os.Exit(1)
}
close(done)
Expand All @@ -72,7 +73,7 @@ func (a *Admin) startServer() {
if err := server.ListenAndServeTLS(a.httpSD.TLS.CertFile, a.httpSD.TLS.KeyFile); err != nil && !errors.Is(err, http.ErrServerClosed) {
a.logger.Error(
"Admin node could not listen",
slog.Any("err", err),
slogx.Err(err),
slog.String("listen", a.listen),
slog.String("ssl_cert", a.httpSD.TLS.CertFile),
slog.String("ssl_key", a.httpSD.TLS.KeyFile),
Expand All @@ -84,7 +85,7 @@ func (a *Admin) startServer() {
if err := server.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
a.logger.Error(
"Admin node could not listen",
slog.Any("err", err),
slogx.Err(err),
slog.String("listen", a.listen),
)
os.Exit(1)
Expand Down Expand Up @@ -144,7 +145,7 @@ func (a *Admin) apiPublish(w http.ResponseWriter, r *http.Request) {
var publish pollerDetails
err := decoder.Decode(&publish)
if err != nil {
a.logger.Error("Unable to parse publish json", slog.Any("err", err))
a.logger.Error("Unable to parse publish json", slogx.Err(err))
w.WriteHeader(http.StatusBadRequest)
return
}
Expand Down Expand Up @@ -174,7 +175,7 @@ func (a *Admin) makeTargets() []byte {
a.logger.Debug("makeTargets", slog.Int("size", len(targets)))
j, err := json.Marshal(targets)
if err != nil {
a.logger.Error("Failed to marshal targets", slog.Any("err", err))
a.logger.Error("Failed to marshal targets", slogx.Err(err))
return []byte{}
}
return j
Expand Down Expand Up @@ -282,7 +283,7 @@ func (a *Admin) setDuration(every string, defaultDur time.Duration, name string)
if err != nil {
a.logger.Warn(
"Failed to parse name",
slog.Any("err", err),
slogx.Err(err),
slog.String(name, every),
slog.String("defaultDur", defaultDur.String()),
)
Expand Down
25 changes: 13 additions & 12 deletions cmd/collectors/commonutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/netapp/harvest/v2/cmd/tools/rest"
"github.com/netapp/harvest/v2/pkg/errs"
"github.com/netapp/harvest/v2/pkg/matrix"
"github.com/netapp/harvest/v2/pkg/slogx"
"github.com/netapp/harvest/v2/pkg/tree/node"
"github.com/netapp/harvest/v2/pkg/util"
"github.com/tidwall/gjson"
Expand Down Expand Up @@ -76,7 +77,7 @@ func InvokeRestCall(client *rest.Client, href string, logger *slog.Logger) ([]gj
if err != nil {
logger.Error(
"Failed to fetch data",
slog.Any("err", err),
slogx.Err(err),
slog.String("href", href),
slog.Int("hrefLength", len(href)),
)
Expand Down Expand Up @@ -121,7 +122,7 @@ func GetClusterTime(client *rest.Client, returnTimeOut *int, logger *slog.Logger
if err != nil {
logger.Error(
"Failed to load cluster date",
slog.Any("err", err),
slogx.Err(err),
slog.String("date", currentClusterDate.String()),
)
continue
Expand Down Expand Up @@ -259,7 +260,7 @@ func UpdateLagTime(instance *matrix.Instance, lastTransferSize *matrix.Metric, l
if lastBytes, ok := lastTransferSize.GetValueFloat64(instance); ok {
if healthy == "true" && schedule != "" && lastError == "" && lastBytes == 0 {
if err := lagTime.SetValueFloat64(instance, 0); err != nil {
logger.Error("Unable to set value on metric", slog.Any("err", err), slog.String("metric", lagTime.GetName()))
logger.Error("Unable to set value on metric", slogx.Err(err), slog.String("metric", lagTime.GetName()))
}
}
}
Expand Down Expand Up @@ -402,12 +403,12 @@ func AggregatePerScanner(logger *slog.Logger, data *matrix.Matrix, latencyKey st
if value != 0 {
err = tempOps.SetValueFloat64(ps, tempOpsV+opsValue)
if err != nil {
logger.Error("error", slog.Any("err", err))
logger.Error("error", slogx.Err(err))
}
}
err = psm.SetValueFloat64(ps, fv+prod)
if err != nil {
logger.Error("error", slog.Any("err", err))
logger.Error("error", slogx.Err(err))
}
}
}
Expand All @@ -421,7 +422,7 @@ func AggregatePerScanner(logger *slog.Logger, data *matrix.Matrix, latencyKey st
if err != nil {
logger.Error(
"Error setting metric value",
slog.Any("err", err),
slogx.Err(err),
slog.String("metric", "scan_request_dispatched_rate"),
)
}
Expand All @@ -434,7 +435,7 @@ func AggregatePerScanner(logger *slog.Logger, data *matrix.Matrix, latencyKey st
value, _ := m.GetValueFloat64(ps)
err := psm.SetValueFloat64(ps, runningTotal+value)
if err != nil {
logger.Error("Failed to set value", slog.Any("err", err), slog.String("mKey", mKey))
logger.Error("Failed to set value", slogx.Err(err), slog.String("mKey", mKey))
}
}
}
Expand All @@ -456,7 +457,7 @@ func AggregatePerScanner(logger *slog.Logger, data *matrix.Matrix, latencyKey st
if err := m.SetValueFloat64(i, value/float64(count)); err != nil {
logger.Error(
"Unable to set average",
slog.Any("err", err),
slogx.Err(err),
slog.String("mKey", mKey),
slog.String("name", m.GetName()),
)
Expand All @@ -469,7 +470,7 @@ func AggregatePerScanner(logger *slog.Logger, data *matrix.Matrix, latencyKey st
if opsValue, ok := ops.GetValueFloat64(i); ok && opsValue != 0 {
err := m.SetValueFloat64(i, value/opsValue)
if err != nil {
logger.Error("error", slog.Any("err", err))
logger.Error("error", slogx.Err(err))
}
} else {
m.SetValueNAN(i)
Expand Down Expand Up @@ -503,7 +504,7 @@ func PopulateIfgroupMetrics(portIfgroupMap map[string]string, portDataMap map[st
if err != nil {
logger.Debug(
"Failed to add instance",
slog.Any("err", err),
slogx.Err(err),
slog.String("ifgrpupInstanceKey", ifgrpupInstanceKey),
)
return err
Expand All @@ -525,13 +526,13 @@ func PopulateIfgroupMetrics(portIfgroupMap map[string]string, portDataMap map[st
rx := nData.GetMetric("rx_bytes")
rxv, _ := rx.GetValueFloat64(ifgroupInstance)
if err = rx.SetValueFloat64(ifgroupInstance, readBytes+rxv); err != nil {
logger.Debug("Failed to parse value", slog.Any("value", readBytes), slog.Any("err", err))
logger.Debug("Failed to parse value", slog.Any("value", readBytes), slogx.Err(err))
}

tx := nData.GetMetric("tx_bytes")
txv, _ := tx.GetValueFloat64(ifgroupInstance)
if err = tx.SetValueFloat64(ifgroupInstance, writeBytes+txv); err != nil {
logger.Debug("Failed to parse value", slog.Any("value", writeBytes), slog.Any("err", err))
logger.Debug("Failed to parse value", slog.Any("value", writeBytes), slogx.Err(err))
}
}
return nil
Expand Down
17 changes: 9 additions & 8 deletions cmd/collectors/ems/ems.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/netapp/harvest/v2/pkg/errs"
"github.com/netapp/harvest/v2/pkg/matrix"
"github.com/netapp/harvest/v2/pkg/set"
"github.com/netapp/harvest/v2/pkg/slogx"
"github.com/netapp/harvest/v2/pkg/tree/node"
"github.com/netapp/harvest/v2/pkg/util"
"github.com/tidwall/gjson"
Expand Down Expand Up @@ -235,7 +236,7 @@ func (e *Ems) InitCache() error {
}
if line1.GetNameS() == "plugins" {
if err := e.LoadPlugins(line1, e, prop.Name); err != nil {
e.Logger.Error("Failed to load plugin", slog.Any("err", err))
e.Logger.Error("Failed to load plugin", slogx.Err(err))
}
}
if line1.GetNameS() == "resolve_when_ems" {
Expand All @@ -259,7 +260,7 @@ func (e *Ems) getTimeStampFilter(clusterTime time.Time) string {
if err != nil {
e.Logger.Warn(
"Failed to parse duration. using default",
slog.Any("err", err),
slogx.Err(err),
slog.String("defaultDataPollDuration", defaultDataPollDuration.String()),
)
}
Expand Down Expand Up @@ -528,7 +529,7 @@ func (e *Ems) HandleResults(result []gjson.Result, prop map[string][]*emsProp) (
if err = metr.SetValueFloat64(instance, 0); err != nil {
e.Logger.Error(
"Unable to set float key on metric",
slog.Any("err", err),
slogx.Err(err),
slog.String("key", "events"),
)
continue
Expand Down Expand Up @@ -576,7 +577,7 @@ func (e *Ems) HandleResults(result []gjson.Result, prop map[string][]*emsProp) (

if instance == nil {
if instance, err = mx.NewInstance(instanceKey); err != nil {
e.Logger.Error("", slog.Any("err", err), slog.String("instanceKey", instanceKey))
e.Logger.Error("", slogx.Err(err), slog.String("instanceKey", instanceKey))
continue
}
}
Expand Down Expand Up @@ -646,7 +647,7 @@ func (e *Ems) HandleResults(result []gjson.Result, prop map[string][]*emsProp) (
if !ok {
if metr, err = mx.NewMetricFloat64(metric.Name); err != nil {
e.Logger.Error("failed to get metric",
slog.Any("err", err),
slogx.Err(err),
slog.String("name", metric.Name),
)
continue
Expand All @@ -657,15 +658,15 @@ func (e *Ems) HandleResults(result []gjson.Result, prop map[string][]*emsProp) (
case metric.Name == "events":
if err = metr.SetValueFloat64(instance, 1); err != nil {
e.Logger.Error("Unable to set float key on metric",
slog.Any("err", err),
slogx.Err(err),
slog.String("key", metric.Name),
slog.String("metric", metric.Label),
)
}
case metric.Name == "timestamp":
if err = metr.SetValueFloat64(instance, float64(time.Now().UnixMicro())); err != nil {
e.Logger.Error("Unable to set timestamp on metric",
slog.Any("err", err),
slogx.Err(err),
slog.String("key", metric.Name),
slog.String("metric", metric.Label),
)
Expand Down Expand Up @@ -767,7 +768,7 @@ func (e *Ems) updateMatrix(begin time.Time) {
if err := eventMetric.SetValueFloat64(instance, 0); err != nil {
e.Logger.Error(
"Unable to set float key on metric",
slog.Any("err", err),
slogx.Err(err),
slog.String("key", "events"),
)
continue
Expand Down
3 changes: 2 additions & 1 deletion cmd/collectors/ems/ems_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/netapp/harvest/v2/cmd/poller/collector"
"github.com/netapp/harvest/v2/cmd/poller/options"
"github.com/netapp/harvest/v2/pkg/conf"
"github.com/netapp/harvest/v2/pkg/slogx"
"github.com/netapp/harvest/v2/pkg/tree"
"github.com/netapp/harvest/v2/pkg/tree/node"
"log/slog"
Expand Down Expand Up @@ -56,7 +57,7 @@ func NewEms() *Ems {
ac := collector.New("Ems", "Ems", opts, emsParams(emsConfigPath), nil)
e := &Ems{}
if err := e.Init(ac); err != nil {
slog.Error("", slog.Any("err", err))
slog.Error("", slogx.Err(err))
os.Exit(1)
}
// Changed the resolve_after for 2 issuing ems for auto resolve testing
Expand Down
9 changes: 5 additions & 4 deletions cmd/collectors/fabricpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package collectors

import (
"github.com/netapp/harvest/v2/pkg/matrix"
"github.com/netapp/harvest/v2/pkg/slogx"
"log/slog"
"maps"
"regexp"
Expand Down Expand Up @@ -86,7 +87,7 @@ func GetFlexGroupFabricPoolMetrics(dataMap map[string]*matrix.Matrix, object str
if !strings.HasPrefix(mkey, "cloud_bin_op_latency_average") {
err := fgm.SetValueFloat64(fg, fgv+value)
if err != nil {
l.Error("error", slog.Any("err", err))
l.Error("error", slogx.Err(err))
}
continue
}
Expand All @@ -113,12 +114,12 @@ func GetFlexGroupFabricPoolMetrics(dataMap map[string]*matrix.Matrix, object str
if value != 0 {
err = tempOps.SetValueFloat64(fg, tempOpsV+opsValue)
if err != nil {
l.Error("error", slog.Any("err", err))
l.Error("error", slogx.Err(err))
}
}
err = fgm.SetValueFloat64(fg, fgv+prod)
if err != nil {
l.Error("error", slog.Any("err", err))
l.Error("error", slogx.Err(err))
}
}
}
Expand All @@ -142,7 +143,7 @@ func GetFlexGroupFabricPoolMetrics(dataMap map[string]*matrix.Matrix, object str
if opsValue, ok := ops.GetValueFloat64(i); ok && opsValue != 0 {
err := m.SetValueFloat64(i, value/opsValue)
if err != nil {
l.Error("error", slog.Any("err", err))
l.Error("error", slogx.Err(err))
}
} else {
m.SetValueNAN(i)
Expand Down
11 changes: 6 additions & 5 deletions cmd/collectors/keyperf/keyperf.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/netapp/harvest/v2/cmd/poller/plugin"
"github.com/netapp/harvest/v2/pkg/errs"
"github.com/netapp/harvest/v2/pkg/matrix"
"github.com/netapp/harvest/v2/pkg/slogx"
"github.com/tidwall/gjson"
"log/slog"
"strconv"
Expand Down Expand Up @@ -329,7 +330,7 @@ func (kp *KeyPerf) pollData(
key := orderedKeys[i]
counter := counterMap[key]
if counter == nil {
kp.Logger.Error("Missing counter", slog.Any("err", err), slog.String("counter", metric.GetName()))
kp.Logger.Error("Missing counter", slogx.Err(err), slog.String("counter", metric.GetName()))
continue
}
property := counter.counterType
Expand All @@ -345,7 +346,7 @@ func (kp *KeyPerf) pollData(

// all other properties - first calculate delta
if skips, err = curMat.Delta(key, prevMat, kp.Logger); err != nil {
kp.Logger.Error("Calculate delta", slog.Any("err", err), slog.String("key", key))
kp.Logger.Error("Calculate delta", slogx.Err(err), slog.String("key", key))
continue
}
totalSkips += skips
Expand Down Expand Up @@ -392,7 +393,7 @@ func (kp *KeyPerf) pollData(
}

if err != nil {
kp.Logger.Error("Division by base", slog.Any("err", err), slog.String("key", key))
kp.Logger.Error("Division by base", slogx.Err(err), slog.String("key", key))
continue
}
totalSkips += skips
Expand All @@ -404,7 +405,7 @@ func (kp *KeyPerf) pollData(

if property == "percent" {
if skips, err = curMat.MultiplyByScalar(key, 100); err != nil {
kp.Logger.Error("Multiply by scalar", slog.Any("err", err), slog.String("key", key))
kp.Logger.Error("Multiply by scalar", slogx.Err(err), slog.String("key", key))
} else {
totalSkips += skips
}
Expand Down Expand Up @@ -432,7 +433,7 @@ func (kp *KeyPerf) pollData(
if skips, err = curMat.Divide(orderedKeys[i], timestampMetricName); err != nil {
kp.Logger.Error(
"Calculate rate",
slog.Any("err", err),
slogx.Err(err),
slog.Int("i", i),
slog.String("key", key),
slog.Int("instIndex", instIndex),
Expand Down
5 changes: 3 additions & 2 deletions cmd/collectors/power.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/netapp/harvest/v2/pkg/conf"
"github.com/netapp/harvest/v2/pkg/errs"
"github.com/netapp/harvest/v2/pkg/matrix"
"github.com/netapp/harvest/v2/pkg/slogx"
"github.com/netapp/harvest/v2/pkg/util"
"log/slog"
"net/http"
Expand Down Expand Up @@ -448,7 +449,7 @@ func (s *Sensor) Init() error {

timeout, _ := time.ParseDuration(rest.DefaultTimeout)
if s.client, err = rest.New(conf.ZapiPoller(s.ParentParams), timeout, s.Auth); err != nil {
s.SLogger.Error("connecting", slog.Any("err", err))
s.SLogger.Error("connecting", slogx.Err(err))
return err
}

Expand All @@ -473,7 +474,7 @@ func (s *Sensor) Init() error {
for _, k := range eMetrics {
err := matrix.CreateMetric(k, s.data)
if err != nil {
s.SLogger.Warn("error while creating metric", slog.Any("err", err), slog.String("key", k))
s.SLogger.Warn("error while creating metric", slogx.Err(err), slog.String("key", k))
}
}
return nil
Expand Down
Loading

0 comments on commit 563871f

Please sign in to comment.