Skip to content

Commit aa3bcf8

Browse files
authored
[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
1 parent 0767148 commit aa3bcf8

File tree

4 files changed

+17
-4
lines changed

4 files changed

+17
-4
lines changed

analyticsservice/analytics.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const (
2525
variationValueAttribute string = "featureValue"
2626
targetAttribute string = "target"
2727
sdkVersionAttribute string = "SDK_VERSION"
28-
SdkVersion string = "0.1.16"
28+
SdkVersion string = "0.1.17"
2929
sdkTypeAttribute string = "SDK_TYPE"
3030
sdkType string = "server"
3131
sdkLanguageAttribute string = "SDK_LANGUAGE"

evaluation/util.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package evaluation
22

33
import (
44
"fmt"
5+
"github.com/harness/ff-golang-server-sdk/sdk_codes"
56
"reflect"
67
"strconv"
78
"strings"
@@ -33,6 +34,8 @@ func getAttrValue(target *Target, attr string) reflect.Value {
3334
value = reflect.ValueOf(target.Identifier)
3435
case "name":
3536
value = reflect.ValueOf(target.Name)
37+
default:
38+
value = reflect.ValueOf("")
3639
}
3740
}
3841
return value
@@ -70,6 +73,7 @@ func findVariation(variations []rest.Variation, identifier string) (rest.Variati
7073

7174
func getNormalizedNumber(identifier, bucketBy string) int {
7275
value := []byte(strings.Join([]string{bucketBy, identifier}, ":"))
76+
log.Debugf("MM3 input [%s]", string(value))
7377
hasher := murmur3.New32()
7478
_, err := hasher.Write(value)
7579
if err != nil {
@@ -83,10 +87,18 @@ func isEnabled(target *Target, bucketBy string, percentage int) bool {
8387
value := getAttrValue(target, bucketBy)
8488
identifier := value.String()
8589
if identifier == "" {
86-
return false
90+
var oldBB = bucketBy
91+
bucketBy = "identifier"
92+
value = getAttrValue(target, bucketBy)
93+
identifier = value.String()
94+
if identifier == "" {
95+
return false
96+
}
97+
log.Warnf("%s BucketBy attribute not found in target attributes, falling back to 'identifier': missing=%s, using value=%s", sdk_codes.MissingBucketBy, oldBB, identifier)
8798
}
8899

89100
bucketID := getNormalizedNumber(identifier, bucketBy)
101+
log.Debugf("MM3 percentage_check=%d bucket_by=%s value=%s bucket=%d", percentage, bucketBy, identifier, bucketID)
90102
return percentage > 0 && bucketID <= percentage
91103
}
92104

evaluation/util_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func Test_getAttrValueIsNil(t *testing.T) {
2626
want: reflect.Value{},
2727
},
2828
{
29-
name: "wrong attribute should return Value{}",
29+
name: "wrong attribute should return ValueOf('')",
3030
args: args{
3131
target: &Target{
3232
Identifier: harness,
@@ -36,7 +36,7 @@ func Test_getAttrValueIsNil(t *testing.T) {
3636
},
3737
attr: "no_identifier",
3838
},
39-
want: reflect.Value{},
39+
want: reflect.ValueOf(""),
4040
},
4141
}
4242
for _, tt := range tests {

sdk_codes/sdk_codes.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const (
2424
StreamStop SDKCode = "SDKCODE:5004"
2525
EvaluationSuccess SDKCode = "SDKCODE:6000"
2626
EvaluationFailed SDKCode = "SDKCODE:6001"
27+
MissingBucketBy SDKCode = "SDKCODE:6002"
2728
MetricsStarted SDKCode = "SDKCODE:7000"
2829
MetricsStopped SDKCode = "SDKCODE:7001"
2930
MetricsSendFail SDKCode = "SDKCODE:7002"

0 commit comments

Comments
 (0)