Skip to content

Commit 3aef0c7

Browse files
committed
feat: add Grafana and Prometheus monitoring stack
- Add Prometheus service to docker-compose.yml for metrics collection - Add Grafana service with pre-configured dashboard - Create monitoring configuration files: - prometheus.yml for metrics scraping - Grafana datasource and dashboard provisioning - Pre-built Go REST API dashboard with comprehensive metrics - Add comprehensive monitoring documentation - Create test script for monitoring setup verification - Update README.md with monitoring section and quick start guide Features: - Real-time metrics visualization - Request rate and response time monitoring - Error rate tracking by status code - HTTP method and status code distribution - Automatic dashboard provisioning - Production-ready configuration
1 parent 9a6b49f commit 3aef0c7

File tree

8 files changed

+1035
-0
lines changed

8 files changed

+1035
-0
lines changed

README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,43 @@ Configuration is managed through `.env`. Environment variables can override thes
198198

199199
API documentation is generated using Swagger. The documentation is available at `http://localhost:8080/swagger/index.html`.
200200

201+
## 📊 Monitoring & Observability
202+
203+
The template includes a comprehensive monitoring stack with Grafana and Prometheus:
204+
205+
### Monitoring Stack
206+
207+
- **Prometheus**: Time-series database for metrics collection
208+
- **Grafana**: Visualization and dashboard platform
209+
- **Jaeger**: Distributed tracing
210+
- **Application Metrics**: Built-in Prometheus metrics middleware
211+
212+
### Quick Start for Monitoring
213+
214+
1. Start the monitoring stack:
215+
216+
```bash
217+
make docker_up
218+
```
219+
220+
2. Access the monitoring tools:
221+
222+
- **Grafana**: http://localhost:3000 (admin/admin)
223+
- **Prometheus**: http://localhost:9090
224+
- **Jaeger**: http://localhost:16686
225+
- **Application Metrics**: http://localhost:8080/metrics
226+
227+
### Pre-configured Dashboards
228+
229+
The template includes a pre-configured Grafana dashboard with:
230+
231+
- Request rate and response time metrics
232+
- Error rates by status code
233+
- HTTP method distribution
234+
- 95th percentile response times
235+
236+
For detailed monitoring setup instructions, see [monitoring/README.md](monitoring/README.md).
237+
201238
## Prometheus Metrics
202239

203240
Prometheus metrics are exposed at `http://localhost:8080/metrics`.

docker-compose.yml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,50 @@ services:
108108
networks:
109109
- app_network
110110

111+
# ───────────────────────────────────────────────────────────
112+
# 🌟 PROMETHEUS
113+
# ───────────────────────────────────────────────────────────
114+
prometheus:
115+
image: prom/prometheus:latest
116+
container_name: prometheus
117+
restart: always
118+
ports:
119+
- "9090:9090"
120+
volumes:
121+
- ./monitoring/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml
122+
- prometheus_data:/prometheus
123+
command:
124+
- '--config.file=/etc/prometheus/prometheus.yml'
125+
- '--storage.tsdb.path=/prometheus'
126+
- '--web.console.libraries=/etc/prometheus/console_libraries'
127+
- '--web.console.templates=/etc/prometheus/consoles'
128+
- '--storage.tsdb.retention.time=200h'
129+
- '--web.enable-lifecycle'
130+
networks:
131+
- app_network
132+
133+
# ───────────────────────────────────────────────────────────
134+
# 🌟 GRAFANA
135+
# ───────────────────────────────────────────────────────────
136+
grafana:
137+
image: grafana/grafana:latest
138+
container_name: grafana
139+
restart: always
140+
ports:
141+
- "3000:3000"
142+
environment:
143+
- GF_SECURITY_ADMIN_USER=admin
144+
- GF_SECURITY_ADMIN_PASSWORD=admin
145+
- GF_USERS_ALLOW_SIGN_UP=false
146+
volumes:
147+
- grafana_data:/var/lib/grafana
148+
- ./monitoring/grafana/provisioning:/etc/grafana/provisioning
149+
- ./monitoring/grafana/dashboards:/var/lib/grafana/dashboards
150+
depends_on:
151+
- prometheus
152+
networks:
153+
- app_network
154+
111155

112156
# ───────────────────────────────────────────────────────────
113157
# 🌟 NETWORK & VOLUME CONFIGURATION
@@ -118,3 +162,5 @@ networks:
118162
volumes:
119163
db_data:
120164
redis_data:
165+
prometheus_data:
166+
grafana_data:

monitoring/README.md

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
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

Comments
 (0)