rspec-otel 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: fa13ed2e866b0e8f13946b28298356ba677c05e0c1939e3dbd1c887dc5c1d5fc
4
+ data.tar.gz: 388d62d4c072e238dcb85a4afce75b02bd444a79b78d14dc4e4a891e4dff3a16
5
+ SHA512:
6
+ metadata.gz: 85046ba3269a35fc48aee92cb1b3e3170fbd3a195a4441febc5739da34e2e73dfbf4931f292c33fcbdd0472d7bf21d28389251c1cca0eb95a57c91055b298996
7
+ data.tar.gz: 3119c4a902e011a8b801315b0bd9db1064eac49bc86b951cac2ce6916fd84229f475f5517a3feb7b20071ff84bf0b19b31a6aa9028c250515c6023e31cc3da26
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Damien MATHIEU
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,50 @@
1
+ # RSpec Otel
2
+
3
+ RSpec matchers to be used with the OpenTelemetry framework
4
+
5
+ ## Installation
6
+
7
+ Add this line to your Gemfile:
8
+
9
+ ```ruby
10
+ gem 'rspec-otel'
11
+ ```
12
+
13
+ Within your spec helper, require the gem:
14
+
15
+ ```ruby
16
+ require 'rspec_otel'
17
+ ```
18
+
19
+ And include the matchers within the rspec configuration:
20
+
21
+ ```ruby
22
+ RSpec.configure do |config|
23
+ config.include RspecOtel::Matchers
24
+ end
25
+ ```
26
+
27
+ ## Usage
28
+
29
+ ### Matching the presence of a span
30
+
31
+ You can match the emission of a span with the `have_emitted_span` matcher:
32
+
33
+ ```ruby
34
+ require 'spec_helper'
35
+
36
+ RSpec.describe 'User API' do
37
+ it 'emits a span' do
38
+ expect do
39
+ get :user, id: 1
40
+ end.to have_emitted_span('GET /user').with_attributes({'user.id' => '1'})
41
+ end
42
+ end
43
+ ```
44
+
45
+ Several conditions can be added to the matcher:
46
+
47
+ * `with_attributes` - Will match only the spans with the specified attributes.
48
+ * `with_event` - Will match only the spans with the specified event. This condition can be called multiple times with different events.
49
+ * `with_status` - Will match only the spans that have the proper status.
50
+ * `with_exception` - Will match only the spans that have the specified exception event.
@@ -0,0 +1,96 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RspecOtel
4
+ module Matchers
5
+ class HaveEmittedSpan
6
+ attr_reader :name
7
+
8
+ def initialize(name)
9
+ @name = name
10
+ @attributes = {}
11
+ end
12
+
13
+ def matches?(block)
14
+ block.call if block.respond_to?(:call)
15
+
16
+ RspecOtel.exporter.finished_spans.each do |span|
17
+ return true if span.name == name &&
18
+ attributes_match?(span.attributes, @attributes) &&
19
+ status_match?(span.status, @status) &&
20
+ events_match?(span.events, @events)
21
+ end
22
+
23
+ false
24
+ end
25
+
26
+ def with_attributes(attributes)
27
+ @attributes = attributes
28
+ self
29
+ end
30
+
31
+ def with_event(name, attributes = {})
32
+ @events ||= []
33
+ @events << OpenTelemetry::SDK::Trace::Event.new(name, attributes)
34
+ self
35
+ end
36
+
37
+ def with_status(code, description)
38
+ @status = { code:, description: }
39
+ self
40
+ end
41
+
42
+ def with_exception(exception)
43
+ with_event('exception', {
44
+ 'exception.type' => exception.class.to_s,
45
+ 'exception.message' => exception.message
46
+ })
47
+ end
48
+
49
+ def failure_message
50
+ "expected span #{name} to have been emitted, but it couldn't be found"
51
+ end
52
+
53
+ def failure_message_when_negated
54
+ "expected span #{name} to not have been emitted"
55
+ end
56
+
57
+ def supports_block_expectations?
58
+ true
59
+ end
60
+
61
+ private
62
+
63
+ def attributes_match?(span_attributes, attributes)
64
+ attributes.each do |ak, av|
65
+ sa = span_attributes.select do |k, v|
66
+ ak == k && av == v
67
+ end
68
+
69
+ return false if sa.empty?
70
+ end
71
+
72
+ true
73
+ end
74
+
75
+ def status_match?(span_status, status)
76
+ status.nil? ||
77
+ (status[:code] == span_status.code && status[:description] == span_status.description)
78
+ end
79
+
80
+ def events_match?(span_events, events)
81
+ return true if span_events.nil?
82
+
83
+ events.each do |e|
84
+ se = span_events.select do |s|
85
+ s.name == e.name &&
86
+ attributes_match?(s.attributes, e.attributes)
87
+ end
88
+
89
+ return false if se.empty?
90
+ end
91
+
92
+ true
93
+ end
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RspecOtel
4
+ module Matchers
5
+ def have_emitted_span(name) # rubocop:disable Naming/PredicateName
6
+ HaveEmittedSpan.new(name)
7
+ end
8
+ end
9
+ end
10
+
11
+ require 'rspec_otel/matchers/have_emitted_span'
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rspec/core'
4
+ require 'rspec/expectations'
5
+
6
+ RSpec.configure do |config|
7
+ config.before(:suite) do
8
+ span_processor = OpenTelemetry::SDK::Trace::Export::SimpleSpanProcessor.new(RspecOtel.exporter)
9
+
10
+ OpenTelemetry::SDK.configure do |c|
11
+ c.add_span_processor span_processor
12
+ end
13
+ end
14
+
15
+ config.around(:each) do |example|
16
+ example.run
17
+ RspecOtel.exporter.reset
18
+ end
19
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RspecOtel
4
+ VERSION = '0.0.1'
5
+ end
data/lib/rspec_otel.rb ADDED
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'opentelemetry/sdk'
4
+
5
+ module RspecOtel
6
+ def self.exporter
7
+ @exporter ||= OpenTelemetry::SDK::Trace::Export::InMemorySpanExporter.new
8
+ end
9
+ end
10
+
11
+ require 'rspec_otel/matchers'
12
+ require 'rspec_otel/rspec'
13
+ require 'rspec_otel/version'
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rspec-otel
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Damien MATHIEU
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-02-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: opentelemetry-api
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: opentelemetry-sdk
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: RSpec matchers for the OpenTelemetry framework
56
+ email:
57
+ - 42@dmathieu.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - LICENSE
63
+ - README.md
64
+ - lib/rspec_otel.rb
65
+ - lib/rspec_otel/matchers.rb
66
+ - lib/rspec_otel/matchers/have_emitted_span.rb
67
+ - lib/rspec_otel/rspec.rb
68
+ - lib/rspec_otel/version.rb
69
+ homepage: https://github.com/dmathieu/rspec-otel
70
+ licenses:
71
+ - MIT
72
+ metadata:
73
+ rubygems_mfa_required: 'true'
74
+ post_install_message:
75
+ rdoc_options: []
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ requirements: []
89
+ rubygems_version: 3.5.3
90
+ signing_key:
91
+ specification_version: 4
92
+ summary: RSpec matchers for the OpenTelemetry framework
93
+ test_files: []