Skip to content

Commit

Permalink
refactor: truncate href when logging
Browse files Browse the repository at this point in the history
  • Loading branch information
cgrinds committed Oct 31, 2024
1 parent 9cad88e commit 8609a80
Show file tree
Hide file tree
Showing 14 changed files with 49 additions and 41 deletions.
23 changes: 13 additions & 10 deletions cmd/collectors/commonutils.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package collectors

import (
"fmt"
"github.com/netapp/harvest/v2/cmd/tools/rest"
"github.com/netapp/harvest/v2/pkg/errs"
"github.com/netapp/harvest/v2/pkg/matrix"
Expand Down Expand Up @@ -60,7 +61,7 @@ func IsEmbedShelf(model string, moduleType string) bool {
return combinations[embedShelf{model, moduleType}]
}

func InvokeRestCallWithTestFile(client *rest.Client, href string, logger *slog.Logger, testFilePath string) ([]gjson.Result, error) {
func InvokeRestCallWithTestFile(client *rest.Client, href string, testFilePath string) ([]gjson.Result, error) {
if testFilePath != "" {
b, err := os.ReadFile(testFilePath)
if err != nil {
Expand All @@ -69,19 +70,13 @@ func InvokeRestCallWithTestFile(client *rest.Client, href string, logger *slog.L
testData := gjson.Result{Type: gjson.JSON, Raw: string(b)}
return testData.Get("records").Array(), nil
}
return InvokeRestCall(client, href, logger)
return InvokeRestCall(client, href)
}

func InvokeRestCall(client *rest.Client, href string, logger *slog.Logger) ([]gjson.Result, error) {
func InvokeRestCall(client *rest.Client, href string) ([]gjson.Result, error) {
result, err := rest.FetchAll(client, href)
if err != nil {
logger.Error(
"Failed to fetch data",
slogx.Err(err),
slog.String("href", href),
slog.Int("hrefLength", len(href)),
)
return []gjson.Result{}, err
return []gjson.Result{}, fmt.Errorf("failed to fetchAll href=%s, hrefLength=%d err=%w", TruncateURL(href), len(href), err)
}

if len(result) == 0 {
Expand All @@ -91,6 +86,14 @@ func InvokeRestCall(client *rest.Client, href string, logger *slog.Logger) ([]gj
return result, nil
}

func TruncateURL(href string) string {
indexOfQuestionMark := strings.Index(href, "?")
if indexOfQuestionMark == -1 {
return href
}
return href[:indexOfQuestionMark] + "..."
}

func GetClusterTime(client *rest.Client, returnTimeOut *int, logger *slog.Logger) (time.Time, error) {
var (
err error
Expand Down
2 changes: 1 addition & 1 deletion cmd/collectors/rest/plugins/aggregate/aggregate.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,5 +177,5 @@ func (a *Aggregate) getObjectStoreData() ([]gjson.Result, error) {
Filter([]string{`tier_name=!" "|""`}).
Build()

return collectors.InvokeRestCall(a.client, href, a.SLogger)
return collectors.InvokeRestCall(a.client, href)
}
4 changes: 2 additions & 2 deletions cmd/collectors/rest/plugins/certificate/certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ func (c *Certificate) GetAdminVserver() (string, error) {
Filter([]string{"type=admin"}).
Build()

if result, err = collectors.InvokeRestCall(c.client, href, c.SLogger); err != nil {
if result, err = collectors.InvokeRestCall(c.client, href); err != nil {
return "", err
}

Expand All @@ -227,7 +227,7 @@ func (c *Certificate) GetSecuritySsl(adminSvm string) (string, error) {
Filter([]string{"vserver=" + adminSvm}).
Build()

if result, err = collectors.InvokeRestCall(c.client, href, c.SLogger); err != nil {
if result, err = collectors.InvokeRestCall(c.client, href); err != nil {
return "", err
}

Expand Down
24 changes: 12 additions & 12 deletions cmd/collectors/rest/plugins/health/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,7 @@ func (h *Health) getDisks() ([]gjson.Result, error) {
Filter([]string{"container_type=broken|unassigned"}).
Build()

return collectors.InvokeRestCall(h.client, href, h.SLogger)
return collectors.InvokeRestCall(h.client, href)
}

func (h *Health) getShelves() ([]gjson.Result, error) {
Expand All @@ -732,7 +732,7 @@ func (h *Health) getShelves() ([]gjson.Result, error) {
MaxRecords(collectors.DefaultBatchSize).
Build()

return collectors.InvokeRestCall(h.client, href, h.SLogger)
return collectors.InvokeRestCall(h.client, href)
}

func (h *Health) getNodes() ([]gjson.Result, error) {
Expand All @@ -745,7 +745,7 @@ func (h *Health) getNodes() ([]gjson.Result, error) {
Filter([]string{"health=false"}).
Build()

return collectors.InvokeRestCall(h.client, href, h.SLogger)
return collectors.InvokeRestCall(h.client, href)
}

func (h *Health) getHADown() ([]gjson.Result, error) {
Expand All @@ -758,7 +758,7 @@ func (h *Health) getHADown() ([]gjson.Result, error) {
Filter([]string{"possible=!true"}).
Build()

return collectors.InvokeRestCall(h.client, href, h.SLogger)
return collectors.InvokeRestCall(h.client, href)
}

func (h *Health) getRansomwareVolumes() ([]gjson.Result, error) {
Expand All @@ -769,7 +769,7 @@ func (h *Health) getRansomwareVolumes() ([]gjson.Result, error) {
Filter([]string{"anti_ransomware.state=enabled", "anti_ransomware.attack_probability=low|moderate|high"}).
Build()

return collectors.InvokeRestCall(h.client, href, h.SLogger)
return collectors.InvokeRestCall(h.client, href)
}

func (h *Health) getNonCompliantLicense() ([]gjson.Result, error) {
Expand All @@ -782,7 +782,7 @@ func (h *Health) getNonCompliantLicense() ([]gjson.Result, error) {
Filter([]string{"state=noncompliant"}).
Build()

return collectors.InvokeRestCall(h.client, href, h.SLogger)
return collectors.InvokeRestCall(h.client, href)
}

func (h *Health) getMoveFailedVolumes() ([]gjson.Result, error) {
Expand All @@ -795,7 +795,7 @@ func (h *Health) getMoveFailedVolumes() ([]gjson.Result, error) {
Filter([]string{"movement.state=cutover_wait|failed|cutover_pending"}).
Build()

return collectors.InvokeRestCall(h.client, href, h.SLogger)
return collectors.InvokeRestCall(h.client, href)
}

func (h *Health) getNonHomeLIFs() ([]gjson.Result, error) {
Expand All @@ -807,7 +807,7 @@ func (h *Health) getNonHomeLIFs() ([]gjson.Result, error) {
Filter([]string{"location.is_home=false"}).
Build()

return collectors.InvokeRestCall(h.client, href, h.SLogger)
return collectors.InvokeRestCall(h.client, href)
}

func (h *Health) getFCPorts() ([]gjson.Result, error) {
Expand All @@ -820,7 +820,7 @@ func (h *Health) getFCPorts() ([]gjson.Result, error) {
Filter([]string{"enabled=true", "state=offlined_by_system"}).
Build()

return collectors.InvokeRestCall(h.client, href, h.SLogger)
return collectors.InvokeRestCall(h.client, href)
}

func (h *Health) getEthernetPorts() ([]gjson.Result, error) {
Expand All @@ -833,7 +833,7 @@ func (h *Health) getEthernetPorts() ([]gjson.Result, error) {
Filter([]string{"enabled=true", "state=down"}).
Build()

return collectors.InvokeRestCall(h.client, href, h.SLogger)
return collectors.InvokeRestCall(h.client, href)
}

func (h *Health) getEmsAlerts() ([]gjson.Result, error) {
Expand All @@ -853,7 +853,7 @@ func (h *Health) getEmsAlerts() ([]gjson.Result, error) {
Filter([]string{timeFilter, severityFilter}).
Build()

return collectors.InvokeRestCall(h.client, href, h.SLogger)
return collectors.InvokeRestCall(h.client, href)
}

func (h *Health) getSupportAlerts(filter []string) ([]gjson.Result, error) {
Expand All @@ -864,7 +864,7 @@ func (h *Health) getSupportAlerts(filter []string) ([]gjson.Result, error) {
Filter(filter).
Build()

return collectors.InvokeRestCall(h.client, href, h.SLogger)
return collectors.InvokeRestCall(h.client, href)
}

// returns time filter (clustertime - polldata duration)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (o *OntapS3Service) Run(dataMap map[string]*matrix.Matrix) ([]*matrix.Matri
MaxRecords(collectors.DefaultBatchSize).
Build()

if result, err = collectors.InvokeRestCall(o.client, href, o.SLogger); err != nil {
if result, err = collectors.InvokeRestCall(o.client, href); err != nil {
return nil, nil, err
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (s *SecurityAccount) Run(dataMap map[string]*matrix.Matrix) ([]*matrix.Matr
Build()

s.client.Metadata.Reset()
if result, err = collectors.InvokeRestCall(s.client, href, s.SLogger); err != nil {
if result, err = collectors.InvokeRestCall(s.client, href); err != nil {
return nil, nil, err
}

Expand Down
8 changes: 4 additions & 4 deletions cmd/collectors/rest/plugins/svm/svm.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ func (s *SVM) GetKerberosConfig() (map[string]string, error) {
MaxRecords(collectors.DefaultBatchSize).
Build()

if result, err = collectors.InvokeRestCall(s.client, href, s.SLogger); err != nil {
if result, err = collectors.InvokeRestCall(s.client, href); err != nil {
return nil, err
}

Expand Down Expand Up @@ -253,7 +253,7 @@ func (s *SVM) GetFpolicy() (map[string]Fpolicy, error) {
MaxRecords(collectors.DefaultBatchSize).
Build()

if result, err = collectors.InvokeRestCall(s.client, href, s.SLogger); err != nil {
if result, err = collectors.InvokeRestCall(s.client, href); err != nil {
return nil, err
}

Expand Down Expand Up @@ -288,7 +288,7 @@ func (s *SVM) GetIscsiServices() (map[string]string, error) {
MaxRecords(collectors.DefaultBatchSize).
Build()

if result, err = collectors.InvokeRestCall(s.client, href, s.SLogger); err != nil {
if result, err = collectors.InvokeRestCall(s.client, href); err != nil {
return nil, err
}

Expand Down Expand Up @@ -322,7 +322,7 @@ func (s *SVM) GetIscsiCredentials() (map[string]string, error) {
MaxRecords(collectors.DefaultBatchSize).
Build()

if result, err = collectors.InvokeRestCall(s.client, href, s.SLogger); err != nil {
if result, err = collectors.InvokeRestCall(s.client, href); err != nil {
return nil, err
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/collectors/rest/plugins/volume/volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ func (v *Volume) getEncryptedDisks() ([]gjson.Result, error) {
Filter([]string{"protection_mode=!data|full"}).
Build()

if result, err = collectors.InvokeRestCall(v.client, href, v.SLogger); err != nil {
if result, err = collectors.InvokeRestCall(v.client, href); err != nil {
return nil, err
}
return result, nil
Expand Down Expand Up @@ -319,7 +319,7 @@ func (v *Volume) getVolume(field string, fields []string, volumeMap map[string]v
Filter([]string{field}).
Build()

if result, err = collectors.InvokeRestCall(v.client, href, v.SLogger); err != nil {
if result, err = collectors.InvokeRestCall(v.client, href); err != nil {
return nil, err
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/collectors/rest/rest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ func TestFields(t *testing.T) {
func TestQuotas(t *testing.T) {
r := newRest("Quota", "quota.yaml", "../../../conf")
var instanceKeys []string
result, err := collectors.InvokeRestCallWithTestFile(r.Client, "", r.Logger, "testdata/quota.json")
result, err := collectors.InvokeRestCallWithTestFile(r.Client, "", "testdata/quota.json")
if err != nil {
t.Errorf("Error while invoking quota rest api call")
}
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 @@ -241,7 +241,7 @@ func (n *Nic) getIfgroupInfo() map[string]string {
MaxRecords(collectors.DefaultBatchSize).
Build()

if ifgroupsData, err = collectors.InvokeRestCallWithTestFile(n.client, href, n.SLogger, n.testFilePath); err != nil {
if ifgroupsData, err = collectors.InvokeRestCallWithTestFile(n.client, href, n.testFilePath); err != nil {
return portIfgroupMap
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ func (t *TopClients) fetchVolumesWithActivityTrackingEnabled() (*set.Set, error)
Filter([]string{"activity_tracking.state=on"}).
Build()

if result, err = collectors.InvokeRestCall(t.client, href, t.SLogger); err != nil {
if result, err = collectors.InvokeRestCall(t.client, href); err != nil {
return va, err
}

Expand Down Expand Up @@ -395,7 +395,7 @@ func (t *TopClients) fetchTopClients(volumes *set.Set, svms *set.Set, metric str
Filter([]string{"top_metric=" + metric, "volume=" + strings.Join(volumes.Values(), "|"), "svm=" + strings.Join(svms.Values(), "|")}).
Build()

if result, err = collectors.InvokeRestCall(t.client, href, t.SLogger); err != nil {
if result, err = collectors.InvokeRestCall(t.client, href); err != nil {
return result, err
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type MockVolume struct {
}

func (mv *MockVolume) fetchTopClients(_ *set.Set, _ *set.Set, _ string) ([]gjson.Result, error) {
return collectors.InvokeRestCallWithTestFile(nil, "", nil, mv.testFilePath)
return collectors.InvokeRestCallWithTestFile(nil, "", mv.testFilePath)
}

func (mv *MockVolume) fetchVolumesWithActivityTrackingEnabled() (*set.Set, error) {
Expand Down
9 changes: 7 additions & 2 deletions cmd/collectors/restperf/restperf.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package restperf
import (
"context"
"fmt"
"github.com/netapp/harvest/v2/cmd/collectors"
rest2 "github.com/netapp/harvest/v2/cmd/collectors/rest"
"github.com/netapp/harvest/v2/cmd/collectors/restperf/plugins/disk"
"github.com/netapp/harvest/v2/cmd/collectors/restperf/plugins/fabricpool"
Expand Down Expand Up @@ -1624,9 +1625,13 @@ func (r *RestPerf) updateQosLabels(qos gjson.Result, instance *matrix.Instance,
func (r *RestPerf) handleError(err error, href string) (map[string]*matrix.Matrix, error) {
if errs.IsRestErr(err, errs.TableNotFound) || errs.IsRestErr(err, errs.APINotFound) {
// the table or API does not exist. return ErrAPIRequestRejected so the task goes to stand-by
return nil, fmt.Errorf("polling href=[%s] err: %w", href, errs.New(errs.ErrAPIRequestRejected, err.Error()))
return nil, fmt.Errorf(
"polling href=[%s] err: %w",
collectors.TruncateURL(href),
errs.New(errs.ErrAPIRequestRejected, err.Error()),
)
}
return nil, fmt.Errorf("failed to fetch data. href=[%s] err: %w", href, err)
return nil, fmt.Errorf("failed to fetch data. href=[%s] err: %w", collectors.TruncateURL(href), err)
}

func (r *RestPerf) InitSchedule() {
Expand Down
2 changes: 1 addition & 1 deletion integration/test/counter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func invokeRestCall(client *rest2.Client, counters map[string][]counterData) err
CounterSchema(counterDetail.perfCounters).
Build()

if _, err := collectors.InvokeRestCall(client, href, slog.Default()); err != nil {
if _, err := collectors.InvokeRestCall(client, href); err != nil {
return fmt.Errorf("failed to invoke rest href=%s call: %w", href, err)
}
}
Expand Down

0 comments on commit 8609a80

Please sign in to comment.