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 29, 2022
1 parent f75251f commit 8ea9ce4
Show file tree
Hide file tree
Showing 113 changed files with 1,515 additions and 1,585 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/xk6-tests/xk6-js-test/jstest.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"time"

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

func init() {
Expand All @@ -19,7 +19,7 @@ type (
JSTest struct {
vu modules.VU

foos *stats.Metric
foos *metrics.Metric
}
)

Expand All @@ -39,7 +39,7 @@ func New() *RootModule {
func (*RootModule) NewModuleInstance(vu modules.VU) modules.Instance {
return &JSTest{
vu: vu,
foos: vu.InitEnv().Registry.MustNewMetric("foos", stats.Counter),
foos: vu.InitEnv().Registry.MustNewMetric("foos", metrics.Counter),
}
}

Expand All @@ -60,9 +60,9 @@ func (j *JSTest) Foo(arg float64) (bool, error) {

tags := state.CloneTags()
tags["foo"] = "bar"
stats.PushIfNotDone(ctx, state.Samples, stats.Sample{
metrics.PushIfNotDone(ctx, state.Samples, metrics.Sample{
Time: time.Now(),
Metric: j.foos, Tags: stats.IntoSampleTags(&tags),
Metric: j.foos, 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 @@ -47,14 +46,14 @@ 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)
testMetric, err := registry.NewMetric("my_metric", metrics.Trend, metrics.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{
engine.MetricsEngine.ObservedMetrics = map[string]*metrics.Metric{
"my_metric": testMetric,
}
engine.MetricsEngine.ObservedMetrics["my_metric"].Tainted = null.BoolFrom(true)
Expand All @@ -81,18 +80,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,15 +107,15 @@ 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)
testMetric, err := registry.NewMetric("my_metric", metrics.Trend, metrics.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{
engine.MetricsEngine.ObservedMetrics = map[string]*metrics.Metric{
"my_metric": testMetric,
}
engine.MetricsEngine.ObservedMetrics["my_metric"].Tainted = null.BoolFrom(true)
Expand Down Expand Up @@ -156,9 +155,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
23 changes: 11 additions & 12 deletions api/v1/metric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,17 @@ import (
"gopkg.in/guregu/null.v3"

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

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 @@ -74,9 +73,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 @@ -109,16 +108,16 @@ func TestNullValueTypeJSON(t *testing.T) {
func TestNewMetric(t *testing.T) {
t.Parallel()

old, err := metrics.NewRegistry().NewMetric("test_metric", stats.Trend, stats.Time)
old, err := metrics.NewRegistry().NewMetric("test_metric", metrics.Trend, metrics.Time)
require.NoError(t, err)
old.Tainted = null.BoolFrom(true)
m := NewMetric(old, 0)
assert.Equal(t, "test_metric", 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
5 changes: 2 additions & 3 deletions cmd/integration_tests/eventloop/eventloop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"go.k6.io/k6/lib/types"
"go.k6.io/k6/loader"
"go.k6.io/k6/metrics"
"go.k6.io/k6/stats"
"gopkg.in/guregu/null.v3"
)

Expand Down Expand Up @@ -201,7 +200,7 @@ export default function() {

func newTestExecutionScheduler(
t *testing.T, runner lib.Runner, logger *logrus.Logger, opts lib.Options, builtinMetrics *metrics.BuiltinMetrics,
) (ctx context.Context, cancel func(), execScheduler *local.ExecutionScheduler, samples chan stats.SampleContainer) {
) (ctx context.Context, cancel func(), execScheduler *local.ExecutionScheduler, samples chan metrics.SampleContainer) {
if runner == nil {
runner = &minirunner.MiniRunner{}
}
Expand All @@ -222,7 +221,7 @@ func newTestExecutionScheduler(
execScheduler, err = local.NewExecutionScheduler(runner, builtinMetrics, logger)
require.NoError(t, err)

samples = make(chan stats.SampleContainer, newOpts.MetricSamplesBufferSize.Int64)
samples = make(chan metrics.SampleContainer, newOpts.MetricSamplesBufferSize.Int64)
go func() {
for {
select {
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
Loading

0 comments on commit 8ea9ce4

Please sign in to comment.