|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## Project Overview |
| 6 | + |
| 7 | +MSStats is a Python tool for extracting Google Cloud MemoryStore (Redis) database metrics using the Google Cloud Monitoring API. It processes Redis databases (single instance and replicated) across multiple GCP service accounts and generates Excel reports with usage statistics. |
| 8 | + |
| 9 | +## Architecture |
| 10 | + |
| 11 | +- **Single Python script**: `msstats.py` - Main application that processes Redis metrics |
| 12 | +- **Batch processing**: Shell scripts for bulk operations across multiple GCP projects |
| 13 | +- **Service account management**: Scripts to grant/revoke monitoring permissions |
| 14 | +- **Output format**: Excel files with detailed Redis command statistics and throughput data |
| 15 | + |
| 16 | +## Key Components |
| 17 | + |
| 18 | +- **Metric Processing**: Categorizes Redis commands by type (Get, Set, Hash, List, etc.) from monitoring data |
| 19 | +- **Multi-node handling**: Aggregates metrics across Redis cluster nodes taking maximum values |
| 20 | +- **Time series data**: Configurable duration (default 7 days) and step intervals (default 60s) |
| 21 | +- **GCP Integration**: Uses Google Cloud Monitoring API with service account authentication |
| 22 | + |
| 23 | +## Development Commands |
| 24 | + |
| 25 | +### Setup Environment |
| 26 | +```bash |
| 27 | +# Create and activate virtual environment |
| 28 | +python -m venv .env && source .env/bin/activate |
| 29 | + |
| 30 | +# Install dependencies |
| 31 | +pip install -r requirements.txt |
| 32 | + |
| 33 | +# Place service account JSON files in root directory |
| 34 | +cp path/to/service_account.json . |
| 35 | +``` |
| 36 | + |
| 37 | +### Running the Tool |
| 38 | +```bash |
| 39 | +# Basic usage (7 days, 60s steps) |
| 40 | +python msstats.py |
| 41 | + |
| 42 | +# Custom duration and step size |
| 43 | +python msstats.py --duration 1800 --step 300 |
| 44 | + |
| 45 | +# Specific project |
| 46 | +python msstats.py -p project-id |
| 47 | +``` |
| 48 | + |
| 49 | +### Batch Operations |
| 50 | +```bash |
| 51 | +# Grant monitoring permissions to service account |
| 52 | +./grant_sa_monitoring_viewer.sh service-account@project.iam.gserviceaccount.com |
| 53 | + |
| 54 | +# Run across all accessible projects |
| 55 | +./batch_run_msstats.sh |
| 56 | + |
| 57 | +# Remove monitoring permissions |
| 58 | +./remove_sa_monitoring_viewer.sh service-account@project.iam.gserviceaccount.com |
| 59 | + |
| 60 | +# Clean up environment |
| 61 | +deactivate |
| 62 | +``` |
| 63 | + |
| 64 | +## Command Line Options |
| 65 | + |
| 66 | +- `--duration SECONDS`: Time period to analyze (default: 604800 = 7 days) |
| 67 | +- `--step SECONDS`: Metric sampling interval (default: 60 seconds) |
| 68 | +- `-p PROJECT_ID`: Target specific GCP project |
| 69 | + |
| 70 | +### Testing and Code Quality |
| 71 | +```bash |
| 72 | +# Run all tests (unit and integration) |
| 73 | +pytest test_msstats.py |
| 74 | + |
| 75 | +# Format code with Black |
| 76 | +black *.py |
| 77 | + |
| 78 | +# Check formatting without making changes |
| 79 | +black --check *.py |
| 80 | +``` |
| 81 | + |
| 82 | +## Dependencies |
| 83 | + |
| 84 | +- `openpyxl>=3.0.4`: Excel file generation |
| 85 | +- `google-cloud-monitoring==2.18.0`: GCP Monitoring API client |
| 86 | +- `black>=25.1.0`: Code formatter |
| 87 | +- `pytest>=8.4.0`: Testing framework |
| 88 | +- `pytest-mock>=3.14.0`: Mock utilities for testing |
| 89 | +- Python 3.9+ required |
| 90 | + |
| 91 | +## Security Notes |
| 92 | + |
| 93 | +- Service account JSON files must be placed in repository root |
| 94 | +- Tool uses read-only monitoring API access |
| 95 | +- Never connects directly to Redis instances |
| 96 | +- No impact on database performance or data |
0 commit comments