From 278fb8cb7f4a0674c4e472f61d5ac3a071ce29ec Mon Sep 17 00:00:00 2001 From: Svyatoslav Kryukov Date: Thu, 23 Jun 2022 10:31:12 +0300 Subject: [PATCH] Update k6 0.38.3 and refactor code to use new metrics registry --- README.md | 4 ++++ cable.go | 23 ++++++++++++----------- client.go | 12 ++++++------ go.mod | 2 +- 4 files changed, 23 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index cae83f9..52f6fc3 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,10 @@ go install github.com/k6io/xk6/cmd/xk6 ```shell xk6 build --with github.com/anycable/xk6-cable@latest +# you can specify k6 version +xk6 build v0.38.3 --with github.com/anycable/xk6-cable@latest +``` + # or if you want to build from the local source xk6 build --with github.com/anycable/xk6-cable@latest=/path/to/source ``` diff --git a/cable.go b/cable.go index fc50233..4e021ae 100644 --- a/cable.go +++ b/cable.go @@ -9,12 +9,13 @@ import ( "strconv" "time" + "go.k6.io/k6/js/common" + "go.k6.io/k6/lib" + "go.k6.io/k6/metrics" + "github.com/dop251/goja" "github.com/gorilla/websocket" "github.com/sirupsen/logrus" - "go.k6.io/k6/js/common" - "go.k6.io/k6/lib" - "go.k6.io/k6/stats" ) // errCableInInitContext is returned when cable used in the init context @@ -75,30 +76,30 @@ func (c *Cable) Connect(cableUrl string, opts goja.Value) (*Client, error) { connectionEnd := time.Now() tags := cOpts.appendTags(state.CloneTags()) - if state.Options.SystemTags.Has(stats.TagIP) && conn != nil && conn.RemoteAddr() != nil { + if state.Options.SystemTags.Has(metrics.TagIP) && conn != nil && conn.RemoteAddr() != nil { if ip, _, err := net.SplitHostPort(conn.RemoteAddr().String()); err == nil { tags["ip"] = ip } } if httpResponse != nil { - if state.Options.SystemTags.Has(stats.TagStatus) { + if state.Options.SystemTags.Has(metrics.TagStatus) { tags["status"] = strconv.Itoa(httpResponse.StatusCode) } - if state.Options.SystemTags.Has(stats.TagSubproto) { + if state.Options.SystemTags.Has(metrics.TagSubproto) { tags["subproto"] = httpResponse.Header.Get("Sec-WebSocket-Protocol") } } - if state.Options.SystemTags.Has(stats.TagURL) { + if state.Options.SystemTags.Has(metrics.TagURL) { tags["url"] = cableUrl } - sampleTags := stats.IntoSampleTags(&tags) + sampleTags := metrics.IntoSampleTags(&tags) - stats.PushIfNotDone(c.vu.Context(), state.Samples, stats.ConnectedSamples{ - Samples: []stats.Sample{ + metrics.PushIfNotDone(c.vu.Context(), state.Samples, metrics.ConnectedSamples{ + Samples: []metrics.Sample{ {Metric: state.BuiltinMetrics.WSSessions, Time: connectionStart, Tags: sampleTags, Value: 1}, - {Metric: state.BuiltinMetrics.WSConnecting, Time: connectionStart, Tags: sampleTags, Value: stats.D(connectionEnd.Sub(connectionStart))}, + {Metric: state.BuiltinMetrics.WSConnecting, Time: connectionStart, Tags: sampleTags, Value: metrics.D(connectionEnd.Sub(connectionStart))}, }, Tags: sampleTags, Time: connectionStart, diff --git a/client.go b/client.go index bf394d2..e56a7c1 100644 --- a/client.go +++ b/client.go @@ -2,11 +2,11 @@ package cable import ( "encoding/json" - "go.k6.io/k6/js/modules" "sync" "time" - "go.k6.io/k6/stats" + "go.k6.io/k6/js/modules" + "go.k6.io/k6/metrics" "github.com/dop251/goja" "github.com/gorilla/websocket" @@ -35,8 +35,8 @@ type Client struct { logger *logrus.Entry recTimeout time.Duration - sampleTags *stats.SampleTags - samplesOutput chan<- stats.SampleContainer + sampleTags *metrics.SampleTags + samplesOutput chan<- metrics.SampleContainer } // Subscribe creates and returns Channel @@ -106,7 +106,7 @@ func (c *Client) send(msg *cableMsg) error { } err := c.codec.Send(c.conn, msg) - stats.PushIfNotDone(c.vu.Context(), c.samplesOutput, stats.Sample{ + metrics.PushIfNotDone(c.vu.Context(), c.samplesOutput, metrics.Sample{ Metric: state.BuiltinMetrics.WSMessagesSent, Time: time.Now(), Tags: c.sampleTags, @@ -181,7 +181,7 @@ func (c *Client) receiveLoop() { if state == nil { continue } - stats.PushIfNotDone(c.vu.Context(), c.samplesOutput, stats.Sample{ + metrics.PushIfNotDone(c.vu.Context(), c.samplesOutput, metrics.Sample{ Metric: state.BuiltinMetrics.WSMessagesReceived, Time: time.Now(), Tags: c.sampleTags, diff --git a/go.mod b/go.mod index 3bf20eb..dff2437 100644 --- a/go.mod +++ b/go.mod @@ -8,7 +8,7 @@ require ( github.com/gorilla/websocket v1.4.2 github.com/sirupsen/logrus v1.8.1 github.com/vmihailenco/msgpack/v5 v5.3.5 - go.k6.io/k6 v0.37.0 + go.k6.io/k6 v0.38.3 ) require (