forked from grafana/k6
-
Notifications
You must be signed in to change notification settings - Fork 0
/
module.go
159 lines (136 loc) · 4.9 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
// Package tracing implements a k6 JS module for instrumenting k6 scripts with tracing context information.
package tracing
import (
"errors"
"fmt"
"math/rand"
"time"
"github.com/dop251/goja"
"go.k6.io/k6/js/common"
"go.k6.io/k6/js/modules"
)
type (
// RootModule is the global module instance that will create Client
// instances for each VU.
RootModule struct{}
// ModuleInstance represents an instance of the JS module.
ModuleInstance struct {
vu modules.VU
// random is a random number generator used by the module.
random *rand.Rand
// Client holds the module's default tracing client.
*Client
}
)
// Ensure the interfaces are implemented correctly
var (
_ modules.Instance = &ModuleInstance{}
_ modules.Module = &RootModule{}
)
// New returns a pointer to a new RootModule instance
func New() *RootModule {
return &RootModule{}
}
// NewModuleInstance implements the modules.Module interface and returns
// a new instance for each VU.
func (*RootModule) NewModuleInstance(vu modules.VU) modules.Instance {
return &ModuleInstance{
vu: vu,
// Seed the random number generator with the current time.
// This ensures that any call to rand.Intn() will return
// less-deterministic results.
//nolint:gosec // we don't need cryptographic randomness here
random: rand.New(rand.NewSource(time.Now().UTC().UnixNano())),
}
}
// Exports implements the modules.Instance interface and returns
// the exports of the JS module.
func (mi *ModuleInstance) Exports() modules.Exports {
return modules.Exports{
Named: map[string]interface{}{
"Client": mi.newClient,
"instrumentHTTP": mi.instrumentHTTP,
},
}
}
// NewClient is the JS constructor for the tracing.Client
//
// It expects a single configuration object as argument, which
// will be used to instantiate an `Object` instance internally,
// and will be used by the client to configure itself.
func (mi *ModuleInstance) newClient(cc goja.ConstructorCall) *goja.Object {
rt := mi.vu.Runtime()
if len(cc.Arguments) < 1 {
common.Throw(rt, errors.New("Client constructor expects a single configuration object as argument; none given"))
}
opts, err := newOptions(rt, cc.Arguments[0])
if err != nil {
common.Throw(rt, fmt.Errorf("unable to parse options object; reason: %w", err))
}
client, err := NewClient(mi.vu, opts)
if err != nil {
common.Throw(rt, err)
}
return rt.ToValue(client).ToObject(rt)
}
// InstrumentHTTP instruments the HTTP module with tracing headers.
//
// When used in the context of a k6 script, it will automatically replace
// the imported http module's methods with instrumented ones.
func (mi *ModuleInstance) instrumentHTTP(options options) {
rt := mi.vu.Runtime()
if mi.vu.State() != nil {
common.Throw(rt, common.NewInitContextError("tracing module's instrumentHTTP can only be called in the init context"))
}
if mi.Client != nil {
err := errors.New(
"tracing module's instrumentHTTP can only be called once. " +
"if you were attempting to reconfigure the instrumentation, " +
"please consider using the tracing.Client instead",
)
common.Throw(rt, err)
}
// Initialize the tracing module's instance default client,
// and configure it using the user-supplied set of options.
var err error
mi.Client, err = NewClient(mi.vu, options)
if err != nil {
common.Throw(rt, err)
}
// Explicitly inject the http module in the VU's runtime.
// This allows us to later on override the http module's methods
// with instrumented ones.
httpModuleValue, err := rt.RunString(`require('k6/http')`)
if err != nil {
common.Throw(rt, err)
}
httpModuleObj := httpModuleValue.ToObject(rt)
// Closure overriding a method of the provided imported module object.
//
// The `onModule` argument should be a *goja.Object obtained by requiring
// or importing the 'k6/http' module and converting it to an object.
//
// The `value` argument is expected to be callable.
mustSetHTTPMethod := func(method string, onModule *goja.Object, value interface{}) {
// Inject the new get function, adding tracing headers
// to the request in the HTTP module object.
err = onModule.Set(method, value)
if err != nil {
common.Throw(
rt,
fmt.Errorf("unable to overwrite http.%s method with instrumented one; reason: %w", method, err),
)
}
}
// Overwrite the implementation of the http module's method with the instrumented
// ones exposed by the `tracing.Client` struct.
mustSetHTTPMethod("del", httpModuleObj, mi.Client.Del)
mustSetHTTPMethod("get", httpModuleObj, mi.Client.Get)
mustSetHTTPMethod("head", httpModuleObj, mi.Client.Head)
mustSetHTTPMethod("options", httpModuleObj, mi.Client.Options)
mustSetHTTPMethod("patch", httpModuleObj, mi.Client.Patch)
mustSetHTTPMethod("post", httpModuleObj, mi.Client.Post)
mustSetHTTPMethod("put", httpModuleObj, mi.Client.Put)
mustSetHTTPMethod("request", httpModuleObj, mi.Client.Request)
mustSetHTTPMethod("asyncRequest", httpModuleObj, mi.Client.AsyncRequest)
}