forked from grafana/xk6-loki
-
Notifications
You must be signed in to change notification settings - Fork 0
/
loki.go
216 lines (181 loc) · 5.43 KB
/
loki.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
// Package loki is the k6 extension module.
package loki
import (
"fmt"
"net/http"
"net/url"
"time"
gofakeit "github.com/brianvoe/gofakeit/v6"
"github.com/dop251/goja"
"github.com/pkg/errors"
"github.com/prometheus/common/model"
"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-loki/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/loki';
// ```
//
// See examples/simple.js for a full example how to use the xk6-loki extension.
func init() {
modules.Register("k6/x/loki", 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)
}
return &Loki{vu: vu, metrics: m}
}
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
}
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 loki.Config(url);
// ```
func (r *Loki) config(c goja.ConstructorCall) *goja.Object {
initEnv := r.vu.InitEnv()
rt := r.vu.Runtime()
if initEnv == nil {
common.Throw(rt, errors.New("Client constructor needs to be called in the init context"))
}
urlString := c.Argument(0).String()
timeoutMs := int(c.Argument(1).ToInteger())
if timeoutMs == 0 {
timeoutMs = DefaultPushTimeout
}
protobufRatio := c.Argument(2).ToFloat()
if protobufRatio == 0 {
protobufRatio = DefaultProtobufRatio
}
var cardinalities map[string]int
if err := rt.ExportTo(c.Argument(3), &cardinalities); err != nil {
common.Throw(rt, fmt.Errorf("Config constructor expects map of string to integers as forth argument"))
}
var labels LabelPool
if err := rt.ExportTo(c.Argument(4), &labels); err != nil {
common.Throw(rt, fmt.Errorf("Config constructor expects Labels as fifth argument"))
}
initEnv.Logger.Debug(fmt.Sprintf("url=%s timeoutMs=%d protobufRatio=%f cardinalities=%v", urlString, timeoutMs, protobufRatio, cardinalities))
faker := gofakeit.New(12345)
u, err := url.Parse(urlString)
if err != nil {
panic(err)
}
if u.User.Username() == "" {
initEnv.Logger.Warn("Running in multi-tenant-mode. Each VU has its own X-Scope-OrgID")
}
if len(labels) == 0 {
if len(cardinalities) == 0 {
cardinalities = map[string]int{
"app": 5,
"namespace": 10,
"pod": 50,
}
}
labels = newLabelPool(faker, cardinalities)
}
config := &Config{
URL: *u,
UserAgent: DefaultUserAgent,
TenantID: u.User.Username(),
Timeout: time.Duration(timeoutMs) * time.Millisecond,
Labels: labels,
ProtobufRatio: protobufRatio,
}
return rt.ToValue(config).ToObject(rt)
}
// client provides a constructor interface for the Config for the Javascript runtime
// ```js
// const client = new loki.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"))
}
return rt.ToValue(&Client{
client: &http.Client{},
cfg: config,
vu: r.vu,
metrics: r.metrics,
}).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
}