-
Notifications
You must be signed in to change notification settings - Fork 0
/
logstore.go
298 lines (248 loc) · 7.64 KB
/
logstore.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
// Package logstore is the k6 extension module.
package logstore
import (
"fmt"
"math/rand"
"net/http"
"net/url"
"reflect"
"time"
"github.com/brianvoe/gofakeit/v6"
"github.com/dop251/goja"
"github.com/insightmon/xk6-logstore/flog"
"github.com/prometheus/common/model"
"github.com/sirupsen/logrus"
"go.k6.io/k6/js/common"
"go.k6.io/k6/js/modules"
"go.k6.io/k6/metrics"
)
var (
DefaultProtobufRatio = 0.9
DefaultPushTimeout = 10000
DefaultUserAgent = "xk6-logstore/0.0.1"
)
// init registers the Go module as Javascript module for k6
// The module can be imported like so:
// ```js
// import remote from 'k6/x/logstore';
// ```
//
// See examples/simple.js for a full example how to use the xk6-logstore extension.
func init() {
modules.Register("k6/x/logstore", new(LokiRoot))
}
var _ modules.Module = &LokiRoot{}
type lokiMetrics struct {
ClientUncompressedBytes *metrics.Metric
ClientLines *metrics.Metric
BytesProcessedTotal *metrics.Metric
BytesProcessedPerSeconds *metrics.Metric
LinesProcessedTotal *metrics.Metric
LinesProcessedPerSeconds *metrics.Metric
}
// LokiRoot is the root module
type LokiRoot struct{}
func (*LokiRoot) NewModuleInstance(vu modules.VU) modules.Instance {
m, err := registerMetrics(vu)
if err != nil {
common.Throw(vu.Runtime(), err)
}
logger := vu.InitEnv().Logger.WithField("component", "xk6-logstore")
return &Loki{vu: vu, metrics: m, logger: logger}
}
func registerMetrics(vu modules.VU) (lokiMetrics, error) {
var err error
registry := vu.InitEnv().Registry
m := lokiMetrics{}
m.ClientUncompressedBytes, err = registry.NewMetric("loki_client_uncompressed_bytes", metrics.Counter, metrics.Data)
if err != nil {
return m, err
}
m.ClientLines, err = registry.NewMetric("loki_client_lines", metrics.Counter, metrics.Default)
if err != nil {
return m, err
}
m.BytesProcessedTotal, err = registry.NewMetric("loki_bytes_processed_total", metrics.Counter, metrics.Data)
if err != nil {
return m, err
}
m.BytesProcessedPerSeconds, err = registry.NewMetric("loki_bytes_processed_per_second", metrics.Trend, metrics.Data)
if err != nil {
return m, err
}
m.LinesProcessedTotal, err = registry.NewMetric("loki_lines_processed_total", metrics.Counter, metrics.Default)
if err != nil {
return m, err
}
m.LinesProcessedPerSeconds, err = registry.NewMetric("loki_lines_processed_per_second", metrics.Trend, metrics.Default)
if err != nil {
return m, err
}
return m, nil
}
// Loki is the k6 extension that can be imported in the Javascript test file.
type Loki struct {
vu modules.VU
metrics lokiMetrics
logger logrus.FieldLogger
}
func (r *Loki) Exports() modules.Exports {
return modules.Exports{
Named: map[string]interface{}{
"Config": r.config,
"Client": r.client,
"Labels": r.createLabels,
"getLables": r.getLabels,
},
}
}
// config provides a constructor interface for the Config for the Javascript runtime
// ```js
// const cfg = new logstore.Config(url);
// ```
func (r *Loki) config(c goja.ConstructorCall) *goja.Object {
rt := r.vu.Runtime()
// The default config, which we might overwrite below
config := &Config{
Timeout: time.Duration(DefaultPushTimeout) * time.Millisecond,
ProtobufRatio: DefaultProtobufRatio,
UserAgent: DefaultUserAgent,
Cardinalities: map[string]int{
"app": 5,
"namespace": 10,
"pod": 50,
},
RandSeed: time.Now().Unix(),
}
if len(c.Arguments) > 1 || c.Argument(0).ExportType().Kind() == reflect.String {
if err := r.parsePositionalConfig(c, config); err != nil {
common.Throw(rt, fmt.Errorf("could not parse positional logstore config: %w", err))
}
} else {
if err := r.parseConfigObject(c.Argument(0).ToObject(rt), config); err != nil {
common.Throw(rt, fmt.Errorf("could not parse logstore config object: %w", err))
}
}
r.logger.Debug(fmt.Sprintf(
"url=%s timeout=%s protobufRatio=%f cardinalities=%v randSeed=%d",
&config.URL, config.Timeout, config.ProtobufRatio, config.Cardinalities, config.RandSeed,
))
if config.TenantID == "" {
r.logger.Warn("Running in multi-tenant-mode. Each VU has its own X-Scope-OrgID")
}
return rt.ToValue(config).ToObject(rt)
}
func (r *Loki) parsePositionalConfig(c goja.ConstructorCall, config *Config) error {
rt := r.vu.Runtime()
urlString := c.Argument(0).String()
u, err := url.Parse(urlString)
if err != nil {
return fmt.Errorf("invalid logstore URL: %w", err)
}
config.URL = *u
if user := u.User.Username(); user != "" {
config.TenantID = user
}
if len(c.Arguments) > 1 {
config.Timeout = time.Duration(c.Argument(1).ToInteger()) * time.Millisecond
}
if len(c.Arguments) > 2 {
config.ProtobufRatio = c.Argument(2).ToFloat()
}
if len(c.Arguments) > 3 {
if err := rt.ExportTo(c.Argument(3), &config.Cardinalities); err != nil {
return fmt.Errorf("Config constructor expects map of string to integers as forth argument")
}
}
if len(c.Arguments) > 4 {
if err := rt.ExportTo(c.Argument(4), &config.Labels); err != nil {
return fmt.Errorf("Config constructor expects Labels as fifth argument")
}
}
return nil
}
func isNully(v goja.Value) bool {
return v == nil || goja.IsUndefined(v) || goja.IsNull(v)
}
func (r *Loki) parseConfigObject(c *goja.Object, config *Config) error {
rt := r.vu.Runtime()
if v := c.Get("url"); !isNully(v) {
u, err := url.Parse(v.String())
if err != nil {
return fmt.Errorf("invalid logstore URL: %w", err)
}
config.URL = *u
if user := u.User.Username(); user != "" {
config.TenantID = user
}
}
if v := c.Get("userAgent"); !isNully(v) {
config.UserAgent = v.String()
}
if v := c.Get("timeout"); !isNully(v) {
config.Timeout = time.Duration(v.ToInteger()) * time.Millisecond
}
if v := c.Get("tenantID"); !isNully(v) {
// This can overwrite the TenantID, even if we set it via the URL
config.TenantID = v.String()
}
if v := c.Get("cardinalities"); !isNully(v) {
if err := rt.ExportTo(v, &config.Cardinalities); err != nil {
return fmt.Errorf("cardinatities should be a map of string to integers: %w", err)
}
}
if v := c.Get("labels"); !isNully(v) {
if err := rt.ExportTo(v, &config.Labels); err != nil {
return fmt.Errorf("could not parse labels: %w", err)
}
}
if v := c.Get("protobufRatio"); !isNully(v) {
config.ProtobufRatio = v.ToFloat()
}
if v := c.Get("randSeed"); !isNully(v) {
config.RandSeed = v.ToInteger()
}
return nil
}
// client provides a constructor interface for the Config for the Javascript runtime
// ```js
// const client = new logstore.Client(cfg);
// ```
func (r *Loki) client(c goja.ConstructorCall) *goja.Object {
rt := r.vu.Runtime()
config, ok := c.Argument(0).Export().(*Config)
if !ok {
common.Throw(rt, fmt.Errorf("Client constructor expect Config as it's argument"))
}
rand := rand.New(rand.NewSource(config.RandSeed))
faker := gofakeit.NewCustom(rand)
flog := flog.New(rand, faker)
if len(config.Labels) == 0 {
config.Labels = newLabelPool(faker, config.Cardinalities)
}
return rt.ToValue(&Client{
client: &http.Client{},
cfg: config,
vu: r.vu,
metrics: r.metrics,
rand: rand,
faker: faker,
flog: flog,
labels: transformLabelPool(config.Labels),
}).ToObject(rt)
}
func (r *Loki) createLabels(c goja.ConstructorCall) *goja.Object {
rt := r.vu.Runtime()
var labels map[string][]string
if err := rt.ExportTo(c.Argument(0), &labels); err != nil {
common.Throw(rt, fmt.Errorf("Labels constructor expects map of string to string array argument"))
}
pool := make(LabelPool, len(labels))
for k, v := range labels {
pool[model.LabelName(k)] = v
}
return rt.ToValue(&pool).ToObject(rt)
}
func (r *Loki) getLabels(config Config) interface{} {
return &config.Labels
}