Mutineer (mutineer v0.2.0)
Copy MarkdownMutineer macro system for introducing controlled failures into functions.
This module provides macros to wrap functions and make them fail randomly, inspired by Netflix's Chaos Monkey. Useful for testing resilience and error handling in development and staging environments.
Configuration
Configure in config/config.exs or environment-specific config files:
# Enable Mutineer globally
config :mutineer,
Mutineer,
enabled: true,
default_failure_rate: 0.1, # 10% failure rate
default_failure_types: :errorUsage
Use the @chaos attribute before function definitions:
defmodule MyModule do
use Mutineer
# Will fail 10% of the time with default settings
@chaos true
def my_function(arg) do
# normal implementation
end
# Custom failure rate (30%)
@chaos failure_rate: 0.3
def risky_function(arg) do
# normal implementation
end
# Custom failure type and message
@chaos failure_rate: 0.2, failure_type: :raise, message: "Chaos!"
def another_function(arg) do
# normal implementation
end
endOr use the defchaos macro directly:
defmodule MyModule do
use Mutineer
defchaos my_function(arg), failure_rate: 0.2 do
# normal implementation
end
endGlobal configuration options
enabled- Enables or disables chaos globally (default:false)default_failure_types- Sets the default failure types for all functions, can be a list of failure types or a single failure type (default::error)default_failure_rate- Sets the default failure rate for all functions (default:0.1)
Failure types
:error- Returns{:error, :mutineer_chaos}(default):raise- Raises either aMutineer.ChaosErrorexception (default) or a custom error specified in theraised_errorsoption:delay- Introduces a random delay (1-5 seconds) before executing function:timeout- Same as:raise, but with a random delay before raising the exception:nil- Returnsnil:exit- Callsexit(:mutineer_chaos); the atom can be specified in theexit_errorsoption
Failure options
Options can be passed to @chaos, defchaos, or defchaosp:
failure_rateis the probability of failure for a given function (0.0-1.0), where1.0or above will always failfailure_types(orfailure_type) is either a list of failure types to trigger (e.g.[:error, :delay]) or a single failure type (e.g.:error)errors(orerror) is either a list of objects to be randomly selected from or a single object to return when the:errortype is triggeredraised_errors(orraised_error) is a list of errors to be randomly selected from or a single error to be raised when the:raisetype is triggeredexit_errors(orexit_error) is a list of errors to be randomly selected from or a single error to be raised when the:exittype is triggereddelayis the upperbound of the delay in milliseconds or a range of milliseconds for:timeoutand:delaytypes
Important Notes
- Mutineer is disabled by default
- When disabled at runtime, functions execute normally with minimal overhead
- Recommended to only enable in development/staging environments
- Never enable in production unless you know what you're doing
Summary
Functions
Returns the default failure rate (0.0 to 1.0).
Returns the default failure type.
Defines a public function that may randomly fail when Mutineer is enabled.
Defines a private function that may randomly fail when Mutineer is enabled.
Returns whether Mutineer is globally enabled at runtime.
Wraps a function call with Mutineer logic.
Determines if a failure should be triggered based on the given rate.
Functions
Returns the default failure rate (0.0 to 1.0).
Returns the default failure type.
Defines a public function that may randomly fail when Mutineer is enabled.
Options
failure_rateis the probability of failure for a given function (0.0-1.0), where1.0or above will always fail and0.0or below will never fail.failure_types(orfailure_type) is either a list of failure types to trigger (e.g.[:error, :delay]) or a single failure type (e.g.:error)errors(orerror) is either a list of objects to be randomly selected from or a single object to return when the:errortype is triggeredraised_errors(orraised_error) is a list of errors to be randomly selected from or a single error to be raised when the:raisetype is triggeredexit_errors(orexit_error) is a list of errors to be randomly selected from or a single error to be raised when the:exittype is triggereddelayis the upperbound of the delay in milliseconds or a range of milliseconds for:timeoutand:delaytypes
Examples
defchaos my_function(arg1, arg2), failure_rate: 0.2 do
# function body
end
defchaos api_call(url), failure_type: :timeout, delay: 3000 do
# function body
end
Defines a private function that may randomly fail when Mutineer is enabled.
Same options as defchaos/2.
Returns whether Mutineer is globally enabled at runtime.
Wraps a function call with Mutineer logic.
This is the runtime function that checks if chaos should trigger. When chaos is disabled, immediately executes the function with minimal overhead.
Determines if a failure should be triggered based on the given rate.