fix(monitor): return error if metric is nil (#17574)

This commit is contained in:
Zexi Li
2023-07-20 11:51:35 +08:00
committed by GitHub
parent 3649a4a66c
commit 58f72985bc
2 changed files with 15 additions and 1 deletions

View File

@@ -266,6 +266,8 @@ func findGuestsOfHost(drv IMetricDriver, host IHost, ds *tsdb.DataSource, ms *mo
}
ret := make([]ICandidate, 0)
found := false
errs := []error{}
for _, obj := range objs {
gHostId, err := obj.GetString("host_id")
if err != nil {
@@ -291,15 +293,24 @@ func findGuestsOfHost(drv IMetricDriver, host IHost, ds *tsdb.DataSource, ms *mo
}
c, err := drv.GetCandidate(obj, host, ds)
if err != nil {
return nil, errors.Wrapf(err, "drv.GetCandidate of guest %s", obj)
errs = append(errs, errors.Wrapf(err, "drv.GetCandidate of guest %s", obj))
continue
}
if c.GetScore() == 0 {
log.Debugf("ignore guest %s cause %s score is 0", c.GetName(), drv.GetType())
continue
}
ret = append(ret, c)
found = true
}
}
if !found {
return nil, errors.NewAggregate(errs)
}
if len(errs) != 0 {
log.Warningf("not all guests found: %s", errors.NewAggregate(errs))
}
return ret, nil
}

View File

@@ -127,6 +127,9 @@ func newCPUCandidate(gst jsonutils.JSONObject, host *HostResource, ds *tsdb.Data
}
metric := metrics.Get(res.GetId())
if metric == nil {
return nil, errors.Errorf("not found resource %q metric from %#v", res.GetId(), metrics.indexes)
}
usage := metric.Values["usage_active"]
return &cpuCandidate{
guestResource: res,