Awesome
BloodContracts::Instrumentation
Refinement types are implemented in BloodContracts::Core, but in production first of all we have to understand which types are used and how frequently. In other words we need instrumentation for types.
Let's say, we want to log to STDOUT every match of your Rubygems API contract:
BloodContracts::Instrumentation.configure do |cfg|
# Attach to every BC::Refined ancestor with Rubygems in the name
cfg.instrument /Rubygems/, lambda { |session|
# see Session class API at lib/blood_contracts/instrumentation/session.rb
puts "SID:#{session.id} "\
"Duration: #{session.finished_at - session.started_at} "\
"Result: #{session.result_type_name}"
}
end
Welcome, Instrumentation for BloodContracts.
Installation
Add this line to your application's Gemfile:
gem 'blood_contracts-instrumentation'
And then execute:
$ bundle
Or install it yourself as:
$ gem install blood_contracts-instrumentation
Usage
Most of the time you will use only BloodContracts::Instrumentation.configure call. It gives you access to instrumentation config and adds different instruments to types by name. First argument is a String or Regex which will used to find relevant Refined type to attach. The second argument is the actual "instrument".
The simplest instrument is just a lambda with 1 argument session, but in advanced case, you could implement "instrument" as a class.
For example, we use Yabeda for instrumentation. So you could introduce Yabeda instrument for that:
# config/initializers/contracts.rb
module Contracts
class YabedaInstrument
def call(session)
valid_marker = session.valid? ? "V" : "I"
result = "[#{valid_marker}] #{session.result_type_name}"
Yabeda.api_contract_matches.increment(result: result)
end
end
end
BloodContracts::Instrumentation.configure do |cfg|
# Attach to every BC::Refined ancestor with Rubygems in the name
cfg.instrument /Rubygems.*Contract/, Contracts::YabedaInstrument.new
# Attach to every BC::Refined ancestor with Github in the name
cfg.instrument /Github.*Contract/, Contracts::YabedaInstrument.new
end
For more details see Instrument class
Finally, you may want to verify, which instruments are already attached to the type:
[1] pry(main)> RubygemsAPI::Contract.instruments
=> [#<Contracts::YabedaInstrument:0x00007fe89ad322c0>, #<Contracts::FailuresInstrument:0x00007fe89ad39e30>]
That's pretty much it!
Uh, oh! Almost forgot, you could choose the strategy for instrumentation finalizer. Imagine that you want to write some debug data into DB. In some cases that will affect the performance of your type matching. To minimize that effect you could try to use Fibers or Threads as simple as:
# config/initializers/contracts.rb
BloodContracts::Instrumentation.configure do |cfg|
# Attach to every BC::Refined ancestor with Rubygems in the name
cfg.instrument /Rubygems.*Contract/, Contracts::YabedaInstrument.new
# Attach to every BC::Refined ancestor with Github in the name
cfg.instrument /Github.*Contract/, Contracts::YabedaInstrument.new
cfg.finalizer = :fibers # or :threads, or :basic
end
See more info about Finalizers here: Basic, Fibers, Threads
Enjoy!
Development
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.
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.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/sclinede/blood_contracts-instrumentation. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
License
The gem is available as open source under the terms of the MIT License.
Code of Conduct
Everyone interacting in the BloodContracts::Instrumentation project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.