Skip to content

Commit

Permalink
Cherry-pick #12816 to 7.2: [Metricbeat] Avoid omitting of errors in V…
Browse files Browse the repository at this point in the history
…sphere module and upgrade to use context.Context (#13004)
  • Loading branch information
sayden committed Aug 2, 2019
1 parent c258217 commit 93e0795
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 13 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ https://github.com/elastic/beats/compare/v7.2.0...7.2[Check the HEAD diff]

*Metricbeat*

- Print errors that were being omitted in vSphere metricsets {pull}12816[12816]

*Packetbeat*

*Winlogbeat*
Expand Down
16 changes: 11 additions & 5 deletions metricbeat/module/vsphere/datastore/datastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ func New(base mb.BaseMetricSet) (mb.MetricSet, error) {
// format. It publishes the event which is then forwarded to the output. In case
// of an error set the Error field of mb.Event or simply call report.Error().
func (m *MetricSet) Fetch(reporter mb.ReporterV2) error {

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

Expand All @@ -86,7 +85,11 @@ func (m *MetricSet) Fetch(reporter mb.ReporterV2) error {
return errors.Wrap(err, "error in NewClient")
}

defer client.Logout(ctx)
defer func() {
if err := client.Logout(ctx); err != nil {
m.Logger().Debug(errors.Wrap(err, "error trying to logout from vshphere"))
}
}()

c := client.Client

Expand All @@ -98,12 +101,15 @@ func (m *MetricSet) Fetch(reporter mb.ReporterV2) error {
return errors.Wrap(err, "error in CreateContainerView")
}

defer v.Destroy(ctx)
defer func() {
if err := v.Destroy(ctx); err != nil {
m.Logger().Debug(errors.Wrap(err, "error trying to destroy view from vshphere"))
}
}()

// Retrieve summary property for all datastores
var dst []mo.Datastore
err = v.Retrieve(ctx, []string{"Datastore"}, []string{"summary"}, &dst)
if err != nil {
if err = v.Retrieve(ctx, []string{"Datastore"}, []string{"summary"}, &dst); err != nil {
return errors.Wrap(err, "error in Retrieve")
}

Expand Down
13 changes: 10 additions & 3 deletions metricbeat/module/vsphere/host/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ func New(base mb.BaseMetricSet) (mb.MetricSet, error) {
// format. It publishes the event which is then forwarded to the output. In case
// of an error set the Error field of mb.Event or simply call report.Error().
func (m *MetricSet) Fetch(reporter mb.ReporterV2) error {

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

Expand All @@ -91,7 +90,11 @@ func (m *MetricSet) Fetch(reporter mb.ReporterV2) error {
return errors.Wrap(err, "error in NewClient")
}

defer client.Logout(ctx)
defer func() {
if err := client.Logout(ctx); err != nil {
m.Logger().Debug(errors.Wrap(err, "error trying to logout from vshphere"))
}
}()

c := client.Client

Expand All @@ -103,7 +106,11 @@ func (m *MetricSet) Fetch(reporter mb.ReporterV2) error {
return errors.Wrap(err, "error in CreateContainerView")
}

defer v.Destroy(ctx)
defer func() {
if err := v.Destroy(ctx); err != nil {
m.Logger().Debug(errors.Wrap(err, "error trying to destroy view from vshphere"))
}
}()

// Retrieve summary property for all hosts.
var hst []mo.HostSystem
Expand Down
18 changes: 13 additions & 5 deletions metricbeat/module/vsphere/virtualmachine/virtualmachine.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,11 @@ func (m *MetricSet) Fetch(reporter mb.ReporterV2) error {
return errors.Wrap(err, "error in NewClient")
}

defer client.Logout(ctx)
defer func() {
if err := client.Logout(ctx); err != nil {
m.Logger().Debug(errors.Wrap(err, "error trying to logout from vshphere"))
}
}()

c := client.Client

Expand All @@ -117,7 +121,11 @@ func (m *MetricSet) Fetch(reporter mb.ReporterV2) error {
return errors.Wrap(err, "error in CreateContainerView")
}

defer v.Destroy(ctx)
defer func() {
if err := v.Destroy(ctx); err != nil {
m.Logger().Debug(errors.Wrap(err, "error trying to destroy view from vshphere"))
}
}()

// Retrieve summary property for all machines
var vmt []mo.VirtualMachine
Expand Down Expand Up @@ -161,7 +169,7 @@ func (m *MetricSet) Fetch(reporter mb.ReporterV2) error {
}

if vm.Summary.Vm != nil {
networkNames, err := getNetworkNames(c, vm.Summary.Vm.Reference())
networkNames, err := getNetworkNames(ctx, c, vm.Summary.Vm.Reference())
if err != nil {
m.Logger().Debug(err.Error())
} else {
Expand Down Expand Up @@ -194,8 +202,8 @@ func getCustomFields(customFields []types.BaseCustomFieldValue, customFieldsMap
return outputFields
}

func getNetworkNames(c *vim25.Client, ref types.ManagedObjectReference) ([]string, error) {
ctx, cancel := context.WithCancel(context.Background())
func getNetworkNames(ctx context.Context, c *vim25.Client, ref types.ManagedObjectReference) ([]string, error) {
ctx, cancel := context.WithCancel(ctx)
defer cancel()

var outputNetworkNames []string
Expand Down

0 comments on commit 93e0795

Please sign in to comment.