|
| 1 | +# Monitoring Setup with Grafana and Prometheus |
| 2 | + |
| 3 | +This directory contains the monitoring configuration for the Go REST API template using Grafana and Prometheus. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The monitoring stack consists of: |
| 8 | + |
| 9 | +- **Prometheus**: Time-series database for storing metrics |
| 10 | +- **Grafana**: Visualization and dashboard platform |
| 11 | +- **Go Application**: Exposes metrics at `/metrics` endpoint |
| 12 | + |
| 13 | +## Architecture |
| 14 | + |
| 15 | +```sh |
| 16 | +┌─────────────┐ ┌─────────────┐ ┌─────────────┐ |
| 17 | +│ Grafana │◄───│ Prometheus │◄───│ Go App │ |
| 18 | +│ :3000 │ │ :9090 │ │ :8080 │ |
| 19 | +└─────────────┘ └─────────────┘ └─────────────┘ |
| 20 | +``` |
| 21 | + |
| 22 | +## Metrics Collected |
| 23 | + |
| 24 | +The Go application exposes the following Prometheus metrics: |
| 25 | + |
| 26 | +- `http_requests_total`: Total number of HTTP requests (counter) |
| 27 | +- `http_request_duration_seconds`: HTTP request duration (histogram) |
| 28 | + |
| 29 | +These metrics are labeled with: |
| 30 | + |
| 31 | +- `code`: HTTP status code |
| 32 | +- `method`: HTTP method (GET, POST, etc.) |
| 33 | +- `path`: Request path |
| 34 | + |
| 35 | +## Getting Started |
| 36 | + |
| 37 | +### 1. Start the Monitoring Stack |
| 38 | + |
| 39 | +```bash |
| 40 | +# Start all services including monitoring |
| 41 | +docker-compose up -d |
| 42 | + |
| 43 | +# Or start only monitoring services |
| 44 | +docker-compose up -d prometheus grafana |
| 45 | +``` |
| 46 | + |
| 47 | +### 2. Access the Services |
| 48 | + |
| 49 | +- **Grafana**: http://localhost:3000 |
| 50 | + - Username: `admin` |
| 51 | + - Password: `admin` |
| 52 | +- **Prometheus**: http://localhost:9090 |
| 53 | +- **Go Application**: http://localhost:8080 |
| 54 | +- **Application Metrics**: http://localhost:8080/metrics |
| 55 | + |
| 56 | +### 3. Import Dashboard |
| 57 | + |
| 58 | +The Go REST API dashboard is automatically provisioned and includes: |
| 59 | + |
| 60 | +- Request rate over time |
| 61 | +- 95th percentile response time |
| 62 | +- Success/error rates by status code |
| 63 | +- Requests by HTTP method |
| 64 | +- Requests by status code |
| 65 | + |
| 66 | +## Configuration Files |
| 67 | + |
| 68 | +### Prometheus Configuration (`monitoring/prometheus/prometheus.yml`) |
| 69 | + |
| 70 | +- Scrapes metrics from the Go application every 5 seconds |
| 71 | +- Stores data for 200 hours |
| 72 | +- Includes Redis monitoring (optional) |
| 73 | + |
| 74 | +### Grafana Configuration |
| 75 | + |
| 76 | +- **Datasources**: Automatically configured to connect to Prometheus |
| 77 | +- **Dashboards**: Pre-configured dashboard for Go REST API monitoring |
| 78 | +- **Provisioning**: Automatic setup of datasources and dashboards |
| 79 | + |
| 80 | +## Customization |
| 81 | + |
| 82 | +### Adding Custom Metrics |
| 83 | + |
| 84 | +To add custom metrics to your Go application: |
| 85 | + |
| 86 | +```go |
| 87 | +import ( |
| 88 | + "github.com/prometheus/client_golang/prometheus" |
| 89 | + "github.com/prometheus/client_golang/prometheus/promauto" |
| 90 | +) |
| 91 | + |
| 92 | +var ( |
| 93 | + customCounter = promauto.NewCounter(prometheus.CounterOpts{ |
| 94 | + Name: "my_custom_counter", |
| 95 | + Help: "Description of my custom counter", |
| 96 | + }) |
| 97 | +) |
| 98 | +``` |
| 99 | + |
| 100 | +### Creating Custom Dashboards |
| 101 | + |
| 102 | +1. Access Grafana at http://localhost:3000 |
| 103 | +2. Create a new dashboard |
| 104 | +3. Add panels with Prometheus queries |
| 105 | +4. Export the dashboard JSON and place it in `monitoring/grafana/dashboards/` |
| 106 | + |
| 107 | +### Alerting |
| 108 | + |
| 109 | +To set up alerts: |
| 110 | + |
| 111 | +1. Configure alerting rules in Prometheus |
| 112 | +2. Set up alert manager |
| 113 | +3. Configure notification channels in Grafana |
| 114 | + |
| 115 | +## Useful Prometheus Queries |
| 116 | + |
| 117 | +### Request Rate |
| 118 | + |
| 119 | +```promql |
| 120 | +rate(http_requests_total[5m]) |
| 121 | +``` |
| 122 | + |
| 123 | +### Error Rate |
| 124 | + |
| 125 | +```promql |
| 126 | +rate(http_requests_total{code=~"5.."}[5m]) |
| 127 | +``` |
| 128 | + |
| 129 | +### 95th Percentile Response Time |
| 130 | + |
| 131 | +```promql |
| 132 | +histogram_quantile(0.95, rate(http_request_duration_seconds_bucket[5m])) |
| 133 | +``` |
| 134 | + |
| 135 | +### Success Rate |
| 136 | + |
| 137 | +```promql |
| 138 | +sum(rate(http_requests_total{code=~"2.."}[5m])) / sum(rate(http_requests_total[5m])) |
| 139 | +``` |
| 140 | + |
| 141 | +## Troubleshooting |
| 142 | + |
| 143 | +### Prometheus Not Scraping Metrics |
| 144 | + |
| 145 | +1. Check if the Go application is running: `docker-compose ps` |
| 146 | +2. Verify metrics endpoint: `curl http://localhost:8080/metrics` |
| 147 | +3. Check Prometheus targets: http://localhost:9090/targets |
| 148 | + |
| 149 | +### Grafana Can't Connect to Prometheus |
| 150 | + |
| 151 | +1. Verify Prometheus is running: `docker-compose ps prometheus` |
| 152 | +2. Check Prometheus logs: `docker-compose logs prometheus` |
| 153 | +3. Verify network connectivity between containers |
| 154 | + |
| 155 | +### Dashboard Not Loading |
| 156 | + |
| 157 | +1. Check if the dashboard JSON is valid |
| 158 | +2. Verify the datasource is properly configured |
| 159 | +3. Check Grafana logs: `docker-compose logs grafana` |
| 160 | + |
| 161 | +## Production Considerations |
| 162 | + |
| 163 | +### Security |
| 164 | + |
| 165 | +- Change default Grafana credentials |
| 166 | +- Use environment variables for sensitive configuration |
| 167 | +- Consider using reverse proxy with authentication |
| 168 | +- Enable HTTPS for production deployments |
| 169 | + |
| 170 | +### Performance |
| 171 | + |
| 172 | +- Adjust Prometheus retention period based on storage requirements |
| 173 | +- Configure appropriate scrape intervals |
| 174 | +- Monitor Prometheus and Grafana resource usage |
| 175 | +- Consider using Prometheus federation for large deployments |
| 176 | + |
| 177 | +### High Availability |
| 178 | + |
| 179 | +- Use external storage for Prometheus data |
| 180 | +- Set up Prometheus federation |
| 181 | +- Configure Grafana with external database |
| 182 | +- Use load balancers for multiple instances |
| 183 | + |
| 184 | +## Additional Resources |
| 185 | + |
| 186 | +- [Prometheus Documentation](https://prometheus.io/docs/) |
| 187 | +- [Grafana Documentation](https://grafana.com/docs/) |
| 188 | +- [Prometheus Client Go](https://github.com/prometheus/client_golang) |
| 189 | +- [Grafana Dashboards](https://grafana.com/grafana/dashboards/) |
0 commit comments