Stateless fault injection for testing resilience pipelines.
Injects errors and/or latency into function calls based on configurable probabilities. Useful for verifying that upstream resilience layers (retry, circuit breaker, etc.) handle failures correctly.
Options
:name-- optional atom for telemetry metadata. Default:chaos.:error_rate-- probability of injecting an error (0.0 to 1.0). Default0.0.:error_fn-- 0-arity function returning the error value. Default:ExResilience.Chaos.default_error/0.:latency_rate-- probability of injecting latency (0.0 to 1.0). Default0.0.:latency_min-- minimum injected latency in milliseconds. Default0.:latency_max-- maximum injected latency in milliseconds. Default100.:seed-- optional integer RNG seed for deterministic behavior.
Execution Order
- If
:seedis provided, seed the process RNG. - Roll for latency injection. If triggered, sleep for a random duration
in
[latency_min, latency_max]. - Roll for error injection. If triggered, return
error_fn.()without calling the wrapped function. - Otherwise, call the wrapped function and return its result.
Examples
iex> ExResilience.Chaos.call(fn -> {:ok, 1} end, error_rate: 0.0, latency_rate: 0.0)
{:ok, 1}
iex> ExResilience.Chaos.call(fn -> {:ok, 1} end, error_rate: 1.0)
{:error, :chaos_fault}
Summary
Functions
Executes fun with optional fault injection.
Returns the default error value used when no :error_fn is provided.
Types
@type option() :: {:name, atom()} | {:error_rate, float()} | {:error_fn, (-> term())} | {:latency_rate, float()} | {:latency_min, non_neg_integer()} | {:latency_max, non_neg_integer()} | {:seed, integer()}
Functions
Executes fun with optional fault injection.
See module documentation for available options and execution order.
Examples
iex> ExResilience.Chaos.call(fn -> :ok end, error_rate: 0.0)
:ok
iex> ExResilience.Chaos.call(fn -> :ok end, error_rate: 1.0, error_fn: fn -> {:error, :boom} end)
{:error, :boom}
@spec default_error() :: {:error, :chaos_fault}
Returns the default error value used when no :error_fn is provided.