Skip to content

Commit

Permalink
Merge pull request #18 from grafana/paul/refactor-stats
Browse files Browse the repository at this point in the history
Refactor usage of stats to metrics as required for k6 v0.38.0
  • Loading branch information
codebien authored May 5, 2022
2 parents 656a93e + bc04dfa commit a79a353
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 131 deletions.
24 changes: 12 additions & 12 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ require (
github.com/prometheus/common v0.32.1
github.com/prometheus/prometheus v1.8.2-0.20211005150130-f29caccc4255
github.com/sirupsen/logrus v1.8.1
github.com/stretchr/testify v1.7.0
go.k6.io/k6 v0.34.1
github.com/stretchr/testify v1.7.1
go.k6.io/k6 v0.38.0
gopkg.in/guregu/null.v3 v3.5.0
)

Expand All @@ -19,21 +19,20 @@ require (
github.com/aws/aws-sdk-go v1.40.37 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/cncf/xds/go v0.0.0-20211011173535-cb28da3451f1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dennwc/varint v1.0.0 // indirect
github.com/dlclark/regexp2 v1.4.1-0.20201116162257-a2a8dda75c91 // indirect
github.com/dop251/goja v0.0.0-20210912140721-ac5354e9a820 // indirect
github.com/fatih/color v1.12.0 // indirect
github.com/envoyproxy/go-control-plane v0.9.10-0.20210907150352-cf90f659a021 // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/ghodss/yaml v1.0.0 // indirect
github.com/go-kit/log v0.1.0 // indirect
github.com/go-logfmt/logfmt v0.5.1 // indirect
github.com/go-sourcemap/sourcemap v2.1.3+incompatible // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/jpillora/backoff v1.0.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.8 // indirect
github.com/mattn/go-isatty v0.0.13 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 // indirect
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f // indirect
github.com/opentracing-contrib/go-stdlib v1.0.0 // indirect
Expand All @@ -47,13 +46,14 @@ require (
github.com/prometheus/procfs v0.6.0 // indirect
github.com/spf13/afero v1.3.4 // indirect
go.uber.org/atomic v1.9.0 // indirect
golang.org/x/net v0.0.0-20210903162142-ad29c8ab022f // indirect
golang.org/x/net v0.0.0-20220225172249-27dd8689420f // indirect
golang.org/x/oauth2 v0.0.0-20210819190943-2bc19b11175f // indirect
golang.org/x/sys v0.0.0-20210906170528-6f6e22806c34 // indirect
golang.org/x/text v0.3.6 // indirect
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac // indirect
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/time v0.0.0-20220224211638-0e9765cccd65 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.27.1 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)
117 changes: 58 additions & 59 deletions go.sum

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions pkg/remotewrite/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package remotewrite

import (
"github.com/prometheus/prometheus/prompb"
"go.k6.io/k6/stats"
"go.k6.io/k6/metrics"
)

func tagsToLabels(tags *stats.SampleTags, config Config) ([]prompb.Label, error) {
func tagsToLabels(tags *metrics.SampleTags, config Config) ([]prompb.Label, error) {
if !config.KeepTags.Bool {
return []prompb.Label{}, nil
}
Expand Down
16 changes: 8 additions & 8 deletions pkg/remotewrite/labels_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,28 @@ import (
"github.com/prometheus/prometheus/prompb"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.k6.io/k6/stats"
"go.k6.io/k6/metrics"
"gopkg.in/guregu/null.v3"
)

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

testCases := map[string]struct {
tags *stats.SampleTags
tags *metrics.SampleTags
config Config
labels []prompb.Label
}{
"empty-tags": {
tags: &stats.SampleTags{},
tags: &metrics.SampleTags{},
config: Config{
KeepTags: null.BoolFrom(true),
KeepNameTag: null.BoolFrom(false),
},
labels: []prompb.Label{},
},
"name-tag-discard": {
tags: stats.NewSampleTags(map[string]string{"foo": "bar", "name": "nnn"}),
tags: metrics.NewSampleTags(map[string]string{"foo": "bar", "name": "nnn"}),
config: Config{
KeepTags: null.BoolFrom(true),
KeepNameTag: null.BoolFrom(false),
Expand All @@ -38,7 +38,7 @@ func TestTagsToLabels(t *testing.T) {
},
},
"name-tag-keep": {
tags: stats.NewSampleTags(map[string]string{"foo": "bar", "name": "nnn"}),
tags: metrics.NewSampleTags(map[string]string{"foo": "bar", "name": "nnn"}),
config: Config{
KeepTags: null.BoolFrom(true),
KeepNameTag: null.BoolFrom(true),
Expand All @@ -49,7 +49,7 @@ func TestTagsToLabels(t *testing.T) {
},
},
"url-tag-discard": {
tags: stats.NewSampleTags(map[string]string{"foo": "bar", "url": "uuu"}),
tags: metrics.NewSampleTags(map[string]string{"foo": "bar", "url": "uuu"}),
config: Config{
KeepTags: null.BoolFrom(true),
KeepUrlTag: null.BoolFrom(false),
Expand All @@ -59,7 +59,7 @@ func TestTagsToLabels(t *testing.T) {
},
},
"url-tag-keep": {
tags: stats.NewSampleTags(map[string]string{"foo": "bar", "url": "uuu"}),
tags: metrics.NewSampleTags(map[string]string{"foo": "bar", "url": "uuu"}),
config: Config{
KeepTags: null.BoolFrom(true),
KeepUrlTag: null.BoolFrom(true),
Expand All @@ -70,7 +70,7 @@ func TestTagsToLabels(t *testing.T) {
},
},
"discard-tags": {
tags: stats.NewSampleTags(map[string]string{"foo": "bar", "name": "nnn"}),
tags: metrics.NewSampleTags(map[string]string{"foo": "bar", "name": "nnn"}),
config: Config{
KeepTags: null.BoolFrom(false),
},
Expand Down
36 changes: 18 additions & 18 deletions pkg/remotewrite/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,26 @@ import (

"github.com/prometheus/prometheus/pkg/timestamp"
"github.com/prometheus/prometheus/prompb"
"go.k6.io/k6/stats"
"go.k6.io/k6/metrics"
)

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

// metricsStorage is an in-memory gather point for metrics
type metricsStorage struct {
m map[string]stats.Sample
m map[string]metrics.Sample
}

func newMetricsStorage() *metricsStorage {
return &metricsStorage{
m: make(map[string]stats.Sample),
m: make(map[string]metrics.Sample),
}
}

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

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

switch sample.Metric.Type {
case stats.Counter:
case metrics.Counter:
newts = mapping.MapCounter(ms, sample, labels)

case stats.Gauge:
case metrics.Gauge:
newts = mapping.MapGauge(ms, sample, labels)

case stats.Rate:
case metrics.Rate:
newts = mapping.MapRate(ms, sample, labels)

case stats.Trend:
case metrics.Trend:
newts = mapping.MapTrend(ms, sample, labels)

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

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

type RawMapping struct{}

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

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

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

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

func (rm *RawMapping) processSample(sample stats.Sample, labels []prompb.Label) []prompb.TimeSeries {
func (rm *RawMapping) processSample(sample metrics.Sample, labels []prompb.Label) []prompb.TimeSeries {
return []prompb.TimeSeries{
{
Labels: append(labels, prompb.Label{
Expand Down
20 changes: 10 additions & 10 deletions pkg/remotewrite/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import (

"github.com/prometheus/prometheus/pkg/timestamp"
"github.com/prometheus/prometheus/prompb"
"go.k6.io/k6/stats"
"go.k6.io/k6/metrics"
)

type PrometheusMapping struct{}

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

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

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

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

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

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

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

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

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

func trendAdd(current, s stats.Sample) stats.Sample {
t := current.Metric.Sink.(*stats.TrendSink)
func trendAdd(current, s metrics.Sample) metrics.Sample {
t := current.Metric.Sink.(*metrics.TrendSink)

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

func p(t *stats.TrendSink, pct float64) float64 {
func p(t *metrics.TrendSink, pct float64) float64 {
switch t.Count {
case 0:
return 0
Expand Down
Loading

0 comments on commit a79a353

Please sign in to comment.