Grafana
Open-source analytics and interactive visualization platform.
Installation
Docker
bash
# Run Grafana
docker run -d \
-p 3000:3000 \
--name grafana \
grafana/grafana
# Run with persistent storage
docker run -d \
-p 3000:3000 \
-v grafana-storage:/var/lib/grafana \
--name grafana \
grafana/grafana
# Run with custom config
docker run -d \
-p 3000:3000 \
-v $(pwd)/grafana.ini:/etc/grafana/grafana.ini \
--name grafana \
grafana/grafanaBinary
bash
# Download
wget https://dl.grafana.com/oss/release/grafana-10.2.0.linux-amd64.tar.gz
# Extract
tar -zxvf grafana-10.2.0.linux-amd64.tar.gz
# Run
cd grafana-10.2.0
./bin/grafana-server webAuthentication
| COMMAND | DESCRIPTION |
|---|---|
admin/admin | Default credentials |
| Settings > Users > Change Password | Change password |
| Settings > Users > Invite | Invite users |
| Settings > Users > Add User | Add user |
Data Sources
Add Data Source
- Go to Configuration > Data Sources
- Click "Add data source"
- Select type (Prometheus, InfluxDB, etc.)
- Configure connection
- Click "Save & Test"
API Commands
bash
# List data sources
curl -X GET \
-H "Authorization: Bearer <api_key>" \
http://localhost:3000/api/datasources
# Add data source
curl -X POST \
-H "Authorization: Bearer <api_key>" \
-H "Content-Type: application/json" \
-d '{
"name": "Prometheus",
"type": "prometheus",
"url": "http://localhost:9090",
"access": "proxy"
}' \
http://localhost:3000/api/datasources
# Delete data source
curl -X DELETE \
-H "Authorization: Bearer <api_key>" \
http://localhost:3000/api/datasources/uid/<uid>Dashboards
Create Dashboard
- Go to Create > Dashboard
- Add panels with visualization
- Configure queries
- Set up variables
- Save dashboard
Import Dashboard
- Go to Create > Import
- Enter dashboard ID or upload JSON
- Select data source
- Click Import
API Commands
bash
# List dashboards
curl -X GET \
-H "Authorization: Bearer <api_key>" \
http://localhost:3000/api/search?query=dashboard
# Get dashboard
curl -X GET \
-H "Authorization: Bearer <api_key>" \
http://localhost:3000/api/dashboards/uid/<uid>
# Create dashboard
curl -X POST \
-H "Authorization: Bearer <api_key>" \
-H "Content-Type: application/json" \
-d '{
"dashboard": {
"title": "My Dashboard",
"panels": []
},
"overwrite": true
}' \
http://localhost:3000/api/dashboards/db
# Delete dashboard
curl -X DELETE \
-H "Authorization: Bearer <api_key>" \
http://localhost:3000/api/dashboards/uid/<uid>Panels
Panel Types
| TYPE | DESCRIPTION |
|---|---|
Graph | Time series visualization |
Stat | Single metric visualization |
Gauge | Metric with threshold |
Table | Tabular data |
Bar Chart | Categorical data |
Pie Chart | Distribution |
Heatmap | Time series density |
Geomap | Geographic data |
Logs | Log visualization |
Trace | Distributed tracing |
Panel Queries
Prometheus
sql
# Rate
rate(http_requests_total[5m])
# Sum by label
sum(rate(http_requests_total[5m])) by (method)
# Average
avg(rate(http_requests_total[5m]))
# With filters
rate(http_requests_total{status="200"}[5m])InfluxDB
sql
SELECT mean(value) FROM "cpu" WHERE $timeFilter GROUP BY time($__interval)Loki
sql
{job="myapp"} |= "error"Variables
Query Variable
- Go to Dashboard Settings > Variables
- Click "Add variable"
- Select type: Query
- Select data source
- Enter query
- Configure options
Interval Variable
- Go to Dashboard Settings > Variables
- Add variable
- Select type: Interval
- Add intervals: 30s, 1m, 5m, 10m, 30m
Custom Variable
- Go to Dashboard Settings > Variables
- Add variable
- Select type: Custom
- Add values: dev, staging, prod
Using Variables
# In queries
rate(metric{instance="$instance"}[5m])
# In panel titles
Requests for $instanceAnnotations
Add Annotation
- Go to Dashboard Settings > Annotations
- Add annotation query
- Configure query
- Set tags and colors
API Commands
bash
# List annotations
curl -X GET \
-H "Authorization: Bearer <api_key>" \
http://localhost:3000/api/annotations
# Create annotation
curl -X POST \
-H "Authorization: Bearer <api_key>" \
-H "Content-Type: application/json" \
-d '{
"dashboardId": 1,
"time": 1640995200000,
"timeEnd": 1640995800000,
"tags": ["deployment"],
"text": "Deployed version 1.0.0"
}' \
http://localhost:3000/api/annotationsAlerts
Create Alert Rule
- Edit panel
- Go to "Alert" tab
- Set condition
- Set threshold
- Configure notification
- Save dashboard
Alert Notification Channels
- Go to Alerting > Notification channels
- Add channel
- Select type (Email, Slack, PagerDuty, etc.)
- Configure settings
- Test notification
Alerting
Alert Rule Syntax
# Condition
avg(metric) > threshold
# Reduce
avg()
# Evaluate every
every 1m
# For duration
for 5mFolders
Create Folder
- Go to Dashboards > Manage
- Create folder
- Add dashboards to folder
API Commands
bash
# List folders
curl -X GET \
-H "Authorization: Bearer <api_key>" \
http://localhost:3000/api/folders
# Create folder
curl -X POST \
-H "Authorization: Bearer <api_key>" \
-H "Content-Type: application/json" \
-d '{
"title": "My Folder"
}' \
http://localhost:3000/api/foldersOrganizations
Create Organization
- Go to Configuration > Users
- Create new organization
- Invite users
Switch Organization
- Click user menu
- Select organization
Users & Teams
Create Team
- Go to Configuration > Teams
- Create team
- Add members
Manage Permissions
- Go to Configuration > Users
- Edit user
- Assign roles: Viewer, Editor, Admin
Plugins
Install Plugin
- Go to Configuration > Plugins
- Find plugin
- Click "Install"
API Commands
bash
# List plugins
curl http://localhost:3000/api/plugins
# Install plugin
curl -X POST \
-H "Content-Type: application/json" \
-d '{
"name": "grafana-piechart-panel",
"version": "1.3.0"
}' \
http://localhost:3000/api/plugins/installProvisioning
Datasource Provisioning (INI)
ini
[datasources]
[datasources.prometheus]
type = prometheus
url = http://localhost:9090Dashboard Provisioning (YAML)
yaml
apiVersion: 1
datasources:
- name: Prometheus
type: prometheus
access: proxy
url: http://localhost:9090
dashboards:
- name: 'My Dashboard'
folder: ''
type: file
options:
path: /var/lib/grafana/dashboardsSnapshots
Create Snapshot
- Go to Share > Snapshot
- Set expiration
- Create snapshot
- Share URL
Export & Import
Export Dashboard
- Go to Dashboard settings
- Click "Export JSON"
- Save file
Import Dashboard
- Go to Create > Import
- Upload JSON file
- Configure data source
- Click Import
Best Practices
- Use folders to organize dashboards
- Use variables for dynamic dashboards
- Set up alerting for critical metrics
- Use annotations to mark events
- Provision configuration as code
- Regularly review and update dashboards
- Use appropriate panel types for data
- Set up user roles and permissions
- Use SSL/TLS for production
- Backup Grafana configuration and dashboards
- Monitor Grafana itself
TIP
Use Grafana's Explore feature for ad-hoc querying before building dashboards.