Skip to content

Commit f39affe

Browse files
feat: add example.id attribute to RSpec instrumentation (#1713)
feat: add example.id attribute to RSpec spans Record RSpec's example.id as a span attribute to provide unique identification for each test example. This is particularly valuable when examples are created dynamically (e.g., within loops), where multiple examples share the same file location but have distinct IDs. Co-authored-by: Ariel Valentin <[email protected]>
1 parent 1186594 commit f39affe

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ def example_group_finished(notification)
5858
def example_started(notification)
5959
example = notification.example
6060
attributes = {
61+
'rspec.example.id' => example.id.to_s,
6162
'rspec.example.location' => example.location.to_s,
6263
'rspec.example.full_description' => example.full_description.to_s,
6364
'rspec.example.described_class' => example.metadata[:described_class].to_s

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,10 @@ def run_example(...)
131131
_(subject.attributes['rspec.example.location']).must_match %r{\./test/opentelemetry/instrumentation/rspec/formatter_test.rb:\d+}
132132
end
133133

134+
it 'has an id attribute' do
135+
_(subject.attributes['rspec.example.id']).must_match %r{\./test/opentelemetry/instrumentation/rspec/formatter_test.rb\[\d+:\d+\]}
136+
end
137+
134138
it 'records when the example passes' do
135139
_(subject.attributes['rspec.example.result']).must_equal 'passed'
136140
end
@@ -326,6 +330,32 @@ def run_example(...)
326330
_(subject.events[1].attributes['exception.message']).must_equal 'another-error'
327331
end
328332
end
333+
334+
describe 'dynamic examples with same location' do
335+
it 'have unique example.id attributes' do
336+
spans = run_rspec_with_tracing do
337+
RSpec.describe('dynamic examples') do
338+
[1, 2, 3].each do |num|
339+
example("example #{num}") { expect(num).to be_positive }
340+
end
341+
end
342+
end
343+
344+
example_spans = spans.select { |span| span.name.start_with?('example ') }
345+
_(example_spans.size).must_equal 3
346+
347+
# All examples have the same location (same line in the loop)
348+
locations = example_spans.map { |span| span.attributes['rspec.example.location'] }
349+
_(locations.uniq.size).must_equal 1
350+
351+
# But each has a unique id
352+
ids = example_spans.map { |span| span.attributes['rspec.example.id'] }
353+
_(ids.uniq.size).must_equal 3
354+
ids.each do |id|
355+
_(id).must_match %r{\./test/opentelemetry/instrumentation/rspec/formatter_test.rb\[\d+:\d+\]}
356+
end
357+
end
358+
end
329359
end
330360

331361
describe 'using a custom tracer provider' do

0 commit comments

Comments
 (0)