mcpField
A
adaptive-metrics

adaptive-metrics

github.com/grafana云与 DevOps2954.3k 次安装1 天前更新
代码仓库

关于 adaptive-metrics

面向管理指标基数和成本的 Grafana Cloud Adaptive Metrics 技能,介绍如何使用推荐或自定义聚合规则、移除未使用指标并验证效果。

主要功能

  1. 01查看并应用推荐规则
  2. 02创建自定义聚合规则
  3. 03移除未使用指标
  4. 04衡量活跃序列影响
  5. 05停用或删除规则

适用场景

  1. 01在存储前减少高基数序列
  2. 02为选定指标应用推荐规则
  3. 03移除仪表板或告警未使用的指标
  4. 04检查规则变更后的活跃序列减少量

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/grafana/skills --skill adaptive-metrics

安装后在 Claude Code 中输入/adaptive-metrics确认已加载。

附带文件references/api.md

详细介绍

Grafana Cloud Adaptive Metrics

文档:https://grafana.com/docs/grafana-cloud/cost-management-and-billing/reduce-costs/metrics-costs/adaptive-metrics.md

在存储前预先缩减高基数指标的聚合规则,可直接降低活跃序列的计费。

前提条件

  • Grafana Cloud Metrics 计划(任意付费层级)
  • 具有 metrics:write 权限的 API 密钥(用于 Adaptive Metrics API — adaptive-metrics.grafana.net,使用 Bearer 身份验证)
  • 对于验证查询:指标查询端点(prometheus-prod-XX.grafana.net)使用 HTTP 基本身份验证 — 使用 <metrics_user>(数字形式的堆栈/实例 ID)以及具有 metrics:read 权限的令牌 — 而不是 Bearer 密钥
  • 可在 Cloud 门户中访问 主页 → Adaptive Metrics

常见工作流

1. 审查并应用自动建议
# 1. Pull the recommendation list (sorted by series-reduction impact)
curl -s -H "Authorization: Bearer <KEY>" \
  "https://adaptive-metrics.grafana.net/api/v1/recommendations" \
  | jq '.recommendations[] | {metric_name, current_series, projected_series, estimated_reduction_percent}'

# 2. Capture the baseline series count for the target metric
#    (metrics query endpoint = basic auth, not the Bearer key)
curl -s -u "<metrics_user>:<metrics_token>" \
  "https://prometheus-prod-XX.grafana.net/api/prom/api/v1/query?query=count({__name__=\"process_cpu_seconds_total\"})" \
  | jq '.data.result[0].value[1]'   # → e.g. "12480"

# 3. Apply the recommendation (or click Apply in the UI)
curl -s -X POST -H "Authorization: Bearer <KEY>" \
  "https://adaptive-metrics.grafana.net/api/v1/recommendations/<ID>/apply"

# 4. Wait ~5 min. Verify — re-run the count query; expect a large drop.
#    Also check the saving metric:
#      grafanacloud_instance_active_series_dropped_by_aggregation_rules

回滚 — 删除规则:

curl -s -H "Authorization: Bearer <KEY>" \
  "https://adaptive-metrics.grafana.net/api/v1/rules" | jq '.rules[] | {id, metric_name}'
curl -s -X DELETE -H "Authorization: Bearer <KEY>" \
  "https://adaptive-metrics.grafana.net/api/v1/rules/<RULE_ID>"
# Or in the UI: Rules → row → Disable
2. 手动编写自定义规则
# 1. Sanity-check the metric is not used WITH that label in dashboards/alerts
grep -r 'process_cpu_seconds_total' dashboards/ alerts/ | grep -E 'version|go_version'
# Expect no hits → safe to drop.

# 2. Create the rule
curl -s -X POST -H "Authorization: Bearer <KEY>" -H "Content-Type: application/json" \
  "https://adaptive-metrics.grafana.net/api/v1/rules" \
  -d '{"rules":[{"metric_name":"process_cpu_seconds_total","match_type":"MATCH_TYPE_EXACT",
                 "drop_labels":["version","go_version"],
                 "aggregations":[{"type":"AGGREGATION_TYPE_SUM"}]}]}'

# 3. Verify — same count() query as above; series count should drop within 5 min.

完整负载(正则表达式匹配、聚合类型及所有注意事项):references/api.md。

3. 完全丢弃未使用的指标
# 1. List unused metrics
curl -s -H "Authorization: Bearer <KEY>" \
  "https://adaptive-metrics.grafana.net/api/v1/usage-analysis?filter=unused" | \
  jq '.metrics[] | {metric_name, series_count, last_queried}'

# 2. Confirm not referenced in dashboards / alerts / recording rules
grep -r '<METRIC_NAME>' dashboards/ alerts/ recording-rules/

# 3. Add a write_relabel_config drop in Alloy (full block in references/api.md)
#    Reload Alloy: curl -X POST http://localhost:12345/-/reload

# 4. Verify — the metric should no longer appear in series counts after ~10 min
curl -s -u "<metrics_user>:<metrics_token>" \
  'https://prometheus-prod-XX.grafana.net/api/prom/api/v1/label/__name__/values' | jq '.data | index("<METRIC_NAME>")'  # → null

衡量影响

# Total active series (billed unit)
grafanacloud_instance_active_series

# Series specifically dropped by Adaptive Metrics rules
grafanacloud_instance_active_series_dropped_by_aggregation_rules

规则会在约 5 分钟内生效;完整的计费影响会在一小时内显现。原始高基数样本仍会持续传入,但被丢弃的标签不再计入费用。

资源

评论

支持 Markdown