forked from mostafa/xk6-kafka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodule.go
62 lines (53 loc) · 1.5 KB
/
module.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package kafka
import (
"github.com/dop251/goja"
"github.com/sirupsen/logrus"
"go.k6.io/k6/js/common"
"go.k6.io/k6/js/modules"
)
// init registers the xk6-kafka module as 'k6/x/kafka'
func init() {
modules.Register("k6/x/kafka", New())
}
type (
Kafka struct {
vu modules.VU
metrics kafkaMetrics
logger *logrus.Logger
serializerRegistry *Serde[Serializer]
deserializerRegistry *Serde[Deserializer]
}
RootModule struct{}
KafkaModule struct {
*Kafka
}
)
var (
_ modules.Instance = &KafkaModule{}
_ modules.Module = &RootModule{}
)
// New creates a new instance of the root module
func New() *RootModule {
return &RootModule{}
}
// NewModuleInstance creates a new instance of the Kafka module
func (*RootModule) NewModuleInstance(vu modules.VU) modules.Instance {
m, err := registerMetrics(vu)
if err != nil {
common.Throw(vu.Runtime(), err)
}
// This causes the struct fields to be exported to the native (camelCases) JS code.
vu.Runtime().SetFieldNameMapper(goja.TagFieldNameMapper("json", true))
return &KafkaModule{Kafka: &Kafka{
vu: vu,
metrics: m,
logger: logrus.New(),
serializerRegistry: NewSerializersRegistry(),
deserializerRegistry: NewDeserializersRegistry()},
}
}
// Exports returns the exports of the Kafka module, which are the functions
// that can be called from the JS code.
func (c *KafkaModule) Exports() modules.Exports {
return modules.Exports{Default: c.Kafka}
}