Skip to content

Commit 509a486

Browse files
authored
Merge pull request #53 from HarrisChu/enhance_logger
Enhance logger
2 parents 1355d16 + a3a804c commit 509a486

File tree

9 files changed

+123
-32
lines changed

9 files changed

+123
-32
lines changed

example/nebula-test-insert-limit-rate.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,13 @@ export default function(data) {
6262
ngql = ngql + " " + batches.join(',')
6363
let response = session.execute(ngql)
6464
check(response, {
65-
"IsSucceed": (r) => r.isSucceed() === true
65+
"IsSucceed": (r) => r !== null && r.isSucceed() === true
6666
});
6767
// add trend
68-
latencyTrend.add(response.getLatency());
69-
responseTrend.add(response.getResponseTime());
70-
68+
if (response !== null) {
69+
latencyTrend.add(response.getLatency() / 1000);
70+
responseTrend.add(response.getResponseTime() / 1000);
71+
}
7172
};
7273

7374
export function teardown() {

example/nebula-test-insert.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,16 @@ export default function (data) {
4444
}
4545
ngql = ngql + " " + batches.join(',')
4646
let response = session.execute(ngql)
47-
check(response, {
48-
"IsSucceed": (r) => r.isSucceed() === true
49-
});
50-
// add trend
51-
latencyTrend.add(response.getLatency() / 1000);
52-
responseTrend.add(response.getResponseTime() / 1000);
53-
rowSize.add(response.getRowSize());
47+
check(response, {
48+
"IsSucceed": (r) => r !== null && r.isSucceed() === true
49+
});
50+
// add trend
51+
if (response !== null) {
52+
latencyTrend.add(response.getLatency() / 1000);
53+
responseTrend.add(response.getResponseTime() / 1000);
54+
rowSize.add(response.getRowSize());
55+
}
56+
5457
};
5558

5659
export function teardown() {

example/nebula-test-ssl.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,13 @@ export default function(data) {
4848
let ngql = 'go 2 steps from {0} over KNOWS yield dst(edge)'.format(d)
4949
let response = session.execute(ngql)
5050
check(response, {
51-
"IsSucceed": (r) => r.isSucceed() === true
51+
"IsSucceed": (r) => r !== null && r.isSucceed() === true
5252
});
5353
// add trend
54-
latencyTrend.add(response.getLatency() / 1000);
55-
responseTrend.add(response.getResponseTime() / 1000);
56-
54+
if (response !== null) {
55+
latencyTrend.add(response.getLatency() / 1000);
56+
responseTrend.add(response.getResponseTime() / 1000);
57+
}
5758
};
5859

5960
export function teardown() {

example/nebula-test.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,13 @@ export default function(data) {
3737
let ngql = 'go 2 steps from {0} over KNOWS yield dst(edge)'.format(d)
3838
let response = session.execute(ngql)
3939
check(response, {
40-
"IsSucceed": (r) => r.isSucceed() === true
40+
"IsSucceed": (r) => r !== null && r.isSucceed() === true
4141
});
4242
// add trend
43-
latencyTrend.add(response.getLatency() / 1000);
44-
responseTrend.add(response.getResponseTime() / 1000);
43+
if (response !== null) {
44+
latencyTrend.add(response.getLatency() / 1000);
45+
responseTrend.add(response.getResponseTime() / 1000);
46+
}
4547
};
4648

4749
export function teardown() {

go.mod

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ go 1.19
44

55
require (
66
github.com/go-echarts/go-echarts/v2 v2.2.4
7+
github.com/sirupsen/logrus v1.9.0
78
github.com/spf13/cobra v1.4.0
8-
github.com/vesoft-inc/nebula-go/v3 v3.5.0
9+
github.com/vesoft-inc/nebula-go/v3 v3.6.1
910
go.k6.io/k6 v0.45.1
1011
)
1112

1213
require (
1314
github.com/dlclark/regexp2 v1.9.0 // indirect
1415
github.com/dop251/goja v0.0.0-20230531210528-d7324b2d74f7 // indirect
15-
github.com/facebook/fbthrift v0.31.1-0.20211129061412-801ed7f9f295 // indirect
1616
github.com/fatih/color v1.15.0 // indirect
1717
github.com/go-sourcemap/sourcemap v2.1.4-0.20211119122758-180fcef48034+incompatible // indirect
1818
github.com/google/pprof v0.0.0-20230207041349-798e818bf904 // indirect
@@ -25,9 +25,10 @@ require (
2525
github.com/onsi/ginkgo v1.16.5 // indirect
2626
github.com/onsi/gomega v1.27.10 // indirect
2727
github.com/serenize/snaker v0.0.0-20201027110005-a7ad2135616e // indirect
28-
github.com/sirupsen/logrus v1.9.0 // indirect
2928
github.com/spf13/afero v1.1.2 // indirect
3029
github.com/spf13/pflag v1.0.5 // indirect
30+
github.com/vesoft-inc/fbthrift v0.0.0-20230214024353-fa2f34755b28 // indirect
31+
golang.org/x/net v0.12.0 // indirect
3132
golang.org/x/sys v0.10.0 // indirect
3233
golang.org/x/text v0.11.0 // indirect
3334
golang.org/x/time v0.3.0 // indirect

go.sum

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ github.com/dop251/goja v0.0.0-20230531210528-d7324b2d74f7 h1:cVGkvrdHgyBkYeB6kMC
1616
github.com/dop251/goja v0.0.0-20230531210528-d7324b2d74f7/go.mod h1:QMWlm50DNe14hD7t24KEqZuUdC9sOTy8W6XbCU1mlw4=
1717
github.com/dop251/goja_nodejs v0.0.0-20210225215109-d91c329300e7/go.mod h1:hn7BA7c8pLvoGndExHudxTDKZ84Pyvv+90pbBjbTz0Y=
1818
github.com/dop251/goja_nodejs v0.0.0-20211022123610-8dd9abb0616d/go.mod h1:DngW8aVqWbuLRMHItjPUyqdj+HWPvnQe8V8y1nDpIbM=
19-
github.com/facebook/fbthrift v0.31.1-0.20211129061412-801ed7f9f295 h1:ZA+qQ3d2In0RNzVpk+D/nq1sjDSv+s1Wy2zrAPQAmsg=
20-
github.com/facebook/fbthrift v0.31.1-0.20211129061412-801ed7f9f295/go.mod h1:2tncLx5rmw69e5kMBv/yJneERbzrr1yr5fdlnTbu8lU=
2119
github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs=
2220
github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw=
2321
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
@@ -98,8 +96,10 @@ github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5
9896
github.com/stretchr/testify v1.6.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
9997
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
10098
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
101-
github.com/vesoft-inc/nebula-go/v3 v3.5.0 h1:2ZSkoBxtIfs15AXJXqrAPDPd0Z9HrzKR7YKXPqlJcR0=
102-
github.com/vesoft-inc/nebula-go/v3 v3.5.0/go.mod h1:+sXv05jYQBARdTbTcIEsWVXCnF/6ttOlDK35xQ6m54s=
99+
github.com/vesoft-inc/fbthrift v0.0.0-20230214024353-fa2f34755b28 h1:gpoPCGeOEuk/TnoY9nLVK1FoBM5ie7zY3BPVG8q43ME=
100+
github.com/vesoft-inc/fbthrift v0.0.0-20230214024353-fa2f34755b28/go.mod h1:xu7e9za8StcJhBZmCDwK1Hyv4/Y0xFsjS+uqp10ECJg=
101+
github.com/vesoft-inc/nebula-go/v3 v3.6.1 h1:RHdt8WC+jmrRqM9r9WWzz4tzM8VrykPHe9RhtLZjSVA=
102+
github.com/vesoft-inc/nebula-go/v3 v3.6.1/go.mod h1:mjMPlpNKnHYhe1pWz4caT7x9R+wKoX7dIm6u1+Rdcws=
103103
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
104104
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
105105
go.k6.io/k6 v0.45.1 h1:z+iVxE7Qze2Ka8tKvnjerOsoTuQb8e27Vqd1wcG2IFI=
@@ -108,7 +108,7 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk
108108
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
109109
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
110110
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
111-
golang.org/x/crypto v0.9.0 h1:LF6fAI+IutBocDJ2OT0Q1g8plpYljMZ4+lty+dsqw3g=
111+
golang.org/x/crypto v0.11.0 h1:6Ewdq3tDic1mg5xRO4milcWCfMVQhI4NkqWWvqejpuA=
112112
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
113113
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
114114
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
@@ -118,7 +118,9 @@ golang.org/x/net v0.0.0-20200520004742-59133d7f0dd7/go.mod h1:qpuaurCH72eLCgpAm/
118118
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
119119
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
120120
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
121+
golang.org/x/net v0.5.0/go.mod h1:DivGGAXEgPSlEBzxGzZI+ZLohi+xUj054jfeKui00ws=
121122
golang.org/x/net v0.12.0 h1:cfawfvKITfUsFCeJIHJrbSxpeu/E81khclypR0GVT50=
123+
golang.org/x/net v0.12.0/go.mod h1:zEVYFnQC7m/vmpQFELhcD1EWkZlX69l4oqgmer6hfKA=
122124
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
123125
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
124126
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
@@ -139,16 +141,19 @@ golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBc
139141
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
140142
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
141143
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
144+
golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
142145
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
143146
golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA=
144147
golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
145148
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
146149
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
150+
golang.org/x/term v0.4.0/go.mod h1:9P2UbLfCdcvo3p/nzKvsmas4TnlujnuoV9hGgYzW1lQ=
147151
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
148152
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
149153
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
150154
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
151155
golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=
156+
golang.org/x/text v0.6.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
152157
golang.org/x/text v0.11.0 h1:LAntKIrcmeSKERyiOh0XMV39LXS8IE9UL2yP7+f5ij4=
153158
golang.org/x/text v0.11.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
154159
golang.org/x/time v0.3.0 h1:rg5rLMjNzMS1RkNLzCG38eapWhnYLFYXDXj2gOlr8j4=

pkg/nebulagraph/client.go

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,22 @@ type (
2323
DataCh chan common.Data
2424
OutputCh chan []string
2525
initialized bool
26+
closed bool
2627
mutex sync.Mutex
2728
csvReader common.ICsvReader
2829
connPool *graph.ConnectionPool
2930
sessPool *graph.SessionPool
3031
clients []common.IGraphClient
3132
graphOption *common.GraphOption
33+
logger logger
3234
}
3335

3436
// GraphClient a wrapper for nebula client, could read data from DataCh
3537
GraphClient struct {
3638
Client *graph.Session
3739
Pool *GraphPool
3840
DataCh chan common.Data
41+
logger logger
3942
}
4043

4144
// Response a wrapper for nebula resultSet
@@ -56,6 +59,14 @@ type (
5659
errorMsg string
5760
firstRecord string
5861
}
62+
63+
logger interface {
64+
Info(msg string)
65+
Warn(msg string)
66+
Debug(msg string)
67+
Error(msg string)
68+
Fatal(msg string)
69+
}
5970
)
6071

6172
var _ common.IGraphClient = &GraphClient{}
@@ -102,10 +113,16 @@ func (gp *GraphPool) Init() (common.IGraphClientPool, error) {
102113
var (
103114
err error
104115
)
116+
gp.mutex.Lock()
117+
defer gp.mutex.Unlock()
105118
if gp.initialized {
106119
return gp, nil
107120
}
108121

122+
if gp.closed {
123+
return nil, fmt.Errorf("pool has been closed")
124+
}
125+
gp.logger.Debug("initializing graph pool")
109126
switch gp.graphOption.PoolPolicy {
110127
case string(common.ConnectionPool):
111128
err = gp.initConnectionPool()
@@ -204,7 +221,7 @@ func (gp *GraphPool) initSessionPool() error {
204221
if err != nil {
205222
return err
206223
}
207-
pool, err := graph.NewSessionPool(*conf, graph.DefaultLogger{})
224+
pool, err := graph.NewSessionPool(*conf, gp.logger)
208225
if err != nil {
209226
return err
210227
}
@@ -259,6 +276,7 @@ func (gp *GraphPool) Close() error {
259276
if gp.sessPool != nil {
260277
gp.sessPool.Close()
261278
}
279+
gp.closed = true
262280

263281
return nil
264282
}
@@ -279,11 +297,11 @@ func (gp *GraphPool) GetSession() (common.IGraphClient, error) {
279297
if err != nil {
280298
return nil, err
281299
}
282-
s := &GraphClient{Client: c, Pool: gp, DataCh: gp.DataCh}
300+
s := &GraphClient{Client: c, Pool: gp, DataCh: gp.DataCh, logger: gp.logger}
283301
gp.clients = append(gp.clients, s)
284302
return s, nil
285303
} else {
286-
s := &GraphClient{Client: nil, Pool: gp, DataCh: gp.DataCh}
304+
s := &GraphClient{Client: nil, Pool: gp, DataCh: gp.DataCh, logger: gp.logger}
287305
return s, nil
288306
}
289307

@@ -298,7 +316,7 @@ func (gp *GraphPool) SetOption(option *common.GraphOption) error {
298316
return err
299317
}
300318
bs, _ := json.Marshal(gp.graphOption)
301-
fmt.Printf("testing option: %s\n", bs)
319+
gp.logger.Debug(fmt.Sprintf("testing option: %s\n", bs))
302320
return nil
303321
}
304322

@@ -341,7 +359,7 @@ func (gc *GraphClient) executeRetry(stmt string) (*graph.ResultSet, error) {
341359
return resp, fmt.Errorf("retry timeout")
342360
}
343361
if err != nil {
344-
fmt.Println("execute error: ", err)
362+
gc.logger.Warn(fmt.Sprintf("execute error: %s", err.Error()))
345363
continue
346364
}
347365

pkg/nebulagraph/module.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package nebulagraph
2+
3+
import (
4+
"github.com/sirupsen/logrus"
5+
"go.k6.io/k6/js/modules"
6+
)
7+
8+
var _ modules.Module = &K6Module{}
9+
10+
// refer: https://k6.io/docs/extensions/get-started/create/javascript-extensions/#use-the-advanced-module-api
11+
// K6Module is a module for k6, using the advanced module API
12+
type K6Module struct {
13+
pool *GraphPool
14+
}
15+
16+
type K6NebulaInstance struct {
17+
vu modules.VU
18+
pool *GraphPool
19+
}
20+
21+
type loggerWrapper struct {
22+
log logrus.FieldLogger
23+
}
24+
25+
func (l *loggerWrapper) Info(msg string) {
26+
l.log.Info(msg)
27+
}
28+
func (l *loggerWrapper) Warn(msg string) {
29+
l.log.Warn(msg)
30+
}
31+
func (l *loggerWrapper) Debug(msg string) {
32+
l.log.Debug(msg)
33+
}
34+
func (l *loggerWrapper) Error(msg string) {
35+
l.log.Error(msg)
36+
}
37+
func (l *loggerWrapper) Fatal(msg string) {
38+
l.log.Fatal(msg)
39+
}
40+
41+
func NewModule() *K6Module {
42+
return &K6Module{
43+
pool: NewNebulaGraph(),
44+
}
45+
}
46+
47+
func (m *K6Module) NewModuleInstance(vu modules.VU) modules.Instance {
48+
return &K6NebulaInstance{
49+
vu: vu,
50+
pool: m.pool,
51+
}
52+
}
53+
54+
func (i *K6NebulaInstance) Exports() modules.Exports {
55+
logger := i.vu.InitEnv().Logger
56+
i.pool.logger = &loggerWrapper{log: logger}
57+
return modules.Exports{
58+
Default: i.pool,
59+
}
60+
}

register.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
)
99

1010
func init() {
11-
modules.Register("k6/x/nebulagraph", nebulagraph.NewNebulaGraph())
11+
modules.Register("k6/x/nebulagraph", nebulagraph.NewModule())
1212
output.RegisterExtension("aggcsv", func(p output.Params) (output.Output, error) {
1313
return aggcsv.New(p)
1414
})

0 commit comments

Comments
 (0)