chaos-rb 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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 38c098285816a10db04fd4ac50388fc2c5a4ab22857820610fa346e8e9335466
4
+ data.tar.gz: 4a9064721287b1e9f7331054ad7a32fc46d7774634c11aa8c85efc0bc2bdb52d
5
+ SHA512:
6
+ metadata.gz: 467b1c95a6a449a120628ae47638655b6ec383b4a6c0d57b10c1cace568a23a945ba2acfbb93c7251c4a0f6ff04a7ddffdd6c1f489b4772c35811ee6da8ea726
7
+ data.tar.gz: 8d35480f7963a13afb05e02e66a62172728ae7b203f6ff4b45bd0e2becbe125409208456b4e4416951128f01e3b8f42b77d3cae1ccbc305ffd16cf1caeac5132
@@ -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
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.7.2
6
+ before_install: gem install bundler -v 2.1.4
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in chaos-rb.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "rspec", "~> 3.0"
@@ -0,0 +1,34 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ chaos-rb (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.4.4)
10
+ rake (12.3.3)
11
+ rspec (3.9.0)
12
+ rspec-core (~> 3.9.0)
13
+ rspec-expectations (~> 3.9.0)
14
+ rspec-mocks (~> 3.9.0)
15
+ rspec-core (3.9.3)
16
+ rspec-support (~> 3.9.3)
17
+ rspec-expectations (3.9.2)
18
+ diff-lcs (>= 1.2.0, < 2.0)
19
+ rspec-support (~> 3.9.0)
20
+ rspec-mocks (3.9.1)
21
+ diff-lcs (>= 1.2.0, < 2.0)
22
+ rspec-support (~> 3.9.0)
23
+ rspec-support (3.9.3)
24
+
25
+ PLATFORMS
26
+ ruby
27
+
28
+ DEPENDENCIES
29
+ chaos-rb!
30
+ rake (~> 12.0)
31
+ rspec (~> 3.0)
32
+
33
+ BUNDLED WITH
34
+ 2.1.4
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Karol Galanciak
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.
@@ -0,0 +1,98 @@
1
+ # Chaos::Rb
2
+
3
+ Bring [chaos engineering](https://en.wikipedia.org/wiki/Chaos_engineering) to your Ruby apps - destroy production for fun and profit :).
4
+
5
+ If you want to introduce some instability to your application in a controlled - way and see what's going to happen (verifying alerts, chekcing what metrics are going to be collected etc.), this is a gem for you.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'chaos-rb'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install chaos-rb
22
+
23
+ ## Usage
24
+
25
+ Chaos RB supports the following types of instabilities:
26
+
27
+ - CPU Usage - simulate 100% of the CPU core load for a specified amount of time
28
+ - Exception - simulate raising exceptions
29
+ - IO Wait - simulate some blocking I/O operation for a specified amount of time
30
+ - Memory Usage - simulate memory usage of a given amount in megabytes for a specified amount of time
31
+
32
+ The way to use them is to use `Chaos::Injector` that modified the specific class/method. You also need to provide probability of the instability being executed (a number between 0 and 1 - 0 means never, 1 means always, 0.7 means 70% of the change that the instability will be executed etc.).
33
+
34
+ You can also provide a lambda or an object responding to `call` method taking one argument which is going to be an object that gets the chaos injection if the logic when given instability should be executed is more complex - that way you can take advantage of the state of the object to decide, e.g. if you only want some users to experience problems.
35
+
36
+ Here are examples how to use `Chaos::Injector`:
37
+
38
+
39
+ ``` rb
40
+ injector = Chaos::Injector.build(logger: Rails.logger)
41
+
42
+ injector.inject do |builder|
43
+ builder.target = ExampleService # required
44
+ builder.method_name = :call # required
45
+ builder.instability_type = :cpu_usage # required
46
+ builder.instability_arguments = { duration_in_seconds: 30.0 } # required
47
+ builder.probability = 0.9 # required
48
+ end
49
+
50
+ injector.inject do |builder|
51
+ builder.target = ExampleService
52
+ builder.method_name = :call
53
+ builder.instability_type = :io_wait
54
+ builder.instability_arguments = { duration_in_seconds: 30.0 }
55
+ builder.probability = 0.5
56
+ builder.execute_if = ->(example_service_instance) { example_service_instance.current_user.admin? } # optional
57
+ end
58
+
59
+ injector.inject do |builder|
60
+ builder.target = ExampleService
61
+ builder.method_name = :call
62
+ builder.instability_type = :memory_usage
63
+ builder.instability_arguments = { duration_in_seconds: 30.0, memory_limit_in_megabytes: 1000 }
64
+ builder.probability = 1
65
+ end
66
+
67
+ injector.inject do |builder|
68
+ builder.target = ExampleService
69
+ builder.method_name = :call
70
+ builder.instability_type = :exception
71
+ builder.instability_arguments = { exceptions: [Faraday::ClientError.new("HTTP connection error"), ActiveRecord::RecordInvalid] }
72
+ builder.probability = 0.1
73
+ end
74
+ ```
75
+
76
+ If `Chaos::Injector` looks like too much magic to you, you can use instability classes directly:
77
+
78
+ ``` rb
79
+ instability_type = :io_wait
80
+ instability_arguments = { duration_in_seconds: 30.0 }
81
+
82
+ Chaos::InstabilityFactory.new.build(instability_type).call(instability_arguments)
83
+ ```
84
+
85
+ ## Development
86
+
87
+ 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.
88
+
89
+ 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).
90
+
91
+ ## Contributing
92
+
93
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/chaos-rb.
94
+
95
+
96
+ ## License
97
+
98
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "chaos/rb"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -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,29 @@
1
+ require_relative "lib/chaos/rb/version"
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "chaos-rb"
5
+ spec.version = Chaos::Rb::VERSION
6
+ spec.authors = ["Karol Galanciak"]
7
+ spec.email = ["karol.galanciak@gmail.com"]
8
+
9
+ spec.summary = %q{Chaos RB: Bringing chaos to your Ruby applications}
10
+ spec.description = %q{Chaos RB: Bringing chaos to your Ruby applications}
11
+ spec.homepage = "https://github.com/BookingSync/chaos-rb"
12
+ spec.license = "MIT"
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
14
+
15
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ # spec.metadata["source_code_uri"] = "TODO: Put your gem's public repo URL here."
19
+ # spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
24
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ end
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+ end
@@ -0,0 +1,13 @@
1
+ require "chaos/rb/version"
2
+ require "chaos/instability"
3
+ require "chaos/instability/cpu_usage"
4
+ require "chaos/instability/exception"
5
+ require "chaos/instability/i_o_wait"
6
+ require "chaos/instability/memory_usage"
7
+ require "chaos/injection"
8
+ require "chaos/injector"
9
+ require "chaos/instability_factory"
10
+ require "chaos/modifier_factory"
11
+
12
+ module Chaos
13
+ end
@@ -0,0 +1,37 @@
1
+ class Chaos::Injection
2
+ ATTRIBUTES = %i(target method_name instability_type instability_arguments probability execute_if).freeze
3
+
4
+ attr_accessor *ATTRIBUTES
5
+
6
+ attr_reader :instability_factory
7
+ private :instability_factory
8
+
9
+ def initialize(instability_factory: Chaos::InstabilityFactory.new)
10
+ @instability_factory = instability_factory
11
+ end
12
+
13
+ def validate!
14
+ ATTRIBUTES.each do |attribute|
15
+ present?(public_send(attribute)) or raise ":#{attribute} is not set!"
16
+ end
17
+ end
18
+
19
+ def instability
20
+ instability_factory.build(instability_type)
21
+ end
22
+
23
+ def execute_if
24
+ @execute_if || ->(_arg) { true }
25
+ end
26
+
27
+ def execute_if=(val)
28
+ raise "is not a lambda-like object" if !val.respond_to?(:call)
29
+ @execute_if = val
30
+ end
31
+
32
+ private
33
+
34
+ def present?(value)
35
+ !value.nil?
36
+ end
37
+ end
@@ -0,0 +1,28 @@
1
+ class Chaos::Injector
2
+ attr_reader :modifier_factory, :injections
3
+ private :modifier_factory
4
+
5
+ def self.build(logger:, random_numbers_generator: Kernel)
6
+ new(Chaos::ModifierFactory.new(logger: logger, random_numbers_generator: random_numbers_generator))
7
+ end
8
+
9
+ def initialize(modifier_factory)
10
+ @modifier_factory = modifier_factory
11
+ @injections = []
12
+ end
13
+
14
+ def inject
15
+ injection = Chaos::Injection.new
16
+ yield injection
17
+ injection.validate!
18
+ injections << injection
19
+
20
+ injection.target.prepend(build_module(injection))
21
+ end
22
+
23
+ private
24
+
25
+ def build_module(injection)
26
+ modifier_factory.build_module(injection)
27
+ end
28
+ end
@@ -0,0 +1,4 @@
1
+ module Chaos
2
+ module Instability
3
+ end
4
+ end
@@ -0,0 +1,22 @@
1
+ class Chaos::Instability::CpuUsage
2
+ attr_reader :clock
3
+ private :clock
4
+
5
+ def initialize(clock: Time)
6
+ @clock = clock
7
+ end
8
+
9
+ def call(duration_in_seconds:)
10
+ expected_execution_end_time = clock.now + duration_in_seconds
11
+
12
+ generate_100_percent_load_on_a_single_cpu_limit(expected_execution_end_time)
13
+ end
14
+
15
+ private
16
+
17
+ def generate_100_percent_load_on_a_single_cpu_limit(expected_execution_end_time)
18
+ while clock.now < expected_execution_end_time
19
+ true
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,5 @@
1
+ class Chaos::Instability::Exception
2
+ def call(exceptions:)
3
+ raise exceptions.sample
4
+ end
5
+ end
@@ -0,0 +1,12 @@
1
+ class Chaos::Instability::IOWait
2
+ attr_reader :sleep_provider
3
+ private :sleep_provider
4
+
5
+ def initialize(sleep_provider: Kernel)
6
+ @sleep_provider = sleep_provider
7
+ end
8
+
9
+ def call(duration_in_seconds:)
10
+ sleep_provider.sleep(duration_in_seconds)
11
+ end
12
+ end
@@ -0,0 +1,35 @@
1
+ class Chaos::Instability::MemoryUsage
2
+ attr_reader :sleep_provider, :accum, :chunk_size_in_bytes, :single_byte_character, :clock
3
+ private :sleep_provider, :accum, :chunk_size_in_bytes, :single_byte_character, :clock
4
+
5
+ MEGABYTES_IN_BYTES = 1048576
6
+ SINGLE_BYE_CHARACTER = "a".freeze
7
+ private_constant :MEGABYTES_IN_BYTES, :SINGLE_BYE_CHARACTER
8
+
9
+ def initialize(sleep_provider: Kernel, accum: [], chunk_size_in_bytes: MEGABYTES_IN_BYTES,
10
+ single_byte_character: SINGLE_BYE_CHARACTER, clock: Time)
11
+ @sleep_provider = sleep_provider
12
+ @accum = accum
13
+ @chunk_size_in_bytes = chunk_size_in_bytes
14
+ @single_byte_character = single_byte_character
15
+ @clock = clock
16
+ end
17
+
18
+ def call(duration_in_seconds:, memory_limit_in_megabytes:)
19
+ start_time = clock.now
20
+ memory_limit_in_megabytes.times.with_object(accum) do |_index, memory_leaking_accum|
21
+ memory_leaking_accum << generate_chunk
22
+ end
23
+ current_time = clock.now
24
+ time_already_spent = current_time - start_time
25
+ time_left = [duration_in_seconds - time_already_spent , 0].max
26
+
27
+ sleep_provider.sleep(time_left)
28
+ end
29
+
30
+ private
31
+
32
+ def generate_chunk
33
+ single_byte_character * chunk_size_in_bytes
34
+ end
35
+ end
@@ -0,0 +1,12 @@
1
+ class Chaos::InstabilityFactory
2
+ REGISTRY = {
3
+ cpu_usage: Chaos::Instability::CpuUsage.new,
4
+ io_wait: Chaos::Instability::IOWait.new,
5
+ memory_usage: Chaos::Instability::MemoryUsage.new,
6
+ exception: Chaos::Instability::Exception.new
7
+ }
8
+
9
+ def build(instability)
10
+ REGISTRY.fetch(instability.to_sym)
11
+ end
12
+ end
@@ -0,0 +1,27 @@
1
+ class Chaos::ModifierFactory
2
+ def initialize(random_numbers_generator: Kernel, logger:)
3
+ @random_numbers_generator = random_numbers_generator
4
+ @logger = logger
5
+ end
6
+
7
+ def build_module(injection)
8
+ instability_type = injection.instability_type
9
+ instability = injection.instability
10
+ target = injection.target
11
+ method_name = injection.method_name
12
+ probability = injection.probability
13
+ execute_if = injection.execute_if
14
+ random_numbers_generator = @random_numbers_generator
15
+ logger = @logger
16
+
17
+ Module.new do
18
+ define_method method_name do |*args, &block|
19
+ if probability >= random_numbers_generator.rand && execute_if.call(self)
20
+ instability.call(injection.instability_arguments)
21
+ logger.info "[Chaos] Triggered :#{instability_type} for :#{method_name} on #{target} with probability: #{probability}"
22
+ end
23
+ super(*args, &block)
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1 @@
1
+ require_relative "../chaos.rb"
@@ -0,0 +1,5 @@
1
+ module Chaos
2
+ module Rb
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: chaos-rb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Karol Galanciak
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-10-09 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: 'Chaos RB: Bringing chaos to your Ruby applications'
14
+ email:
15
+ - karol.galanciak@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".gitignore"
21
+ - ".rspec"
22
+ - ".travis.yml"
23
+ - Gemfile
24
+ - Gemfile.lock
25
+ - LICENSE.txt
26
+ - README.md
27
+ - Rakefile
28
+ - bin/console
29
+ - bin/setup
30
+ - chaos-rb.gemspec
31
+ - lib/chaos.rb
32
+ - lib/chaos/injection.rb
33
+ - lib/chaos/injector.rb
34
+ - lib/chaos/instability.rb
35
+ - lib/chaos/instability/cpu_usage.rb
36
+ - lib/chaos/instability/exception.rb
37
+ - lib/chaos/instability/i_o_wait.rb
38
+ - lib/chaos/instability/memory_usage.rb
39
+ - lib/chaos/instability_factory.rb
40
+ - lib/chaos/modifier_factory.rb
41
+ - lib/chaos/rb.rb
42
+ - lib/chaos/rb/version.rb
43
+ homepage: https://github.com/BookingSync/chaos-rb
44
+ licenses:
45
+ - MIT
46
+ metadata:
47
+ homepage_uri: https://github.com/BookingSync/chaos-rb
48
+ post_install_message:
49
+ rdoc_options: []
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: 2.3.0
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ requirements: []
63
+ rubygems_version: 3.1.2
64
+ signing_key:
65
+ specification_version: 4
66
+ summary: 'Chaos RB: Bringing chaos to your Ruby applications'
67
+ test_files: []