This repository has been archived by the owner on Jul 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
xk6-temporal.go
60 lines (49 loc) · 1.59 KB
/
xk6-temporal.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
package temporal
import (
"os"
"go.k6.io/k6/js/modules"
"github.com/temporalio/xk6-temporal/client"
"github.com/temporalio/xk6-temporal/metrics"
)
func init() {
modules.Register("k6/x/temporal", new(RootModule))
}
// RootModule is the global module object type. It is instantiated once per test
// run and will be used to create `k6/x/temporal` module instances for each VU.
type RootModule struct{}
// ModuleInstance represents an instance of the module for every VU.
type ModuleInstance struct {
vu modules.VU
customMetrics metrics.CustomMetrics
}
// Ensure the interfaces are implemented correctly.
var (
_ modules.Module = &RootModule{}
_ modules.Instance = &ModuleInstance{}
)
// NewModuleInstance implements the modules.Module interface to return
// a new instance for each VU.
func (*RootModule) NewModuleInstance(vu modules.VU) modules.Instance {
return &ModuleInstance{
vu: vu,
customMetrics: metrics.RegisterClientMetrics(vu.InitEnv().Registry),
}
}
// Exports implements the modules.Instance interface and returns the exports
// of the JS module.
func (temporal *ModuleInstance) Exports() modules.Exports {
return modules.Exports{Default: temporal}
}
// NewClient returns a new Temporal Client.
func (m *ModuleInstance) NewClient(options client.Options) (*client.Client, error) {
if options.HostPort == "" {
options.HostPort = os.Getenv("TEMPORAL_GRPC_ENDPOINT")
}
options.MetricsHandler = metrics.NewClientMetricsHandler(
m.vu.Context(),
m.vu.State().Samples,
m.vu.State().Tags.GetCurrentValues(),
m.customMetrics,
)
return client.NewClient(options)
}