Skip to content

Commit

Permalink
Move the stats package content to metrics package
Browse files Browse the repository at this point in the history
Until now, we had two separate `stats` and `metrics` packages. Although
their content was related, some components were living in one, some in
the other. It lead to difficulties when having to work with both, and
made cyclic dependencies really easy to run into.

To simplify our workflow, and facilitate further developments, this 
commit moves the content of the `stats` package in the top-level 
`metrics` package. As of this commit, the `stats` package is removed,
and all dependencies using it have been updated to use the `metrics`
package instead.
  • Loading branch information
oleiade committed Mar 10, 2022
1 parent 2476cfc commit 75d5b00
Show file tree
Hide file tree
Showing 111 changed files with 1,499 additions and 1,537 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/xk6-tests/xk6-js-test/jstest.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"time"

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

"go.k6.io/k6/js/modules"
)
Expand All @@ -44,12 +44,12 @@ 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)
allTheFoos := metrics.New("foos", metrics.Counter)
tags := state.CloneTags()
tags["foo"] = "bar"
stats.PushIfNotDone(ctx, state.Samples, stats.Sample{
metrics.PushIfNotDone(ctx, state.Samples, metrics.Sample{
Time: time.Now(),
Metric: allTheFoos, Tags: stats.IntoSampleTags(&tags),
Metric: allTheFoos, Tags: metrics.IntoSampleTags(&tags),
Value: arg,
})

Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/xk6-tests/xk6-output-test/outputtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ package outputtest
import (
"strconv"

"go.k6.io/k6/output"
"go.k6.io/k6/stats"
"github.com/spf13/afero"
"go.k6.io/k6/metrics"
"go.k6.io/k6/output"
)

func init() {
Expand Down Expand Up @@ -57,7 +57,7 @@ func (o *Output) Start() error {
}

// AddMetricSamples just plucks out the metric we're interested in.
func (o *Output) AddMetricSamples(sampleContainers []stats.SampleContainer) {
func (o *Output) AddMetricSamples(sampleContainers []metrics.SampleContainer) {
for _, sc := range sampleContainers {
for _, sample := range sc.GetSamples() {
if sample.Metric.Name == "foos" {
Expand Down
9 changes: 5 additions & 4 deletions api/v1/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ import (

"gopkg.in/guregu/null.v3"

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

type NullMetricType struct {
Type stats.MetricType
Type metrics.MetricType
Valid bool
}

Expand All @@ -52,7 +52,7 @@ func (t *NullMetricType) UnmarshalJSON(data []byte) error {
}

type NullValueType struct {
Type stats.ValueType
Type metrics.ValueType
Valid bool
}

Expand Down Expand Up @@ -82,7 +82,8 @@ type Metric struct {
Sample map[string]float64 `json:"sample" yaml:"sample"`
}

func NewMetric(m *stats.Metric, t time.Duration) Metric {
// NewMetric constructs a new Metric
func NewMetric(m *metrics.Metric, t time.Duration) Metric {
return Metric{
Name: m.Name,
Type: NullMetricType{m.Type, true},
Expand Down
8 changes: 4 additions & 4 deletions api/v1/metric_jsonapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ package v1
import (
"time"

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

// MetricsJSONAPI is JSON API envelop for metrics
Expand All @@ -41,13 +41,13 @@ type metricData struct {
Attributes Metric `json:"attributes"`
}

func newMetricEnvelope(m *stats.Metric, t time.Duration) metricJSONAPI {
func newMetricEnvelope(m *metrics.Metric, t time.Duration) metricJSONAPI {
return metricJSONAPI{
Data: newMetricData(m, t),
}
}

func newMetricsJSONAPI(list map[string]*stats.Metric, t time.Duration) MetricsJSONAPI {
func newMetricsJSONAPI(list map[string]*metrics.Metric, t time.Duration) MetricsJSONAPI {
metrics := make([]metricData, 0, len(list))

for _, m := range list {
Expand All @@ -59,7 +59,7 @@ func newMetricsJSONAPI(list map[string]*stats.Metric, t time.Duration) MetricsJS
}
}

func newMetricData(m *stats.Metric, t time.Duration) metricData {
func newMetricData(m *metrics.Metric, t time.Duration) metricData {
metric := NewMetric(m, t)

return metricData{
Expand Down
25 changes: 12 additions & 13 deletions api/v1/metric_routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import (
"go.k6.io/k6/lib/testutils"
"go.k6.io/k6/lib/testutils/minirunner"
"go.k6.io/k6/metrics"
"go.k6.io/k6/stats"
)

func TestGetMetrics(t *testing.T) {
Expand All @@ -52,8 +51,8 @@ func TestGetMetrics(t *testing.T) {
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),
engine.MetricsEngine.ObservedMetrics = map[string]*metrics.Metric{
"my_metric": metrics.New("my_metric", metrics.Trend, metrics.Time),
}
engine.MetricsEngine.ObservedMetrics["my_metric"].Tainted = null.BoolFrom(true)

Expand All @@ -79,18 +78,18 @@ func TestGetMetrics(t *testing.T) {
var envelop MetricsJSONAPI
assert.NoError(t, json.Unmarshal(rw.Body.Bytes(), &envelop))

metrics := envelop.Data
if !assert.Len(t, metrics, 1) {
metricsData := envelop.Data
if !assert.Len(t, metricsData, 1) {
return
}

metric := metrics[0].Attributes
metric := metricsData[0].Attributes

assert.Equal(t, "my_metric", metrics[0].ID)
assert.Equal(t, "my_metric", metricsData[0].ID)
assert.True(t, metric.Type.Valid)
assert.Equal(t, stats.Trend, metric.Type.Type)
assert.Equal(t, metrics.Trend, metric.Type.Type)
assert.True(t, metric.Contains.Valid)
assert.Equal(t, stats.Time, metric.Contains.Type)
assert.Equal(t, metrics.Time, metric.Contains.Type)
assert.True(t, metric.Tainted.Valid)
assert.True(t, metric.Tainted.Bool)
})
Expand All @@ -108,8 +107,8 @@ func TestGetMetric(t *testing.T) {
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),
engine.MetricsEngine.ObservedMetrics = map[string]*metrics.Metric{
"my_metric": metrics.New("my_metric", metrics.Trend, metrics.Time),
}
engine.MetricsEngine.ObservedMetrics["my_metric"].Tainted = null.BoolFrom(true)

Expand Down Expand Up @@ -148,9 +147,9 @@ func TestGetMetric(t *testing.T) {

assert.Equal(t, "my_metric", envelop.Data.ID)
assert.True(t, metric.Type.Valid)
assert.Equal(t, stats.Trend, metric.Type.Type)
assert.Equal(t, metrics.Trend, metric.Type.Type)
assert.True(t, metric.Contains.Valid)
assert.Equal(t, stats.Time, metric.Contains.Type)
assert.Equal(t, metrics.Time, metric.Contains.Type)
assert.True(t, metric.Tainted.Valid)
assert.True(t, metric.Tainted.Bool)
})
Expand Down
24 changes: 12 additions & 12 deletions api/v1/metric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,18 @@ import (
"github.com/stretchr/testify/assert"
"gopkg.in/guregu/null.v3"

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

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

values := map[NullMetricType]string{
{}: `null`,
{stats.Counter, true}: `"counter"`,
{stats.Gauge, true}: `"gauge"`,
{stats.Trend, true}: `"trend"`,
{stats.Rate, true}: `"rate"`,
{}: `null`,
{metrics.Counter, true}: `"counter"`,
{metrics.Gauge, true}: `"gauge"`,
{metrics.Trend, true}: `"trend"`,
{metrics.Rate, true}: `"rate"`,
}
t.Run("Marshal", func(t *testing.T) {
t.Parallel()
Expand Down Expand Up @@ -72,9 +72,9 @@ func TestNullValueTypeJSON(t *testing.T) {
t.Parallel()

values := map[NullValueType]string{
{}: `null`,
{stats.Default, true}: `"default"`,
{stats.Time, true}: `"time"`,
{}: `null`,
{metrics.Default, true}: `"default"`,
{metrics.Time, true}: `"time"`,
}
t.Run("Marshal", func(t *testing.T) {
t.Parallel()
Expand Down Expand Up @@ -107,15 +107,15 @@ func TestNullValueTypeJSON(t *testing.T) {
func TestNewMetric(t *testing.T) {
t.Parallel()

old := stats.New("name", stats.Trend, stats.Time)
old := metrics.New("name", metrics.Trend, metrics.Time)
old.Tainted = null.BoolFrom(true)
m := NewMetric(old, 0)
assert.Equal(t, "name", m.Name)
assert.True(t, m.Type.Valid)
assert.Equal(t, stats.Trend, m.Type.Type)
assert.Equal(t, metrics.Trend, m.Type.Type)
assert.True(t, m.Contains.Valid)
assert.True(t, m.Tainted.Bool)
assert.True(t, m.Tainted.Valid)
assert.Equal(t, stats.Time, m.Contains.Type)
assert.Equal(t, metrics.Time, m.Contains.Type)
assert.NotEmpty(t, m.Sample)
}
6 changes: 3 additions & 3 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import (
"go.k6.io/k6/lib"
"go.k6.io/k6/lib/executor"
"go.k6.io/k6/lib/types"
"go.k6.io/k6/stats"
"go.k6.io/k6/metrics"
)

// configFlagSet returns a FlagSet with the default run configuration flags.
Expand Down Expand Up @@ -201,7 +201,7 @@ func getConsolidatedConfig(globalState *globalState, cliConf Config, runnerOpts
// for CLI flags in cmd.getOptions, in case other configuration sources
// (e.g. env vars) overrode our default value. This is not done in
// lib.Options.Validate to avoid circular imports.
if _, err = stats.GetResolversForTrendColumns(conf.SummaryTrendStats); err != nil {
if _, err = metrics.GetResolversForTrendColumns(conf.SummaryTrendStats); err != nil {
return conf, err
}

Expand All @@ -214,7 +214,7 @@ func getConsolidatedConfig(globalState *globalState, cliConf Config, runnerOpts
// Note that if you add option default value here, also add it in command line argument help text.
func applyDefault(conf Config) Config {
if conf.Options.SystemTags == nil {
conf.Options.SystemTags = &stats.DefaultSystemTagSet
conf.Options.SystemTags = &metrics.DefaultSystemTagSet
}
if conf.Options.SummaryTrendStats == nil {
conf.Options.SummaryTrendStats = lib.DefaultSummaryTrendStats
Expand Down
10 changes: 5 additions & 5 deletions cmd/config_consolidation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import (
"go.k6.io/k6/lib"
"go.k6.io/k6/lib/executor"
"go.k6.io/k6/lib/types"
"go.k6.io/k6/stats"
"go.k6.io/k6/metrics"
)

func verifyOneIterPerOneVU(t *testing.T, c Config) {
Expand Down Expand Up @@ -350,22 +350,22 @@ func getConfigConsolidationTestCases() []configConsolidationTestCase {

// Test system tags
{opts{}, exp{}, func(t *testing.T, c Config) {
assert.Equal(t, &stats.DefaultSystemTagSet, c.Options.SystemTags)
assert.Equal(t, &metrics.DefaultSystemTagSet, c.Options.SystemTags)
}},
{opts{cli: []string{"--system-tags", `""`}}, exp{}, func(t *testing.T, c Config) {
assert.Equal(t, stats.SystemTagSet(0), *c.Options.SystemTags)
assert.Equal(t, metrics.SystemTagSet(0), *c.Options.SystemTags)
}},
{
opts{
runner: &lib.Options{
SystemTags: stats.NewSystemTagSet(stats.TagSubproto, stats.TagURL),
SystemTags: metrics.NewSystemTagSet(metrics.TagSubproto, metrics.TagURL),
},
},
exp{},
func(t *testing.T, c Config) {
assert.Equal(
t,
*stats.NewSystemTagSet(stats.TagSubproto, stats.TagURL),
*metrics.NewSystemTagSet(metrics.TagSubproto, metrics.TagURL),
*c.Options.SystemTags,
)
},
Expand Down
10 changes: 5 additions & 5 deletions cmd/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (
"go.k6.io/k6/lib"
"go.k6.io/k6/lib/consts"
"go.k6.io/k6/lib/types"
"go.k6.io/k6/stats"
"go.k6.io/k6/metrics"
)

var (
Expand Down Expand Up @@ -86,7 +86,7 @@ func optionFlagSet() *pflag.FlagSet {
// set it to nil here, and add the default in applyDefault() instead.
systemTagsCliHelpText := fmt.Sprintf(
"only include these system tags in metrics (default %q)",
stats.DefaultSystemTagSet.SetString(),
metrics.DefaultSystemTagSet.SetString(),
)
flags.StringSlice("system-tags", nil, systemTagsCliHelpText)
flags.StringSlice("tag", nil, "add a `tag` to be applied to all samples, as `[name]=[value]`")
Expand Down Expand Up @@ -181,7 +181,7 @@ func getOptions(flags *pflag.FlagSet) (lib.Options, error) {
if err != nil {
return opts, err
}
opts.SystemTags = stats.ToSystemTagSet(systemTagList)
opts.SystemTags = metrics.ToSystemTagSet(systemTagList)
}

blacklistIPStrings, err := flags.GetStringSlice("blacklist-ip")
Expand Down Expand Up @@ -223,7 +223,7 @@ func getOptions(flags *pflag.FlagSet) (lib.Options, error) {
if errSts != nil {
return opts, errSts
}
if _, errSts = stats.GetResolversForTrendColumns(trendStats); err != nil {
if _, errSts = metrics.GetResolversForTrendColumns(trendStats); err != nil {
return opts, errSts
}
opts.SummaryTrendStats = trendStats
Expand Down Expand Up @@ -255,7 +255,7 @@ func getOptions(flags *pflag.FlagSet) (lib.Options, error) {
}
parsedRunTags[name] = value
}
opts.RunTags = stats.IntoSampleTags(&parsedRunTags)
opts.RunTags = metrics.IntoSampleTags(&parsedRunTags)
}

redirectConFile, err := flags.GetString("console-output")
Expand Down
9 changes: 4 additions & 5 deletions core/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import (
"go.k6.io/k6/metrics"
"go.k6.io/k6/metrics/engine"
"go.k6.io/k6/output"
"go.k6.io/k6/stats"
)

const (
Expand Down Expand Up @@ -66,7 +65,7 @@ type Engine struct {
stopOnce sync.Once
stopChan chan struct{}

Samples chan stats.SampleContainer
Samples chan metrics.SampleContainer

// Are thresholds tainted?
thresholdsTaintedLock sync.Mutex
Expand All @@ -86,7 +85,7 @@ func NewEngine(
ExecutionScheduler: ex,

runtimeOptions: rtOpts,
Samples: make(chan stats.SampleContainer, opts.MetricSamplesBufferSize.Int64),
Samples: make(chan metrics.SampleContainer, opts.MetricSamplesBufferSize.Int64),
stopChan: make(chan struct{}),
logger: logger.WithField("component", "engine"),
}
Expand Down Expand Up @@ -246,7 +245,7 @@ func (e *Engine) startBackgroundProcesses(
}

func (e *Engine) processMetrics(globalCtx context.Context, processMetricsAfterRun chan struct{}) {
sampleContainers := []stats.SampleContainer{}
sampleContainers := []metrics.SampleContainer{}

defer func() {
// Process any remaining metrics in the pipeline, by this point Run()
Expand Down Expand Up @@ -278,7 +277,7 @@ func (e *Engine) processMetrics(globalCtx context.Context, processMetricsAfterRu
// Make the new container with the same size as the previous
// one, assuming that we produce roughly the same amount of
// metrics data between ticks...
sampleContainers = make([]stats.SampleContainer, 0, cap(sampleContainers))
sampleContainers = make([]metrics.SampleContainer, 0, cap(sampleContainers))
}
}
for {
Expand Down
Loading

0 comments on commit 75d5b00

Please sign in to comment.