Skip to content

Commit

Permalink
Update k6 0.38.3 and refactor code to use new metrics registry
Browse files Browse the repository at this point in the history
  • Loading branch information
skryukov committed Jun 23, 2022
1 parent 84961e7 commit 278fb8c
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 18 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand Down
23 changes: 12 additions & 11 deletions cable.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
12 changes: 6 additions & 6 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down

0 comments on commit 278fb8c

Please sign in to comment.