
grafana-oss
热门配置 Grafana OSS — 通过 YAML 配置仪表盘,设置数据源(Prometheus / Loki / Tempo / Pyroscope),编写带模板变量的仪表盘 JSON,构建面板查询,分配内置角色(Viewer / Editor / Admin / GrafanaAdmin),生成服务账户令牌,编辑 grafana.ini 服务器配置,创建注释,通过配置安装插件,并使用健康检查 curl 验证每一步。适用于构建仪表盘、配置数据源、设置配置 YAML、选择面板类型、编写模板变量、管理用户和角色、在 grafana.ini 中配置 SMTP/OAuth、通过 API 创建注释、排查配置的仪表盘未显示的问题,或在本地运行 Grafana OSS — 即使用户只说“设置 Prometheus 数据源”、“从 git 配置仪表盘”、“创建服务账户”或“在 OSS 中配置 SSO”而未提及“Grafana OSS”。
Configure Grafana OSS — provisions dashboards from YAML, sets up data sources (Prometheus / Loki / Tempo / Pyroscope), writes dashboard JSON with template variables, builds panel queries, assigns built-in roles (Viewer / Editor / Admin / GrafanaAdmin), mints service-account tokens, edits grafana.ini server config, creates annotations, installs plugins via provisioning, and validates each step with a health-check curl. Use when building dashboards, configuring data sources, setting up provisioning YAML, picking a panel type, writing template variables, managing users and roles, configuring SMTP/OAuth in grafana.ini, creating annotations via API, troubleshooting why a provisioned dashboard isn't showing up, or running Grafana OSS locally — even when the user says "set up a Prometheus data source", "provision dashboards from git", "make a service account", or "configure SSO in OSS" without saying "Grafana OSS".
Grafana OSS
常见工作流
从磁盘配置仪表盘
- 将仪表盘 JSON 文件放入
/var/lib/grafana/dashboards/目录下 - 在
provisioning/dashboards/default.yaml中添加提供者(参见下面的 § 仪表盘配置) - 重启 Grafana 以加载提供者配置
- 验证仪表盘是否已加载:
返回标题 → 成功。404 → 配置未生效;检查 Grafana 服务器日志(curl https://grafana.example.com/api/dashboards/uid/<uid> \ -H "Authorization: Bearer <token>" | jq '.dashboard.title'journalctl -u grafana-server | grep -i provisioning)以查找解析错误。
配置数据源
- 编写
provisioning/datasources/datasources.yaml(参见下面的 § 数据源配置) - 重启 Grafana
- 通过 API 健康检查数据源:
curl https://grafana.example.com/api/datasources/uid/<uid>/health \ -H "Authorization: Bearer <token>" # { "status": "OK", "message": "..." } → 工作正常 # { "status": "ERROR", ... } → URL 不可达或认证配置错误
创建服务账户 + 令牌
- 通过 YAML 或
POST /api/serviceaccounts配置(完整 API 见 references/api.md § 用户与服务账户) - 通过
POST /api/serviceaccounts/{id}/tokens生成令牌 - 验证令牌是否有效:
curl https://grafana.example.com/api/org \ -H "Authorization: Bearer <new-token>" # 200 + org JSON → 令牌和角色分配正常 # 401 → 令牌错误;403 → 角色错误
仪表盘配置
# provisioning/dashboards/default.yaml
apiVersion: 1
providers:
- name: default
folder: MyFolder
type: file
disableDeletion: false
updateIntervalSeconds: 30
options:
path: /var/lib/grafana/dashboards
foldersFromFilesStructure: true
关于仪表盘 JSON 结构本身(面板、查询、模板变量),请参见 references/dashboard-json.md。
数据源配置
# provisioning/datasources/datasources.yaml
apiVersion: 1
datasources:
- name: Prometheus
type: prometheus
access: proxy
url: http://prometheus:9090
isDefault: true
jsonData:
timeInterval: 15s
httpMethod: POST
- name: Loki
type: loki
access: proxy
url: http://loki:3100
- name: Tempo
type: tempo
access: proxy
url: http://tempo:3200
jsonData:
tracesToLogsV2:
datasourceUid: loki_uid
tags: [{ key: "service.name", value: "app" }]
serviceMap:
datasourceUid: prometheus_uid
nodeGraph:
enabled: true
- name: Pyroscope
type: grafana-pyroscope-datasource
url: http://pyroscope:4040
RBAC(内置角色)
| 角色 | 权限 |
|---|---|
| Viewer | 读取仪表盘、警报 |
| Editor | 创建/编辑仪表盘、警报 |
| Admin | 管理数据源、用户、插件 |
| GrafanaAdmin | 服务器级管理员(超级用户) |
服务账户配置:
# provisioning/access-control/service_accounts.yaml
apiVersion: 1
serviceAccounts:
- name: ci-reader
orgId: 1
role: Viewer
tokens:
- name: ci-token
# expires: 可选 ISO 8601 时间戳;省略则令牌永不过期
(具有细粒度权限的自定义 RBAC 角色仅限企业版/云版 — 如果需要,请参见 grafana-cloud/admin 技能。)
插件配置
# provisioning/plugins/plugins.yaml
apiVersion: 1
apps:
- type: grafana-pyroscope-app
disabled: false
jsonData:
backendUrl: http://pyroscope:4040
重启后,通过 GET /api/plugins/<plugin-id>/health 验证。
参考
references/dashboard-json.md— 完整的仪表盘 JSON 模型 + 模板变量 + 常见问题(uid 唯一性、gridPos 算术、数据源 uid 匹配)references/dashboards.md— 仪表盘工作流、设置、变量、注释、分享、版本、播放列表和配置即代码references/datasources.md— 数据源设置和查询示例(Prometheus、Loki、Tempo、SQL、CloudWatch 和插件)references/panel-types.md— 面板类型表 + 选择合适面板的决策指南references/panels.md— 面板编辑器、可视化选项、字段配置、转换、查询选项、检查和性能提示references/alerting.md— 警报概念、联系点、通知策略、模板、静默和常见规则示例references/api.md— 完整的 Grafana OSS API 参考(仪表盘、数据源、用户、服务账户、注释),包含验证 curl 和常见故障模式references/config.md—grafana.ini服务器/数据库/SMTP/认证/安全/功能开关配置 + 需要重启的问题





