关于 ml-ai
为 Grafana Cloud 用户提供机器学习告警、自动化根因调查和 LLM 助手相关流程指南,涵盖预测、异常检测与插件配置。
主要功能
- 01基于 Prophet 的动态告警预测
- 02使用 DBSCAN 检测异常服务
- 03通过 Sift 发起调查
- 04配置 LLM 插件与助手
适用场景
- 01预测指标并设置动态告警
- 02比较服务指标以定位异常
- 03调查指定服务和时间段的问题
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ grafana/ skills --skill ml-ai安装后在 Claude Code 中输入/ml-ai确认已加载。
附带文件references/llm-and-graph.mdreferences/sift.md
详细介绍
Grafana Cloud AI 与 ML
文档:https://grafana.com/docs/grafana-cloud/alerting-and-irm/machine-learning/
在一个 Grafana Cloud 堆栈中提供 ML 告警、自动化 RCA 和由大模型驱动的助手。
前置条件
- Grafana Cloud 堆栈(Pro / Advanced——大多数功能已正式发布,部分功能仍处于预览阶段)
- 用于 ML / Sift / LLM-plugin 端点的 API 令牌,具备
plugins:write权限 - 对于 Dynamic Alerting:要预测的指标至少需要 14 天(最好是 90 天)的历史数据
常见工作流
1. 使用 Dynamic Alerting 进行预测告警
# 1. Create forecast job (Prophet — learns daily/weekly seasonality)
curl -X POST https://<stack>.grafana.net/api/plugins/grafana-ml-app/resources/ml/v1/forecast \
-H "Authorization: Bearer <token>" \
-H "Content-Type: application/json" \
-d '{
"name": "cpu-forecast",
"metric": "avg(rate(node_cpu_seconds_total{mode=\"user\"}[5m]))",
"datasourceId": 1,
"interval": 300,
"trainingWindow": "90d",
"forecastWindow": "7d",
"algorithm": { "name": "prophet", "config": {} }
}'
# 2. Verify job is producing the predicted-value metric (may take a few minutes).
# <datasourceId> must match the datasourceId used above (find it via
# GET /api/datasources), or run the query from Explore instead.
curl -s -H "Authorization: Bearer <token>" \
'https://<stack>.grafana.net/api/datasources/proxy/<datasourceId>/api/v1/query?query=ml_forecast_upper{job="cpu-forecast"}' \
| jq '.data.result | length'
# Expect > 0
# 3. Add an alert that fires when actual exceeds the upper bound
# expr: avg(rate(node_cpu_seconds_total{mode="user"}[5m]))
# > ml_forecast_upper{job="cpu-forecast"} * 1.1
2. 离群告警——某个服务偏离同类服务
# 1. Create outlier job (DBSCAN — groups peers, flags the odd one)
curl -X POST https://<stack>.grafana.net/api/plugins/grafana-ml-app/resources/ml/v1/outlier \
-H "Authorization: Bearer <token>" -H "Content-Type: application/json" \
-d '{
"name": "service-error-outliers",
"metric": "sum(rate(http_requests_total{status=~\"5..\"}[5m])) by (service)",
"datasourceId": 1,
"interval": 300,
"algorithm": { "name": "dbscan", "sensitivity": 0.5, "config": { "epsilon": 0.5 } }
}'
# 2. Verify the score metric exists (<datasourceId> must match the
# datasourceId used above, or run the query from Explore instead)
curl -s -H "Authorization: Bearer <token>" \
'https://<stack>.grafana.net/api/datasources/proxy/<datasourceId>/api/v1/query?query=ml_outlier_score{job="service-error-outliers"}' \
| jq '.data.result | length'
# 3. Alert when ml_outlier_score{job="service-error-outliers"} > 0.8 for 5m
3. 运行 Sift 调查
# 1. Trigger from API (or from Explore / Incident / OnCall)
curl -X POST https://<stack>.grafana.net/api/plugins/grafana-sift-app/resources/sift/v1/investigations \
-H "Authorization: Bearer <token>" -H "Content-Type: application/json" \
-d '{ "name":"checkout-spike","start":"2024-02-01T10:00:00Z","end":"2024-02-01T10:30:00Z",
"filters":{"service":"checkout","namespace":"production"} }'
# 2. The response includes an investigation ID — open it in the UI:
# https://<stack>.grafana.net/a/grafana-sift-app/investigations/<id>
# 3. Verify analyses ran — each of the 8 checks shows ✔ or ✖ with linked evidence.
完整的 8 种分析表请参阅 references/sift.md。
4. 配置 LLM Plugin
# 1. Provision (provisioning/plugins/llm.yaml — see references/llm-and-graph.md)
apiVersion: 1
apps:
- type: grafana-llm-app
jsonData: { openAIUrl: https://api.openai.com, openAIModel: gpt-4o }
secureJsonData: { openAIKey: sk-... }
# 2. Restart Grafana, then verify the health endpoint reports the configured provider
curl -s -H "Authorization: Bearer <token>" \
https://<stack>.grafana.net/api/plugins/grafana-llm-app/health | jq
# Expect: {"status":"ok", ...}
# 3. Verify in a panel — open any panel, click the Assistant icon, ask "what does this query do?"
有关助手功能、Knowledge Graph 搜索语法和 Adaptive Metrics 建议,请参阅 references/llm-and-graph.md。
资源
评论
支持 Markdown