forked from mostafa/xk6-kafka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
module.go
224 lines (189 loc) · 6.76 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package kafka
import (
"crypto/tls"
"time"
"github.com/grafana/sobek"
"github.com/riferrei/srclient"
kafkago "github.com/segmentio/kafka-go"
"github.com/segmentio/kafka-go/compress"
"github.com/sirupsen/logrus"
"go.k6.io/k6/js/common"
"go.k6.io/k6/js/modules"
"go.k6.io/k6/lib/netext"
)
// logger is globally used by the Kafka module.
var logger *logrus.Logger
// init registers the xk6-kafka module as 'k6/x/kafka'.
func init() {
// Initialize the global logger.
logger = logrus.New()
// Initialize the TLS versions map.
TLSVersions = map[string]uint16{
netext.TLS_1_0: tls.VersionTLS10,
netext.TLS_1_1: tls.VersionTLS11,
netext.TLS_1_2: tls.VersionTLS12,
netext.TLS_1_3: tls.VersionTLS13,
}
// Initialize the compression types map.
CompressionCodecs = map[string]compress.Compression{
codecGzip: compress.Gzip,
codecSnappy: compress.Snappy,
codecLz4: compress.Lz4,
codecZstd: compress.Zstd,
}
// Initialize the balancer types map.
Balancers = map[string]kafkago.Balancer{
balancerRoundRobin: &kafkago.RoundRobin{},
balancerLeastBytes: &kafkago.LeastBytes{},
balancerHash: &kafkago.Hash{},
balancerCrc32: &kafkago.CRC32Balancer{},
balancerMurmur2: &kafkago.Murmur2Balancer{},
}
// Initialize the group balancer types map.
GroupBalancers = map[string]kafkago.GroupBalancer{
groupBalancerRange: &kafkago.RangeGroupBalancer{},
groupBalancerRoundRobin: &kafkago.RoundRobinGroupBalancer{},
groupBalancerRackAffinity: &kafkago.RackAffinityGroupBalancer{},
}
// Initialize the isolation levels map.
IsolationLevels = map[string]kafkago.IsolationLevel{
isolationLevelReadUncommitted: kafkago.ReadUncommitted,
isolationLevelReadCommitted: kafkago.ReadCommitted,
}
// Initialize the start offsets map.
StartOffsets = map[string]int64{
lastOffset: kafkago.LastOffset, // The most recent offset available for a partition.
firstOffset: kafkago.FirstOffset, // The least recent offset available for a partition.
}
// Register the module namespace (aka. JS import path).
modules.Register("k6/x/kafka", New())
}
type (
Kafka struct {
vu modules.VU
metrics kafkaMetrics
exports *sobek.Object
schemaCache map[string]*Schema
}
RootModule struct{}
Module struct {
*Kafka
}
)
var (
_ modules.Instance = &Module{}
_ 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(virtualUser modules.VU) modules.Instance {
runtime := virtualUser.Runtime()
metrics, err := registerMetrics(virtualUser)
if err != nil {
common.Throw(virtualUser.Runtime(), err)
}
// Create a new Kafka module.
moduleInstance := &Module{
Kafka: &Kafka{
vu: virtualUser,
metrics: metrics,
exports: runtime.NewObject(),
schemaCache: make(map[string]*Schema),
},
}
// Export constants to the JS code.
moduleInstance.defineConstants()
mustExport := func(name string, value interface{}) {
if err := moduleInstance.exports.Set(name, value); err != nil {
common.Throw(runtime, err)
}
}
// Export the constructors and functions from the Kafka module to the JS code.
// The Writer is a constructor and must be called with new, e.g. new Writer(...).
mustExport("Writer", moduleInstance.writerClass)
// The Reader is a constructor and must be called with new, e.g. new Reader(...).
mustExport("Reader", moduleInstance.readerClass)
// The Connection is a constructor and must be called with new, e.g. new Connection(...).
mustExport("Connection", moduleInstance.connectionClass)
// The SchemaRegistry is a constructor and must be called with new, e.g. new SchemaRegistry(...).
mustExport("SchemaRegistry", moduleInstance.schemaRegistryClientClass)
// The LoadJKS is a function and must be called without new, e.g. LoadJKS(...).
mustExport("LoadJKS", moduleInstance.loadJKSFunction)
return moduleInstance
}
// Exports returns the exports of the Kafka module, which are the functions
// that can be called from the JS code.
func (m *Module) Exports() modules.Exports {
return modules.Exports{
Default: m.Kafka.exports,
}
}
// defineConstants defines the constants that can be used in the JS code.
// nolint: funlen
func (m *Module) defineConstants() {
runtime := m.vu.Runtime()
mustAddProp := func(name string, val interface{}) {
err := m.exports.DefineDataProperty(
name, runtime.ToValue(val), sobek.FLAG_FALSE, sobek.FLAG_FALSE, sobek.FLAG_TRUE,
)
if err != nil {
common.Throw(runtime, err)
}
}
// TLS versions
mustAddProp("TLS_1_0", netext.TLS_1_0)
mustAddProp("TLS_1_1", netext.TLS_1_1)
mustAddProp("TLS_1_2", netext.TLS_1_2)
mustAddProp("TLS_1_3", netext.TLS_1_3)
// SASL mechanisms
mustAddProp("NONE", none)
mustAddProp("SASL_PLAIN", saslPlain)
mustAddProp("SASL_SCRAM_SHA256", saslScramSha256)
mustAddProp("SASL_SCRAM_SHA512", saslScramSha512)
mustAddProp("SASL_SSL", saslSsl)
mustAddProp("SASL_AWS_IAM", saslAwsIam)
// Compression codecs
mustAddProp("CODEC_GZIP", codecGzip)
mustAddProp("CODEC_SNAPPY", codecSnappy)
mustAddProp("CODEC_LZ4", codecLz4)
mustAddProp("CODEC_ZSTD", codecZstd)
// Balancer types
mustAddProp("BALANCER_ROUND_ROBIN", balancerRoundRobin)
mustAddProp("BALANCER_LEAST_BYTES", balancerLeastBytes)
mustAddProp("BALANCER_HASH", balancerHash)
mustAddProp("BALANCER_CRC32", balancerCrc32)
mustAddProp("BALANCER_MURMUR2", balancerMurmur2)
// Group balancer types
mustAddProp("GROUP_BALANCER_RANGE", groupBalancerRange)
mustAddProp("GROUP_BALANCER_ROUND_ROBIN", groupBalancerRoundRobin)
mustAddProp("GROUP_BALANCER_RACK_AFFINITY", groupBalancerRackAffinity)
// Isolation levels
mustAddProp("ISOLATION_LEVEL_READ_UNCOMMITTED", isolationLevelReadUncommitted)
mustAddProp("ISOLATION_LEVEL_READ_COMMITTED", isolationLevelReadCommitted)
// Start offsets
mustAddProp("FIRST_OFFSET", firstOffset)
mustAddProp("LAST_OFFSET", lastOffset)
// TopicNameStrategy types
mustAddProp("TOPIC_NAME_STRATEGY", TopicNameStrategy)
mustAddProp("RECORD_NAME_STRATEGY", RecordNameStrategy)
mustAddProp("TOPIC_RECORD_NAME_STRATEGY", TopicRecordNameStrategy)
// Element types
mustAddProp("KEY", string(Key))
mustAddProp("VALUE", string(Value))
// Schema types
mustAddProp("SCHEMA_TYPE_STRING", String)
mustAddProp("SCHEMA_TYPE_BYTES", Bytes)
mustAddProp("SCHEMA_TYPE_AVRO", srclient.Avro)
mustAddProp("SCHEMA_TYPE_JSON", srclient.Json)
mustAddProp("SCHEMA_TYPE_PROTOBUF", srclient.Protobuf)
// Time constants
mustAddProp("NANOSECOND", int64(time.Nanosecond))
mustAddProp("MICROSECOND", int64(time.Microsecond))
mustAddProp("MILLISECOND", int64(time.Millisecond))
mustAddProp("SECOND", int64(time.Second))
mustAddProp("MINUTE", int64(time.Minute))
mustAddProp("HOUR", int64(time.Hour))
}