From ed2bc172af514a4f18e8d15aa9814c605a235fe4 Mon Sep 17 00:00:00 2001 From: Phillip Wirth Date: Wed, 10 Sep 2025 14:17:43 +0200 Subject: [PATCH] enhance error message for missing values The error message "value not found in result set" was not helpful in identifying the failing metric. This change enhances the error message to include the metric name and the key that was not found, making it easier to debug user-defined aggregations. The new error message is: "value for metric with key not found in result set" --- internal/collector/collector.go | 5 ++--- internal/collector/collector_test.go | 7 ++++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/internal/collector/collector.go b/internal/collector/collector.go index efcd40e0..324a578b 100644 --- a/internal/collector/collector.go +++ b/internal/collector/collector.go @@ -76,8 +76,7 @@ type Metric struct { var ( //Only Gauge is a supported metric types ErrInvalidType = errors.New("unknown metric type provided. Only gauge is supported") - //The value was not found in the aggregation result set - ErrValueNotFound = errors.New("value not found in result set") + //No cached metric available ErrNotCached = errors.New("metric not available from cache") ) @@ -471,7 +470,7 @@ func (metric *Metric) getValue(result AggregationResult) (float64, error) { } } - return 0, ErrValueNotFound + return 0, fmt.Errorf("value for metric %s with key %s not found in result set", metric.Name, metric.Value) } func (metric *Metric) getLabels(result AggregationResult) ([]string, error) { diff --git a/internal/collector/collector_test.go b/internal/collector/collector_test.go index b120f776..fb9ec02d 100644 --- a/internal/collector/collector_test.go +++ b/internal/collector/collector_test.go @@ -142,14 +142,15 @@ func TestInitializeMetrics(t *testing.T) { aggregation: &Aggregation{ Metrics: []*Metric{ { - Name: "simple_gauge_value_not_found", - Type: "gauge", + Name: "simple_gauge_value_not_found", + Type: "gauge", + Value: "total", }, }, Pipeline: "[{\"$match\":{\"foo\":\"bar\"}}]", }, docs: []interface{}{AggregationResult{}}, - //error: "1 error occurred:\n\t* value not found in result set\n\n", + //error: "value for metric simple_gauge_value_not_found with key total not found in result set", expected: ``, }, {