Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove stats.New #2442

Merged
merged 2 commits into from
Mar 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions api/v1/metric_routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,15 @@ func TestGetMetrics(t *testing.T) {
logger.SetOutput(testutils.NewTestOutput(t))
registry := metrics.NewRegistry()
builtinMetrics := metrics.RegisterBuiltinMetrics(registry)
testMetric, err := registry.NewMetric("my_metric", stats.Trend, stats.Time)
require.NoError(t, err)
execScheduler, err := local.NewExecutionScheduler(&minirunner.MiniRunner{}, builtinMetrics, logger)
require.NoError(t, err)
engine, err := core.NewEngine(execScheduler, lib.Options{}, lib.RuntimeOptions{}, nil, logger, registry)
require.NoError(t, err)

engine.MetricsEngine.ObservedMetrics = map[string]*stats.Metric{
"my_metric": stats.New("my_metric", stats.Trend, stats.Time),
"my_metric": testMetric,
}
engine.MetricsEngine.ObservedMetrics["my_metric"].Tainted = null.BoolFrom(true)

Expand Down Expand Up @@ -106,14 +108,16 @@ func TestGetMetric(t *testing.T) {
logger := logrus.New()
logger.SetOutput(testutils.NewTestOutput(t))
registry := metrics.NewRegistry()
testMetric, err := registry.NewMetric("my_metric", stats.Trend, stats.Time)
require.NoError(t, err)
builtinMetrics := metrics.RegisterBuiltinMetrics(registry)
execScheduler, err := local.NewExecutionScheduler(&minirunner.MiniRunner{}, builtinMetrics, logger)
require.NoError(t, err)
engine, err := core.NewEngine(execScheduler, lib.Options{}, lib.RuntimeOptions{}, nil, logger, registry)
require.NoError(t, err)

engine.MetricsEngine.ObservedMetrics = map[string]*stats.Metric{
"my_metric": stats.New("my_metric", stats.Trend, stats.Time),
"my_metric": testMetric,
}
engine.MetricsEngine.ObservedMetrics["my_metric"].Tainted = null.BoolFrom(true)

Expand Down
7 changes: 5 additions & 2 deletions 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/metrics"
"go.k6.io/k6/stats"
)

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

old := stats.New("name", stats.Trend, stats.Time)
old, err := metrics.NewRegistry().NewMetric("test_metric", stats.Trend, stats.Time)
require.NoError(t, err)
old.Tainted = null.BoolFrom(true)
m := NewMetric(old, 0)
assert.Equal(t, "name", m.Name)
assert.Equal(t, "test_metric", m.Name)
assert.True(t, m.Type.Valid)
assert.Equal(t, stats.Trend, m.Type.Type)
assert.True(t, m.Contains.Valid)
Expand Down
3 changes: 2 additions & 1 deletion core/local/local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1227,7 +1227,8 @@ 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
35 changes: 24 additions & 11 deletions js/summary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (

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

Expand Down Expand Up @@ -98,21 +99,28 @@ 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)
subMetric.Sink.Add(stats.Sample{Value: 1})
subMetricPost := stats.New("my_parent_post{sub:2}", stats.Counter)
subMetricPost.Sink.Add(stats.Sample{Value: 2})
subMetric, err := parentMetric.AddSubmetric("sub:1")
require.NoError(t, err)
subMetric.Metric.Sink.Add(stats.Sample{Value: 1})

subMetricPost, err := parentMetricPost.AddSubmetric("sub:2")
require.NoError(t, err)
subMetricPost.Metric.Sink.Add(stats.Sample{Value: 2})

metrics := map[string]*stats.Metric{
parentMetric.Name: parentMetric,
parentMetricPost.Name: parentMetricPost,
subMetric.Name: subMetric,
subMetricPost.Name: subMetricPost,
subMetric.Name: subMetric.Metric,
subMetricPost.Name: subMetricPost.Metric,
}

summary := &lib.Summary{
Expand Down Expand Up @@ -147,15 +155,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
28 changes: 27 additions & 1 deletion metrics/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (r *Registry) NewMetric(name string, typ stats.MetricType, t ...stats.Value
oldMetric, ok := r.metrics[name]

if !ok {
m := stats.New(name, typ, t...)
m := newMetric(name, typ, t...)
r.metrics[name] = m
return m, nil
}
Expand Down Expand Up @@ -91,3 +91,29 @@ func (r *Registry) MustNewMetric(name string, typ stats.MetricType, t ...stats.V
func (r *Registry) Get(name string) *stats.Metric {
return r.metrics[name]
}

func newMetric(name string, mt stats.MetricType, vt ...stats.ValueType) *stats.Metric {
oleiade marked this conversation as resolved.
Show resolved Hide resolved
valueType := stats.Default
if len(vt) > 0 {
valueType = vt[0]
}
var sink stats.Sink
switch mt {
case stats.Counter:
sink = &stats.CounterSink{}
case stats.Gauge:
sink = &stats.GaugeSink{}
case stats.Trend:
sink = &stats.TrendSink{}
case stats.Rate:
sink = &stats.RateSink{}
default:
return nil
}
return &stats.Metric{
Name: name,
Type: mt,
Contains: valueType,
Sink: sink,
}
}
22 changes: 15 additions & 7 deletions output/csv/output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (

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

func TestSampleToRow(t *testing.T) {
testMetric, err := metrics.NewRegistry().NewMetric("my_metric", stats.Gauge)
require.NoError(t, err)

testData := []struct {
testname string
sample *stats.Sample
Expand All @@ -75,7 +79,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 +94,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 +111,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 +218,10 @@ func readCompressedFile(fileName string, fs afero.Fs) string {

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

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

testData := []struct {
samples []stats.SampleContainer
fileName string
Expand All @@ -224,7 +232,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 +242,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 +260,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 +270,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
14 changes: 12 additions & 2 deletions output/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,20 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

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

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

registry := metrics.NewRegistry()
metric, err := registry.NewMetric("my_metric", stats.Rate)
require.NoError(t, err)

single := stats.Sample{
Time: time.Now(),
Metric: stats.New("my_metric", stats.Rate),
Metric: metric,
Value: float64(123),
Tags: stats.NewSampleTags(map[string]string{"tag1": "val1"}),
}
Expand Down Expand Up @@ -70,6 +76,10 @@ func TestSampleBufferConcurrently(t *testing.T) {
r := rand.New(rand.NewSource(seed)) //nolint:gosec
t.Logf("Random source seeded with %d\n", seed)

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

producersCount := 50 + r.Intn(50)
sampleCount := 10 + r.Intn(10)
sleepModifier := 10 + r.Intn(10)
Expand All @@ -80,7 +90,7 @@ func TestSampleBufferConcurrently(t *testing.T) {
for i := 0; i < sampleCount; i++ {
buffer.AddMetricSamples([]stats.SampleContainer{stats.Sample{
Time: time.Unix(1562324644, 0),
Metric: stats.New("my_metric", stats.Gauge),
Metric: metric,
Value: float64(i),
Tags: stats.NewSampleTags(map[string]string{"tag1": "val1"}),
}})
Expand Down
7 changes: 6 additions & 1 deletion output/influxdb/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,15 @@ import (
"testing"
"time"

"github.com/stretchr/testify/require"
"go.k6.io/k6/metrics"
"go.k6.io/k6/stats"
)

func benchmarkInfluxdb(b *testing.B, t time.Duration) {
metric, err := metrics.NewRegistry().NewMetric("test_gauge", stats.Gauge)
require.NoError(b, err)

testOutputCycle(b, func(rw http.ResponseWriter, r *http.Request) {
for {
time.Sleep(t)
Expand All @@ -47,7 +52,7 @@ func benchmarkInfluxdb(b *testing.B, t time.Duration) {
samples := make(stats.Samples, 10)
for i := 0; i < len(samples); i++ {
samples[i] = stats.Sample{
Metric: stats.New("testGauge", stats.Gauge),
Metric: metric,
Time: time.Now(),
Tags: stats.NewSampleTags(map[string]string{
"something": "else",
Expand Down
13 changes: 11 additions & 2 deletions output/influxdb/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/testutils"
"go.k6.io/k6/metrics"
"go.k6.io/k6/output"
"go.k6.io/k6/stats"
)
Expand Down Expand Up @@ -108,10 +109,14 @@ func testOutputCycle(t testing.TB, handler http.HandlerFunc, body func(testing.T
func TestOutput(t *testing.T) {
t.Parallel()

metric, err := metrics.NewRegistry().NewMetric("test_gauge", stats.Gauge)
require.NoError(t, err)

var samplesRead int
defer func() {
require.Equal(t, samplesRead, 20)
}()

testOutputCycle(t, func(rw http.ResponseWriter, r *http.Request) {
b := bytes.NewBuffer(nil)
_, _ = io.Copy(b, r.Body)
Expand All @@ -130,7 +135,7 @@ func TestOutput(t *testing.T) {
samples := make(stats.Samples, 10)
for i := 0; i < len(samples); i++ {
samples[i] = stats.Sample{
Metric: stats.New("testGauge", stats.Gauge),
Metric: metric,
Time: time.Now(),
Tags: stats.NewSampleTags(map[string]string{
"something": "else",
Expand All @@ -152,6 +157,7 @@ func TestOutputFlushMetricsConcurrency(t *testing.T) {
requests = int32(0)
block = make(chan struct{})
)

wg := sync.WaitGroup{}
ts := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
// block all the received requests
Expand All @@ -170,6 +176,9 @@ func TestOutputFlushMetricsConcurrency(t *testing.T) {
ts.Close()
}()

metric, err := metrics.NewRegistry().NewMetric("test_gauge", stats.Gauge)
require.NoError(t, err)

o, err := newOutput(output.Params{
Logger: testutils.NewLogger(t),
ConfigArgument: ts.URL,
Expand All @@ -183,7 +192,7 @@ func TestOutputFlushMetricsConcurrency(t *testing.T) {
wg.Add(1)
o.AddMetricSamples([]stats.SampleContainer{stats.Samples{
stats.Sample{
Metric: stats.New("gauge", stats.Gauge),
Metric: metric,
Value: 2.0,
},
}})
Expand Down
Loading