README
ΒΆ
Grafana Dashboard Generator from OpenAPI
A powerful tool to automatically generate comprehensive Grafana dashboards from OpenAPI specifications with enhanced metrics, versioning, and monitoring capabilities.
Features
- π Enhanced Metrics: P50, P90, P95, P99 percentiles, throughput, error rates
- π Dashboard Versioning: Track changes and update existing dashboards
- π Multiple Panel Types: Time series, stats, and gauge panels
- ποΈ Advanced Templating: Dynamic service, environment, and datasource variables
- π gRPC Support: Automatic detection and monitoring of gRPC services
- π³ Docker Integration: Complete monitoring stack with Prometheus and Grafana
- π¨ Modern UI: Beautiful, responsive panels with proper thresholds
- π Alerting: Built-in AlertManager integration
- π§ Automation: Watch mode for automatic regeneration
Quick Start
Prerequisites
- Go 1.18+
- Docker & Docker Compose
- jq (for JSON validation)
Installation
# Clone the repository
git clone <repository-url>
cd gen-grafana-from-api
# Setup development environment
make dev-setup
# Check prerequisites
make check
π Quick Demo with Sample API
We provide a complete sample API service that demonstrates all features:
# Option 1: One-command demo (recommended)
make demo
# Option 2: Manual setup
make generate-sample && make start-fresh
# Wait for services to start (about 30 seconds)
# Then visit:
# - Grafana: http://localhost:3000 (admin/admin)
# - Sample API: http://localhost:8080/api/inventory/v1/livez
# - Prometheus: http://localhost:9090
The sample API includes:
- β Full OpenAPI spec implementation with 8+ endpoints
- β Real Prometheus metrics (request rates, latency histograms, error rates)
- β Realistic behavior with artificial latency and error simulation
- β Background traffic generation for demonstration
- β Health checks and proper service discovery
Basic Usage
# Quick demo with sample API
make generate-sample && make start-fresh
# Generate dashboard from your OpenAPI spec
make generate
# Start monitoring stack
make start
# Run full integration test with sample API
make test-full
# Clean up everything
make clean
Advanced Usage
Dashboard Generation Options
# Generate with custom options
go run main.go openapi.yaml dashboard.json --datasource prometheus --title "My API"
# Update existing dashboard
go run main.go openapi.yaml dashboard.json --update --uid my-dashboard
# Custom configuration
go run main.go openapi.yaml dashboard.json \
--datasource prometheus \
--title "Production API Dashboard" \
--uid prod-api-dashboard
Available Make Targets
| Target | Description |
|---|---|
make build |
Build the binary |
make generate |
Generate dashboard from OpenAPI spec |
make generate-sample |
Generate dashboard from sample API spec |
make update |
Update existing dashboard |
make build-sample-api |
Build sample API locally |
make run-sample-api |
Run sample API locally on :8080 |
make start |
Start monitoring stack |
make start-fresh |
Start monitoring stack with fresh build |
make stop |
Stop monitoring stack |
make test-full |
Run full integration test |
make validate |
Validate generated dashboard |
make watch |
Watch for changes and auto-regenerate |
make demo |
Run complete demo with sample API |
make clean |
Clean up all resources |
Test Script Options
# Run basic test
./test-dashboard.sh
# Show logs after startup
./test-dashboard.sh --logs
# Clean up containers
./test-dashboard.sh --cleanup
# Show help
./test-dashboard.sh --help
Dashboard Features
Metrics Generated
For each API endpoint, the dashboard includes:
-
Request Rate Panel
- Requests per second by status code
- Time series visualization
- Color-coded by HTTP status
-
Latency Percentiles Panel
- P50, P90, P95, P99 response times
- Time series with threshold alerts
- Millisecond precision
-
Error Rate Panel
- Percentage of 5xx errors
- Stat panel with color thresholds
- Real-time updates
-
Throughput Panel
- Total requests per second
- Stat panel with trends
- Performance indicators
Variables & Templating
- Datasource: Dynamic datasource selection
- Environment: Filter by environment (prod, stage, dev)
- Service: Filter by service name
- Custom Variables: Easily extensible
gRPC Support
When gRPC extensions are detected in the OpenAPI spec:
# In your OpenAPI spec
x-grpc:
UserService:
GetUser: {}
CreateUser: {}
The tool automatically generates gRPC-specific panels with:
- gRPC status codes
- Method-specific latency
- Service-level metrics
Configuration
Prometheus Configuration
The generated dashboard expects these Prometheus metrics:
# HTTP metrics
- http_requests_total{method, path, status_code, service}
- http_request_duration_seconds_bucket{method, path, service}
# gRPC metrics (if applicable)
- grpc_server_handled_total{grpc_service, grpc_method, grpc_code}
- grpc_server_handling_seconds_bucket{grpc_service, grpc_method}
Customization
Adding Custom Metrics
Extend the Panel creation functions in main.go:
func createCustomPanel(title, query string, panelID, height, yPos int) Panel {
return Panel{
ID: panelID,
Title: title,
// ... panel configuration
}
}
Custom Thresholds
Modify threshold values in the panel creation functions:
Thresholds: ThresholdOptions{
Mode: "absolute",
Steps: []ThresholdStep{
{Color: "green", Value: nil},
{Color: "yellow", Value: floatPtr(0.5)},
{Color: "red", Value: floatPtr(1.0)},
},
},
Monitoring Stack
Services Included
- Grafana: Dashboard visualization (port 3000)
- Prometheus: Metrics collection (port 9090)
- AlertManager: Alert routing (port 9093)
- Node Exporter: System metrics (port 9100)
Service URLs
| Service | URL | Credentials |
|---|---|---|
| Grafana | http://localhost:3000 | admin/admin |
| Prometheus | http://localhost:9090 | - |
| AlertManager | http://localhost:9093 | - |
Data Persistence
- Grafana data:
grafana_datavolume - Prometheus data:
prometheus_datavolume - Dashboards: Auto-provisioned from
output_dashboard.json
Dashboard Structure
Panel Layout
βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Request Rate β
β β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β Latency Percentiles β
β β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β Error Rate β Throughput β
β β β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Versioning
The dashboard includes metadata for version tracking:
{
"meta": {
"version": 2,
"generated": "2024-01-01T12:00:00Z",
"spec_hash": "abc123...",
"last_updated": "2024-01-01T12:00:00Z"
}
}
Troubleshooting
Common Issues
-
Dashboard not importing
# Check Grafana logs docker-compose logs grafana # Validate dashboard JSON make validate -
No metrics appearing
# Check Prometheus targets curl http://localhost:9090/api/v1/targets # Verify metrics endpoint curl http://your-api:8080/metrics -
Permission issues
# Fix script permissions chmod +x test-dashboard.sh
Debug Mode
Enable debug logging:
# Set environment variable
export DEBUG=true
# Run with verbose output
./test-dashboard.sh --logs
Development
Code Structure
main.go # Main application logic
types.go # Grafana dashboard types
panels.go # Panel creation functions
docker-compose.yaml # Monitoring stack
prometheus.yml # Prometheus configuration
test-dashboard.sh # Integration test script
Makefile # Build automation
Adding New Panel Types
- Define the panel structure in the types
- Create a panel creation function
- Add the panel to the dashboard generation logic
- Update tests
Testing
# Run unit tests
go test ./...
# Run integration tests
make test-full
# Manual testing
make generate && make start
Contributing
- Fork the repository
- Create a feature branch
- Add tests for new functionality
- Run
make test-fullto verify - Submit a pull request
License
MIT License - see LICENSE file for details.
Changelog
v2.0.0
- β¨ Enhanced metrics with P90, P99 percentiles
- π Dashboard versioning and update mechanism
- π¨ Modern panel designs with proper thresholds
- π³ Improved Docker Compose setup
- π Better templating and variables
- π Enhanced gRPC support
- π― Comprehensive test suite
v1.0.0
- π Initial release with basic dashboard generation
- π HTTP metrics support
- π§ Basic Docker integration
Documentation
ΒΆ
There is no documentation for this package.