rspec-otel 0.0.4 → 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '081125f70f4963d1a9f414811abbbe65e0113a14131a0cc982a2ce9247b447c5'
4
- data.tar.gz: ab28a32cc178212a05e0216ad84a6300eb4509c17537d4499df6d0b2dfa4fd30
3
+ metadata.gz: f5e1d1bb621033e9a0fb1d1195d745a2d4300f00723b83ca8cb380f170a660d5
4
+ data.tar.gz: 0d4812bb0b05657bcae39dae45e89f2db35cbb13c25bb47c8ff180e562e6c050
5
5
  SHA512:
6
- metadata.gz: d867cbc82ace4ea550c9c14024dc91db343964d749a13075f9a947f4193cdfbbba434270ba85d6de01ae7cdfc919a5ad388a13d7dc903f9dbd06ed1ea4bde0f3
7
- data.tar.gz: ff369cb3a9390130bdd354cc5ac99e7b0136525f8d39bb9c5fe793d2142843edc48a9556efe232b2b6397554561d1a17dfb2e39ae36fa6b2494a54af0ab23323
6
+ metadata.gz: 948d9807ebe6e5a96c6555c6e707f170928feae596cfc304550342ce894b94aa51db2c755c0aa77814ff8a3e7bd518d5004a328ec9e9f4f67c278d806a5fd265
7
+ data.tar.gz: 7780de8e139754b3255514c49ebbf742f09c73011737245d509cc9f04284a90f28d6edf9dac148d357e09e0839d22242ffae05c4df3ad5d2cc3c8f19a9d7613c
data/README.md CHANGED
@@ -37,7 +37,21 @@ RSpec.describe 'User API' do
37
37
  it 'emits a span' do
38
38
  expect do
39
39
  get :user, id: 1
40
- end.to emit_span('GET /user').with_attributes({'user.id' => '1'})
40
+ end.to emit_span('GET /user')
41
+ end
42
+ end
43
+ ```
44
+
45
+ `emit_span` will also match a regular expression:
46
+
47
+ ```ruby
48
+ require 'spec_helper'
49
+
50
+ RSpec.describe 'User API' do
51
+ it 'emits a span' do
52
+ expect do
53
+ get :user, id: 1
54
+ end.to emit_span(/^GET /)
41
55
  end
42
56
  end
43
57
  ```
@@ -56,7 +70,22 @@ Several conditions can be added to the matcher:
56
70
  * `with_exception` - Will match only the spans that have the specified exception event.
57
71
  * `without_exception` - Will match only the spans that do not have the specified exception event.
58
72
 
59
- The `*_event` condition can be called multiple times with different events.
73
+ _The `*_event` condition can be called multiple times with different events._
74
+
75
+
76
+ ### Disabling
77
+
78
+ We wrap every example in a new OpenTelemetry SDK configuration by default, if you wish to disable this you can tag your example with `:rspec_otel_disable_tracing`:
79
+
80
+ ```ruby
81
+ require 'spec_helper'
82
+
83
+ RSpec.describe 'User API', :rspec_otel_disable_tracing do
84
+ it 'tests my code' do
85
+ expect(true).to be true
86
+ end
87
+ end
88
+ ```
60
89
 
61
90
  ## Compatibility
62
91
 
@@ -7,19 +7,19 @@ module RspecOtel
7
7
 
8
8
  def initialize(name = nil)
9
9
  @name = name
10
- @filters = [
11
- ->(span) { span.name == name }
12
- ]
10
+ @filters = []
11
+ @before_spans = []
12
+
13
+ @filters << name_filter
13
14
  end
14
15
 
15
16
  def matches?(block)
16
- before_spans = []
17
17
  if block.respond_to?(:call)
18
- before_spans = RspecOtel.exporter.finished_spans
18
+ @before_spans = RspecOtel.exporter.finished_spans
19
19
  block.call
20
20
  end
21
21
 
22
- (RspecOtel.exporter.finished_spans - before_spans).each do |span|
22
+ (RspecOtel.exporter.finished_spans - @before_spans).each do |span|
23
23
  return true if @filters.all? { |f| f.call(span) }
24
24
  end
25
25
 
@@ -110,11 +110,21 @@ module RspecOtel
110
110
  end
111
111
 
112
112
  def failure_message
113
- "expected span #{name} to have been emitted, but it couldn't be found"
113
+ closest = closest_span
114
+ expect_content = "expected span #{failure_match_description} #{printable_name} to have been emitted"
115
+
116
+ case closest
117
+ when nil
118
+ "#{expect_content}, but there were no spans emitted at all"
119
+ when OpenTelemetry::SDK::Trace::SpanData
120
+ "#{expect_content}, but it couldn't be found. Found a close matching span named `#{closest.name}`"
121
+ else
122
+ raise "I don't know what to do with a #{closest.class} span"
123
+ end
114
124
  end
115
125
 
116
126
  def failure_message_when_negated
117
- "expected span #{name} to not have been emitted"
127
+ "expected span #{failure_match_description} #{printable_name} to not have been emitted"
118
128
  end
119
129
 
120
130
  def supports_block_expectations?
@@ -123,6 +133,44 @@ module RspecOtel
123
133
 
124
134
  private
125
135
 
136
+ def closest_span
137
+ all_spans = (RspecOtel.exporter.finished_spans - @before_spans)
138
+ span = all_spans.filter_map do |s|
139
+ span if name_filter.call(s)
140
+ end.first
141
+ span = all_spans.first if span.nil?
142
+ span
143
+ end
144
+
145
+ def failure_match_description
146
+ case name
147
+ when String
148
+ 'named'
149
+ when Regexp
150
+ 'matching'
151
+ end
152
+ end
153
+
154
+ def printable_name
155
+ case name
156
+ when String
157
+ "'#{name}'"
158
+ when Regexp
159
+ name.inspect
160
+ end
161
+ end
162
+
163
+ def name_filter
164
+ lambda do |span|
165
+ case name
166
+ when String
167
+ span.name == name
168
+ when Regexp
169
+ span.name.match?(name)
170
+ end
171
+ end
172
+ end
173
+
126
174
  def exception_attributes(exception)
127
175
  attributes = {}
128
176
  unless exception.nil?
@@ -3,16 +3,13 @@
3
3
  require 'rspec/core'
4
4
 
5
5
  RSpec.configure do |config|
6
- config.before(:suite) do
7
- span_processor = OpenTelemetry::SDK::Trace::Export::SimpleSpanProcessor.new(RspecOtel.exporter)
8
-
9
- OpenTelemetry::SDK.configure do |c|
10
- c.add_span_processor span_processor
11
- end
12
- end
13
-
14
6
  config.around(:each) do |example|
15
- example.run
16
- RspecOtel.exporter.reset
7
+ if example.metadata[:rspec_otel_disable_tracing]
8
+ example.run
9
+ else
10
+ RspecOtel.record do
11
+ example.run
12
+ end
13
+ end
17
14
  end
18
15
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RspecOtel
4
- VERSION = '0.0.4'
4
+ VERSION = '0.0.6'
5
5
  end
data/lib/rspec_otel.rb CHANGED
@@ -1,11 +1,29 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'opentelemetry/sdk'
4
+ require 'opentelemetry-test-helpers'
4
5
 
5
6
  module RspecOtel
6
7
  def self.exporter
7
8
  @exporter ||= OpenTelemetry::SDK::Trace::Export::InMemorySpanExporter.new
8
9
  end
10
+
11
+ def self.record
12
+ span_processor = OpenTelemetry::SDK::Trace::Export::SimpleSpanProcessor.new(exporter)
13
+
14
+ OpenTelemetry::SDK.configure do |c|
15
+ c.add_span_processor span_processor
16
+ end
17
+
18
+ yield
19
+ ensure
20
+ reset
21
+ end
22
+
23
+ def self.reset
24
+ OpenTelemetry::TestHelpers.reset_opentelemetry
25
+ @exporter = nil
26
+ end
9
27
  end
10
28
 
11
29
  require 'rspec_otel/matchers'
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rspec-otel
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Damien MATHIEU
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2024-08-23 00:00:00.000000000 Z
10
+ date: 2025-03-21 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: opentelemetry-api
@@ -38,6 +37,20 @@ dependencies:
38
37
  - - "~>"
39
38
  - !ruby/object:Gem::Version
40
39
  version: '1.0'
40
+ - !ruby/object:Gem::Dependency
41
+ name: opentelemetry-test-helpers
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ type: :runtime
48
+ prerelease: false
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
41
54
  - !ruby/object:Gem::Dependency
42
55
  name: rspec-core
43
56
  requirement: !ruby/object:Gem::Requirement
@@ -71,7 +84,6 @@ licenses:
71
84
  - MIT
72
85
  metadata:
73
86
  rubygems_mfa_required: 'true'
74
- post_install_message:
75
87
  rdoc_options: []
76
88
  require_paths:
77
89
  - lib
@@ -86,8 +98,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
86
98
  - !ruby/object:Gem::Version
87
99
  version: '0'
88
100
  requirements: []
89
- rubygems_version: 3.5.6
90
- signing_key:
101
+ rubygems_version: 3.6.2
91
102
  specification_version: 4
92
103
  summary: RSpec matchers for the OpenTelemetry framework
93
104
  test_files: []