3.9 KiB
3.9 KiB
Grafana Integration Guide
This guide shows how to visualize your application metrics with Grafana using real Prometheus.
Why Prometheus?
We've replaced custom tracking with actual Prometheus because:
- ✅ Industry standard - Battle-tested metrics collection
- ✅ Rich ecosystem - Thousands of existing dashboards
- ✅ Better performance - Optimized time-series storage
- ✅ Less code - No custom database or API endpoints
- ✅ Standard metrics - HTTP requests, response times, etc.
Available Metrics
Automatic Metrics (from echo-contrib/prometheus)
http_requests_total- Total HTTP requests by method/status/endpointhttp_request_duration_seconds- Request duration histogramhttp_request_size_bytes- Request size histogramhttp_response_size_bytes- Response size histogram
Custom Application Metrics
knet_page_views_total{route="/"}- Page views by routeknet_page_views_total{route="/draw"}- Draw page viewsknet_box_clicks_total{position="1"}- Box clicks by positionknet_websocket_connections- Active WebSocket connections
Authentication
The /metrics endpoint uses Basic Authentication in production:
- Username:
api(or set viaAPI_USERNAMEenvironment variable) - Password: Your API token (set via
API_TOKENenvironment variable)
Example:
curl -u api:your-api-token http://your-server:54321/metrics
Grafana Setup
Prometheus Data Source (Recommended)
- Add Prometheus Data Source in Grafana
- Set URL to:
http://your-server:54321 - Set Basic Auth: Username:
api, Password:your-api-token - Set Scrape Interval: 15s (Prometheus scrapes our
/metricsendpoint)
That's it! Grafana will automatically discover all available metrics.
Sample Dashboard Queries
Traffic Overview:
# Total requests per minute
rate(http_requests_total[1m]) * 60
# Page views
knet_page_views_total
# Request duration 95th percentile
histogram_quantile(0.95, rate(http_request_duration_seconds_bucket[5m]))
# Error rate
rate(http_requests_total{status=~"4.."}[5m]) / rate(http_requests_total[5m]) * 100
Application Metrics:
# Box interactions
rate(knet_box_clicks_total[5m])
# Active WebSocket connections
knet_websocket_connections
# Most popular pages
topk(10, rate(knet_page_views_total[1h]))
Dashboard Examples
1. HTTP Overview Panel
- Metric:
rate(http_requests_total[1m]) * 60 - Type: Graph
- Title: "Requests per Minute"
2. Response Time Panel
- Metric:
histogram_quantile(0.95, rate(http_request_duration_seconds_bucket[5m])) - Type: Single Stat
- Title: "95th Percentile Response Time"
3. Page Views Panel
- Metric:
knet_page_views_total - Type: Graph
- Title: "Page Views by Route"
4. Box Clicks Heatmap
- Metric:
rate(knet_box_clicks_total[5m]) - Type: Graph
- Title: "Box Clicks per Second"
Environment Variables
Set these for production:
export API_TOKEN="your-secure-api-token"
export API_USERNAME="api" # Optional, defaults to "api"
export BUILD_NUMBER="prod-v1.0" # Enables production mode
Benefits of This Approach
- Standard Prometheus - Works with all Grafana features
- Rich Metrics - HTTP performance + custom business metrics
- No Database - Prometheus handles storage and retention
- Better Performance - Optimized time-series queries
- Alerting Ready - Use Prometheus AlertManager
- Ecosystem - Tons of existing dashboards and tools
Quick Start
- Start your server:
./tmp/main - Test metrics:
curl -u api:your-token http://localhost:54321/metrics - Add Prometheus data source in Grafana:
http://localhost:54321 - Create dashboard with queries like
http_requests_total - Enjoy real-time metrics! 📊
Your analytics are now powered by industry-standard Prometheus! <20>