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

feat: use a computed field for current descendant count in rules #950

Merged
merged 2 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
18 changes: 18 additions & 0 deletions config/sampler_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ const (
NotIn = "not-in"
)

// ComputedField is a virtual field. It's value is calculated during rule evaluation.
// We use the `?.` prefix to distinguish computed fields from regular fields.
type ComputedField string

const (
// ComputedFieldPrefix is the prefix for computed fields.
ComputedFieldPrefix = "?."
NUM_DESCENDANTS ComputedField = ComputedFieldPrefix + "NUM_DESCENDANTS"
)

// 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 @@ -250,6 +260,14 @@ func (r *RulesBasedSamplerCondition) String() string {
return fmt.Sprintf("%+v", *r)
}

func (r *RulesBasedSamplerCondition) GetComputedField() (ComputedField, bool) {
if strings.HasPrefix(r.Field, ComputedFieldPrefix) {
return ComputedField(r.Field), true
}
return "", false

}

func (r *RulesBasedSamplerCondition) setMatchesFunction() error {
switch r.Operator {
case Exists:
Expand Down
14 changes: 11 additions & 3 deletions sample/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func ruleMatchesTrace(t *types.Trace, rule *config.RulesBasedSamplerRule, checkN

span:
for _, span := range t.GetSpans() {
value, exists := extractValueFromSpan(span, condition, checkNestedFields)
value, exists := extractValueFromSpan(t, span, condition, checkNestedFields)
if condition.Matches == nil {
if conditionMatchesValue(condition, value, exists) {
matched++
Expand All @@ -194,7 +194,7 @@ func ruleMatchesSpanInTrace(trace *types.Trace, rule *config.RulesBasedSamplerRu
ruleMatched := true
for _, condition := range rule.Conditions {
// whether this condition is matched by this span.
value, exists := extractValueFromSpan(span, condition, checkNestedFields)
value, exists := extractValueFromSpan(trace, span, condition, checkNestedFields)
if condition.Matches == nil {
if !conditionMatchesValue(condition, value, exists) {
ruleMatched = false
Expand All @@ -220,7 +220,15 @@ func ruleMatchesSpanInTrace(trace *types.Trace, rule *config.RulesBasedSamplerRu
return false
}

func extractValueFromSpan(span *types.Span, condition *config.RulesBasedSamplerCondition, checkNestedFields bool) (interface{}, bool) {
func extractValueFromSpan(trace *types.Trace, span *types.Span, condition *config.RulesBasedSamplerCondition, checkNestedFields bool) (interface{}, bool) {
// If the condition is a descendant count, we extract the count from trace and return it.
if f, ok := condition.GetComputedField(); ok {
VinozzZ marked this conversation as resolved.
Show resolved Hide resolved
switch f {
case config.NUM_DESCENDANTS:
return int64(trace.DescendantCount()), true
}
}

// whether this condition is matched by this span.
var value any
var exists bool
Expand Down
120 changes: 120 additions & 0 deletions sample/rules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -772,6 +772,126 @@ func TestRules(t *testing.T) {
ExpectedName: "no rule matched",
ExpectedRate: 1,
},
{
Rules: &config.RulesBasedSamplerConfig{
Rules: []*config.RulesBasedSamplerRule{
{
Name: "Check that the number of descendants is greater than 3",
SampleRate: 1,
Conditions: []*config.RulesBasedSamplerCondition{
{
Field: string(config.NUM_DESCENDANTS),
Operator: config.GT,
Value: int(3),
Datatype: "int",
},
},
Drop: true,
},
},
},
Spans: []*types.Span{
{
Event: types.Event{
Data: map[string]interface{}{
"trace.trace_id": "12345",
"trace.span_id": "54322",
"trace.parent_id": "54321",
"meta.span_count": int64(2),
},
},
},
{
Event: types.Event{
Data: map[string]interface{}{
"trace.trace_id": "12345",
"trace.span_id": "654321",
"trace.parent_id": "54322",
},
},
},
{
Event: types.Event{
Data: map[string]interface{}{
"trace.trace_id": "12345",
"trace.span_id": "754321",
"trace.parent_id": "54322",
},
},
},
{
Event: types.Event{
Data: map[string]interface{}{
"trace.trace_id": "12345",
"trace.span_id": "754321",
"trace.parent_id": "54322",
},
},
},
},
ExpectedName: "Check that the number of descendants is greater than 3",
ExpectedKeep: false,
ExpectedRate: 1,
},
{
Rules: &config.RulesBasedSamplerConfig{
Rules: []*config.RulesBasedSamplerRule{
{
Name: "Check that the number of descendants is less than 3",
SampleRate: 1,
Conditions: []*config.RulesBasedSamplerCondition{
{
Field: string(config.NUM_DESCENDANTS),
Operator: config.LT,
Value: int(3),
},
},
},
},
},
Spans: []*types.Span{
{
Event: types.Event{
Data: map[string]interface{}{
"trace.trace_id": "12345",
"trace.span_id": "54322",
"trace.parent_id": "54321",
"meta.span_count": int64(2),
},
},
},
{
Event: types.Event{
Data: map[string]interface{}{
"trace.trace_id": "12345",
"trace.span_id": "654321",
"trace.parent_id": "54322",
},
},
},
{
Event: types.Event{
Data: map[string]interface{}{
"trace.trace_id": "12345",
"trace.span_id": "754321",
"trace.parent_id": "54322",
},
},
},
{
Event: types.Event{
Data: map[string]interface{}{
"trace.trace_id": "12345",
"trace.span_id": "754321",
"trace.parent_id": "54322",
},
},
},
},
ExpectedName: "no rule matched",
ExpectedKeep: true,
ExpectedRate: 1,
},
}

for _, d := range data {
Expand Down