From aa3bcf8f5f6f3c25f12523a21ecf369148decef5 Mon Sep 17 00:00:00 2001 From: Andrew Bell <115623869+andybharness@users.noreply.github.com> Date: Mon, 20 Nov 2023 13:47:49 +0000 Subject: [PATCH] [FFM-10027] - Fall back to identifier if bucketBy attribute is not found (#136) * [FFM-10027] - Fall back to identifier if bucketBy attribute is not found What Update BucketBy logic to fall back to the target identifier if given BucketBy attribute is not found. Why Keep the SDK consistent with the behaviour of the other server SDKs. Testing Testgrid + manual --- analyticsservice/analytics.go | 2 +- evaluation/util.go | 14 +++++++++++++- evaluation/util_test.go | 4 ++-- sdk_codes/sdk_codes.go | 1 + 4 files changed, 17 insertions(+), 4 deletions(-) diff --git a/analyticsservice/analytics.go b/analyticsservice/analytics.go index 3dcee844..51ee2d57 100644 --- a/analyticsservice/analytics.go +++ b/analyticsservice/analytics.go @@ -25,7 +25,7 @@ const ( variationValueAttribute string = "featureValue" targetAttribute string = "target" sdkVersionAttribute string = "SDK_VERSION" - SdkVersion string = "0.1.16" + SdkVersion string = "0.1.17" sdkTypeAttribute string = "SDK_TYPE" sdkType string = "server" sdkLanguageAttribute string = "SDK_LANGUAGE" diff --git a/evaluation/util.go b/evaluation/util.go index 565c23da..1625f57f 100644 --- a/evaluation/util.go +++ b/evaluation/util.go @@ -2,6 +2,7 @@ package evaluation import ( "fmt" + "github.com/harness/ff-golang-server-sdk/sdk_codes" "reflect" "strconv" "strings" @@ -33,6 +34,8 @@ func getAttrValue(target *Target, attr string) reflect.Value { value = reflect.ValueOf(target.Identifier) case "name": value = reflect.ValueOf(target.Name) + default: + value = reflect.ValueOf("") } } return value @@ -70,6 +73,7 @@ func findVariation(variations []rest.Variation, identifier string) (rest.Variati func getNormalizedNumber(identifier, bucketBy string) int { value := []byte(strings.Join([]string{bucketBy, identifier}, ":")) + log.Debugf("MM3 input [%s]", string(value)) hasher := murmur3.New32() _, err := hasher.Write(value) if err != nil { @@ -83,10 +87,18 @@ func isEnabled(target *Target, bucketBy string, percentage int) bool { value := getAttrValue(target, bucketBy) identifier := value.String() if identifier == "" { - return false + var oldBB = bucketBy + bucketBy = "identifier" + value = getAttrValue(target, bucketBy) + identifier = value.String() + if identifier == "" { + return false + } + log.Warnf("%s BucketBy attribute not found in target attributes, falling back to 'identifier': missing=%s, using value=%s", sdk_codes.MissingBucketBy, oldBB, identifier) } bucketID := getNormalizedNumber(identifier, bucketBy) + log.Debugf("MM3 percentage_check=%d bucket_by=%s value=%s bucket=%d", percentage, bucketBy, identifier, bucketID) return percentage > 0 && bucketID <= percentage } diff --git a/evaluation/util_test.go b/evaluation/util_test.go index 602ed1ff..4b6920a8 100644 --- a/evaluation/util_test.go +++ b/evaluation/util_test.go @@ -26,7 +26,7 @@ func Test_getAttrValueIsNil(t *testing.T) { want: reflect.Value{}, }, { - name: "wrong attribute should return Value{}", + name: "wrong attribute should return ValueOf('')", args: args{ target: &Target{ Identifier: harness, @@ -36,7 +36,7 @@ func Test_getAttrValueIsNil(t *testing.T) { }, attr: "no_identifier", }, - want: reflect.Value{}, + want: reflect.ValueOf(""), }, } for _, tt := range tests { diff --git a/sdk_codes/sdk_codes.go b/sdk_codes/sdk_codes.go index 4958374b..744744b4 100644 --- a/sdk_codes/sdk_codes.go +++ b/sdk_codes/sdk_codes.go @@ -24,6 +24,7 @@ const ( StreamStop SDKCode = "SDKCODE:5004" EvaluationSuccess SDKCode = "SDKCODE:6000" EvaluationFailed SDKCode = "SDKCODE:6001" + MissingBucketBy SDKCode = "SDKCODE:6002" MetricsStarted SDKCode = "SDKCODE:7000" MetricsStopped SDKCode = "SDKCODE:7001" MetricsSendFail SDKCode = "SDKCODE:7002"