Skip to content

Commit feec27d

Browse files
committed
Fix filter_ai_spans bypass for root spans
Remove the unconditional `return true if is_root` check in SpanProcessor#should_forward_span? so that filters (including filter_ai_spans) are applied to root spans as well. This aligns the Ruby SDK behavior with the Python and JS SDKs. Fixes #123
1 parent aa1e7f8 commit feec27d

2 files changed

Lines changed: 12 additions & 7 deletions

File tree

lib/braintrust/trace/span_processor.rb

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,6 @@ def get_parent_from_context(parent_context)
8080
# Determine if a span should be forwarded to the wrapped processor
8181
# based on configured filters
8282
def should_forward_span?(span)
83-
# Always keep root spans (spans with no parent)
84-
# Check if parent_span_id is the invalid/zero span ID
85-
is_root = span.parent_span_id == OpenTelemetry::Trace::INVALID_SPAN_ID
86-
return true if is_root
87-
8883
# If no filters, keep everything
8984
return true if @filters.empty?
9085

test/braintrust/trace/span_filter_test.rb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,23 @@ def test_init_with_filter_ai_spans_ignores_system_attributes
106106
assert_equal "gen_ai.root", spans[0].name
107107
end
108108

109-
def test_init_with_filter_ai_spans_always_keeps_root_spans
109+
def test_init_with_filter_ai_spans_filters_root_spans
110110
exporter, tracer = setup_with_filter(filter_ai_spans: true)
111111

112112
tracer.in_span("database.query") {}
113113

114114
spans = exporter.finished_spans
115-
assert_equal 1, spans.length, "Root spans should always be kept"
115+
assert_equal 0, spans.length, "Non-AI root spans should be filtered"
116+
end
117+
118+
def test_init_with_filter_ai_spans_keeps_ai_root_spans
119+
exporter, tracer = setup_with_filter(filter_ai_spans: true)
120+
121+
tracer.in_span("gen_ai.completion") {}
122+
123+
spans = exporter.finished_spans
124+
assert_equal 1, spans.length, "AI root spans should be kept"
125+
assert_equal "gen_ai.completion", spans[0].name
116126
end
117127

118128
def test_init_with_filter_ai_spans_drops_non_root_non_ai_spans

0 commit comments

Comments
 (0)