Monitoring & Logging
Monitoring and logging setup for production.
Google Sheets Logging
In production, logs are automatically sent to Google Sheets.
Setup
- Create Google Cloud service account
- Create spreadsheet
- Share spreadsheet with service account email
- Set environment variables:
bash
SHEETS_CLIENT_EMAIL=...@iam.gserviceaccount.com
SHEETS_PRIVATE_KEY=-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----\n
SHEETS_LOGS_SPREADSHEET_ID=spreadsheet-idLog Levels
typescript
logger.log('Info message'); // General info
logger.warn('Warning message'); // Warnings
logger.error('Error message'); // Errors
logger.debug('Debug message'); // Debug (dev only)Monitoring Metrics
Key Metrics
- Response Time - Bot response latency
- Error Rate - Percentage of failed requests
- API Rate Limits - External API usage
- Memory Usage - Process memory consumption
- Database Connections - MongoDB connection pool
Using PM2
bash
# Monitor all processes
pm2 monit
# Save monitoring data
pm2 save
pm2 resurrectAlerting
Set up alerts for:
- Error rate > 5%
- Response time > 5s
- Memory usage > 80%
- Database connection failures
Health Checks
Implement health check endpoint:
bash
curl http://localhost:3000/health
# Returns: { status: 'ok', uptime: 3600 }Troubleshooting
Memory Leak
- Monitor:
pm2 monit - Check for unclosed connections
- Review MongoDB operations
- Restart if necessary:
pm2 restart mmps
High CPU Usage
- Check active tasks:
top - Review logs for errors
- Check API rate limiting
- Optimize database queries
Database Issues
- Check connection:
mongosh "..." - Monitor connection pool
- Check indexes
- Review slow queries