Skip to content

Commit c8d2f6a

Browse files
authored
fix: RSpec one-liner example descriptions (#2)
## Why? Span names and full descriptions for RSpec one-liner syntax (like `it { is_expected.not_to be_nil }`) were incorrectly showing file locations instead of the matcher-generated descriptions. This made tracing output less useful for tests using the concise one-liner syntax. ## How? RSpec generates descriptions for one-liner examples after execution, when the matcher runs. The formatter was capturing `description` and `full_description` at `example_started` time, before these values were populated. The fix updates both the span name and `full_description` attribute in the `example_finished` callback, after RSpec has generated the proper description from the matcher. This ensures accurate span names and descriptions for both one-liner and traditional examples. We need to point Intercom's Gemfile to this repo so that we get those updates without waiting for the upstream repo to accept them. --- **Note:** Upstream PR created at open-telemetry#1712
1 parent 536fd04 commit c8d2f6a

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

instrumentation/rspec/lib/opentelemetry/instrumentation/rspec/formatter.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,12 @@ def example_started(notification)
6868

6969
def example_finished(notification)
7070
pop_and_finalize_span do |span|
71-
result = notification.example.execution_result
71+
example = notification.example
72+
result = example.execution_result
7273

74+
# Update name and full_description for one-liner examples where description is generated after execution
75+
span.name = example.description
76+
span.set_attribute('rspec.example.full_description', example.full_description.to_s)
7377
span.set_attribute('rspec.example.result', result.status.to_s)
7478

7579
add_exception_and_failures(span, result.exception)

instrumentation/rspec/test/opentelemetry/instrumentation/rspec/formatter_test.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,26 @@ def run_example(...)
326326
_(subject.events[1].attributes['exception.message']).must_equal 'another-error'
327327
end
328328
end
329+
330+
describe 'one-liner syntax' do
331+
subject do
332+
run_example do
333+
it { is_expected.not_to be_nil }
334+
end
335+
end
336+
337+
it 'has a name that matches the auto-generated description' do
338+
_(subject.name).must_equal 'is expected not to be nil'
339+
end
340+
341+
it 'has a full_description attribute that includes the group description' do
342+
_(subject.attributes['rspec.example.full_description']).must_equal 'group one is expected not to be nil'
343+
end
344+
345+
it 'records when the example passes' do
346+
_(subject.attributes['rspec.example.result']).must_equal 'passed'
347+
end
348+
end
329349
end
330350

331351
describe 'using a custom tracer provider' do

0 commit comments

Comments
 (0)