Skip to content

Commit

Permalink
[FFM-10027] - Fall back to identifier if bucketBy attribute is not fo…
Browse files Browse the repository at this point in the history
…und (#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
  • Loading branch information
andybharness authored Nov 20, 2023
1 parent 0767148 commit aa3bcf8
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 4 deletions.
2 changes: 1 addition & 1 deletion analyticsservice/analytics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
14 changes: 13 additions & 1 deletion evaluation/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package evaluation

import (
"fmt"
"github.com/harness/ff-golang-server-sdk/sdk_codes"
"reflect"
"strconv"
"strings"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}

Expand Down
4 changes: 2 additions & 2 deletions evaluation/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -36,7 +36,7 @@ func Test_getAttrValueIsNil(t *testing.T) {
},
attr: "no_identifier",
},
want: reflect.Value{},
want: reflect.ValueOf(""),
},
}
for _, tt := range tests {
Expand Down
1 change: 1 addition & 0 deletions sdk_codes/sdk_codes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit aa3bcf8

Please sign in to comment.