Development Setup¶
This guide covers setting up a development environment for Gunicorn Prometheus Exporter.
Prerequisites¶
- Python 3.8 or higher
- Git
- pip
- tox (for testing)
Setup¶
Clone the Repository¶
git clone https://github.com/Agent-Hellboy/gunicorn-prometheus-exporter.git
cd gunicorn-prometheus-exporter
Install Development Dependencies¶
# Install in development mode with all dependencies
pip install -e ".[dev]"
# Or install specific extras
pip install -e ".[async,redis,dev]"
Development Dependencies¶
The [dev]
extra includes:
pytest
- Testing frameworkpytest-cov
- Coverage reportingruff
- Linting and formattingmypy
- Type checkingtox
- Testing across Python versions
Running Tests¶
Using pytest¶
# Run all tests
pytest
# Run with coverage
pytest --cov=src/gunicorn_prometheus_exporter --cov-report=html
# Run specific test file
pytest tests/test_plugin.py
# Run tests with verbose output
pytest -v
# Run tests in parallel
pytest -n auto
Using tox¶
# Run all test environments
tox
# Run specific environment
tox -e py311
# Run linting only
tox -e lint
# Run formatting only
tox -e format
Code Quality¶
Linting¶
# Check code with ruff
ruff check src/ tests/
# Fix auto-fixable issues
ruff check --fix src/ tests/
Formatting¶
# Format code with ruff
ruff format src/ tests/
# Check formatting without changes
ruff format --check src/ tests/
Type Checking¶
Project Structure¶
gunicorn-prometheus-exporter/
├── src/
│ └── gunicorn_prometheus_exporter/
│ ├── __init__.py
│ ├── backend/ # Redis backend implementation
│ ├── config.py # Configuration management
│ ├── hooks.py # Gunicorn hooks
│ ├── logging.py # Logging utilities
│ ├── master.py # Master process monitoring
│ ├── metrics.py # Metrics collection
│ ├── plugin.py # Gunicorn plugin
│ └── utils.py # Utility functions
├── tests/ # Test files
├── docs/ # Documentation
├── example/ # Example applications
├── system-test/ # System integration tests
├── pyproject.toml # Project configuration
├── tox.ini # Tox configuration
└── README.md
Adding New Features¶
1. Create a Feature Branch¶
2. Implement the Feature¶
- Add your code to the appropriate module
- Follow existing code patterns and style
- Add type hints where appropriate
- Include docstrings for public functions
3. Add Tests¶
- Create test files in the
tests/
directory - Test both success and failure cases
- Aim for high test coverage
4. Update Documentation¶
- Update relevant documentation files
- Add examples if applicable
- Update the changelog
5. Run Quality Checks¶
# Run all quality checks
tox
# Or run individually
ruff check src/ tests/
ruff format src/ tests/
mypy src/gunicorn_prometheus_exporter/
pytest
Testing Guidelines¶
Unit Tests¶
- Test individual functions and methods
- Mock external dependencies
- Use descriptive test names
- Follow AAA pattern (Arrange, Act, Assert)
Integration Tests¶
- Test component interactions
- Use real dependencies where appropriate
- Test error handling and edge cases
System Tests¶
- Test complete workflows
- Use the
system-test/
directory - Test with different worker types
- Verify metrics collection
Code Style¶
Python Style¶
- Follow PEP 8
- Use type hints
- Write descriptive docstrings
- Use meaningful variable names
Import Organization¶
# Standard library imports
import os
import sys
# Third-party imports
import prometheus_client
from gunicorn import util
# Local imports
from .config import Config
from .metrics import Metrics
Error Handling¶
try:
# Risky operation
result = risky_function()
except SpecificException as e:
logger.error(f"Specific error occurred: {e}")
raise
except Exception as e:
logger.error(f"Unexpected error: {e}")
raise
Debugging¶
Enable Debug Logging¶
# Set debug environment variables
export PROMETHEUS_DEBUG="true"
export GUNICORN_DEBUG="true"
export REDIS_DEBUG="true"
Using pdb¶
Using logging¶
import logging
logger = logging.getLogger(__name__)
logger.debug("Debug message")
logger.info("Info message")
logger.warning("Warning message")
logger.error("Error message")
Performance Testing¶
Load Testing¶
# Install load testing tools
pip install locust
# Run load tests
locust -f load_test.py --host=http://localhost:8000
Memory Profiling¶
# Install memory profiler
pip install memory-profiler
# Profile memory usage
python -m memory_profiler your_script.py
Release Process¶
1. Update Version¶
2. Create Release¶
3. Build and Publish¶
Contributing¶
Pull Request Process¶
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests
- Run quality checks
- Submit a pull request
Pull Request Guidelines¶
- Include a clear description
- Reference related issues
- Ensure all tests pass
- Update documentation if needed
- Keep changes focused and atomic
Common Issues¶
Import Errors¶
Test Failures¶
Linting Errors¶
# Check specific file
ruff check src/gunicorn_prometheus_exporter/plugin.py
# Fix auto-fixable issues
ruff check --fix src/gunicorn_prometheus_exporter/plugin.py
Resources¶
Related Documentation¶
- Contributing Guide - How to contribute
- Backend API - Backend API documentation
- Config API - Configuration API documentation
- Hooks API - Hooks API documentation
- Metrics API - Metrics API documentation
- Plugin API - Plugin API documentation
- Troubleshooting Guide - Common issues