Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

maint: convert hardcoded operators to constants #813

Merged
merged 4 commits into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 53 additions & 26 deletions config/sampler_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,25 @@ import (
"strings"
)

// Define some constants for rule comparison operators
const (
NEQ = "!="
EQ = "="
GT = ">"
LT = "<"
GTE = ">="
LTE = "<="
)

// and also the rule keyword operators
const (
Contains = "contains"
DoesNotContain = "does-not-contain"
StartsWith = "starts-with"
Exists = "exists"
NotExists = "not-exists"
)

// The json tags in this file are used for conversion from the old format (see tools/convert for details).
// They are deliberately all lowercase.
// The yaml tags are used for the new format and are PascalCase.
Expand Down Expand Up @@ -213,19 +232,19 @@ func (r *RulesBasedSamplerCondition) String() string {

func (r *RulesBasedSamplerCondition) setMatchesFunction() error {
switch r.Operator {
case "exists":
case Exists:
r.Matches = func(value any, exists bool) bool {
return exists
}
return nil
case "not-exists":
case NotExists:
r.Matches = func(value any, exists bool) bool {
return !exists
}
return nil
case "!=", "=", ">", "<", "<=", ">=":
case NEQ, EQ, GT, LT, LTE, GTE:
return setCompareOperators(r, r.Operator)
case "starts-with", "contains", "does-not-contain":
case StartsWith, Contains, DoesNotContain:
err := setMatchStringBasedOperators(r, r.Operator)
if err != nil {
return err
Expand Down Expand Up @@ -309,39 +328,47 @@ func setCompareOperators(r *RulesBasedSamplerCondition, condition string) error

// check if conditionValue and spanValue are not equal
switch condition {
case "!=":
case NEQ:
r.Matches = func(spanValue any, exists bool) bool {
if n, ok := tryConvertToString(spanValue); exists && ok {
return n != conditionValue
}
return false
}
return nil
case "=":
case EQ:
r.Matches = func(spanValue any, exists bool) bool {
if n, ok := tryConvertToString(spanValue); exists && ok {
return n == conditionValue
}
return false
}
return nil
case ">":
case GT:
r.Matches = func(spanValue any, exists bool) bool {
if n, ok := tryConvertToString(spanValue); exists && ok {
return n > conditionValue
}
return false
}
return nil
case "<":
case GTE:
r.Matches = func(spanValue any, exists bool) bool {
if n, ok := tryConvertToString(spanValue); exists && ok {
return n >= conditionValue
}
return false
}
return nil
case LT:
r.Matches = func(spanValue any, exists bool) bool {
if n, ok := tryConvertToString(spanValue); exists && ok {
return n < conditionValue
}
return false
}
return nil
case "<=":
case LTE:
r.Matches = func(spanValue any, exists bool) bool {
if n, ok := tryConvertToString(spanValue); exists && ok {
return n <= conditionValue
Expand All @@ -357,47 +384,47 @@ func setCompareOperators(r *RulesBasedSamplerCondition, condition string) error
return fmt.Errorf("could not convert %v to string", r.Value)
}
switch condition {
case "!=":
case NEQ:
r.Matches = func(spanValue any, exists bool) bool {
if n, ok := tryConvertToInt(spanValue); exists && ok {
return n != conditionValue
}
return false
}
return nil
case "=":
case EQ:
r.Matches = func(spanValue any, exists bool) bool {
if n, ok := tryConvertToInt(spanValue); exists && ok {
return n == conditionValue
}
return false
}
return nil
case ">":
case GT:
r.Matches = func(spanValue any, exists bool) bool {
if n, ok := tryConvertToInt(spanValue); exists && ok {
return n > conditionValue
}
return false
}
return nil
case ">=":
case GTE:
r.Matches = func(spanValue any, exists bool) bool {
if n, ok := tryConvertToInt(spanValue); exists && ok {
return n >= conditionValue
}
return false
}
return nil
case "<":
case LT:
r.Matches = func(spanValue any, exists bool) bool {
if n, ok := tryConvertToInt(spanValue); exists && ok {
return n < conditionValue
}
return false
}
return nil
case "<=":
case LTE:
r.Matches = func(spanValue any, exists bool) bool {
if n, ok := tryConvertToInt(spanValue); exists && ok {
return n <= conditionValue
Expand All @@ -413,47 +440,47 @@ func setCompareOperators(r *RulesBasedSamplerCondition, condition string) error
}
// check if conditionValue and spanValue are not equal
switch condition {
case "!=":
case NEQ:
r.Matches = func(spanValue any, exists bool) bool {
if n, ok := tryConvertToFloat(spanValue); exists && ok {
return n != conditionValue
}
return false
}
return nil
case "=":
case EQ:
r.Matches = func(spanValue any, exists bool) bool {
if n, ok := tryConvertToFloat(spanValue); exists && ok {
return n == conditionValue
}
return false
}
return nil
case ">":
case GT:
r.Matches = func(spanValue any, exists bool) bool {
if n, ok := tryConvertToFloat(spanValue); exists && ok {
return n > conditionValue
}
return false
}
return nil
case ">=":
case GTE:
r.Matches = func(spanValue any, exists bool) bool {
if n, ok := tryConvertToFloat(spanValue); exists && ok {
return n >= conditionValue
}
return false
}
return nil
case "<":
case LT:
r.Matches = func(spanValue any, exists bool) bool {
if n, ok := tryConvertToFloat(spanValue); exists && ok {
return n < conditionValue
}
return false
}
return nil
case "<=":
case LTE:
r.Matches = func(spanValue any, exists bool) bool {
if n, ok := tryConvertToFloat(spanValue); exists && ok {
return n <= conditionValue
Expand All @@ -466,15 +493,15 @@ func setCompareOperators(r *RulesBasedSamplerCondition, condition string) error
conditionValue := tryConvertToBool(r.Value)

switch condition {
case "!=":
case NEQ:
r.Matches = func(spanValue any, exists bool) bool {
if n := tryConvertToBool(spanValue); exists && n {
return n != conditionValue
}
return false
}
return nil
case "=":
case EQ:
r.Matches = func(spanValue any, exists bool) bool {
if n := tryConvertToBool(spanValue); exists && n {
return n == conditionValue
Expand All @@ -498,23 +525,23 @@ func setMatchStringBasedOperators(r *RulesBasedSamplerCondition, condition strin
}

switch condition {
case "starts-with":
case StartsWith:
r.Matches = func(spanValue any, exists bool) bool {
s, ok := tryConvertToString(spanValue)
if ok {
return strings.HasPrefix(s, conditionValue)
}
return false
}
case "contains":
case Contains:
r.Matches = func(spanValue any, exists bool) bool {
s, ok := tryConvertToString(spanValue)
if ok {
return strings.Contains(s, conditionValue)
}
return false
}
case "does-not-contain":
case DoesNotContain:
r.Matches = func(spanValue any, exists bool) bool {
s, ok := tryConvertToString(spanValue)
if ok {
Expand Down
22 changes: 11 additions & 11 deletions sample/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,49 +226,49 @@ func conditionMatchesValue(condition *config.RulesBasedSamplerCondition, value i
switch exists {
case true:
switch condition.Operator {
case "exists":
case config.Exists:
match = exists
case "!=":
case config.NEQ:
if comparison, ok := compare(value, condition.Value); ok {
match = comparison != equal
}
case "=":
case config.EQ:
if comparison, ok := compare(value, condition.Value); ok {
match = comparison == equal
}
case ">":
case config.GT:
if comparison, ok := compare(value, condition.Value); ok {
match = comparison == more
}
case ">=":
case config.GTE:
if comparison, ok := compare(value, condition.Value); ok {
match = comparison == more || comparison == equal
}
case "<":
case config.LT:
if comparison, ok := compare(value, condition.Value); ok {
match = comparison == less
}
case "<=":
case config.LTE:
if comparison, ok := compare(value, condition.Value); ok {
match = comparison == less || comparison == equal
}
case "starts-with":
case config.StartsWith:
switch a := value.(type) {
case string:
switch b := condition.Value.(type) {
case string:
match = strings.HasPrefix(a, b)
}
}
case "contains":
case config.Contains:
switch a := value.(type) {
case string:
switch b := condition.Value.(type) {
case string:
match = strings.Contains(a, b)
}
}
case "does-not-contain":
case config.DoesNotContain:
switch a := value.(type) {
case string:
switch b := condition.Value.(type) {
Expand All @@ -279,7 +279,7 @@ func conditionMatchesValue(condition *config.RulesBasedSamplerCondition, value i
}
case false:
switch condition.Operator {
case "not-exists":
case config.NotExists:
match = !exists
}
}
Expand Down
Loading