forked from grafana/xk6-loki
-
Notifications
You must be signed in to change notification settings - Fork 0
/
loki.go
112 lines (97 loc) · 2.87 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
// Package loki is the k6 extension module.
package loki
import (
"context"
"fmt"
"net/http"
"net/url"
"time"
gofakeit "github.com/brianvoe/gofakeit/v6"
"go.k6.io/k6/js/common"
"go.k6.io/k6/js/modules"
"go.k6.io/k6/stats"
)
var (
DefaultProtobufRatio = 0.9
DefaultPushTimeout = 10000
DefaultUserAgent = "xk6-loki/0.0.1"
ClientUncompressedBytes = stats.New("loki_client_uncompressed_bytes", stats.Counter, stats.Data)
ClientLines = stats.New("loki_client_lines", stats.Counter, stats.Default)
)
// 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(Loki))
}
// Loki is the k6 extension that can be imported in the Javascript test file.
type Loki struct{}
// XConfig provides a constructor interface for the Config for the Javascript runtime
// ```js
// const cfg = new loki.Config(url);
// ```
func (r *Loki) XConfig(ctxPtr *context.Context, urlString string, timeoutMs int, protobufRatio float64, cardinalities map[string]int) interface{} {
if timeoutMs == 0 {
timeoutMs = DefaultPushTimeout
}
if protobufRatio == 0 {
protobufRatio = DefaultProtobufRatio
}
if len(cardinalities) == 0 {
cardinalities = map[string]int{
"app": 5,
"namespace": 10,
"pod": 50,
}
}
logger := common.GetInitEnv(*ctxPtr).Logger
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)
}
rt := common.GetRuntime(*ctxPtr)
if timeoutMs == 0 {
timeoutMs = DefaultPushTimeout
}
if protobufRatio == 0.0 {
protobufRatio = DefaultProtobufRatio
}
if u.User.Username() == "" {
logger := common.GetInitEnv(*ctxPtr).Logger
logger.Warn("Running in multi-tenant-mode. Each VU has its own X-Scope-OrgID")
}
return common.Bind(
rt,
&Config{
URL: *u,
UserAgent: DefaultUserAgent,
TenantID: u.User.Username(),
Timeout: time.Duration(timeoutMs) * time.Millisecond,
Labels: newLabelPool(faker, cardinalities),
ProtobufRatio: protobufRatio,
},
ctxPtr)
}
// XClient provides a constructor interface for the Config for the Javascript runtime
// ```js
// const client = new loki.Client(cfg);
// ```
func (r *Loki) XClient(ctxPtr *context.Context, config Config) interface{} {
rt := common.GetRuntime(*ctxPtr)
logger := common.GetInitEnv(*ctxPtr).Logger
return common.Bind(rt, &Client{
client: &http.Client{},
cfg: &config,
logger: logger,
}, ctxPtr)
}
func (r *Loki) GetLabels(ctxPtr *context.Context, config Config) interface{} {
rt := common.GetRuntime(*ctxPtr)
return common.Bind(rt, &config.Labels, ctxPtr)
}