Runtime Safety Protection for Ruby Applications
Anzen prevents catastrophic crashes from recursive call stacks and memory overflow conditions. Deploy with confidence knowing your Ruby applications (Rails, microservices, background jobs) are protected from common runtime failures that can bring down production systems.
Key Benefits:
- 🚀 Zero Code Changes: Drop-in protection with single initialization
- đź”§ Enterprise Ready: Production-tested safety monitoring
- 📊 Observable: CLI tools for status monitoring and debugging
- đź§© Extensible: Built-in monitors + custom safety checks
- ⚡ Low Overhead: Sampling-based monitoring with minimal performance impact
- 🔄 Real-Time Protection: Automatic interception without manual checks
Add Anzen to your Gemfile:
gem 'anzen', '~> 0.1.0'Install the gem:
bundle install# config/initializers/anzen.rb (Rails)
# or lib/anzen.rb (standalone)
require 'anzen'
Anzen.setup(
config: {
enabled_monitors: ['recursion', 'memory'],
monitors: {
recursion: { depth_limit: 1000 },
memory: { limit_mb: 512, sampling_interval_ms: 100 }
}
}
)
# Your application is now protected!See Rails example in Rails test
Anzen supports multiple configuration sources:
export ANZEN_CONFIG='{
"enabled_monitors": ["recursion", "memory"],
"monitors": {
"recursion": {"depth_limit": 500},
"memory": {"limit_mb": 1024}
}
}'# config/anzen.yml
anzen:
enabled_monitors:
- recursion
- memory
monitors:
recursion:
depth_limit: 1000
memory:
limit_mb: 512
sampling_interval_ms: 100Anzen.setup(config: {
enabled_monitors: ['recursion'],
monitors: {
recursion: { depth_limit: 500 }
}
})Anzen integrates cleanly with any Ruby application:
- Initializer:
config/initializers/anzen.rb - Middleware: Automatic request-level protection
- Background Jobs: Sidekiq/Resque job protection
- Middleware:
use Anzen::Middleware - Config.ru: Centralized setup
- Direct Setup:
Anzen.setup()at script start - Error Handling: Rescue
Anzen::ViolationError
# Initialize protection
Anzen.setup(config: {...})
# Runtime control
Anzen.enable('recursion')
Anzen.disable('memory')
# Status and monitoring (optional - monitoring happens automatically)
status = Anzen.status
Anzen.check! # Manual check if needed
# Custom monitors
Anzen.register_monitor(my_monitor)begin
Anzen.check!
rescue Anzen::RecursionLimitExceeded => e
# Recursion violation
rescue Anzen::MemoryLimitExceeded => e
# Memory violation
rescue Anzen::CheckFailedError => e
# Infrastructure error
end"Anzen not initialized"
- Ensure
Anzen.setup()is called before using CLI or API - Check that the gem is properly required
High Memory Usage
- Adjust
sampling_interval_msto reduce monitoring frequency - Memory monitor only samples, doesn't cause overhead
Recursion Detection Too Sensitive
- Increase
depth_limitfor applications with deep call stacks - Recursion monitor tracks all method calls including framework code
CLI Commands Not Found
- Ensure
bin/anzenis in PATH or usebundle exec anzen - Check that the gem is installed:
gem list anzen
# Reduce monitoring overhead
Anzen.setup(config: {
enabled_monitors: ['recursion'], # Only enable needed monitors
monitors: {
memory: {
sampling_interval_ms: 500 # Check less frequently
}
}
})- Ruby 3.0+
- Bundler
git clone https://github.com/korakotlee/anzen
cd anzen
bin/setup# Run all tests
bundle exec rspec
# Run with coverage
bundle exec rspec --coverage
# Run specific test
bundle exec rspec spec/unit/cli_spec.rb# Lint code
bundle exec rubocop
# Auto-fix issues
bundle exec rubocop -a
# Run all checks
bundle exec rake- Fork the repository
- Create a feature branch:
git checkout -b feature/my-feature - Write tests for your changes
- Ensure all tests pass:
bundle exec rspec - Check code quality:
bundle exec rubocop - Submit a pull request
- Test-First: Write specs before implementation
- Code Quality: RuboCop strict compliance required
- Documentation: Update docs for public API changes
- Backwards Compatibility: Maintain API stability
Anzen mean safety in Japanese
Copyright (c) 2025 Korakot Leemakdej. Released under the MIT License. See LICENSE for details.
This project follows the Contributor Covenant. We are committed to providing a welcoming and inclusive environment for all contributors.