From 6bb2765ad3dfac76290320baed44dc8843d9fadf Mon Sep 17 00:00:00 2001 From: "americodls@gmail.com" Date: Thu, 2 Oct 2025 12:06:25 +0100 Subject: [PATCH] fix: capture correct descriptions for RSpec one-liner examples RSpec one-liner examples (e.g., `it { is_expected.not_to be_nil }`) generate descriptions after execution. Updated formatter to capture the final description in `example_finished` instead of relying on the incomplete description at `example_started`. --- .../instrumentation/rspec/formatter.rb | 6 +++++- .../instrumentation/rspec/formatter_test.rb | 20 +++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/instrumentation/rspec/lib/opentelemetry/instrumentation/rspec/formatter.rb b/instrumentation/rspec/lib/opentelemetry/instrumentation/rspec/formatter.rb index 11a37b8f82..86ec5abd84 100644 --- a/instrumentation/rspec/lib/opentelemetry/instrumentation/rspec/formatter.rb +++ b/instrumentation/rspec/lib/opentelemetry/instrumentation/rspec/formatter.rb @@ -68,8 +68,12 @@ def example_started(notification) def example_finished(notification) pop_and_finalize_span do |span| - result = notification.example.execution_result + example = notification.example + result = example.execution_result + # Update name and full_description for one-liner examples where description is generated after execution + span.name = example.description + span.set_attribute('rspec.example.full_description', example.full_description.to_s) span.set_attribute('rspec.example.result', result.status.to_s) add_exception_and_failures(span, result.exception) diff --git a/instrumentation/rspec/test/opentelemetry/instrumentation/rspec/formatter_test.rb b/instrumentation/rspec/test/opentelemetry/instrumentation/rspec/formatter_test.rb index d54e6a96d1..bb549a6f22 100644 --- a/instrumentation/rspec/test/opentelemetry/instrumentation/rspec/formatter_test.rb +++ b/instrumentation/rspec/test/opentelemetry/instrumentation/rspec/formatter_test.rb @@ -326,6 +326,26 @@ def run_example(...) _(subject.events[1].attributes['exception.message']).must_equal 'another-error' end end + + describe 'one-liner syntax' do + subject do + run_example do + it { is_expected.not_to be_nil } + end + end + + it 'has a name that matches the auto-generated description' do + _(subject.name).must_equal 'is expected not to be nil' + end + + it 'has a full_description attribute that includes the group description' do + _(subject.attributes['rspec.example.full_description']).must_equal 'group one is expected not to be nil' + end + + it 'records when the example passes' do + _(subject.attributes['rspec.example.result']).must_equal 'passed' + end + end end describe 'using a custom tracer provider' do