rspec-otel 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '081125f70f4963d1a9f414811abbbe65e0113a14131a0cc982a2ce9247b447c5'
4
- data.tar.gz: ab28a32cc178212a05e0216ad84a6300eb4509c17537d4499df6d0b2dfa4fd30
3
+ metadata.gz: c2db5ebf1aa940bae5b290f6d6683e1f1bea754fc83e8eaf9997bb3971235244
4
+ data.tar.gz: 5c7b7ca60e93ca6ccf82479cfe3374c5420ab5be80484787c4cdc74ed67548cb
5
5
  SHA512:
6
- metadata.gz: d867cbc82ace4ea550c9c14024dc91db343964d749a13075f9a947f4193cdfbbba434270ba85d6de01ae7cdfc919a5ad388a13d7dc903f9dbd06ed1ea4bde0f3
7
- data.tar.gz: ff369cb3a9390130bdd354cc5ac99e7b0136525f8d39bb9c5fe793d2142843edc48a9556efe232b2b6397554561d1a17dfb2e39ae36fa6b2494a54af0ab23323
6
+ metadata.gz: f99a6e31d6acc0fc5dc26453690a7b66a6ed8352cc4790b26fccdad93dfa1692adaaa9f3f4fd5798c8d1e9084a8c509ebf1800936a3b4a1354ca3ddc6e9b701e
7
+ data.tar.gz: 12a5346b1eb3b3247d9d5d62e46aa1c4e3c13f891a0abdd72a2f2931e054f86bcc6d9634959faf65329689933af438075de259682b7357995bb596dfd8b5a008
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,9 +7,9 @@ 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
+
12
+ add_name_filter
13
13
  end
14
14
 
15
15
  def matches?(block)
@@ -110,11 +110,11 @@ 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
+ "expected span #{failure_match_description} #{printable_name} to have been emitted, but it couldn't be found"
114
114
  end
115
115
 
116
116
  def failure_message_when_negated
117
- "expected span #{name} to not have been emitted"
117
+ "expected span #{failure_match_description} #{printable_name} to not have been emitted"
118
118
  end
119
119
 
120
120
  def supports_block_expectations?
@@ -123,6 +123,35 @@ module RspecOtel
123
123
 
124
124
  private
125
125
 
126
+ def failure_match_description
127
+ case name
128
+ when String
129
+ 'named'
130
+ when Regexp
131
+ 'matching'
132
+ end
133
+ end
134
+
135
+ def printable_name
136
+ case name
137
+ when String
138
+ "'#{name}'"
139
+ when Regexp
140
+ name.inspect
141
+ end
142
+ end
143
+
144
+ def add_name_filter
145
+ @filters << lambda do |span|
146
+ case name
147
+ when String
148
+ span.name == name
149
+ when Regexp
150
+ span.name.match?(name)
151
+ end
152
+ end
153
+ end
154
+
126
155
  def exception_attributes(exception)
127
156
  attributes = {}
128
157
  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.5'
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,14 @@
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.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Damien MATHIEU
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-08-23 00:00:00.000000000 Z
11
+ date: 2024-09-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: opentelemetry-api
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '1.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: opentelemetry-test-helpers
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: rspec-core
43
57
  requirement: !ruby/object:Gem::Requirement