Skip to content

Commit

Permalink
feat: handled review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Hardikl committed Jul 9, 2024
1 parent b1bcc9f commit 3158513
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 23 deletions.
7 changes: 7 additions & 0 deletions cmd/collectors/commonutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,3 +245,10 @@ func ReadPluginKey(param *node.Node, key string) bool {
}
return false
}

func RunPlugin(runPluginIfNoData bool, mat map[string]*matrix.Matrix, err error) (map[string]*matrix.Matrix, error) {
if runPluginIfNoData {
return mat, nil
}
return nil, err
}
34 changes: 34 additions & 0 deletions cmd/collectors/commonutils_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package collectors

import (
"errors"
"github.com/netapp/harvest/v2/pkg/errs"
"github.com/netapp/harvest/v2/pkg/logging"
"github.com/netapp/harvest/v2/pkg/matrix"
"github.com/netapp/harvest/v2/pkg/tree/node"
Expand Down Expand Up @@ -334,3 +336,35 @@ func populateInstance(data *matrix.Matrix, instanceName, healthy, schedule, last
}
return instance
}

func TestRunPlugin(t *testing.T) {
type test struct {
name string
runPluginIfNoData bool
mat map[string]*matrix.Matrix
err error
wantMatrix bool
wantErr error
}

testMat := make(map[string]*matrix.Matrix)
err := errs.New(errs.ErrConfig, "test Error")

tests := []test{
{"runPluginIfNoData_false", false, testMat, err, false, err},
{"runPluginIfNoData_true", true, testMat, err, true, nil},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := RunPlugin(tt.runPluginIfNoData, tt.mat, tt.err)
if !errors.Is(err, tt.wantErr) {
t.Errorf("RunPlugin() error = %v, wantErr %v", err, tt.wantErr)
return
}
if (got != nil) != tt.wantMatrix {
t.Errorf("RunPlugin() got = %v, wantMatrix %v", got, tt.wantMatrix)
}
})
}
}
14 changes: 5 additions & 9 deletions cmd/collectors/rest/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,7 @@ func (r *Rest) InitVars(config *node.Node) {
}

// if the object template includes a run_plugins_if_no_data, then honour that
r.RunPluginsIfNoData = false
if r.Params.HasChildS("run_plugins_if_no_data") {
r.RunPluginsIfNoData = true
}
r.RunPluginsIfNoData = r.Params.HasChildS("run_plugins_if_no_data")
}

func (r *Rest) InitClient() error {
Expand Down Expand Up @@ -402,14 +399,13 @@ func (r *Rest) PollData() (map[string]*matrix.Matrix, error) {
startTime = time.Now()

if records, err = r.GetRestData(r.Prop.Href); err != nil {
return nil, err
r.Logger.Warn().Msgf("error while fetching " + r.Object + " records on cluster")
return collectors.RunPlugin(r.RunPluginsIfNoData, r.Matrix, err)
}

if len(records) == 0 {
if r.RunPluginsIfNoData {
return r.Matrix, errs.New(errs.ErrNoInstance, "no "+r.Object+" instances on cluster")
}
return nil, errs.New(errs.ErrNoInstance, "no "+r.Object+" instances on cluster")
r.Logger.Warn().Msgf("no " + r.Object + " instances on cluster")
return collectors.RunPlugin(r.RunPluginsIfNoData, r.Matrix, errs.New(errs.ErrNoInstance, "no "+r.Object+" instances on cluster"))
}

return r.pollData(startTime, records, func(e *endPoint) ([]gjson.Result, time.Duration, error) {
Expand Down
17 changes: 7 additions & 10 deletions cmd/collectors/zapi/collector/zapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,7 @@ func (z *Zapi) InitVars() error {
}

// if the object template includes a run_plugins_if_no_data, then honour that
z.RunPluginsIfNoData = false
if z.Params.HasChildS("run_plugins_if_no_data") {
z.RunPluginsIfNoData = true
}
z.RunPluginsIfNoData = z.Params.HasChildS("run_plugins_if_no_data")
return nil
}

Expand Down Expand Up @@ -343,7 +340,8 @@ func (z *Zapi) PollData() (map[string]*matrix.Matrix, error) {
response, tag, ad, pd, err = z.Client.InvokeBatchWithTimers(request, tag)

if err != nil {
return nil, err
z.Logger.Warn().Msgf("error while fetching " + z.Object + " records on cluster")
return collectors.RunPlugin(z.RunPluginsIfNoData, z.Matrix, err)
}

if response == nil {
Expand All @@ -363,7 +361,8 @@ func (z *Zapi) PollData() (map[string]*matrix.Matrix, error) {
instance := mat.GetInstance("cluster")
if instance == nil {
if instance, err = mat.NewInstance("cluster"); err != nil {
return nil, err
z.Logger.Warn().Msgf("error while creating " + z.Object + " instances on cluster")
return collectors.RunPlugin(z.RunPluginsIfNoData, z.Matrix, err)
}
}
fetch(instance, instances[0], make([]string, 0), false)
Expand Down Expand Up @@ -412,10 +411,8 @@ func (z *Zapi) PollData() (map[string]*matrix.Matrix, error) {
z.AddCollectCount(count)

if numInstances == 0 {
if z.RunPluginsIfNoData {
return z.Matrix, errs.New(errs.ErrNoInstance, "")
}
return nil, errs.New(errs.ErrNoInstance, "")
z.Logger.Warn().Msgf("no " + z.Object + " instances on cluster")
return collectors.RunPlugin(z.RunPluginsIfNoData, z.Matrix, errs.New(errs.ErrNoInstance, ""))
}

return z.Matrix, nil
Expand Down
5 changes: 1 addition & 4 deletions cmd/poller/collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,6 @@ func (c *AbstractCollector) Start(wg *sync.WaitGroup) {
Msg("target unreachable, entering standby mode and retry")
c.Schedule.SetStandByMode(task, time.Duration(retryDelay)*time.Second)
c.SetStatus(1, errs.ErrConnection.Error())
continue
case errs.IsRestErr(err, errs.CMReject):
// Try again in 30 to 60 seconds
retryAfter := 30 + rand.Int63n(30) //nolint:gosec
Expand All @@ -387,7 +386,6 @@ func (c *AbstractCollector) Start(wg *sync.WaitGroup) {
Str("task", task.Name).
Int64("retryAfterSecs", retryAfter).
Msg("CM reject, entering standby mode and retry")
continue
// there are no instances to collect
case errors.Is(err, errs.ErrNoInstance):
c.Schedule.SetStandByModeMax(task, 5*time.Minute)
Expand All @@ -403,7 +401,6 @@ func (c *AbstractCollector) Start(wg *sync.WaitGroup) {
Str("task", task.Name).
Str("object", c.Object).
Msg("no metrics of object on system, entering standby mode")
continue
// not an error we are expecting, so enter failed or standby state
default:
switch {
Expand All @@ -427,8 +424,8 @@ func (c *AbstractCollector) Start(wg *sync.WaitGroup) {
}

c.SetStatus(2, errMsg)
continue
}
continue
case c.Schedule.IsStandBy():
c.Schedule.Recover()
retryDelay = 1
Expand Down

0 comments on commit 3158513

Please sign in to comment.