Skip to content

数据源配置 ​

Grafana 支持多种数据源,包括 Prometheus、InfluxDB、MySQL、PostgreSQL 等。

支持的数据源 ​

时序数据库 ​

数据源类型用途
Prometheus时序数据库监控指标
InfluxDB时序数据库IoT、监控
Graphite时序数据库监控指标
TimescaleDB时序数据库PostgreSQL 扩展

关系型数据库 ​

数据源类型用途
MySQL关系型数据库业务数据
PostgreSQL关系型数据库业务数据
Microsoft SQL Server关系型数据库企业应用

其他数据源 ​

数据源类型用途
Elasticsearch搜索引擎日志分析
Loki日志系统日志查询
Jaeger追踪系统分布式追踪
CloudWatch云监控AWS 监控

Prometheus 数据源 ​

通过界面配置 ​

  1. 导航到 Configuration → Data Sources
  2. 点击 Add data source
  3. 选择 Prometheus
  4. 配置参数

配置参数 ​

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: false

InfluxDB 数据源 ​

配置示例 ​

yaml
apiVersion: 1

datasources:
  - name: InfluxDB
    type: influxdb
    access: proxy
    url: http://localhost:8086
    database: mydb
    user: admin
    password: password

Flux 查询 ​

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: password

SQL 查询示例 ​

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_at

Elasticsearch 数据源 ​

配置示例 ​

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:3100

LogQL 查询 ​

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

最佳实践 ​

  1. 使用配置文件:避免手动配置,使用 YAML 文件管理数据源
  2. 命名规范:使用有意义的数据源名称,如 Prometheus-Prod、Prometheus-Dev
  3. 分离环境:生产和开发环境使用不同的数据源
  4. 设置默认:指定默认数据源简化面板配置
  5. 定期备份:导出数据源配置进行备份
  6. 使用代理模式:Server 模式比 Browser 模式更安全
  7. 配置超时:设置合理的超时时间防止查询阻塞
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'

常见问题 ​

  1. 数据源未列出:检查 provisioning 配置文件路径
  2. 查询超时:增加 timeout 配置或优化查询
  3. 认证失败:验证用户名密码或 Token
  4. 权限不足:检查数据源权限设置

参考:https://grafana.com/docs/grafana/latest/datasources/