blood_contracts-instrumentation 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 4a1ff83954224dfbb24664768832c7db0e4b621364a92de707c3b1774fbc9e30
4
+ data.tar.gz: e271b417bdd7082067d91faf6826d91c0f13f5e2f3f01068ddba0a23af3edf8c
5
+ SHA512:
6
+ metadata.gz: d6c19029329fbf97adbea0fe440b0951c2ac6f9dd251ba4bf0c2e93d40421e9a823e9f5445c3483f4b78a4f3f23a5827bfa42406757d27f8d82264dbb660a66d
7
+ data.tar.gz: bdb1e16379fd159cff96592bff34fd0ed43182bf8da38720acc3a0bf4e759ee0fe78ba746f84a7fcdf7f5dea2f19463c32f68b11e1c5589c776b8abcb56a51f3
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,31 @@
1
+ ---
2
+
3
+ AllCops:
4
+ DisplayCopNames: true
5
+ DisplayStyleGuide: true
6
+ StyleGuideCopsOnly: true
7
+ TargetRubyVersion: 2.4
8
+ Exclude:
9
+ - examples/*
10
+ - blood_contracts-instrumentation.gemspec
11
+ - vendor/bundle/**/*
12
+
13
+ Metrics/LineLength:
14
+ AllowHeredoc: true
15
+ AllowURI: true
16
+ URISchemes:
17
+ - http
18
+ - https
19
+
20
+ Style/ClassAndModuleChildren:
21
+ Enabled: false
22
+
23
+ Style/Documentation:
24
+ Enabled: false
25
+
26
+ Style/StringLiterals:
27
+ EnforcedStyle: double_quotes
28
+
29
+ Naming/FileName:
30
+ Exclude:
31
+ - lib/blood_contracts-instrumentation.rb
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.2
7
+ before_install: gem install bundler -v 2.0.1
data/CHANGELOG.md ADDED
@@ -0,0 +1,12 @@
1
+ # Change Log
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](http://keepachangelog.com/)
6
+ and this project adheres to [Semantic Versioning](http://semver.org/).
7
+
8
+ ## [0.1.0] - [2019-07-05]
9
+
10
+ This is a first public release marked in change log with features extracted from production app.
11
+ Includes:
12
+ TBD
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at sclinede@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in blood_contracts-monitoring.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Sergey Dolganov
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
13
+ all 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
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,51 @@
1
+ [![Build Status](https://travis-ci.org/sclinede/blood_contracts-instrumentation.svg?branch=master)][travis]
2
+ [![Code Climate](https://codeclimate.com/github/sclinede/blood_contracts-instrumentation/badges/gpa.svg)][codeclimate]
3
+ [![Inch CI](https://inch-ci.org/github/sclinede/blood_contracts-instrumentation.svg?branch=master)][inch_ci]
4
+
5
+ [gem]: https://rubygems.org/gems/blood_contracts-instrumentation
6
+ [travis]: https://travis-ci.org/sclinede/blood_contracts-instrumentation
7
+ [codeclimate]: https://codeclimate.com/github/sclinede/blood_contracts-instrumentation
8
+ [inch_ci]: https://inch-ci.org/github/sclinede/blood_contracts-instrumentation
9
+
10
+
11
+ # BloodContracts::Instrumentation
12
+
13
+ ...WIP...
14
+
15
+ ## Installation
16
+
17
+ Add this line to your application's Gemfile:
18
+
19
+ ```ruby
20
+ gem 'blood_contracts-monitoring'
21
+ ```
22
+
23
+ And then execute:
24
+
25
+ $ bundle
26
+
27
+ Or install it yourself as:
28
+
29
+ $ gem install blood_contracts-monitoring
30
+
31
+ ## Usage
32
+
33
+ TODO: Write usage instructions here
34
+
35
+ ## Development
36
+
37
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
38
+
39
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
40
+
41
+ ## Contributing
42
+
43
+ Bug reports and pull requests are welcome on GitHub at https://github.com/sclinede/blood_contracts-monitoring. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
44
+
45
+ ## License
46
+
47
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
48
+
49
+ ## Code of Conduct
50
+
51
+ Everyone interacting in the BloodContracts::Monitoring project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/sclinede/blood_contracts-monitoring/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "blood_contracts/monitoring"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ Gem::Specification.new do |gem|
4
+ gem.name = 'blood_contracts-instrumentation'
5
+ gem.version = '0.1.0'
6
+ gem.authors = ['Sergey Dolganov (sclinede)']
7
+ gem.email = ['sclinede@evilmartians.com']
8
+
9
+ gem.summary = 'Adds instrumentation to BloodContracts refinement types'
10
+ gem.description = 'Adds instrumentation to BloodContracts refinement types'
11
+ gem.homepage = 'https://github.com/sclinede/blood_contracts-core'
12
+ gem.license = 'MIT'
13
+
14
+ gem.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
15
+ gem.test_files = gem.files.grep(/^spec/)
16
+ gem.extra_rdoc_files = Dir['CODE_OF_CONDUCT.md', 'README.md', 'LICENSE', 'CHANGELOG.md']
17
+
18
+ gem.required_ruby_version = '>= 2.4'
19
+
20
+ gem.add_runtime_dependency 'blood_contracts-core', '~> 0.4'
21
+
22
+ gem.add_development_dependency 'bundler', '~> 2.0'
23
+ gem.add_development_dependency 'pry'
24
+ gem.add_development_dependency 'rake', '~> 10.0'
25
+ gem.add_development_dependency 'rspec', '~> 3.0'
26
+ gem.add_development_dependency 'rubocop', '~> 0.49'
27
+ end
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "blood_contracts/instrumentation.rb"
@@ -0,0 +1,72 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "blood_contracts/core"
4
+ require "securerandom"
5
+
6
+ # Top-level scope for BloodContracts data validation and monitoring tools
7
+ module BloodContracts
8
+ # Top-level interface for BloodContracts insturmentation
9
+ module Instrumentation
10
+ module_function
11
+
12
+ # Configure the instrumentation by modification of the Config object
13
+ #
14
+ # @yieldparam [Config]
15
+ #
16
+ # @return [Config]
17
+ #
18
+ def configure
19
+ config.tap { |c| yield c }
20
+ end
21
+
22
+ # Register type in the config
23
+ #
24
+ # @param type [BC::Refined] which added to the registry
25
+ #
26
+ # @return [Nothing]
27
+ #
28
+ def register_type(type)
29
+ config.types << type
30
+ type.reset_instruments! unless type.anonymous?
31
+ end
32
+
33
+ # Select instruments for the type
34
+ #
35
+ # @param type_name [String] used to filter the instruments
36
+ #
37
+ # @return [Array<Instrument>]
38
+ #
39
+ def select_instruments(type_name)
40
+ config.send(:select_instruments, type_name)
41
+ end
42
+
43
+ # Resets current instance of Session finalizer
44
+ #
45
+ # @return [#finalize!]
46
+ #
47
+ def reset_session_finalizer!
48
+ config.send(:reset_session_finalizer!)
49
+ end
50
+
51
+ # Instrumentation config
52
+ #
53
+ # @return [Config]
54
+ #
55
+ def config
56
+ @config ||= Config.new
57
+ end
58
+
59
+ require_relative "./instrumentation/failed_match.rb"
60
+ require_relative "./instrumentation/session.rb"
61
+
62
+ require_relative "./instrumentation/instrument.rb"
63
+ require_relative "./instrumentation/session_finalizer.rb"
64
+ require_relative "./instrumentation/session_recording.rb"
65
+ BC::Refined.prepend(SessionRecording)
66
+
67
+ require_relative "./instrumentation/config.rb"
68
+ end
69
+ end
70
+
71
+ # Alias for top-level BloodContracts instrumentation
72
+ BCI = BloodContracts::Instrumentation
@@ -0,0 +1,126 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "set"
4
+
5
+ module BloodContracts
6
+ module Instrumentation
7
+ # Class that configures the instrumentation for refinement types matching
8
+ class Config
9
+ # Map of instrument classes where the key is the matching pattern
10
+ #
11
+ # @return [Hash<Regexp, Array<Instrument>>]
12
+ #
13
+ attr_reader :instruments
14
+
15
+ # List of refinement types defined in the app
16
+ #
17
+ # @return [Array<BC::Refined>]
18
+ #
19
+ attr_reader :types
20
+
21
+ # Pool size of finalizer instance
22
+ #
23
+ # @return [Integer]
24
+ #
25
+ attr_reader :finalizer_pool_size
26
+
27
+ # Type of finalizer instance
28
+ #
29
+ # @return [Symbol]
30
+ #
31
+ attr_reader :session_finalizer
32
+
33
+ # Initialize the config with default values
34
+ # Also inits the SessionFinalizer
35
+ def initialize
36
+ @instruments = {}
37
+ @finalizer_pool_size = SessionFinalizer::DEFAULT_POOL_SIZE
38
+ @session_finalizer = :basic
39
+
40
+ reset_types!
41
+ reset_session_finalizer!
42
+ end
43
+
44
+ # Set the pool size for SessionFinalizer (make sense only for
45
+ # SessionFinalizer::Fibers right now)
46
+ #
47
+ # @param value [Integer] size of finalizer fibers pool
48
+ # @return [Integer]
49
+ #
50
+ def finalizer_pool_size=(value)
51
+ @finalizer_pool_size = Integer(value)
52
+ ensure
53
+ reset_session_finalizer!
54
+ end
55
+
56
+ # Set the type of SessionFinalizer for instrumentation
57
+ # See SessionFinalizer::Basic, SessionFinalizer::Threads and
58
+ # SessionFinalizer::Fibers
59
+ #
60
+ # @param value [Symbol] name of SessionFinalizer, could be one of
61
+ # :basic, :threads, :fibers
62
+ # @return [Symbol]
63
+ #
64
+ def session_finalizer=(value)
65
+ @session_finalizer = value
66
+ ensure
67
+ reset_session_finalizer!
68
+ end
69
+
70
+ # Main setting in the config
71
+ # Define an instument for Refinement Types, applies only to types
72
+ # matched by the pattern by type class name
73
+ #
74
+ # @param pattern [String, Regexp] defines which types to apply this
75
+ # instrument
76
+ # @param processor [Proc, #call] defines the processor for insturmentation
77
+ # session, during the finalize phase the processor#call method would be
78
+ # called with Session instance as a parameter
79
+ #
80
+ # @option before [Proc, #call] defines a method that is called right
81
+ # after Session#start inside matching process of the refinement type
82
+ # @option after [Proc, #call] defines a method that is called right
83
+ # after Session#finish inside matching process of the refinement type
84
+ #
85
+ # @return [Nothing]
86
+ #
87
+ def instrument(pattern, processor, **kwargs)
88
+ pattern = /#{pattern}/i unless pattern.is_a?(Regexp)
89
+
90
+ old_value = @instruments[pattern].to_a
91
+ new_value = old_value << Instrument.build(processor, **kwargs)
92
+ @instruments[pattern] = new_value
93
+
94
+ reset_cache!(pattern)
95
+ end
96
+
97
+ # @protected
98
+ # Select only instruments matching the type_name
99
+ protected def select_instruments(type_name)
100
+ @instruments.flat_map do |pattern, instruments|
101
+ next unless type_name =~ /#{pattern}/i
102
+ instruments
103
+ end.compact
104
+ end
105
+
106
+ # @protected
107
+ # Reset instruments cache for refinement types that match filter
108
+ protected def reset_cache!(filter = nil)
109
+ filter = /#{filter}/i unless filter.is_a?(Regexp)
110
+
111
+ types.each { |type| type.reset_instruments! if type.name =~ filter }
112
+ end
113
+
114
+ # @protected
115
+ protected def reset_types!
116
+ @types = Set.new
117
+ end
118
+
119
+ # @protected
120
+ # Reset session finalizer instance using current config
121
+ protected def reset_session_finalizer!
122
+ SessionFinalizer.init(session_finalizer, pool_size: finalizer_pool_size)
123
+ end
124
+ end
125
+ end
126
+ end