Skip to content

Commit

Permalink
[code] reduce Gather() cyclomatic complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
Tesifonte Belda committed May 21, 2022
1 parent caa3ca5 commit 457283d
Showing 1 changed file with 84 additions and 39 deletions.
123 changes: 84 additions & 39 deletions plugins/inputs/vcstat/vcstat.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,67 +179,38 @@ func (vcs *VCstatConfig) Description() string {
// the data collection and writes all metrics into the Accumulator passed as an argument.
func (vcs *VCstatConfig) Gather(acc telegraf.Accumulator) error {
var (
col *vccollector.VcCollector
startTime time.Time
err error
)

//--- re-Init if needed
if vcs.ctx == nil || vcs.ctx.Err() != nil || vcs.vcc == nil {
if err = vcs.Init(); err != nil {
return gatherError(acc, err)
}
}
col = vcs.vcc
if !col.IsActive(vcs.ctx) {
if vcs.SessionsCreated.Get() > 0 {
acc.AddError(fmt.Errorf("vCenter session not active, re-authenticating..."))
}
if err = col.Open(vcs.ctx, time.Duration(vcs.Timeout)); err != nil {
return gatherError(acc, err)
}
vcs.SessionsCreated.Incr(1)
if err = vcs.keepActiveSession(acc); err != nil {
return gatherError(acc, err)
}
acc.SetPrecision(getPrecision(vcs.pollInterval))

// poll using a context with timeout
ctx1, cancel1 := context.WithTimeout(vcs.ctx, time.Duration(vcs.Timeout))
defer cancel1()
startTime = time.Now()

//--- Get vCenter basic stats
if err = col.CollectVcenterInfo(ctx1, acc); err != nil {
return gatherError(acc, err)
}

//--- Get Datacenters info
if vcs.ClusterInstances || vcs.HostInstances {
if err = col.CollectDatacenterInfo(ctx1, acc); err != nil {
return gatherError(acc, err)
}
}

//--- Get Clusters info
if vcs.ClusterInstances {
if err = col.CollectClusterInfo(ctx1, acc); err != nil {
return gatherError(acc, err)
}
//--- Get vCenter, DCs and Clusters info
if err = vcs.gatherHighLevelEntities(ctx1, acc); err != nil {
return gatherError(acc, err)
}

//--- Get Hosts, Network,... info
//--- Get Hosts, Networks and Storage info
if err = vcs.gatherHost(ctx1, acc); err != nil {
return gatherError(acc, err)
}
if err = vcs.gatherNetwork(ctx1, acc); err != nil {
return gatherError(acc, err)
}

//--- Get Datastores info
if vcs.DatastoreInstances {
if err = col.CollectDatastoresInfo(ctx1, acc); err != nil {
return gatherError(acc, err)
}
if err = vcs.gatherStorage(ctx1, acc); err != nil {
return gatherError(acc, err)
}


// selfmonitoring
vcs.GatherTime.Set(int64(time.Since(startTime).Nanoseconds()))
vcs.NotRespondingHosts.Set(int64(vcs.vcc.GetNumberNotRespondingHosts()))
Expand All @@ -252,6 +223,63 @@ func (vcs *VCstatConfig) Gather(acc telegraf.Accumulator) error {
return nil
}

// keepActiveSession keeps an active session with vsphere
func (vcs *VCstatConfig) keepActiveSession(acc telegraf.Accumulator) error {
var (
col *vccollector.VcCollector
err error
)

if vcs.ctx == nil || vcs.ctx.Err() != nil || vcs.vcc == nil {
if err = vcs.Init(); err != nil {
return err
}
}
col = vcs.vcc
if !col.IsActive(vcs.ctx) {
if vcs.SessionsCreated.Get() > 0 {
acc.AddError(fmt.Errorf("vCenter session not active, re-authenticating..."))
}
if err = col.Open(vcs.ctx, time.Duration(vcs.Timeout)); err != nil {
return err
}
vcs.SessionsCreated.Incr(1)
}

return nil
}

// gatherHighLevelEntities gathers datacenters and clusters stats
func (vcs *VCstatConfig) gatherHighLevelEntities(ctx context.Context, acc telegraf.Accumulator) error {
var (
col *vccollector.VcCollector
err error
)

col = vcs.vcc

//--- Get vCenter basic stats
if err = col.CollectVcenterInfo(ctx, acc); err != nil {
return err
}

//--- Get Datacenters info
if vcs.ClusterInstances || vcs.HostInstances {
if err = col.CollectDatacenterInfo(ctx, acc); err != nil {
return err
}
}

//--- Get Clusters info
if vcs.ClusterInstances {
if err = col.CollectClusterInfo(ctx, acc); err != nil {
return err
}
}

return nil
}

// gatherHost gathers info and stats per host
func (vcs *VCstatConfig) gatherHost(ctx context.Context, acc telegraf.Accumulator) error {
var (
Expand Down Expand Up @@ -316,6 +344,23 @@ func (vcs *VCstatConfig) gatherNetwork(ctx context.Context, acc telegraf.Accumul
return nil
}

// gatherStorage gathers storage entities info
func (vcs *VCstatConfig) gatherStorage(ctx context.Context, acc telegraf.Accumulator) error {
var (
col *vccollector.VcCollector
err error
)

col = vcs.vcc
if vcs.DatastoreInstances {
if err = col.CollectDatastoresInfo(ctx, acc); err != nil {
return gatherError(acc, err)
}
}

return nil
}

// gatherError adds the error to the telegraf accumulator
func gatherError(acc telegraf.Accumulator, err error) error {
// No need to signal errors if we were merely canceled.
Expand Down

0 comments on commit 457283d

Please sign in to comment.