Skip to content

Commit

Permalink
Add logging to plugin fetch treatment method (#61)
Browse files Browse the repository at this point in the history
* Add logging to plugin fetch treatment method

* Regroup dependencies in go mod file
  • Loading branch information
deadlycoconuts committed Feb 2, 2023
1 parent 2182325 commit 8e222b6
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
2 changes: 1 addition & 1 deletion plugins/turing/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
github.com/caraml-dev/xp/treatment-service v0.0.0
github.com/go-playground/validator/v10 v10.11.1
github.com/gojek/mlp v1.5.3
github.com/google/uuid v1.3.0
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.12.2 // indirect
github.com/stretchr/testify v1.8.0
Expand Down Expand Up @@ -61,7 +62,6 @@ require (
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.2.0 // indirect
github.com/googleapis/gax-go/v2 v2.6.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.11.2 // indirect
Expand Down
59 changes: 58 additions & 1 deletion plugins/turing/runner/experiment_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ import (
"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/gojek/mlp/api/pkg/instrumentation/metrics"
"github.com/google/uuid"
"github.com/pkg/errors"
"golang.org/x/net/context"

Expand Down Expand Up @@ -64,7 +66,9 @@ func (er *experimentRunner) GetTreatmentForRequest(
var treatment schema.SelectedTreatment
var switchbackWindowId *int64
var err error

statusCode := http.StatusBadRequest
filterParams := api.FetchTreatmentRequestBody{AdditionalProperties: requestParams}

defer func() {
if requestFilter == nil {
Expand All @@ -78,12 +82,65 @@ func (er *experimentRunner) GetTreatmentForRequest(
projectId,
statusCode,
err,
api.FetchTreatmentRequestBody{AdditionalProperties: requestParams},
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 {
Expand Down

0 comments on commit 8e222b6

Please sign in to comment.