Handle the metric/host not found case differently

This commit is contained in:
Aditya Ujeniya
2026-01-28 17:47:38 +01:00
parent 3452891613
commit 7101d2bb3b
3 changed files with 43 additions and 5 deletions

View File

@@ -18,6 +18,8 @@ import (
)
var (
// ErrNoHostOrMetric is returned when the metric store does not find the host or the metric
ErrNoHostOrMetric error = errors.New("[METRICSTORE]> [METRICSTORE]> metric or host not found")
// ErrInvalidTimeRange is returned when a query has 'from' >= 'to'
ErrInvalidTimeRange = errors.New("[METRICSTORE]> invalid time range: 'from' must be before 'to'")
// ErrEmptyCluster is returned when a query with ForAllNodes has no cluster specified
@@ -278,10 +280,20 @@ func FetchData(req APIQueryRequest) (*APIQueryResponse, error) {
data.Data, data.From, data.To, data.Resolution, err = ms.Read(sel, query.Metric, req.From, req.To, query.Resolution)
if err != nil {
msg := err.Error()
data.Error = &msg
res = append(res, data)
continue
// Check a special case where only the metric or host.
// Dont send errors, instead just send empty array
// where frontend already renders error for empty array.
if err == ErrNoHostOrMetric {
data.Data = make([]schema.Float, 0)
data.From = req.From
data.To = req.To
data.Resolution = query.Resolution
} else {
msg := err.Error()
data.Error = &msg
res = append(res, data)
continue
}
}
if req.WithStats {

View File

@@ -699,7 +699,7 @@ func (m *MemoryStore) Read(selector util.Selector, metric string, from, to, reso
if err != nil {
return nil, 0, 0, 0, err
} else if n == 0 {
return nil, 0, 0, 0, errors.New("[METRICSTORE]> metric or host not found")
return nil, 0, 0, 0, ErrNoHostOrMetric
} else if n > 1 {
if minfo.Aggregation == AvgAggregation {
normalize := 1. / schema.Float(n)