Skip to content

Commit

Permalink
Remove usage of stats.New in testing code
Browse files Browse the repository at this point in the history
  • Loading branch information
oleiade committed Mar 14, 2022
1 parent 69821b2 commit 6d90d23
Show file tree
Hide file tree
Showing 14 changed files with 153 additions and 75 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/xk6-tests/xk6-js-test/jstest.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"time"

"go.k6.io/k6/lib"
"go.k6.io/k6/lib/metrics"
"go.k6.io/k6/stats"

"go.k6.io/k6/js/modules"
Expand All @@ -44,7 +45,8 @@ func (j JSTest) Foo(ctx context.Context, arg float64) (bool, error) {
return false, fmt.Errorf("called in init context")
}

allTheFoos := stats.New("foos", stats.Counter)
registry := metrics.NewRegistry()
allTheFoos := registry.MustNewMetric("foos", stats.Counter)
tags := state.CloneTags()
tags["foo"] = "bar"
stats.PushIfNotDone(ctx, state.Samples, stats.Sample{
Expand Down
8 changes: 6 additions & 2 deletions api/v1/metric_routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,10 @@ func TestGetMetrics(t *testing.T) {
engine, err := core.NewEngine(execScheduler, lib.Options{}, lib.RuntimeOptions{}, nil, logger, builtinMetrics)
require.NoError(t, err)

m, err := registry.NewMetric("my_metric", stats.Trend, stats.Time)
require.NoError(t, err)
engine.Metrics = map[string]*stats.Metric{
"my_metric": stats.New("my_metric", stats.Trend, stats.Time),
"my_metric": m,
}
engine.Metrics["my_metric"].Tainted = null.BoolFrom(true)

Expand Down Expand Up @@ -112,8 +114,10 @@ func TestGetMetric(t *testing.T) {
engine, err := core.NewEngine(execScheduler, lib.Options{}, lib.RuntimeOptions{}, nil, logger, builtinMetrics)
require.NoError(t, err)

m, err := registry.NewMetric("my_metric", stats.Trend, stats.Time)
require.NoError(t, err)
engine.Metrics = map[string]*stats.Metric{
"my_metric": stats.New("my_metric", stats.Trend, stats.Time),
"my_metric": m,
}
engine.Metrics["my_metric"].Tainted = null.BoolFrom(true)

Expand Down
6 changes: 5 additions & 1 deletion api/v1/metric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/guregu/null.v3"

"go.k6.io/k6/lib/metrics"
"go.k6.io/k6/stats"
)

Expand Down Expand Up @@ -107,7 +109,9 @@ func TestNullValueTypeJSON(t *testing.T) {
func TestNewMetric(t *testing.T) {
t.Parallel()

old := stats.New("name", stats.Trend, stats.Time)
registry := metrics.NewRegistry()
old, err := registry.NewMetric("name", stats.Trend, stats.Time)
require.NoError(t, err)
old.Tainted = null.BoolFrom(true)
m := NewMetric(old, 0)
assert.Equal(t, "name", m.Name)
Expand Down
29 changes: 22 additions & 7 deletions core/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ func TestNewEngine(t *testing.T) {
func TestEngineRun(t *testing.T) {
t.Parallel()
logrus.SetLevel(logrus.DebugLevel)
registry := metrics.NewRegistry()

t.Run("exits with context", func(t *testing.T) {
t.Parallel()
done := make(chan struct{})
Expand Down Expand Up @@ -139,7 +141,8 @@ func TestEngineRun(t *testing.T) {
// Make sure samples are discarded after context close (using "cutoff" timestamp in local.go)
t.Run("collects samples", func(t *testing.T) {
t.Parallel()
testMetric := stats.New("test_metric", stats.Trend)
testMetric, err := registry.NewMetric("test_metric", stats.Trend)
require.NoError(t, err)

signalChan := make(chan interface{})

Expand Down Expand Up @@ -211,7 +214,9 @@ func TestEngineStopped(t *testing.T) {

func TestEngineOutput(t *testing.T) {
t.Parallel()
testMetric := stats.New("test_metric", stats.Trend)
registry := metrics.NewRegistry()
testMetric, err := registry.NewMetric("test_metric", stats.Trend)
require.NoError(t, err)

runner := &minirunner.MiniRunner{
Fn: func(ctx context.Context, _ *lib.State, out chan<- stats.SampleContainer) error {
Expand Down Expand Up @@ -248,7 +253,9 @@ func TestEngineOutput(t *testing.T) {

func TestEngine_processSamples(t *testing.T) {
t.Parallel()
metric := stats.New("my_metric", stats.Gauge)
registry := metrics.NewRegistry()
metric, err := registry.NewMetric("my_metric", stats.Gauge)
require.NoError(t, err)

t.Run("metric", func(t *testing.T) {
t.Parallel()
Expand Down Expand Up @@ -290,7 +297,9 @@ func TestEngine_processSamples(t *testing.T) {

func TestEngineThresholdsWillAbort(t *testing.T) {
t.Parallel()
metric := stats.New("my_metric", stats.Gauge)
registry := metrics.NewRegistry()
metric, err := registry.NewMetric("my_metric", stats.Gauge)
require.NoError(t, err)

// The incoming samples for the metric set it to 1.25. Considering
// the metric is of type Gauge, value > 1.25 should always fail, and
Expand All @@ -313,7 +322,9 @@ func TestEngineThresholdsWillAbort(t *testing.T) {

func TestEngineAbortedByThresholds(t *testing.T) {
t.Parallel()
metric := stats.New("my_metric", stats.Gauge)
registry := metrics.NewRegistry()
metric, err := registry.NewMetric("my_metric", stats.Gauge)
require.NoError(t, err)

// The MiniRunner sets the value of the metric to 1.25. Considering
// the metric is of type Gauge, value > 1.25 should always fail, and
Expand Down Expand Up @@ -353,7 +364,9 @@ func TestEngineAbortedByThresholds(t *testing.T) {

func TestEngine_processThresholds(t *testing.T) {
t.Parallel()
metric := stats.New("my_metric", stats.Gauge)
registry := metrics.NewRegistry()
metric, err := registry.NewMetric("my_metric", stats.Gauge)
require.NoError(t, err)

testdata := map[string]struct {
pass bool
Expand Down Expand Up @@ -1128,7 +1141,9 @@ func TestMinIterationDurationInSetupTeardownStage(t *testing.T) {

func TestEngineRunsTeardownEvenAfterTestRunIsAborted(t *testing.T) {
t.Parallel()
testMetric := stats.New("teardown_metric", stats.Counter)
registry := metrics.NewRegistry()
testMetric, err := registry.NewMetric("teardown_metric", stats.Counter)
require.NoError(t, err)

ctx, cancel := context.WithCancel(context.Background())

Expand Down
4 changes: 3 additions & 1 deletion core/local/local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1228,7 +1228,9 @@ func TestRealTimeAndSetupTeardownMetrics(t *testing.T) {
}
return stats.IntoSampleTags(&tags)
}
testCounter := stats.New("test_counter", stats.Counter)

testCounter, err := registry.NewMetric("test_counter", stats.Counter)
require.NoError(t, err)
getSample := func(expValue float64, expMetric *stats.Metric, expTags ...string) stats.SampleContainer {
return stats.Sample{
Metric: expMetric,
Expand Down
39 changes: 32 additions & 7 deletions js/summary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"gopkg.in/guregu/null.v3"

"go.k6.io/k6/lib"
"go.k6.io/k6/lib/metrics"
"go.k6.io/k6/lib/testutils"
"go.k6.io/k6/stats"
)
Expand Down Expand Up @@ -98,14 +99,33 @@ func TestTextSummary(t *testing.T) {
func TestTextSummaryWithSubMetrics(t *testing.T) {
t.Parallel()

parentMetric := stats.New("my_parent", stats.Counter)
registry := metrics.NewRegistry()
parentMetric, err := registry.NewMetric("my_parent", stats.Counter)
require.NoError(t, err)
parentMetric.Sink.Add(stats.Sample{Value: 11})
parentMetricPost := stats.New("my_parent_post", stats.Counter)

parentMetricPost, err := registry.NewMetric("my_parent_post", stats.Counter)
require.NoError(t, err)
parentMetricPost.Sink.Add(stats.Sample{Value: 22})

subMetric := stats.New("my_parent{sub:1}", stats.Counter)
// We instantiate Metric directly as Registry.NewMetric uses
// `checkName`, which does not handle the tag syntax.
subMetric := &stats.Metric{
Name: "my_parent{sub:1}",
Type: stats.Counter,
Contains: stats.Default,
Sink: &stats.CounterSink{},
}
subMetric.Sink.Add(stats.Sample{Value: 1})
subMetricPost := stats.New("my_parent_post{sub:2}", stats.Counter)

// We instantiate Metric directly as Registry.NewMetric uses
// `checkName`, which does not handle the tag syntax.
subMetricPost := &stats.Metric{
Name: "my_parent_post{sub:2}",
Type: stats.Counter,
Contains: stats.Default,
Sink: &stats.CounterSink{},
}
subMetricPost.Sink.Add(stats.Sample{Value: 2})

metrics := map[string]*stats.Metric{
Expand Down Expand Up @@ -147,15 +167,20 @@ func TestTextSummaryWithSubMetrics(t *testing.T) {
}

func createTestMetrics(t *testing.T) (map[string]*stats.Metric, *lib.Group) {
registry := metrics.NewRegistry()

metrics := make(map[string]*stats.Metric)
gaugeMetric := stats.New("vus", stats.Gauge)
gaugeMetric, err := registry.NewMetric("vus", stats.Gauge)
require.NoError(t, err)
gaugeMetric.Sink.Add(stats.Sample{Value: 1})

countMetric := stats.New("http_reqs", stats.Counter)
countMetric, err := registry.NewMetric("http_reqs", stats.Counter)
require.NoError(t, err)
countMetric.Tainted = null.BoolFrom(true)
countMetric.Thresholds = stats.Thresholds{Thresholds: []*stats.Threshold{{Source: "rate<100", LastFailed: true}}}

checksMetric := stats.New("checks", stats.Rate)
checksMetric, err := registry.NewMetric("checks", stats.Rate)
require.NoError(t, err)
checksMetric.Tainted = null.BoolFrom(false)
checksMetric.Thresholds = stats.Thresholds{Thresholds: []*stats.Threshold{{Source: "rate>70", LastFailed: false}}}
sink := &stats.TrendSink{}
Expand Down
30 changes: 16 additions & 14 deletions lib/metrics/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func checkName(name string) bool {

// NewMetric returns new metric registered to this registry
// TODO have multiple versions returning specific metric types when we have such things
func (r *Registry) NewMetric(name string, typ stats.MetricType, t ...stats.ValueType) (*stats.Metric, error) {
func (r *Registry) NewMetric(name string, mt stats.MetricType, vt ...stats.ValueType) (*stats.Metric, error) {
r.l.Lock()
defer r.l.Unlock()

Expand All @@ -61,12 +61,13 @@ func (r *Registry) NewMetric(name string, typ stats.MetricType, t ...stats.Value
oldMetric, ok := r.metrics[name]

if !ok {
vt := stats.Default
if len(t) > 0 {
vt = t[0]
valueType := stats.Default
if len(vt) > 0 {
valueType = vt[0]
}

var sink stats.Sink
switch typ {
switch mt {
case stats.Counter:
sink = &stats.CounterSink{}
case stats.Gauge:
Expand All @@ -76,22 +77,23 @@ func (r *Registry) NewMetric(name string, typ stats.MetricType, t ...stats.Value
case stats.Rate:
sink = &stats.RateSink{}
default:
return nil, fmt.Errorf("unable to create metric of type %q; reason: unknown metric type", typ.String())
return nil, fmt.Errorf("unable to create metric of type %q; reason: unknown metric type", mt.String())
}

m := &stats.Metric{Name: name, Type: typ, Contains: vt, Sink: sink}
m := &stats.Metric{Name: name, Type: mt, Contains: valueType, Sink: sink}
r.metrics[name] = m
return m, nil
}
if oldMetric.Type != typ {
return nil, fmt.Errorf("metric '%s' already exists but with type %s, instead of %s", name, oldMetric.Type, typ)

if oldMetric.Type != mt {
return nil, fmt.Errorf("metric '%s' already exists but with type %s, instead of %s", name, oldMetric.Type, mt)
}
if len(t) > 0 {
if t[0] != oldMetric.Contains {
return nil, fmt.Errorf("metric '%s' already exists but with a value type %s, instead of %s",
name, oldMetric.Contains, t[0])
}

if len(vt) > 0 && vt[0] != oldMetric.Contains {
return nil, fmt.Errorf("metric '%s' already exists but with a value type %s, instead of %s",
name, oldMetric.Contains, vt[0])
}

return oldMetric, nil
}

Expand Down
25 changes: 18 additions & 7 deletions output/csv/output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"github.com/stretchr/testify/require"

"go.k6.io/k6/lib"
"go.k6.io/k6/lib/metrics"
"go.k6.io/k6/lib/testutils"
"go.k6.io/k6/output"
"go.k6.io/k6/stats"
Expand Down Expand Up @@ -65,6 +66,12 @@ func TestMakeHeader(t *testing.T) {
}

func TestSampleToRow(t *testing.T) {
t.Parallel()

testRegistry := metrics.NewRegistry()
testMetric, err := testRegistry.NewMetric("my_metric", stats.Gauge)
require.NoError(t, err)

testData := []struct {
testname string
sample *stats.Sample
Expand All @@ -75,7 +82,7 @@ func TestSampleToRow(t *testing.T) {
testname: "One res tag, one ignored tag, one extra tag",
sample: &stats.Sample{
Time: time.Unix(1562324644, 0),
Metric: stats.New("my_metric", stats.Gauge),
Metric: testMetric,
Value: 1,
Tags: stats.NewSampleTags(map[string]string{
"tag1": "val1",
Expand All @@ -90,7 +97,7 @@ func TestSampleToRow(t *testing.T) {
testname: "Two res tags, three extra tags",
sample: &stats.Sample{
Time: time.Unix(1562324644, 0),
Metric: stats.New("my_metric", stats.Gauge),
Metric: testMetric,
Value: 1,
Tags: stats.NewSampleTags(map[string]string{
"tag1": "val1",
Expand All @@ -107,7 +114,7 @@ func TestSampleToRow(t *testing.T) {
testname: "Two res tags, two ignored",
sample: &stats.Sample{
Time: time.Unix(1562324644, 0),
Metric: stats.New("my_metric", stats.Gauge),
Metric: testMetric,
Value: 1,
Tags: stats.NewSampleTags(map[string]string{
"tag1": "val1",
Expand Down Expand Up @@ -214,6 +221,10 @@ func readCompressedFile(fileName string, fs afero.Fs) string {

func TestRun(t *testing.T) {
t.Parallel()

testRegistry := metrics.NewRegistry()
testMetric, err := testRegistry.NewMetric("my_metric", stats.Gauge)
require.NoError(t, err)
testData := []struct {
samples []stats.SampleContainer
fileName string
Expand All @@ -224,7 +235,7 @@ func TestRun(t *testing.T) {
samples: []stats.SampleContainer{
stats.Sample{
Time: time.Unix(1562324643, 0),
Metric: stats.New("my_metric", stats.Gauge),
Metric: testMetric,
Value: 1,
Tags: stats.NewSampleTags(map[string]string{
"check": "val1",
Expand All @@ -234,7 +245,7 @@ func TestRun(t *testing.T) {
},
stats.Sample{
Time: time.Unix(1562324644, 0),
Metric: stats.New("my_metric", stats.Gauge),
Metric: testMetric,
Value: 1,
Tags: stats.NewSampleTags(map[string]string{
"check": "val1",
Expand All @@ -252,7 +263,7 @@ func TestRun(t *testing.T) {
samples: []stats.SampleContainer{
stats.Sample{
Time: time.Unix(1562324643, 0),
Metric: stats.New("my_metric", stats.Gauge),
Metric: testMetric,
Value: 1,
Tags: stats.NewSampleTags(map[string]string{
"check": "val1",
Expand All @@ -262,7 +273,7 @@ func TestRun(t *testing.T) {
},
stats.Sample{
Time: time.Unix(1562324644, 0),
Metric: stats.New("my_metric", stats.Gauge),
Metric: testMetric,
Value: 1,
Tags: stats.NewSampleTags(map[string]string{
"check": "val1",
Expand Down
Loading

0 comments on commit 6d90d23

Please sign in to comment.