-
Notifications
You must be signed in to change notification settings - Fork 23
/
rpc.go
193 lines (158 loc) · 5.28 KB
/
rpc.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
package runner
import (
"encoding/json"
"net/http"
"net/rpc"
"time"
"github.com/caraml-dev/mlp/api/pkg/instrumentation/metrics"
"github.com/hashicorp/go-plugin"
"github.com/caraml-dev/turing/engines/experiment/plugin/rpc/shared"
"github.com/caraml-dev/turing/engines/experiment/runner"
"github.com/caraml-dev/turing/engines/router/missionctl/instrumentation"
)
// rpcClient implements ConfigurableExperimentRunner interface
type rpcClient struct {
*plugin.MuxBroker
shared.RPCClient
}
func (c *rpcClient) Configure(cfg json.RawMessage) error {
return c.Call("Plugin.Configure", cfg, new(interface{}))
}
func (c *rpcClient) GetTreatmentForRequest(
header http.Header,
payload []byte,
options runner.GetTreatmentOptions,
) (*runner.Treatment, error) {
req := GetTreatmentRequest{
Header: header,
Payload: payload,
Options: options,
}
var resp runner.Treatment
err := c.Call("Plugin.GetTreatmentForRequest", &req, &resp)
if err != nil {
return nil, err
}
return &resp, nil
}
func (c *rpcClient) RegisterMetricsCollector(
_ metrics.Collector,
metricsRegistrationHelper runner.MetricsRegistrationHelper,
) error {
collectorBrokerID := c.MuxBroker.NextId()
go c.MuxBroker.AcceptAndServe(collectorBrokerID, &rpcCollectorServer{})
metricsRegistrationHelperBrokerID := c.MuxBroker.NextId()
go c.MuxBroker.AcceptAndServe(metricsRegistrationHelperBrokerID,
&rpcMetricsRegistrationHelperServer{Impl: metricsRegistrationHelper})
return c.Call(
"Plugin.RegisterMetricsCollector",
[]uint32{collectorBrokerID, metricsRegistrationHelperBrokerID},
new(interface{}),
)
}
// rpcCollectorClient is an implementation of Collector used by the plugin to talk to the router over RPC.
type rpcCollectorClient struct {
*plugin.MuxBroker
shared.RPCClient
}
func (c *rpcCollectorClient) MeasureDurationMsSince(
key metrics.MetricName,
starttime time.Time,
labels map[string]string,
) error {
req := MeasureDurationMsSinceRequest{
Key: key,
Starttime: starttime,
Labels: labels,
}
return c.Call("Plugin.MeasureDurationMsSince", &req, new(interface{}))
}
func (c *rpcCollectorClient) MeasureDurationMs(key metrics.MetricName, labels map[string]func() string) func() {
var resp func()
req := MeasureDurationMsRequest{
Key: key,
Labels: labels,
}
err := c.Call("Plugin.MeasureDurationMs", &req, &resp)
if err != nil {
return nil
}
return resp
}
func (c *rpcCollectorClient) RecordGauge(key metrics.MetricName, value float64, labels map[string]string) error {
req := RecordGaugeRequest{
Key: key,
Value: value,
Labels: labels,
}
return c.Call("Plugin.RecordGauge", &req, new(interface{}))
}
func (c *rpcCollectorClient) Inc(key metrics.MetricName, labels map[string]string) error {
req := IncRequest{
Key: key,
Labels: labels,
}
return c.Call("Plugin.Inc", &req, new(interface{}))
}
// rpcMetricsRegistrationHelperClient is an implementation of MetricsRegistrationHelper used by the plugin to talk to
// the router over RPC.
type rpcMetricsRegistrationHelperClient struct {
*plugin.MuxBroker
shared.RPCClient
}
func (c *rpcMetricsRegistrationHelperClient) Register(metrics []instrumentation.Metric) error {
return c.Call("Plugin.Register", metrics, new(interface{}))
}
// rpcServer serves the implementation of a ConfigurableExperimentRunner
type rpcServer struct {
*plugin.MuxBroker
Impl ConfigurableExperimentRunner
}
func (s *rpcServer) Configure(cfg json.RawMessage, _ *interface{}) (err error) {
return s.Impl.Configure(cfg)
}
func (s *rpcServer) GetTreatmentForRequest(req *GetTreatmentRequest, resp *runner.Treatment) error {
treatment, err := s.Impl.GetTreatmentForRequest(req.Header, req.Payload, req.Options)
if err != nil {
return err
}
*resp = *treatment
return nil
}
func (s *rpcServer) RegisterMetricsCollector(brokerIDs []uint32, _ *interface{}) (err error) {
collectorConn, err := s.MuxBroker.Dial(brokerIDs[0])
if err != nil {
return err
}
metricsRegistrationHelperConn, err := s.MuxBroker.Dial(brokerIDs[1])
if err != nil {
return err
}
return s.Impl.RegisterMetricsCollector(
&rpcCollectorClient{RPCClient: rpc.NewClient(collectorConn)},
&rpcMetricsRegistrationHelperClient{RPCClient: rpc.NewClient(metricsRegistrationHelperConn)},
)
}
// rpcCollectorServer is used by the router to talk to the plugin over RPC.
type rpcCollectorServer struct{}
func (s *rpcCollectorServer) MeasureDurationMsSince(req *MeasureDurationMsSinceRequest, _ *interface{}) error {
return metrics.Glob().MeasureDurationMsSince(req.Key, req.Starttime, req.Labels)
}
func (s *rpcCollectorServer) MeasureDurationMs(req *MeasureDurationMsRequest, resp *func()) error {
returnFunc := metrics.Glob().MeasureDurationMs(req.Key, req.Labels)
*resp = returnFunc
return nil
}
func (s *rpcCollectorServer) RecordGauge(req *RecordGaugeRequest, _ *interface{}) error {
return metrics.Glob().RecordGauge(req.Key, req.Value, req.Labels)
}
func (s *rpcCollectorServer) Inc(req *IncRequest, _ *interface{}) error {
return metrics.Glob().Inc(req.Key, req.Labels)
}
// rpcMetricsRegistrationHelperServer is used by the router to talk to the plugin over RPC.
type rpcMetricsRegistrationHelperServer struct {
Impl runner.MetricsRegistrationHelper
}
func (s *rpcMetricsRegistrationHelperServer) Register(req []instrumentation.Metric, _ *interface{}) error {
return s.Impl.Register(req)
}