rspec-instrumentation 0.1.0

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: ace1f9c18c62b0764ca54620189d5fc24ce40a54c1286e2e4175d21126702597
4
+ data.tar.gz: 8b1d125baaa519fe94e34b7e6c51fb9918ad70e27eef0024b40a37f0ce1cf770
5
+ SHA512:
6
+ metadata.gz: 9824916ea0800a44a3289d8d4b18b51e725579db5e00b85f8749c8766c513ef5f524497d35cb053174fd4ed14f59c5ca165176ec23e05c623be68bb19a77a920
7
+ data.tar.gz: e0355ddbead096ffaa7c893a19d2811134f2fab0434f9a1143aa880632d3b7ae3bca826587043822396ead61d712790c760ccc7cdb7a8c8c1aea7d3acf7f6368
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2022 Mattia Roccoberton
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,56 @@
1
+ # RSpec instrumentation
2
+
3
+ RSpec instrumentation component to measure performances, profile and monitor the test suite using an external service.
4
+
5
+ > DISCLAIMER: this is an initial version, major changes will probably happens in the first releases
6
+
7
+ Support for:
8
+ - New Relic
9
+ - Sentry APM (partial for now)
10
+
11
+ Setup the gem, execute the test suite, check the results in the service interface.
12
+
13
+ ## Install
14
+
15
+ - Add to the Gemfile (in the test group): `gem 'rspec-instrumentation'`
16
+ - Follow the specific instructions below per service
17
+
18
+ ### New Relic
19
+
20
+ - Configure New Relic on your application (also adding the gem 'newrelic_rpm' to the Gemfile)
21
+ - Add an initializer in your app (_config/initializers/new_relic.rb_) with:
22
+
23
+ ```rb
24
+ RSpecInstrumentation::NewRelic.setup if Rails.env.test?
25
+ ```
26
+
27
+ - Add to your _rails_helper.rb_ (or _spec_helper.rb_):
28
+
29
+ ```rb
30
+ # It's better to put in at the end, after the other configuration options
31
+ RSpec.configure do |config|
32
+ RSpecInstrumentation::RSpec.setup(config, service: :new_relic)
33
+ end
34
+ ```
35
+
36
+ - If you are using VCR, depending on your configuration you could need also to permit external requests to the New Relic service, example:
37
+
38
+ ```rb
39
+ VCR.configure do |config|
40
+ config.ignore_hosts 'collector-001.eu01.nr-data.net' # the host could be different for your account
41
+ end
42
+ ```
43
+
44
+ ## Do you like it? Star it!
45
+
46
+ If you use this component just star it. A developer is more motivated to improve a project when there is some interest.
47
+
48
+ Or consider offering me a coffee, it's a small thing but it is greatly appreciated: [about me](https://www.blocknot.es/about-me).
49
+
50
+ ## Contributors
51
+
52
+ - [Mattia Roccoberton](https://www.blocknot.es): author
53
+
54
+ ## License
55
+
56
+ The gem is available as open-source under the terms of the [MIT](LICENSE.txt).
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'rspec_instrumentation/rspec'
4
+
5
+ require_relative 'rspec_instrumentation/new_relic'
6
+ require_relative 'rspec_instrumentation/sentry'
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RSpecInstrumentation
4
+ class NewRelic
5
+ RSPEC_EVENTS = %i[example_finished example_started start stop].freeze
6
+
7
+ def initialize(config)
8
+ config.reporter.register_listener(self, *RSPEC_EVENTS)
9
+ end
10
+
11
+ def example_finished(notification)
12
+ result = notification.example.execution_result
13
+ if result.status == :failed
14
+ details = failed_example_details(notification.example)
15
+ ::NewRelic::Agent.notice_error(result.exception.to_s, custom_params: details)
16
+ end
17
+ @transaction.finish
18
+ end
19
+
20
+ def example_started(notification)
21
+ name = notification.example.id.delete_prefix('./')
22
+ @transaction = ::NewRelic::Agent::Tracer.start_transaction_or_segment(partial_name: name, category: :task)
23
+ end
24
+
25
+ def failed_example_details(example)
26
+ {
27
+ location: example.metadata[:location],
28
+ type: example.metadata[:type]
29
+ }
30
+ end
31
+
32
+ def start(_notification)
33
+ # puts '> start'
34
+ end
35
+
36
+ def stop(_notification)
37
+ ::NewRelic::Agent.shutdown
38
+ end
39
+
40
+ class << self
41
+ def setup
42
+ require 'newrelic_rpm'
43
+ require 'new_relic/agent/tracer'
44
+
45
+ ::NewRelic::Agent.manual_start
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RSpecInstrumentation
4
+ module RSpec
5
+ module_function
6
+
7
+ def setup(config, service:)
8
+ case service
9
+ when :new_relic then NewRelic.new(config)
10
+ when :sentry then Sentry.new(config)
11
+ else warn("Unknown service: #{service}")
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RSpecInstrumentation
4
+ class Sentry
5
+ RSPEC_EVENTS = %i[example_finished example_started start stop].freeze
6
+
7
+ def initialize(config)
8
+ config.reporter.register_listener(self, *RSPEC_EVENTS)
9
+ end
10
+
11
+ def example_finished(notification)
12
+ example = notification.example
13
+ case example.execution_result.status
14
+ when :passed
15
+ @transaction.set_status('ok')
16
+ when :failed
17
+ @transaction.set_data('failure', example.execution_result.exception.to_s)
18
+ @transaction.set_status('internal_error')
19
+ end
20
+ @transaction.finish
21
+ end
22
+
23
+ def example_started(notification)
24
+ example = notification.example
25
+ location = example.metadata[:location].delete_prefix('./')
26
+ @transaction = ::Sentry.start_transaction(name: example.full_description, op: 'test-run', description: location)
27
+ @transaction.set_tag('type', example.metadata[:type])
28
+ @transaction.set_data('location', example.metadata[:location])
29
+ end
30
+
31
+ def start(_notification)
32
+ @start_timestamp = Time.now.to_f
33
+ end
34
+
35
+ def stop(notification)
36
+ info = "#{notification.examples.count} examples, #{notification.failed_examples.count} failures"
37
+ attrs = { name: 'Suite run', op: 'suite-run', start_timestamp: @start_timestamp, description: info }
38
+ transaction = ::Sentry.start_transaction(attrs)
39
+ transaction.set_data('examples_count', notification.examples.count)
40
+ transaction.set_data('failed_examples_count', notification.failed_examples.count)
41
+ transaction.finish
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RSpecInstrumentation
4
+ VERSION = '0.1.0'
5
+ end
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rspec-instrumentation
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Mattia Roccoberton
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-05-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.11'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.11'
27
+ description: A RSpec instrumentation component
28
+ email: mat@blocknot.es
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - LICENSE.txt
34
+ - README.md
35
+ - lib/rspec-instrumentation.rb
36
+ - lib/rspec_instrumentation/new_relic.rb
37
+ - lib/rspec_instrumentation/rspec.rb
38
+ - lib/rspec_instrumentation/sentry.rb
39
+ - lib/rspec_instrumentation/version.rb
40
+ homepage: https://github.com/blocknotes/rspec-instrumentation
41
+ licenses:
42
+ - MIT
43
+ metadata:
44
+ homepage_uri: https://github.com/blocknotes/rspec-instrumentation
45
+ source_code_uri: https://github.com/blocknotes/rspec-instrumentation
46
+ rubygems_mfa_required: 'true'
47
+ post_install_message:
48
+ rdoc_options: []
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: 2.6.0
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ requirements: []
62
+ rubygems_version: 3.1.6
63
+ signing_key:
64
+ specification_version: 4
65
+ summary: RSpec instrumentation
66
+ test_files: []