Skip to content
This repository has been archived by the owner on Jan 30, 2025. It is now read-only.

Commit

Permalink
Fix k6/stats package removal
Browse files Browse the repository at this point in the history
This is a fix for a breaking change introduced in k6 v0.38.0[1].

[1]: grafana/k6#2433
  • Loading branch information
Ivan Mirić committed May 5, 2022
1 parent b7b6bd9 commit 43ec8eb
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 41 deletions.
14 changes: 7 additions & 7 deletions common/frame.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (
"github.com/chromedp/cdproto/runtime"
"github.com/dop251/goja"
k6modules "go.k6.io/k6/js/modules"
k6stats "go.k6.io/k6/stats"
k6metrics "go.k6.io/k6/metrics"

"github.com/grafana/xk6-browser/api"
)
Expand Down Expand Up @@ -354,8 +354,8 @@ func (f *Frame) cachedDocumentHandle() (*ElementHandle, bool) {
return f.documentHandle, f.documentHandle != nil
}

func (f *Frame) emitMetric(m *k6stats.Metric, t time.Time) {
value := k6stats.D(t.Sub(f.initTime))
func (f *Frame) emitMetric(m *k6metrics.Metric, t time.Time) {
value := k6metrics.D(t.Sub(f.initTime))
f.log.Debugf("Frame:emitMetric", "fid:%s furl:%q m:%s init:%q t:%q v:%f",
f.ID(), f.URL(), m.Name, f.initTime, t, value)

Expand All @@ -370,12 +370,12 @@ func (f *Frame) emitMetric(m *k6stats.Metric, t time.Time) {

state := f.vu.State()
tags := state.CloneTags()
if state.Options.SystemTags.Has(k6stats.TagURL) {
if state.Options.SystemTags.Has(k6metrics.TagURL) {
tags["url"] = f.URL()
}
sampleTags := k6stats.IntoSampleTags(&tags)
k6stats.PushIfNotDone(f.ctx, state.Samples, k6stats.ConnectedSamples{
Samples: []k6stats.Sample{
sampleTags := k6metrics.IntoSampleTags(&tags)
k6metrics.PushIfNotDone(f.ctx, state.Samples, k6metrics.ConnectedSamples{
Samples: []k6metrics.Sample{
{
Metric: m,
Tags: sampleTags,
Expand Down
2 changes: 1 addition & 1 deletion common/frame_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ import (
"github.com/chromedp/cdproto/target"
"github.com/sirupsen/logrus"
k6modules "go.k6.io/k6/js/modules"
k6stats "go.k6.io/k6/stats"
k6metrics "go.k6.io/k6/metrics"

"github.com/grafana/xk6-browser/api"
)
Expand Down
7 changes: 3 additions & 4 deletions common/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ import (
k6eventloop "go.k6.io/k6/js/eventloop"
k6modulestest "go.k6.io/k6/js/modulestest"
k6lib "go.k6.io/k6/lib"
k6metrics "go.k6.io/k6/lib/metrics"
k6stats "go.k6.io/k6/stats"
k6metrics "go.k6.io/k6/metrics"
"gopkg.in/guregu/null.v3"
)

Expand Down Expand Up @@ -251,7 +250,7 @@ func newMockVU(tb testing.TB) *mockVU {
rt := goja.New()
rt.SetFieldNameMapper(k6common.FieldNameMapper{})

samples := make(chan k6stats.SampleContainer, 1000)
samples := make(chan k6metrics.SampleContainer, 1000)

root, err := k6lib.NewGroup("", nil)
require.NoError(tb, err)
Expand All @@ -261,7 +260,7 @@ func newMockVU(tb testing.TB) *mockVU {
MaxRedirects: null.IntFrom(10),
UserAgent: null.StringFrom("TestUserAgent"),
Throw: null.BoolFrom(true),
SystemTags: &k6stats.DefaultSystemTagSet,
SystemTags: &k6metrics.DefaultSystemTagSet,
Batch: null.IntFrom(20),
BatchPerHost: null.IntFrom(20),
// HTTPDebug: null.StringFrom("full"),
Expand Down
File renamed without changes.
46 changes: 23 additions & 23 deletions common/network_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import (
k6lib "go.k6.io/k6/lib"
k6netext "go.k6.io/k6/lib/netext"
k6types "go.k6.io/k6/lib/types"
k6stats "go.k6.io/k6/stats"
k6metrics "go.k6.io/k6/metrics"
)

// Ensure NetworkManager implements the EventEmitter interface.
Expand Down Expand Up @@ -161,19 +161,19 @@ func (m *NetworkManager) emitRequestMetrics(req *Request) {
state := m.vu.State()

tags := state.CloneTags()
if state.Options.SystemTags.Has(k6stats.TagGroup) {
if state.Options.SystemTags.Has(k6metrics.TagGroup) {
tags["group"] = state.Group.Path
}
if state.Options.SystemTags.Has(k6stats.TagMethod) {
if state.Options.SystemTags.Has(k6metrics.TagMethod) {
tags["method"] = req.method
}
if state.Options.SystemTags.Has(k6stats.TagURL) {
if state.Options.SystemTags.Has(k6metrics.TagURL) {
tags["url"] = req.URL()
}

sampleTags := k6stats.IntoSampleTags(&tags)
k6stats.PushIfNotDone(m.ctx, state.Samples, k6stats.ConnectedSamples{
Samples: []k6stats.Sample{
sampleTags := k6metrics.IntoSampleTags(&tags)
k6metrics.PushIfNotDone(m.ctx, state.Samples, k6metrics.ConnectedSamples{
Samples: []k6metrics.Sample{
{
Metric: state.BuiltinMetrics.DataSent,
Tags: sampleTags,
Expand Down Expand Up @@ -213,32 +213,32 @@ func (m *NetworkManager) emitResponseMetrics(resp *Response, req *Request) {
}

tags := state.CloneTags()
if state.Options.SystemTags.Has(k6stats.TagGroup) {
if state.Options.SystemTags.Has(k6metrics.TagGroup) {
tags["group"] = state.Group.Path
}
if state.Options.SystemTags.Has(k6stats.TagMethod) {
if state.Options.SystemTags.Has(k6metrics.TagMethod) {
tags["method"] = req.method
}
if state.Options.SystemTags.Has(k6stats.TagURL) {
if state.Options.SystemTags.Has(k6metrics.TagURL) {
tags["url"] = url
}
if state.Options.SystemTags.Has(k6stats.TagIP) {
if state.Options.SystemTags.Has(k6metrics.TagIP) {
tags["ip"] = ipAddress
}
if state.Options.SystemTags.Has(k6stats.TagStatus) {
if state.Options.SystemTags.Has(k6metrics.TagStatus) {
tags["status"] = strconv.Itoa(int(status))
}
if state.Options.SystemTags.Has(k6stats.TagProto) {
if state.Options.SystemTags.Has(k6metrics.TagProto) {
tags["proto"] = protocol
}

tags["from_cache"] = strconv.FormatBool(fromCache)
tags["from_prefetch_cache"] = strconv.FormatBool(fromPreCache)
tags["from_service_worker"] = strconv.FormatBool(fromSvcWrk)

sampleTags := k6stats.IntoSampleTags(&tags)
k6stats.PushIfNotDone(m.ctx, state.Samples, k6stats.ConnectedSamples{
Samples: []k6stats.Sample{
sampleTags := k6metrics.IntoSampleTags(&tags)
k6metrics.PushIfNotDone(m.ctx, state.Samples, k6metrics.ConnectedSamples{
Samples: []k6metrics.Sample{
{
Metric: state.BuiltinMetrics.HTTPReqs,
Tags: sampleTags,
Expand All @@ -254,7 +254,7 @@ func (m *NetworkManager) emitResponseMetrics(resp *Response, req *Request) {
// issues with the parsing and conversion to `time.Time`.
// Have not spent time looking for the root cause of this in the Chromium source to file a bug report, and neither
// Puppeteer nor Playwright seems to care about the `responseTime` value and don't use/expose it.
Value: k6stats.D(timestamp.Sub(req.timestamp)),
Value: k6metrics.D(timestamp.Sub(req.timestamp)),
Time: timestamp,
},
{
Expand All @@ -267,30 +267,30 @@ func (m *NetworkManager) emitResponseMetrics(resp *Response, req *Request) {
})

if resp != nil && resp.timing != nil {
k6stats.PushIfNotDone(m.ctx, state.Samples, k6stats.ConnectedSamples{
Samples: []k6stats.Sample{
k6metrics.PushIfNotDone(m.ctx, state.Samples, k6metrics.ConnectedSamples{
Samples: []k6metrics.Sample{
{
Metric: state.BuiltinMetrics.HTTPReqConnecting,
Tags: sampleTags,
Value: k6stats.D(time.Duration(resp.timing.ConnectEnd-resp.timing.ConnectStart) * time.Millisecond),
Value: k6metrics.D(time.Duration(resp.timing.ConnectEnd-resp.timing.ConnectStart) * time.Millisecond),
Time: resp.timestamp,
},
{
Metric: state.BuiltinMetrics.HTTPReqTLSHandshaking,
Tags: sampleTags,
Value: k6stats.D(time.Duration(resp.timing.SslEnd-resp.timing.SslStart) * time.Millisecond),
Value: k6metrics.D(time.Duration(resp.timing.SslEnd-resp.timing.SslStart) * time.Millisecond),
Time: resp.timestamp,
},
{
Metric: state.BuiltinMetrics.HTTPReqSending,
Tags: sampleTags,
Value: k6stats.D(time.Duration(resp.timing.SendEnd-resp.timing.SendStart) * time.Millisecond),
Value: k6metrics.D(time.Duration(resp.timing.SendEnd-resp.timing.SendStart) * time.Millisecond),
Time: resp.timestamp,
},
{
Metric: state.BuiltinMetrics.HTTPReqReceiving,
Tags: sampleTags,
Value: k6stats.D(time.Duration(resp.timing.ReceiveHeadersEnd-resp.timing.SendEnd) * time.Millisecond),
Value: k6metrics.D(time.Duration(resp.timing.ReceiveHeadersEnd-resp.timing.SendEnd) * time.Millisecond),
Time: resp.timestamp,
},
},
Expand Down
4 changes: 2 additions & 2 deletions tests/test_browser.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (
k6http "go.k6.io/k6/js/modules/k6/http"
k6lib "go.k6.io/k6/lib"
k6httpmultibin "go.k6.io/k6/lib/testutils/httpmultibin"
k6stats "go.k6.io/k6/stats"
k6metrics "go.k6.io/k6/metrics"

"github.com/grafana/xk6-browser/api"
"github.com/grafana/xk6-browser/chromium"
Expand All @@ -50,7 +50,7 @@ type testBrowser struct {
http *k6httpmultibin.HTTPMultiBin
vu *mockVU
logCache *logCache
samples chan<- k6stats.SampleContainer
samples chan<- k6metrics.SampleContainer
api.Browser
}

Expand Down
7 changes: 3 additions & 4 deletions tests/test_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ import (
k6eventloop "go.k6.io/k6/js/eventloop"
k6modulestest "go.k6.io/k6/js/modulestest"
k6lib "go.k6.io/k6/lib"
k6metrics "go.k6.io/k6/lib/metrics"
k6stats "go.k6.io/k6/stats"
k6metrics "go.k6.io/k6/metrics"
"gopkg.in/guregu/null.v3"

"github.com/grafana/xk6-browser/common"
Expand All @@ -30,7 +29,7 @@ func newMockVU(tb testing.TB) *mockVU {
rt := goja.New()
rt.SetFieldNameMapper(k6common.FieldNameMapper{})

samples := make(chan k6stats.SampleContainer, 1000)
samples := make(chan k6metrics.SampleContainer, 1000)

root, err := k6lib.NewGroup("", nil)
require.NoError(tb, err)
Expand All @@ -40,7 +39,7 @@ func newMockVU(tb testing.TB) *mockVU {
MaxRedirects: null.IntFrom(10),
UserAgent: null.StringFrom("TestUserAgent"),
Throw: null.BoolFrom(true),
SystemTags: &k6stats.DefaultSystemTagSet,
SystemTags: &k6metrics.DefaultSystemTagSet,
Batch: null.IntFrom(20),
BatchPerHost: null.IntFrom(20),
// HTTPDebug: null.StringFrom("full"),
Expand Down

0 comments on commit 43ec8eb

Please sign in to comment.