关于 cost-management
Grafana Cloud 成本管理技能面向管理可观测性支出的团队,提供成本归因、Adaptive 信号优化和用量告警的操作流程。
主要功能
- 01按团队和服务归因成本
- 02应用 Adaptive Metrics 建议
- 03在 Alloy 中过滤日志
- 04设置用量告警
- 05参考 PromQL 成本查询
适用场景
- 01识别不同团队或项目的遥测支出
- 02减少指标序列基数和日志摄入量
- 03在配额耗尽前监控用量
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ grafana/ skills --skill cost-management安装后在 Claude Code 中输入/cost-management确认已加载。
附带文件references/adaptive-signals.mdreferences/alerts-and-queries.md
详细介绍
Grafana Cloud 成本管理
文档:https://grafana.com/docs/grafana-cloud/cost-management-and-billing/
通过自适应信号和成本归属标签,降低指标、日志和追踪的开销。
前提条件
- 已启用 Adaptive Metrics / Logs / Traces 的 Grafana Cloud 堆栈(可在 Cost Management 下查看)
- Alloy(或 Grafana Agent)正在采集遥测数据,且 API 密钥具有
metrics:write+logs:write(以及traces:write)范围 - 对该堆栈拥有管理员权限,以应用 Adaptive 建议
常见工作流
1. 将成本归属到团队 / 服务
# 1. Add external labels in Alloy (metrics + logs configs)
prometheus.remote_write "cloud" {
endpoint { url = sys.env("PROMETHEUS_URL") /* ... */ }
external_labels = { team = "platform", project = "checkout-service" }
}
# 2. Reload Alloy
curl -X POST http://localhost:12345/-/reload
# 3. Verify labels arrived in Grafana Cloud
# In Explore, run: count by (team, project) ({__name__=~".+"})
# Then visit Cost Management → group by `team` / `project`
完整的 Alloy 代码片段请参见 references/adaptive-signals.md。
2. 使用 Adaptive Metrics 降低指标基数
# 1. Pull recommendations
curl https://<stack>.grafana.net/api/plugins/grafana-adaptive-metrics-app/resources/v1/recommendations \
-H "Authorization: Bearer <token>" | jq '.recommendations | length'
# 2. In the UI: Grafana Cloud → Adaptive Metrics → review rules sorted by series-reduction impact
# 3. Test in "Preview" mode before applying
# 4. Apply (takes effect within 5 min)
# 5. Verify — series count should drop on the affected metrics
# Before applying, capture baseline:
# count({__name__="http_request_duration_seconds_bucket"})
# Wait 10 min after apply, run again — expect 10x+ reduction for high-card metrics.
# Rollback if needed: open the rule in the UI → Disable, or DELETE /v1/rules/<id>.
3. 在 Alloy 中丢弃嘈杂的日志
# 1. Add a filter stage (see references/adaptive-signals.md for the full block)
loki.process "filter_logs" {
forward_to = [loki.write.cloud.receiver]
stage.drop { expression = ".*GET /health.*" }
}
# 2. Reload Alloy
curl -X POST http://localhost:12345/-/reload
# 3. Verify the filter — health logs should NOT appear in Logs Drilldown
# LogQL check (should return 0):
# sum(rate({app="my-app"} |= "GET /health" [5m]))
# Bytes-ingested should also drop. Compare 24h before/after:
# sum(increase(loki_ingester_chunk_size_bytes_sum[24h])) by (namespace)
4. 在达到配额前设置用量告警
可在 references/alerts-and-queries.md 查看可直接粘贴的规则(MetricsUsageHigh、LogsIngestionHigh)。
优化清单
- 应用 Adaptive Metrics 建议 — 通常可减少 40-60% 的序列
- 在 Alloy 中丢弃健康检查 / 就绪探针日志
- 对追踪进行尾部采样,采样率设为 5-10%,并保留错误 / 慢速跨度
- 在每个 Alloy 配置中添加
team+project外部标签 - 在达到配额的 80% 时设置用量告警
- 用录制规则替代开销高的临时查询
参考资料
references/adaptive-signals.md— Adaptive Metrics / Logs / Traces 配置;成本归属标签references/alerts-and-queries.md— 用量告警规则、用于查找成本的 PromQL、计费单位表
资源
评论
支持 Markdown