Skip to content
This repository was archived by the owner on Mar 18, 2025. It is now read-only.

Commit a79a353

Browse files
authored
Merge pull request #18 from grafana/paul/refactor-stats
Refactor usage of stats to metrics as required for k6 v0.38.0
2 parents 656a93e + bc04dfa commit a79a353

File tree

8 files changed

+130
-131
lines changed

8 files changed

+130
-131
lines changed

go.mod

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ require (
99
github.com/prometheus/common v0.32.1
1010
github.com/prometheus/prometheus v1.8.2-0.20211005150130-f29caccc4255
1111
github.com/sirupsen/logrus v1.8.1
12-
github.com/stretchr/testify v1.7.0
13-
go.k6.io/k6 v0.34.1
12+
github.com/stretchr/testify v1.7.1
13+
go.k6.io/k6 v0.38.0
1414
gopkg.in/guregu/null.v3 v3.5.0
1515
)
1616

@@ -19,21 +19,20 @@ require (
1919
github.com/aws/aws-sdk-go v1.40.37 // indirect
2020
github.com/beorn7/perks v1.0.1 // indirect
2121
github.com/cespare/xxhash/v2 v2.1.2 // indirect
22+
github.com/cncf/xds/go v0.0.0-20211011173535-cb28da3451f1 // indirect
2223
github.com/davecgh/go-spew v1.1.1 // indirect
2324
github.com/dennwc/varint v1.0.0 // indirect
24-
github.com/dlclark/regexp2 v1.4.1-0.20201116162257-a2a8dda75c91 // indirect
25-
github.com/dop251/goja v0.0.0-20210912140721-ac5354e9a820 // indirect
26-
github.com/fatih/color v1.12.0 // indirect
25+
github.com/envoyproxy/go-control-plane v0.9.10-0.20210907150352-cf90f659a021 // indirect
26+
github.com/fatih/color v1.13.0 // indirect
2727
github.com/ghodss/yaml v1.0.0 // indirect
2828
github.com/go-kit/log v0.1.0 // indirect
2929
github.com/go-logfmt/logfmt v0.5.1 // indirect
30-
github.com/go-sourcemap/sourcemap v2.1.3+incompatible // indirect
3130
github.com/gogo/protobuf v1.3.2 // indirect
3231
github.com/jmespath/go-jmespath v0.4.0 // indirect
3332
github.com/jpillora/backoff v1.0.0 // indirect
3433
github.com/mailru/easyjson v0.7.7 // indirect
35-
github.com/mattn/go-colorable v0.1.8 // indirect
36-
github.com/mattn/go-isatty v0.0.13 // indirect
34+
github.com/mattn/go-colorable v0.1.12 // indirect
35+
github.com/mattn/go-isatty v0.0.14 // indirect
3736
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 // indirect
3837
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f // indirect
3938
github.com/opentracing-contrib/go-stdlib v1.0.0 // indirect
@@ -47,13 +46,14 @@ require (
4746
github.com/prometheus/procfs v0.6.0 // indirect
4847
github.com/spf13/afero v1.3.4 // indirect
4948
go.uber.org/atomic v1.9.0 // indirect
50-
golang.org/x/net v0.0.0-20210903162142-ad29c8ab022f // indirect
49+
golang.org/x/net v0.0.0-20220225172249-27dd8689420f // indirect
5150
golang.org/x/oauth2 v0.0.0-20210819190943-2bc19b11175f // indirect
52-
golang.org/x/sys v0.0.0-20210906170528-6f6e22806c34 // indirect
53-
golang.org/x/text v0.3.6 // indirect
54-
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac // indirect
51+
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect
52+
golang.org/x/text v0.3.7 // indirect
53+
golang.org/x/time v0.0.0-20220224211638-0e9765cccd65 // indirect
5554
google.golang.org/appengine v1.6.7 // indirect
5655
google.golang.org/protobuf v1.27.1 // indirect
56+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
5757
gopkg.in/yaml.v2 v2.4.0 // indirect
5858
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
5959
)

go.sum

Lines changed: 58 additions & 59 deletions
Large diffs are not rendered by default.

pkg/remotewrite/labels.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ package remotewrite
22

33
import (
44
"github.com/prometheus/prometheus/prompb"
5-
"go.k6.io/k6/stats"
5+
"go.k6.io/k6/metrics"
66
)
77

8-
func tagsToLabels(tags *stats.SampleTags, config Config) ([]prompb.Label, error) {
8+
func tagsToLabels(tags *metrics.SampleTags, config Config) ([]prompb.Label, error) {
99
if !config.KeepTags.Bool {
1010
return []prompb.Label{}, nil
1111
}

pkg/remotewrite/labels_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,28 @@ import (
77
"github.com/prometheus/prometheus/prompb"
88
"github.com/stretchr/testify/assert"
99
"github.com/stretchr/testify/require"
10-
"go.k6.io/k6/stats"
10+
"go.k6.io/k6/metrics"
1111
"gopkg.in/guregu/null.v3"
1212
)
1313

1414
func TestTagsToLabels(t *testing.T) {
1515
t.Parallel()
1616

1717
testCases := map[string]struct {
18-
tags *stats.SampleTags
18+
tags *metrics.SampleTags
1919
config Config
2020
labels []prompb.Label
2121
}{
2222
"empty-tags": {
23-
tags: &stats.SampleTags{},
23+
tags: &metrics.SampleTags{},
2424
config: Config{
2525
KeepTags: null.BoolFrom(true),
2626
KeepNameTag: null.BoolFrom(false),
2727
},
2828
labels: []prompb.Label{},
2929
},
3030
"name-tag-discard": {
31-
tags: stats.NewSampleTags(map[string]string{"foo": "bar", "name": "nnn"}),
31+
tags: metrics.NewSampleTags(map[string]string{"foo": "bar", "name": "nnn"}),
3232
config: Config{
3333
KeepTags: null.BoolFrom(true),
3434
KeepNameTag: null.BoolFrom(false),
@@ -38,7 +38,7 @@ func TestTagsToLabels(t *testing.T) {
3838
},
3939
},
4040
"name-tag-keep": {
41-
tags: stats.NewSampleTags(map[string]string{"foo": "bar", "name": "nnn"}),
41+
tags: metrics.NewSampleTags(map[string]string{"foo": "bar", "name": "nnn"}),
4242
config: Config{
4343
KeepTags: null.BoolFrom(true),
4444
KeepNameTag: null.BoolFrom(true),
@@ -49,7 +49,7 @@ func TestTagsToLabels(t *testing.T) {
4949
},
5050
},
5151
"url-tag-discard": {
52-
tags: stats.NewSampleTags(map[string]string{"foo": "bar", "url": "uuu"}),
52+
tags: metrics.NewSampleTags(map[string]string{"foo": "bar", "url": "uuu"}),
5353
config: Config{
5454
KeepTags: null.BoolFrom(true),
5555
KeepUrlTag: null.BoolFrom(false),
@@ -59,7 +59,7 @@ func TestTagsToLabels(t *testing.T) {
5959
},
6060
},
6161
"url-tag-keep": {
62-
tags: stats.NewSampleTags(map[string]string{"foo": "bar", "url": "uuu"}),
62+
tags: metrics.NewSampleTags(map[string]string{"foo": "bar", "url": "uuu"}),
6363
config: Config{
6464
KeepTags: null.BoolFrom(true),
6565
KeepUrlTag: null.BoolFrom(true),
@@ -70,7 +70,7 @@ func TestTagsToLabels(t *testing.T) {
7070
},
7171
},
7272
"discard-tags": {
73-
tags: stats.NewSampleTags(map[string]string{"foo": "bar", "name": "nnn"}),
73+
tags: metrics.NewSampleTags(map[string]string{"foo": "bar", "name": "nnn"}),
7474
config: Config{
7575
KeepTags: null.BoolFrom(false),
7676
},

pkg/remotewrite/metrics.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,26 @@ import (
55

66
"github.com/prometheus/prometheus/pkg/timestamp"
77
"github.com/prometheus/prometheus/prompb"
8-
"go.k6.io/k6/stats"
8+
"go.k6.io/k6/metrics"
99
)
1010

1111
// Note: k6 Registry is not used here since Output is getting
1212
// samples only from k6 engine, hence we assume they are already vetted.
1313

1414
// metricsStorage is an in-memory gather point for metrics
1515
type metricsStorage struct {
16-
m map[string]stats.Sample
16+
m map[string]metrics.Sample
1717
}
1818

1919
func newMetricsStorage() *metricsStorage {
2020
return &metricsStorage{
21-
m: make(map[string]stats.Sample),
21+
m: make(map[string]metrics.Sample),
2222
}
2323
}
2424

2525
// update modifies metricsStorage and returns updated sample
2626
// so that the stored metric and the returned metric hold the same value
27-
func (ms *metricsStorage) update(sample stats.Sample, add func(current, s stats.Sample) stats.Sample) stats.Sample {
27+
func (ms *metricsStorage) update(sample metrics.Sample, add func(current, s metrics.Sample) metrics.Sample) metrics.Sample {
2828
if current, ok := ms.m[sample.Metric.Name]; ok {
2929
if add == nil {
3030
current.Metric.Sink.Add(sample)
@@ -47,20 +47,20 @@ func (ms *metricsStorage) update(sample stats.Sample, add func(current, s stats.
4747
}
4848

4949
// transform k6 sample into TimeSeries for remote-write
50-
func (ms *metricsStorage) transform(mapping Mapping, sample stats.Sample, labels []prompb.Label) ([]prompb.TimeSeries, error) {
50+
func (ms *metricsStorage) transform(mapping Mapping, sample metrics.Sample, labels []prompb.Label) ([]prompb.TimeSeries, error) {
5151
var newts []prompb.TimeSeries
5252

5353
switch sample.Metric.Type {
54-
case stats.Counter:
54+
case metrics.Counter:
5555
newts = mapping.MapCounter(ms, sample, labels)
5656

57-
case stats.Gauge:
57+
case metrics.Gauge:
5858
newts = mapping.MapGauge(ms, sample, labels)
5959

60-
case stats.Rate:
60+
case metrics.Rate:
6161
newts = mapping.MapRate(ms, sample, labels)
6262

63-
case stats.Trend:
63+
case metrics.Trend:
6464
newts = mapping.MapTrend(ms, sample, labels)
6565

6666
default:
@@ -74,10 +74,10 @@ func (ms *metricsStorage) transform(mapping Mapping, sample stats.Sample, labels
7474
// remote agent. As each remote agent can use different ways to store metrics as well as
7575
// expect different values on remote write endpoint, they must have their own support.
7676
type Mapping interface {
77-
MapCounter(ms *metricsStorage, sample stats.Sample, labels []prompb.Label) []prompb.TimeSeries
78-
MapGauge(ms *metricsStorage, sample stats.Sample, labels []prompb.Label) []prompb.TimeSeries
79-
MapRate(ms *metricsStorage, sample stats.Sample, labels []prompb.Label) []prompb.TimeSeries
80-
MapTrend(ms *metricsStorage, sample stats.Sample, labels []prompb.Label) []prompb.TimeSeries
77+
MapCounter(ms *metricsStorage, sample metrics.Sample, labels []prompb.Label) []prompb.TimeSeries
78+
MapGauge(ms *metricsStorage, sample metrics.Sample, labels []prompb.Label) []prompb.TimeSeries
79+
MapRate(ms *metricsStorage, sample metrics.Sample, labels []prompb.Label) []prompb.TimeSeries
80+
MapTrend(ms *metricsStorage, sample metrics.Sample, labels []prompb.Label) []prompb.TimeSeries
8181

8282
// AdjustLabels(labels []prompb.Label) []prompb.Label
8383
}
@@ -93,23 +93,23 @@ func NewMapping(mapping string) Mapping {
9393

9494
type RawMapping struct{}
9595

96-
func (rm *RawMapping) MapCounter(ms *metricsStorage, sample stats.Sample, labels []prompb.Label) []prompb.TimeSeries {
96+
func (rm *RawMapping) MapCounter(ms *metricsStorage, sample metrics.Sample, labels []prompb.Label) []prompb.TimeSeries {
9797
return rm.processSample(sample, labels)
9898
}
9999

100-
func (rm *RawMapping) MapGauge(ms *metricsStorage, sample stats.Sample, labels []prompb.Label) []prompb.TimeSeries {
100+
func (rm *RawMapping) MapGauge(ms *metricsStorage, sample metrics.Sample, labels []prompb.Label) []prompb.TimeSeries {
101101
return rm.processSample(sample, labels)
102102
}
103103

104-
func (rm *RawMapping) MapRate(ms *metricsStorage, sample stats.Sample, labels []prompb.Label) []prompb.TimeSeries {
104+
func (rm *RawMapping) MapRate(ms *metricsStorage, sample metrics.Sample, labels []prompb.Label) []prompb.TimeSeries {
105105
return rm.processSample(sample, labels)
106106
}
107107

108-
func (rm *RawMapping) MapTrend(ms *metricsStorage, sample stats.Sample, labels []prompb.Label) []prompb.TimeSeries {
108+
func (rm *RawMapping) MapTrend(ms *metricsStorage, sample metrics.Sample, labels []prompb.Label) []prompb.TimeSeries {
109109
return rm.processSample(sample, labels)
110110
}
111111

112-
func (rm *RawMapping) processSample(sample stats.Sample, labels []prompb.Label) []prompb.TimeSeries {
112+
func (rm *RawMapping) processSample(sample metrics.Sample, labels []prompb.Label) []prompb.TimeSeries {
113113
return []prompb.TimeSeries{
114114
{
115115
Labels: append(labels, prompb.Label{

pkg/remotewrite/prometheus.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ import (
77

88
"github.com/prometheus/prometheus/pkg/timestamp"
99
"github.com/prometheus/prometheus/prompb"
10-
"go.k6.io/k6/stats"
10+
"go.k6.io/k6/metrics"
1111
)
1212

1313
type PrometheusMapping struct{}
1414

15-
func (pm *PrometheusMapping) MapCounter(ms *metricsStorage, sample stats.Sample, labels []prompb.Label) []prompb.TimeSeries {
15+
func (pm *PrometheusMapping) MapCounter(ms *metricsStorage, sample metrics.Sample, labels []prompb.Label) []prompb.TimeSeries {
1616
sample = ms.update(sample, nil)
1717
aggr := sample.Metric.Sink.Format(0)
1818

@@ -32,7 +32,7 @@ func (pm *PrometheusMapping) MapCounter(ms *metricsStorage, sample stats.Sample,
3232
}
3333
}
3434

35-
func (pm *PrometheusMapping) MapGauge(ms *metricsStorage, sample stats.Sample, labels []prompb.Label) []prompb.TimeSeries {
35+
func (pm *PrometheusMapping) MapGauge(ms *metricsStorage, sample metrics.Sample, labels []prompb.Label) []prompb.TimeSeries {
3636
sample = ms.update(sample, nil)
3737
aggr := sample.Metric.Sink.Format(0)
3838

@@ -52,7 +52,7 @@ func (pm *PrometheusMapping) MapGauge(ms *metricsStorage, sample stats.Sample, l
5252
}
5353
}
5454

55-
func (pm *PrometheusMapping) MapRate(ms *metricsStorage, sample stats.Sample, labels []prompb.Label) []prompb.TimeSeries {
55+
func (pm *PrometheusMapping) MapRate(ms *metricsStorage, sample metrics.Sample, labels []prompb.Label) []prompb.TimeSeries {
5656
sample = ms.update(sample, nil)
5757
aggr := sample.Metric.Sink.Format(0)
5858

@@ -72,10 +72,10 @@ func (pm *PrometheusMapping) MapRate(ms *metricsStorage, sample stats.Sample, la
7272
}
7373
}
7474

75-
func (pm *PrometheusMapping) MapTrend(ms *metricsStorage, sample stats.Sample, labels []prompb.Label) []prompb.TimeSeries {
75+
func (pm *PrometheusMapping) MapTrend(ms *metricsStorage, sample metrics.Sample, labels []prompb.Label) []prompb.TimeSeries {
7676
sample = ms.update(sample, trendAdd)
7777

78-
s := sample.Metric.Sink.(*stats.TrendSink)
78+
s := sample.Metric.Sink.(*metrics.TrendSink)
7979
aggr := map[string]float64{
8080
"min": s.Min,
8181
"max": s.Max,
@@ -166,11 +166,11 @@ func (pm *PrometheusMapping) MapTrend(ms *metricsStorage, sample stats.Sample, l
166166
}
167167

168168
// The following functions are an attempt to add ad-hoc optimization to TrendSink,
169-
// and are a partial copy-paste from k6/stats.
169+
// and are a partial copy-paste from k6/metrics.
170170
// TODO: re-write & refactor this once metrics refactoring progresses in k6.
171171

172-
func trendAdd(current, s stats.Sample) stats.Sample {
173-
t := current.Metric.Sink.(*stats.TrendSink)
172+
func trendAdd(current, s metrics.Sample) metrics.Sample {
173+
t := current.Metric.Sink.(*metrics.TrendSink)
174174

175175
// insert into sorted array instead of sorting anew on each addition
176176
index := sort.Search(len(t.Values), func(i int) bool {
@@ -201,7 +201,7 @@ func trendAdd(current, s stats.Sample) stats.Sample {
201201
return current
202202
}
203203

204-
func p(t *stats.TrendSink, pct float64) float64 {
204+
func p(t *metrics.TrendSink, pct float64) float64 {
205205
switch t.Count {
206206
case 0:
207207
return 0

0 commit comments

Comments
 (0)