Skip to content

Commit

Permalink
Update to k6-core master and fix logCache logger
Browse files Browse the repository at this point in the history
This commit fixes an incompatibility detected with k6 core usage of
state.Logger, which has been modified in grafana/k6#2960.
  • Loading branch information
ka3de committed Apr 3, 2023
1 parent dc8784e commit d506083
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 6 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/mccutchen/go-httpbin v1.1.2-0.20190116014521-c5cb2f4802fa
github.com/sirupsen/logrus v1.9.0
github.com/stretchr/testify v1.8.2
go.k6.io/k6 v0.43.2-0.20230315144817-12f5dd846338
go.k6.io/k6 v0.43.2-0.20230331123120-988f2f6b5f6c
golang.org/x/net v0.7.0
golang.org/x/sync v0.0.0-20220819030929-7fc1605a5dde
gopkg.in/guregu/null.v3 v3.3.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4=
github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
go.k6.io/k6 v0.43.2-0.20230315144817-12f5dd846338 h1:U++K4C7gWBx/BSiJEvPq67qAqVwi2nILfqHjv5sIew8=
go.k6.io/k6 v0.43.2-0.20230315144817-12f5dd846338/go.mod h1:vJpKriw44UO0oN63Kp7ES4cxXULAe/S6k9hCVR5hQWs=
go.k6.io/k6 v0.43.2-0.20230331123120-988f2f6b5f6c h1:C09qdiSJsWMKPyFUDxUdh39jRbEvggWJpRexADeDyIg=
go.k6.io/k6 v0.43.2-0.20230331123120-988f2f6b5f6c/go.mod h1:Azozhj76R5Fa1pPatdrTgl7+cL5JHBTIqp4aWroBMw4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
Expand Down
14 changes: 12 additions & 2 deletions tests/logrus_hook.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tests

import (
"fmt"
"io/ioutil"
"strings"
"sync"
Expand Down Expand Up @@ -74,10 +75,19 @@ var _ logrus.Hook = &logCache{}

// attachLogCache sets logger to DebugLevel, attaches a LogCache hook and
// returns it.
func attachLogCache(logger *logrus.Logger) *logCache {
func attachLogCache(fl logrus.FieldLogger) (*logCache, error) {
var ok bool
var logger *logrus.Logger
if logger, ok = fl.(*logrus.Logger); !ok {
// TODO: Fix this to always work with logrus.FieldLoger.
// See: https://github.com/grafana/xk6-browser/issues/818
return nil, fmt.Errorf("unexpected logger type: %T", fl)
}

lc := &logCache{HookedLevels: []logrus.Level{logrus.DebugLevel, logrus.WarnLevel}}
logger.SetLevel(logrus.DebugLevel)
logger.AddHook(lc)
logger.SetOutput(ioutil.Discard)
return lc

return lc, nil
}
6 changes: 5 additions & 1 deletion tests/test_browser.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,14 @@ func newTestBrowser(tb testing.TB, opts ...any) *testBrowser {
state = vu.StateField
rt = vu.RuntimeField
lc *logCache
err error
)

if enableLogCache {
lc = attachLogCache(state.Logger)
lc, err = attachLogCache(state.Logger)
if err != nil {
panic(fmt.Errorf("testBrowser: attaching logger: %w", err))
}
}
if enableHTTPMultiBin {
testServer = k6httpmultibin.NewHTTPMultiBin(tb)
Expand Down

0 comments on commit d506083

Please sign in to comment.