Monitoring and Backups
LeafLock now includes comprehensive monitoring with Prometheus metrics and automated backup capabilities with S3 support.
Prometheus Metrics
Section titled “Prometheus Metrics”Enabling Metrics
Section titled “Enabling Metrics”Metrics are enabled by default. To disable them, set ENABLE_METRICS=false
in your environment.
Available Metrics
Section titled “Available Metrics”The application exposes the following metrics at /metrics
:
HTTP Metrics
Section titled “HTTP Metrics”leaflock_http_requests_total
- Total HTTP requests by method, endpoint, and status codeleaflock_http_request_duration_seconds
- HTTP request duration histogram
Application Metrics
Section titled “Application Metrics”leaflock_active_users
- Number of currently active usersleaflock_notes_total
- Total notes operations (create, update, delete)leaflock_collaborations_active
- Number of active collaborationsleaflock_websocket_connections
- Active WebSocket connections
Database Metrics
Section titled “Database Metrics”leaflock_db_connections_active
- Active database connectionsleaflock_db_connections_idle
- Idle database connectionsleaflock_db_queries_total
- Total database queries by operation
Redis Metrics
Section titled “Redis Metrics”leaflock_redis_connections_active
- Active Redis connectionsleaflock_redis_operations_total
- Total Redis operations
Error Metrics
Section titled “Error Metrics”leaflock_errors_total
- Total errors by type and component
Backup Metrics
Section titled “Backup Metrics”leaflock_backups_total
- Total backup operations by statusleaflock_backup_duration_seconds
- Backup duration histogramleaflock_backup_size_bytes
- Last backup size
Accessing Metrics
Section titled “Accessing Metrics”# Check if metrics are enabledcurl http://localhost:8080/metrics
# View specific metricscurl http://localhost:8080/metrics | grep leaflock_notes_total
Automated S3 Backups
Section titled “Automated S3 Backups”Configuration
Section titled “Configuration”Add the following environment variables to your .env
file:
# Enable backupsENABLE_BACKUPS=true
# S3 ConfigurationBACKUP_S3_BUCKET=your-backup-bucket-nameBACKUP_S3_ACCESS_KEY=your_access_keyBACKUP_S3_SECRET_KEY=your_secret_keyBACKUP_S3_REGION=us-east-1BACKUP_S3_ENDPOINT=https://s3.amazonaws.com
# Backup encryption (32 characters)BACKUP_ENCRYPTION_KEY=your_32_character_encryption_key
# Schedule (cron format) - default: daily at 2 AMBACKUP_SCHEDULE=0 2 * * *
# Retention (days) - default: 30 daysBACKUP_RETENTION_DAYS=30
S3 Bucket Setup
Section titled “S3 Bucket Setup”- Create an S3 bucket for backups
- Create an IAM user with the following policy:
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "s3:PutObject", "s3:GetObject", "s3:DeleteObject", "s3:ListBucket" ], "Resource": [ "arn:aws:s3:::your-backup-bucket/*", "arn:aws:s3:::your-backup-bucket" ] } ]}
Backup Features
Section titled “Backup Features”Running Backups
Section titled “Running Backups”With Docker Compose
Section titled “With Docker Compose”# Start with backup servicemake up
# Run immediate backupdocker compose exec backup /usr/local/bin/backup.sh
# View backup logsdocker compose logs backup -f
Manual Backup
Section titled “Manual Backup”# Run backup script directly./scripts/backup.sh
# List available backups./scripts/restore.sh --list
# Restore from specific backup./scripts/restore.sh --file backups/2025/01/21/leaflock_backup_20250121_020000.sql.gz.enc
Backup Storage Structure
Section titled “Backup Storage Structure”Backups are stored in S3 with the following structure:
s3://your-bucket/└── backups/ └── 2025/ └── 01/ └── 21/ ├── leaflock_backup_20250121_020000.sql.gz.enc ├── leaflock_backup_20250121_140000.sql.gz.enc └── ...
Kubernetes Monitoring
Section titled “Kubernetes Monitoring”When deploying with Helm, monitoring is automatically configured:
ServiceMonitor
Section titled “ServiceMonitor”The chart creates a ServiceMonitor for Prometheus Operator:
monitoring: enabled: true serviceMonitor: enabled: true namespace: monitoring interval: 30s
Prometheus Rules
Section titled “Prometheus Rules”Pre-configured alerts include:
LeafLockBackendDown
- Backend service is unavailableLeafLockHighErrorRate
- High error rate detectedLeafLockBackupFailed
- Backup operation failed
Backup CronJob
Section titled “Backup CronJob”Automated backups run as a Kubernetes CronJob:
backup: enabled: true schedule: "0 2 * * *" retentionDays: 30
Grafana Dashboard
Section titled “Grafana Dashboard”A sample Grafana dashboard is available at docs/grafana-dashboard.json
with:
- Request rate and latency graphs
- Error rate monitoring
- Database and Redis connection metrics
- Backup status and size tracking
- Active user and collaboration counts
Troubleshooting
Section titled “Troubleshooting”Metrics Not Working
Section titled “Metrics Not Working”- Check
ENABLE_METRICS
environment variable - Verify
/metrics
endpoint is accessible - Check application logs for errors
Backup Failures
Section titled “Backup Failures”- Verify S3 credentials and permissions
- Check
BACKUP_ENCRYPTION_KEY
is set - Review backup logs:
docker compose logs backup
- Test S3 connectivity manually
Common Issues
Section titled “Common Issues”Security Considerations
Section titled “Security Considerations”- Backup encryption keys should be stored securely
- S3 credentials should use least-privilege IAM policies
- Regular backup restore testing is recommended
- Metrics endpoint should be secured in production environments