forked from caraml-dev/xp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexperiment_runner.go
328 lines (290 loc) · 10.3 KB
/
experiment_runner.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
package runner
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"time"
"github.com/caraml-dev/mlp/api/pkg/instrumentation/metrics"
"github.com/caraml-dev/turing/engines/experiment/log"
"github.com/caraml-dev/turing/engines/experiment/pkg/request"
inproc "github.com/caraml-dev/turing/engines/experiment/plugin/inproc/runner"
"github.com/caraml-dev/turing/engines/experiment/runner"
routerMetrics "github.com/caraml-dev/turing/engines/router/missionctl/instrumentation"
"github.com/caraml-dev/xp/common/api/schema"
"github.com/caraml-dev/xp/common/pubsub"
_segmenters "github.com/caraml-dev/xp/common/segmenters"
"github.com/caraml-dev/xp/treatment-service/api"
"github.com/caraml-dev/xp/treatment-service/controller"
"github.com/caraml-dev/xp/treatment-service/instrumentation"
"github.com/caraml-dev/xp/treatment-service/models"
"github.com/caraml-dev/xp/treatment-service/monitoring"
"github.com/google/uuid"
"github.com/pkg/errors"
"golang.org/x/net/context"
"github.com/caraml-dev/xp/plugins/turing/config"
"github.com/caraml-dev/xp/treatment-service/appcontext"
)
// init ensures this runner is registered when the package is imported.
func init() {
err := inproc.Register("xp", NewExperimentRunner)
if err != nil {
log.Panicf("failed to register xp experiment runner: %v", err)
}
}
// experimentRunner implements runner.ExperimentRunner
type experimentRunner struct {
projectID int64
parameters []config.Variable
appContext *appcontext.AppContext
}
func (er *experimentRunner) GetTreatmentForRequest(
reqHeader http.Header,
body []byte,
options runner.GetTreatmentOptions,
) (*runner.Treatment, error) {
logger := log.With("turing_req_id", options.TuringRequestID)
// Get the request parameters for the current request
requestParams := er.getRequestParams(logger, reqHeader, body)
projectId := models.NewProjectId(er.projectID)
// Initialize metric / log variables
begin := time.Now()
var requestFilter map[string][]*_segmenters.SegmenterValue
var filteredExperiment *pubsub.Experiment
var selectedTreatment *pubsub.ExperimentTreatment
var treatment schema.SelectedTreatment
var switchbackWindowId *int64
var err error
statusCode := http.StatusBadRequest
filterParams := api.FetchTreatmentRequestBody{AdditionalProperties: requestParams}
defer func() {
if requestFilter == nil {
requestFilter = map[string][]*_segmenters.SegmenterValue{}
}
er.appContext.MetricService.LogFetchTreatmentMetrics(begin, projectId, treatment, requestFilter, statusCode)
if statusCode == http.StatusInternalServerError && err != nil {
// This is typically a problem with the experiment configuration that should not have been allowed
// by the Management Service, or other unexpected errors. Log the response to console, for tracking.
controller.LogFetchTreatmentError(
projectId,
statusCode,
err,
filterParams,
requestFilter,
)
}
}()
var lookupRequestFilters []models.SegmentFilter
var errorLog *monitoring.ErrorResponseLog
if er.appContext.AssignedTreatmentLogger != nil {
defer func() {
// Capture potential errors from other calls to service layer and prevent it from
// slipping pass subsequent JSON marshaling errors
if err != nil {
errorLog = &monitoring.ErrorResponseLog{Code: statusCode, Error: err.Error()}
}
headerJson, err := json.Marshal(reqHeader)
if err != nil {
errorLog = &monitoring.ErrorResponseLog{Code: statusCode, Error: err.Error()}
}
bodyJson, err := json.Marshal(filterParams)
if err != nil {
errorLog = &monitoring.ErrorResponseLog{Code: statusCode, Error: err.Error()}
}
requestJson := &monitoring.Request{
Header: string(headerJson),
Body: string(bodyJson),
}
var requestFilters []models.SegmentFilter
if errorLog == nil {
requestFilters = lookupRequestFilters
}
assignedTreatmentLog := &monitoring.AssignedTreatmentLog{
ProjectID: projectId,
RequestID: uuid.New().String(),
Experiment: filteredExperiment,
Treatment: selectedTreatment,
Request: requestJson,
Segmenters: requestFilters,
}
if filteredExperiment != nil {
assignedTreatmentLog.TreatmentMetadata = &monitoring.TreatmentMetadata{
ExperimentVersion: filteredExperiment.Version,
ExperimentType: string(models.ProtobufExperimentTypeToOpenAPI(filteredExperiment.Type)),
SwitchbackWindowId: switchbackWindowId,
}
}
if errorLog != nil {
assignedTreatmentLog.Error = errorLog
}
_ = er.appContext.AssignedTreatmentLogger.Append(assignedTreatmentLog)
}()
}
// Use the S2ID at the max configured level (most granular level) to generate the filter
requestFilter, err = er.appContext.SchemaService.GetRequestFilter(projectId, requestParams)
if err != nil {
return nil, err
}
_, filteredExperiment, err = er.appContext.ExperimentService.GetExperiment(projectId, requestFilter)
if err != nil {
return nil, err
}
experimentLookupLabels := er.appContext.MetricService.GetProjectNameLabel(projectId)
er.appContext.MetricService.LogLatencyHistogram(begin, experimentLookupLabels, instrumentation.ExperimentLookupDurationMs)
// Fetch treatment
if filteredExperiment == nil {
statusCode = http.StatusOK
return &runner.Treatment{
Config: nil,
}, nil
}
randomizationKeyValue, err := er.appContext.SchemaService.GetRandomizationKeyValue(projectId, requestParams)
if err != nil {
return nil, err
}
selectedTreatment, switchbackWindowId, err = er.appContext.TreatmentService.GetTreatment(filteredExperiment, randomizationKeyValue)
if err != nil {
return nil, err
}
treatmentRepr := models.ExperimentTreatmentToOpenAPITreatment(selectedTreatment)
// Marshal and return response
treatment = schema.SelectedTreatment{
ExperimentId: filteredExperiment.Id,
ExperimentName: filteredExperiment.Name,
Treatment: treatmentRepr,
Metadata: schema.SelectedTreatmentMetadata{
ExperimentVersion: filteredExperiment.Version,
ExperimentType: models.ProtobufExperimentTypeToOpenAPI(filteredExperiment.Type),
SwitchbackWindowId: switchbackWindowId,
},
}
// Marshal Response Body
rawConfig, err := json.Marshal(treatment)
if err != nil {
return nil, fmt.Errorf("Error marshalling the treatment config: %s", err.Error())
}
statusCode = http.StatusOK
return &runner.Treatment{
ExperimentName: filteredExperiment.Name,
Name: selectedTreatment.Name,
Config: rawConfig,
}, nil
}
func (er *experimentRunner) RegisterMetricsCollector(
collector metrics.Collector,
metricsRegistrationHelper runner.MetricsRegistrationHelper,
) error {
er.appContext.MetricService.SetMetricsCollector(collector)
err := metricsRegistrationHelper.Register([]routerMetrics.Metric{
{
Name: string(instrumentation.FetchTreatmentRequestDurationMs),
Type: routerMetrics.HistogramMetricType,
Description: instrumentation.FetchTreatmentRequestDurationMsHelpString,
Buckets: instrumentation.RequestLatencyBuckets,
Labels: instrumentation.FetchTreatmentRequestDurationMsLabels,
},
{
Name: string(instrumentation.ExperimentLookupDurationMs),
Type: routerMetrics.HistogramMetricType,
Description: instrumentation.ExperimentLookupDurationMsHelpString,
Buckets: instrumentation.RequestLatencyBuckets,
Labels: instrumentation.ExperimentLookupDurationMsLabels,
},
{
Name: string(instrumentation.FetchTreatmentRequestCount),
Type: routerMetrics.CounterMetricType,
Description: instrumentation.FetchTreatmentRequestCountHelpString,
Labels: append(
er.appContext.MetricService.GetMetricLabels(),
instrumentation.AdditionalFetchTreatmentRequestCountLabels...,
),
},
{
Name: string(instrumentation.NoMatchingExperimentRequestCount),
Type: routerMetrics.CounterMetricType,
Description: instrumentation.NoMatchingExperimentRequestCountHelpString,
Labels: append(
er.appContext.MetricService.GetMetricLabels(),
instrumentation.AdditionalNoMatchingExperimentRequestCountLabels...,
),
},
})
if err != nil {
return err
}
return nil
}
func (er *experimentRunner) startBackgroundServices(
errChannel chan error,
) {
backgroundSvcCtx := context.Background()
if er.appContext.MessageQueueService != nil {
go func() {
err := er.appContext.MessageQueueService.SubscribeToManagementService(backgroundSvcCtx)
if err != nil {
errChannel <- err
}
}()
}
if er.appContext.PollerService != nil {
er.appContext.PollerService.Start()
}
}
func (er *experimentRunner) getRequestParams(
logger log.Logger,
reqHeader http.Header,
body []byte,
) map[string]interface{} {
// Get the request parameters for the current request
requestParams := map[string]interface{}{}
for _, param := range er.parameters {
if param.FieldSource == "none" || param.Field == "" {
// Parameter not configured
continue
}
val, err := request.GetValueFromHTTPRequest(reqHeader, body, request.FieldSource(param.FieldSource), param.Field)
if err != nil {
logger.Errorf(err.Error())
} else {
requestParams[param.Name] = val
}
}
return requestParams
}
// NewExperimentRunner creates an instance of ExperimentRunner with the provided JSON config.
func NewExperimentRunner(jsonCfg json.RawMessage) (runner.ExperimentRunner, error) {
// Ensure valid schema for the JSON config.
var config config.ExperimentRunnerConfig
err := json.Unmarshal(jsonCfg, &config)
if err != nil {
return nil, errors.Wrapf(err, "Could not parse the XP runner configuration")
}
// Validate that all required configs exist.
err = config.Validate()
if err != nil {
return nil, err
}
// Init AppContext
appCtx, err := appcontext.NewAppContext(config.TreatmentServiceConfig)
if err != nil {
log.Panicf("Failed initializing application appcontext: %v", err)
}
// Retrieve project ID
if len(config.TreatmentServiceConfig.ProjectIds) != 1 {
return nil, fmt.Errorf("One and only one project id must be specified")
}
projectId, err := strconv.ParseInt(config.TreatmentServiceConfig.ProjectIds[0], 10, 64)
if err != nil {
return nil, errors.Wrapf(err, "Error parsing project id string into int64")
}
// Return new XP Runner
r := &experimentRunner{
projectID: projectId,
parameters: config.RequestParameters,
appContext: appCtx,
}
// TODO: To find a way to handle errors from the errChannel in the future
errChannel := make(chan error, 1)
r.startBackgroundServices(errChannel)
return r, nil
}