From 8548e4d89a1ae74f3911f174fa8dbfa6e1208cf7 Mon Sep 17 00:00:00 2001 From: Pierre Tessier Date: Tue, 9 Mar 2021 11:24:47 -0500 Subject: [PATCH 1/2] yaml config: add int as a type for rules compare --- sample/rules.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/sample/rules.go b/sample/rules.go index 01fa8b4172..90000f03f7 100644 --- a/sample/rules.go +++ b/sample/rules.go @@ -146,6 +146,11 @@ const ( ) func compare(a, b interface{}) (int, bool) { + // a is the tracing data field value. This can be: float64, int64, bool, or string + // b is the Rule condition value. This can be: float64, int64, int, bool, or string + // Note: in YAML config parsing, the Value may be returned as int + // When comparing numeric values, we need to check across the 3 types: float64, int64, and int + if a == nil { if b == nil { return equal, true @@ -161,6 +166,16 @@ func compare(a, b interface{}) (int, bool) { switch at := a.(type) { case int64: switch bt := b.(type) { + case int: + i := int(at) + switch { + case i < bt: + return less, true + case i > bt: + return more, true + default: + return equal, true + } case int64: switch { case at < bt: @@ -183,6 +198,16 @@ func compare(a, b interface{}) (int, bool) { } case float64: switch bt := b.(type) { + case int: + f := float64(bt) + switch { + case at < f: + return less, true + case at > f: + return more, true + default: + return equal, true + } case int64: f := float64(bt) switch { From 3a236ec65dd02afdff9cbc193fd2a934e0508e0a Mon Sep 17 00:00:00 2001 From: Pierre Tessier Date: Tue, 9 Mar 2021 20:42:51 -0500 Subject: [PATCH 2/2] test case for int compare --- sample/rules_test.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/sample/rules_test.go b/sample/rules_test.go index 4531f2a69a..7add1ae669 100644 --- a/sample/rules_test.go +++ b/sample/rules_test.go @@ -439,6 +439,34 @@ func TestRules(t *testing.T) { ExpectedKeep: true, ExpectedRate: 4, }, + { + Rules: &config.RulesBasedSamplerConfig{ + Rule: []*config.RulesBasedSamplerRule{ + { + Name: "YAMLintgeaterthan", + SampleRate: 10, + Condition: []*config.RulesBasedSamplerCondition{ + { + Field: "test", + Operator: ">", + Value: int(1), + }, + }, + }, + }, + }, + Spans: []*types.Span{ + { + Event: types.Event{ + Data: map[string]interface{}{ + "test": int64(2), + }, + }, + }, + }, + ExpectedKeep: true, + ExpectedRate: 10, + }, } for _, d := range data {