数据源配置
Grafana 支持多种数据源,包括 Prometheus、InfluxDB、MySQL、PostgreSQL 等。
支持的数据源
时序数据库
| 数据源 | 类型 | 用途 |
|---|---|---|
| Prometheus | 时序数据库 | 监控指标 |
| InfluxDB | 时序数据库 | IoT、监控 |
| Graphite | 时序数据库 | 监控指标 |
| TimescaleDB | 时序数据库 | PostgreSQL 扩展 |
关系型数据库
| 数据源 | 类型 | 用途 |
|---|---|---|
| MySQL | 关系型数据库 | 业务数据 |
| PostgreSQL | 关系型数据库 | 业务数据 |
| Microsoft SQL Server | 关系型数据库 | 企业应用 |
其他数据源
| 数据源 | 类型 | 用途 |
|---|---|---|
| Elasticsearch | 搜索引擎 | 日志分析 |
| Loki | 日志系统 | 日志查询 |
| Jaeger | 追踪系统 | 分布式追踪 |
| CloudWatch | 云监控 | AWS 监控 |
Prometheus 数据源
通过界面配置
- 导航到 Configuration → Data Sources
- 点击 Add data source
- 选择 Prometheus
- 配置参数
配置参数
yaml
Name: Prometheus
URL: http://localhost:9090
Access: Server (default)
Basic Auth: 可选通过配置文件
yaml
# /etc/grafana/provisioning/datasources/prometheus.yml
apiVersion: 1
datasources:
- name: Prometheus
type: prometheus
access: proxy
url: http://localhost:9090
isDefault: true
editable: false带认证配置
yaml
apiVersion: 1
datasources:
- name: Prometheus
type: prometheus
access: proxy
url: http://localhost:9090
basicAuth: true
basicAuthUser: admin
basicAuthPassword: password
withCredentials: false
tlsAuth: false
tlsAuthWithCACert: falseInfluxDB 数据源
配置示例
yaml
apiVersion: 1
datasources:
- name: InfluxDB
type: influxdb
access: proxy
url: http://localhost:8086
database: mydb
user: admin
password: passwordFlux 查询
flux
from(bucket: "mybucket")
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r._measurement == "cpu")
|> aggregateWindow(every: v.windowPeriod, fn: mean)InfluxQL 查询
sql
SELECT mean("value")
FROM "cpu"
WHERE $timeFilter
GROUP BY time($__interval)MySQL 数据源
配置示例
yaml
apiVersion: 1
datasources:
- name: MySQL
type: mysql
access: proxy
url: localhost:3306
database: mydb
user: root
password: passwordSQL 查询示例
sql
SELECT
time_column as time,
metric_name as metric,
value
FROM metrics_table
WHERE time_column BETWEEN FROM_UNIXTIME(${__from}/1000)
AND FROM_UNIXTIME(${__to}/1000)
ORDER BY time_column使用宏
sql
SELECT
created_at as time,
COUNT(*) as value
FROM orders
WHERE created_at BETWEEN $__timeFrom() AND $__timeTo()
GROUP BY created_at
ORDER BY created_atElasticsearch 数据源
配置示例
yaml
apiVersion: 1
datasources:
- name: Elasticsearch
type: elasticsearch
access: proxy
url: http://localhost:9200
database: "logstash-*"
jsonData:
esVersion: "7.10.0"
timeField: "@timestamp"Lucene 查询
status:200 AND method:GET聚合查询
json
{
"query": {
"bool": {
"must": [
{"range": {"@timestamp": {"gte": "now-1h"}}}
]
}
},
"aggs": {
"status_codes": {
"terms": {"field": "status"}
}
}
}Loki 数据源
配置示例
yaml
apiVersion: 1
datasources:
- name: Loki
type: loki
access: proxy
url: http://localhost:3100LogQL 查询
logql
{app="myapp"} |= "error" | json | line_format "{{.level}} {{.message}}"日志指标
logql
sum by (status) (
count_over_time({app="myapp"} | json | status="200" [5m])
)多数据源配置
配置文件示例
yaml
# /etc/grafana/provisioning/datasources/datasources.yml
apiVersion: 1
datasources:
- name: Prometheus-Prod
type: prometheus
access: proxy
url: http://prometheus-prod:9090
isDefault: true
- name: Prometheus-Dev
type: prometheus
access: proxy
url: http://prometheus-dev:9090
- name: InfluxDB
type: influxdb
access: proxy
url: http://influxdb:8086
database: metrics
- name: MySQL
type: mysql
access: proxy
url: mysql:3306
database: business混合查询
在面板中可以使用多个数据源:
promql
# Prometheus 查询
rate(http_requests_total[5m])
# InfluxDB 查询
SELECT mean("value") FROM "requests" WHERE $timeFilter
# MySQL 查询
SELECT time, value FROM metrics WHERE time BETWEEN $__timeFrom() AND $__timeTo()数据源管理
通过 API 管理
bash
# 列出数据源
curl -X GET http://admin:admin@localhost:3000/api/datasources
# 创建数据源
curl -X POST http://admin:admin@localhost:3000/api/datasources \
-H "Content-Type: application/json" \
-d '{
"name": "Prometheus",
"type": "prometheus",
"url": "http://localhost:9090",
"access": "proxy"
}'
# 删除数据源
curl -X DELETE http://admin:admin@localhost:3000/api/datasources/1测试数据源
bash
curl -X POST http://admin:admin@localhost:3000/api/datasources/1/health数据源权限
组织级别
yaml
apiVersion: 1
datasources:
- name: Prometheus
type: prometheus
url: http://localhost:9090
orgId: 1只读数据源
yaml
apiVersion: 1
datasources:
- name: Prometheus
type: prometheus
url: http://localhost:9090
readOnly: true环境变量配置
使用环境变量
bash
docker run -d --name grafana \
-p 3000:3000 \
-e "GF_INSTALL_PLUGINS=grafana-clock-panel" \
-e "GF_DEFAULT_INSTANCE_NAME=my-grafana" \
grafana/grafana:10.4.0数据源环境变量
bash
docker run -d --name grafana \
-p 3000:3000 \
-e "GF_SECURITY_ADMIN_PASSWORD=admin" \
-e "GF_DATASOURCES_PROMETHEUS_NAME=Prometheus" \
-e "GF_DATASOURCES_PROMETHEUS_TYPE=prometheus" \
-e "GF_DATASOURCES_PROMETHEUS_URL=http://prometheus:9090" \
grafana/grafana:10.4.0最佳实践
- 使用配置文件:避免手动配置,使用 YAML 文件管理数据源
- 命名规范:使用有意义的数据源名称,如 Prometheus-Prod、Prometheus-Dev
- 分离环境:生产和开发环境使用不同的数据源
- 设置默认:指定默认数据源简化面板配置
- 定期备份:导出数据源配置进行备份
- 使用代理模式:Server 模式比 Browser 模式更安全
- 配置超时:设置合理的超时时间防止查询阻塞
yaml
apiVersion: 1
datasources:
- name: Prometheus
type: prometheus
url: http://localhost:9090
jsonData:
httpMethod: POST
timeout: 30
customQueryParameters: ""故障排查
连接问题
bash
# 检查数据源连通性
curl http://localhost:9090/-/healthy
# 查看 Grafana 日志
docker logs grafana
# 测试 Prometheus 查询
curl 'http://localhost:9090/api/v1/query?query=up'常见问题
- 数据源未列出:检查 provisioning 配置文件路径
- 查询超时:增加 timeout 配置或优化查询
- 认证失败:验证用户名密码或 Token
- 权限不足:检查数据源权限设置