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分以内に有効になります。課金への影響がすべて反映されるまでには1時間ほどかかります。元の高カーディナリティのサンプルは引き続き送信されますが、ドロップされたラベルは課金対象に含まれなくなります。

リソース

コメント