Skip to content

Commit bd6adbd

Browse files
authored
Merge pull request #2442 from grafana/refactor/remove_stats_New
Remove `stats.New`
2 parents 6ed97b4 + f75251f commit bd6adbd

File tree

13 files changed

+144
-47
lines changed

13 files changed

+144
-47
lines changed

api/v1/metric_routes_test.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,15 @@ func TestGetMetrics(t *testing.T) {
4747
logger.SetOutput(testutils.NewTestOutput(t))
4848
registry := metrics.NewRegistry()
4949
builtinMetrics := metrics.RegisterBuiltinMetrics(registry)
50+
testMetric, err := registry.NewMetric("my_metric", stats.Trend, stats.Time)
51+
require.NoError(t, err)
5052
execScheduler, err := local.NewExecutionScheduler(&minirunner.MiniRunner{}, builtinMetrics, logger)
5153
require.NoError(t, err)
5254
engine, err := core.NewEngine(execScheduler, lib.Options{}, lib.RuntimeOptions{}, nil, logger, registry)
5355
require.NoError(t, err)
5456

5557
engine.MetricsEngine.ObservedMetrics = map[string]*stats.Metric{
56-
"my_metric": stats.New("my_metric", stats.Trend, stats.Time),
58+
"my_metric": testMetric,
5759
}
5860
engine.MetricsEngine.ObservedMetrics["my_metric"].Tainted = null.BoolFrom(true)
5961

@@ -106,14 +108,16 @@ func TestGetMetric(t *testing.T) {
106108
logger := logrus.New()
107109
logger.SetOutput(testutils.NewTestOutput(t))
108110
registry := metrics.NewRegistry()
111+
testMetric, err := registry.NewMetric("my_metric", stats.Trend, stats.Time)
112+
require.NoError(t, err)
109113
builtinMetrics := metrics.RegisterBuiltinMetrics(registry)
110114
execScheduler, err := local.NewExecutionScheduler(&minirunner.MiniRunner{}, builtinMetrics, logger)
111115
require.NoError(t, err)
112116
engine, err := core.NewEngine(execScheduler, lib.Options{}, lib.RuntimeOptions{}, nil, logger, registry)
113117
require.NoError(t, err)
114118

115119
engine.MetricsEngine.ObservedMetrics = map[string]*stats.Metric{
116-
"my_metric": stats.New("my_metric", stats.Trend, stats.Time),
120+
"my_metric": testMetric,
117121
}
118122
engine.MetricsEngine.ObservedMetrics["my_metric"].Tainted = null.BoolFrom(true)
119123

api/v1/metric_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@ import (
2525
"testing"
2626

2727
"github.com/stretchr/testify/assert"
28+
"github.com/stretchr/testify/require"
2829
"gopkg.in/guregu/null.v3"
2930

31+
"go.k6.io/k6/metrics"
3032
"go.k6.io/k6/stats"
3133
)
3234

@@ -107,10 +109,11 @@ func TestNullValueTypeJSON(t *testing.T) {
107109
func TestNewMetric(t *testing.T) {
108110
t.Parallel()
109111

110-
old := stats.New("name", stats.Trend, stats.Time)
112+
old, err := metrics.NewRegistry().NewMetric("test_metric", stats.Trend, stats.Time)
113+
require.NoError(t, err)
111114
old.Tainted = null.BoolFrom(true)
112115
m := NewMetric(old, 0)
113-
assert.Equal(t, "name", m.Name)
116+
assert.Equal(t, "test_metric", m.Name)
114117
assert.True(t, m.Type.Valid)
115118
assert.Equal(t, stats.Trend, m.Type.Type)
116119
assert.True(t, m.Contains.Valid)

core/local/local_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1227,7 +1227,8 @@ func TestRealTimeAndSetupTeardownMetrics(t *testing.T) {
12271227
}
12281228
return stats.IntoSampleTags(&tags)
12291229
}
1230-
testCounter := stats.New("test_counter", stats.Counter)
1230+
testCounter, err := registry.NewMetric("test_counter", stats.Counter)
1231+
require.NoError(t, err)
12311232
getSample := func(expValue float64, expMetric *stats.Metric, expTags ...string) stats.SampleContainer {
12321233
return stats.Sample{
12331234
Metric: expMetric,

js/summary_test.go

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535

3636
"go.k6.io/k6/lib"
3737
"go.k6.io/k6/lib/testutils"
38+
"go.k6.io/k6/metrics"
3839
"go.k6.io/k6/stats"
3940
)
4041

@@ -98,21 +99,28 @@ func TestTextSummary(t *testing.T) {
9899
func TestTextSummaryWithSubMetrics(t *testing.T) {
99100
t.Parallel()
100101

101-
parentMetric := stats.New("my_parent", stats.Counter)
102+
registry := metrics.NewRegistry()
103+
parentMetric, err := registry.NewMetric("my_parent", stats.Counter)
104+
require.NoError(t, err)
102105
parentMetric.Sink.Add(stats.Sample{Value: 11})
103-
parentMetricPost := stats.New("my_parent_post", stats.Counter)
106+
107+
parentMetricPost, err := registry.NewMetric("my_parent_post", stats.Counter)
108+
require.NoError(t, err)
104109
parentMetricPost.Sink.Add(stats.Sample{Value: 22})
105110

106-
subMetric := stats.New("my_parent{sub:1}", stats.Counter)
107-
subMetric.Sink.Add(stats.Sample{Value: 1})
108-
subMetricPost := stats.New("my_parent_post{sub:2}", stats.Counter)
109-
subMetricPost.Sink.Add(stats.Sample{Value: 2})
111+
subMetric, err := parentMetric.AddSubmetric("sub:1")
112+
require.NoError(t, err)
113+
subMetric.Metric.Sink.Add(stats.Sample{Value: 1})
114+
115+
subMetricPost, err := parentMetricPost.AddSubmetric("sub:2")
116+
require.NoError(t, err)
117+
subMetricPost.Metric.Sink.Add(stats.Sample{Value: 2})
110118

111119
metrics := map[string]*stats.Metric{
112120
parentMetric.Name: parentMetric,
113121
parentMetricPost.Name: parentMetricPost,
114-
subMetric.Name: subMetric,
115-
subMetricPost.Name: subMetricPost,
122+
subMetric.Name: subMetric.Metric,
123+
subMetricPost.Name: subMetricPost.Metric,
116124
}
117125

118126
summary := &lib.Summary{
@@ -147,15 +155,20 @@ func TestTextSummaryWithSubMetrics(t *testing.T) {
147155
}
148156

149157
func createTestMetrics(t *testing.T) (map[string]*stats.Metric, *lib.Group) {
158+
registry := metrics.NewRegistry()
150159
metrics := make(map[string]*stats.Metric)
151-
gaugeMetric := stats.New("vus", stats.Gauge)
160+
161+
gaugeMetric, err := registry.NewMetric("vus", stats.Gauge)
162+
require.NoError(t, err)
152163
gaugeMetric.Sink.Add(stats.Sample{Value: 1})
153164

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

158-
checksMetric := stats.New("checks", stats.Rate)
170+
checksMetric, err := registry.NewMetric("checks", stats.Rate)
171+
require.NoError(t, err)
159172
checksMetric.Tainted = null.BoolFrom(false)
160173
checksMetric.Thresholds = stats.Thresholds{Thresholds: []*stats.Threshold{{Source: "rate>70", LastFailed: false}}}
161174
sink := &stats.TrendSink{}

metrics/registry.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func (r *Registry) NewMetric(name string, typ stats.MetricType, t ...stats.Value
6161
oldMetric, ok := r.metrics[name]
6262

6363
if !ok {
64-
m := stats.New(name, typ, t...)
64+
m := newMetric(name, typ, t...)
6565
r.metrics[name] = m
6666
return m, nil
6767
}
@@ -91,3 +91,29 @@ func (r *Registry) MustNewMetric(name string, typ stats.MetricType, t ...stats.V
9191
func (r *Registry) Get(name string) *stats.Metric {
9292
return r.metrics[name]
9393
}
94+
95+
func newMetric(name string, mt stats.MetricType, vt ...stats.ValueType) *stats.Metric {
96+
valueType := stats.Default
97+
if len(vt) > 0 {
98+
valueType = vt[0]
99+
}
100+
var sink stats.Sink
101+
switch mt {
102+
case stats.Counter:
103+
sink = &stats.CounterSink{}
104+
case stats.Gauge:
105+
sink = &stats.GaugeSink{}
106+
case stats.Trend:
107+
sink = &stats.TrendSink{}
108+
case stats.Rate:
109+
sink = &stats.RateSink{}
110+
default:
111+
return nil
112+
}
113+
return &stats.Metric{
114+
Name: name,
115+
Type: mt,
116+
Contains: valueType,
117+
Sink: sink,
118+
}
119+
}

output/csv/output_test.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import (
3737

3838
"go.k6.io/k6/lib"
3939
"go.k6.io/k6/lib/testutils"
40+
"go.k6.io/k6/metrics"
4041
"go.k6.io/k6/output"
4142
"go.k6.io/k6/stats"
4243
)
@@ -65,6 +66,9 @@ func TestMakeHeader(t *testing.T) {
6566
}
6667

6768
func TestSampleToRow(t *testing.T) {
69+
testMetric, err := metrics.NewRegistry().NewMetric("my_metric", stats.Gauge)
70+
require.NoError(t, err)
71+
6872
testData := []struct {
6973
testname string
7074
sample *stats.Sample
@@ -75,7 +79,7 @@ func TestSampleToRow(t *testing.T) {
7579
testname: "One res tag, one ignored tag, one extra tag",
7680
sample: &stats.Sample{
7781
Time: time.Unix(1562324644, 0),
78-
Metric: stats.New("my_metric", stats.Gauge),
82+
Metric: testMetric,
7983
Value: 1,
8084
Tags: stats.NewSampleTags(map[string]string{
8185
"tag1": "val1",
@@ -90,7 +94,7 @@ func TestSampleToRow(t *testing.T) {
9094
testname: "Two res tags, three extra tags",
9195
sample: &stats.Sample{
9296
Time: time.Unix(1562324644, 0),
93-
Metric: stats.New("my_metric", stats.Gauge),
97+
Metric: testMetric,
9498
Value: 1,
9599
Tags: stats.NewSampleTags(map[string]string{
96100
"tag1": "val1",
@@ -107,7 +111,7 @@ func TestSampleToRow(t *testing.T) {
107111
testname: "Two res tags, two ignored",
108112
sample: &stats.Sample{
109113
Time: time.Unix(1562324644, 0),
110-
Metric: stats.New("my_metric", stats.Gauge),
114+
Metric: testMetric,
111115
Value: 1,
112116
Tags: stats.NewSampleTags(map[string]string{
113117
"tag1": "val1",
@@ -214,6 +218,10 @@ func readCompressedFile(fileName string, fs afero.Fs) string {
214218

215219
func TestRun(t *testing.T) {
216220
t.Parallel()
221+
222+
testMetric, err := metrics.NewRegistry().NewMetric("my_metric", stats.Gauge)
223+
require.NoError(t, err)
224+
217225
testData := []struct {
218226
samples []stats.SampleContainer
219227
fileName string
@@ -224,7 +232,7 @@ func TestRun(t *testing.T) {
224232
samples: []stats.SampleContainer{
225233
stats.Sample{
226234
Time: time.Unix(1562324643, 0),
227-
Metric: stats.New("my_metric", stats.Gauge),
235+
Metric: testMetric,
228236
Value: 1,
229237
Tags: stats.NewSampleTags(map[string]string{
230238
"check": "val1",
@@ -234,7 +242,7 @@ func TestRun(t *testing.T) {
234242
},
235243
stats.Sample{
236244
Time: time.Unix(1562324644, 0),
237-
Metric: stats.New("my_metric", stats.Gauge),
245+
Metric: testMetric,
238246
Value: 1,
239247
Tags: stats.NewSampleTags(map[string]string{
240248
"check": "val1",
@@ -252,7 +260,7 @@ func TestRun(t *testing.T) {
252260
samples: []stats.SampleContainer{
253261
stats.Sample{
254262
Time: time.Unix(1562324643, 0),
255-
Metric: stats.New("my_metric", stats.Gauge),
263+
Metric: testMetric,
256264
Value: 1,
257265
Tags: stats.NewSampleTags(map[string]string{
258266
"check": "val1",
@@ -262,7 +270,7 @@ func TestRun(t *testing.T) {
262270
},
263271
stats.Sample{
264272
Time: time.Unix(1562324644, 0),
265-
Metric: stats.New("my_metric", stats.Gauge),
273+
Metric: testMetric,
266274
Value: 1,
267275
Tags: stats.NewSampleTags(map[string]string{
268276
"check": "val1",

output/helpers_test.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,20 @@ import (
2929
"github.com/stretchr/testify/assert"
3030
"github.com/stretchr/testify/require"
3131

32+
"go.k6.io/k6/metrics"
3233
"go.k6.io/k6/stats"
3334
)
3435

3536
func TestSampleBufferBasics(t *testing.T) {
3637
t.Parallel()
38+
39+
registry := metrics.NewRegistry()
40+
metric, err := registry.NewMetric("my_metric", stats.Rate)
41+
require.NoError(t, err)
42+
3743
single := stats.Sample{
3844
Time: time.Now(),
39-
Metric: stats.New("my_metric", stats.Rate),
45+
Metric: metric,
4046
Value: float64(123),
4147
Tags: stats.NewSampleTags(map[string]string{"tag1": "val1"}),
4248
}
@@ -70,6 +76,10 @@ func TestSampleBufferConcurrently(t *testing.T) {
7076
r := rand.New(rand.NewSource(seed)) //nolint:gosec
7177
t.Logf("Random source seeded with %d\n", seed)
7278

79+
registry := metrics.NewRegistry()
80+
metric, err := registry.NewMetric("my_metric", stats.Gauge)
81+
require.NoError(t, err)
82+
7383
producersCount := 50 + r.Intn(50)
7484
sampleCount := 10 + r.Intn(10)
7585
sleepModifier := 10 + r.Intn(10)
@@ -80,7 +90,7 @@ func TestSampleBufferConcurrently(t *testing.T) {
8090
for i := 0; i < sampleCount; i++ {
8191
buffer.AddMetricSamples([]stats.SampleContainer{stats.Sample{
8292
Time: time.Unix(1562324644, 0),
83-
Metric: stats.New("my_metric", stats.Gauge),
93+
Metric: metric,
8494
Value: float64(i),
8595
Tags: stats.NewSampleTags(map[string]string{"tag1": "val1"}),
8696
}})

output/influxdb/bench_test.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,15 @@ import (
2727
"testing"
2828
"time"
2929

30+
"github.com/stretchr/testify/require"
31+
"go.k6.io/k6/metrics"
3032
"go.k6.io/k6/stats"
3133
)
3234

3335
func benchmarkInfluxdb(b *testing.B, t time.Duration) {
36+
metric, err := metrics.NewRegistry().NewMetric("test_gauge", stats.Gauge)
37+
require.NoError(b, err)
38+
3439
testOutputCycle(b, func(rw http.ResponseWriter, r *http.Request) {
3540
for {
3641
time.Sleep(t)
@@ -47,7 +52,7 @@ func benchmarkInfluxdb(b *testing.B, t time.Duration) {
4752
samples := make(stats.Samples, 10)
4853
for i := 0; i < len(samples); i++ {
4954
samples[i] = stats.Sample{
50-
Metric: stats.New("testGauge", stats.Gauge),
55+
Metric: metric,
5156
Time: time.Now(),
5257
Tags: stats.NewSampleTags(map[string]string{
5358
"something": "else",

output/influxdb/output_test.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import (
3636
"github.com/stretchr/testify/require"
3737

3838
"go.k6.io/k6/lib/testutils"
39+
"go.k6.io/k6/metrics"
3940
"go.k6.io/k6/output"
4041
"go.k6.io/k6/stats"
4142
)
@@ -108,10 +109,14 @@ func testOutputCycle(t testing.TB, handler http.HandlerFunc, body func(testing.T
108109
func TestOutput(t *testing.T) {
109110
t.Parallel()
110111

112+
metric, err := metrics.NewRegistry().NewMetric("test_gauge", stats.Gauge)
113+
require.NoError(t, err)
114+
111115
var samplesRead int
112116
defer func() {
113117
require.Equal(t, samplesRead, 20)
114118
}()
119+
115120
testOutputCycle(t, func(rw http.ResponseWriter, r *http.Request) {
116121
b := bytes.NewBuffer(nil)
117122
_, _ = io.Copy(b, r.Body)
@@ -130,7 +135,7 @@ func TestOutput(t *testing.T) {
130135
samples := make(stats.Samples, 10)
131136
for i := 0; i < len(samples); i++ {
132137
samples[i] = stats.Sample{
133-
Metric: stats.New("testGauge", stats.Gauge),
138+
Metric: metric,
134139
Time: time.Now(),
135140
Tags: stats.NewSampleTags(map[string]string{
136141
"something": "else",
@@ -152,6 +157,7 @@ func TestOutputFlushMetricsConcurrency(t *testing.T) {
152157
requests = int32(0)
153158
block = make(chan struct{})
154159
)
160+
155161
wg := sync.WaitGroup{}
156162
ts := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
157163
// block all the received requests
@@ -170,6 +176,9 @@ func TestOutputFlushMetricsConcurrency(t *testing.T) {
170176
ts.Close()
171177
}()
172178

179+
metric, err := metrics.NewRegistry().NewMetric("test_gauge", stats.Gauge)
180+
require.NoError(t, err)
181+
173182
o, err := newOutput(output.Params{
174183
Logger: testutils.NewLogger(t),
175184
ConfigArgument: ts.URL,
@@ -183,7 +192,7 @@ func TestOutputFlushMetricsConcurrency(t *testing.T) {
183192
wg.Add(1)
184193
o.AddMetricSamples([]stats.SampleContainer{stats.Samples{
185194
stats.Sample{
186-
Metric: stats.New("gauge", stats.Gauge),
195+
Metric: metric,
187196
Value: 2.0,
188197
},
189198
}})

0 commit comments

Comments
 (0)