diff --git a/core/engine.go b/core/engine.go index a0c3d651f90..77488ee69d2 100644 --- a/core/engine.go +++ b/core/engine.go @@ -153,6 +153,16 @@ func (e *Engine) initSubMetricsAndThresholds() error { metric.Thresholds = thresholds e.metricsWithThresholds = append(e.metricsWithThresholds, metric) + + // Mark the metric (and the parent metricq, if we're dealing with a + // submetric) as observed, so they are shown in the end-of-test summary, + // even if they don't have any metric samples during the test run + metric.Observed = true + e.Metrics[metric.Name] = metric + if metric.Sub != nil { + metric.Sub.Metric.Observed = true + e.Metrics[metric.Sub.Metric.Name] = metric.Sub.Metric + } } // TODO: refactor out of here when https://github.com/grafana/k6/issues/1321 @@ -439,7 +449,7 @@ func (e *Engine) processThresholds() (shouldAbort bool) { t := e.executionState.GetCurrentTestRunDuration() e.thresholdsTainted = false - for _, m := range e.Metrics { + for _, m := range e.metricsWithThresholds { if len(m.Thresholds.Thresholds) == 0 { continue } diff --git a/core/engine_test.go b/core/engine_test.go index eb20cba88fa..c3aac19c467 100644 --- a/core/engine_test.go +++ b/core/engine_test.go @@ -395,7 +395,12 @@ func TestEngine_processThresholds(t *testing.T) { "submetric,match,passing": {true, map[string][]string{"my_metric{a:1}": {"value<2"}}, false}, "submetric,match,failing": {false, map[string][]string{"my_metric{a:1}": {"value>1.25"}}, false}, "submetric,nomatch,passing": {true, map[string][]string{"my_metric{a:2}": {"value<2"}}, false}, - "submetric,nomatch,failing": {true, map[string][]string{"my_metric{a:2}": {"value>1.25"}}, false}, + "submetric,nomatch,failing": {false, map[string][]string{"my_metric{a:2}": {"value>1.25"}}, false}, + + "unused,passing": {true, map[string][]string{"unused_counter": {"count==0"}}, false}, + "unused,failing": {false, map[string][]string{"unused_counter": {"count>1"}}, false}, + "unused,subm,passing": {true, map[string][]string{"unused_counter{a:2}": {"count<1"}}, false}, + "unused,subm,failing": {false, map[string][]string{"unused_counter{a:2}": {"count>1"}}, false}, } for name, data := range testdata { @@ -406,6 +411,8 @@ func TestEngine_processThresholds(t *testing.T) { registry := metrics.NewRegistry() metric, err := registry.NewMetric("my_metric", stats.Gauge) require.NoError(t, err) + _, err = registry.NewMetric("unused_counter", stats.Counter) + require.NoError(t, err) thresholds := make(map[string]stats.Thresholds, len(data.ths)) for m, srcs := range data.ths {