[receiver/vcenter] Fix nil pointer dereference in recordVMStats#47005
Conversation
Signed-off-by: singhvibhanshu <singhvibhanshu@hotmail.com>
axw
left a comment
There was a problem hiding this comment.
@singhvibhanshu is it possible to add a unit test for this, to avoid regressions?
|
yes @axw, I can surely add a unit test this shortly. |
|
@singhvibhanshu there were a few other issues I ran into: index 03e5b2e..57ea880 100644
--- a/receiver/vcenterreceiver/client.go
+++ b/receiver/vcenterreceiver/client.go
@@ -328,7 +328,23 @@ func (vc *vcenterClient) PerfMetricsQuery(
vc.pm.Sort = true
sample, err := vc.pm.SampleByName(ctx, spec, names, objs)
if err != nil {
- return nil, err
+ // Batch query failed -- fall back to querying objects one at a time.
+ // An orphaned VM can cause the entire batch SOAP call to fail.
+ resultsByRef := map[string]*performance.EntityMetric{}
+ for _, obj := range objs {
+ s, sErr := vc.pm.SampleByName(ctx, spec, names, []vt.ManagedObjectReference{obj})
+ if sErr != nil {
+ continue
+ }
+ r, rErr := vc.pm.ToMetricSeries(ctx, s)
+ if rErr != nil {
+ continue
+ }
+ for i := range r {
+ resultsByRef[r[i].Entity.Value] = &r[i]
+ }
+ }
+ return &perfMetricsQueryResult{resultsByRef: resultsByRef}, nil
}
result, err := vc.pm.ToMetricSeries(ctx, sample)
if err != nil {
diff --git a/receiver/vcenterreceiver/metrics.go b/receiver/vcenterreceiver/metrics.go
index 78b7f5f..bd3dfb4 100644
--- a/receiver/vcenterreceiver/metrics.go
+++ b/receiver/vcenterreceiver/metrics.go
@@ -258,6 +258,10 @@ func (v *vcenterMetricScraper) recordVMStats(
vm *mo.VirtualMachine,
hs *mo.HostSystem,
) {
+ // Guard against VMs with incomplete data.
+ if vm.Config == nil || vm.Summary.Storage == nil {
+ return
+ }
diskUsed := vm.Summary.Storage.Committed
diskFree := vm.Summary.Storage.Uncommitted
diff --git a/receiver/vcenterreceiver/processors.go b/receiver/vcenterreceiver/processors.go
index aed6608..02d0cb2 100644
--- a/receiver/vcenterreceiver/processors.go
+++ b/receiver/vcenterreceiver/processors.go
@@ -289,6 +289,13 @@ func (v *vcenterMetricScraper) buildVMMetrics(
return crRef, groupInfo, fmt.Errorf("no collected ComputeResource for VM [%s]'s ComputeResource ref: %s", vm.Name, crRef)
}
+ // Guard against VMs with incomplete data -- powered-off, suspended, or
+ // template VMs may have nil Config, nil Summary.Storage, or nil
+ // Summary.Runtime fields from the vSphere API.
+ if vm.Config == nil || vm.Summary.Storage == nil {
+ return crRef, groupInfo, nil // powered-off/template VM — skip silently
+ }
+
// Get related VM host info
hsRef := vm.Summary.Runtime.Host
if hsRef == nil {
|
|
@snowch while writing a unit test for regression locally, I found that solely checking I am still looking in it and will update the PR soon. |
Signed-off-by: singhvibhanshu <singhvibhanshu@hotmail.com>
ad6adc0 to
fa93225
Compare
|
First of all apologies for responding so late. @axw,
@snowch,
|
Signed-off-by: singhvibhanshu <singhvibhanshu@hotmail.com>
|
@axw, |
|
Thank you for your contribution @singhvibhanshu! 🎉 We would like to hear from you about your experience contributing to OpenTelemetry by taking a few minutes to fill out this survey. If you are getting started contributing, you can also join the CNCF Slack channel #opentelemetry-new-contributors to ask for guidance and get help. |
…-telemetry#47005) Fixes open-telemetry#46977 --------- Signed-off-by: singhvibhanshu <singhvibhanshu@hotmail.com>
…-telemetry#47005) Fixes open-telemetry#46977 --------- Signed-off-by: singhvibhanshu <singhvibhanshu@hotmail.com>
Fixes #46977