cardinality-gate.sh 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #!/bin/bash
  2. # Cardinality Gate - CI stage validation per Log.md
  3. # Run this script in CI before deploying Vector to ensure labels stay within cardinality limits
  4. set -euo pipefail
  5. LOKI_ENDPOINT="${LOKI_ENDPOINT:-http://loki:3100}"
  6. MAX_CARDINALITY=5000
  7. echo "🔍 Checking label cardinality limits for Vector-Loki deployment..."
  8. # Helper function to get cardinality from Loki
  9. check_label_cardinality() {
  10. local label=$1
  11. local max=$2
  12. echo -n " Checking ${label}... "
  13. # Query Loki for unique values of a label in the last 24h
  14. # This requires logcli installed: https://github.com/grafana/loki/releases
  15. COUNT=$(logcli query '{job="vector"}' --since=24h -o raw 2>/dev/null | \
  16. jq -r ".[] | .${label}" 2>/dev/null | sort | uniq | wc -l) || COUNT=0
  17. if [ "$COUNT" -gt "$max" ]; then
  18. echo "❌ FAILED (${COUNT} > ${max})"
  19. return 1
  20. else
  21. echo "✅ OK (${COUNT} <= ${max})"
  22. return 0
  23. fi
  24. }
  25. # Check critical low-cardinality labels (should be <<5000)
  26. FAILED=false
  27. check_label_cardinality "env" 10 || FAILED=true
  28. check_label_cardinality "app" 50 || FAILED=true
  29. check_label_cardinality "level" 10 || FAILED=true
  30. check_label_cardinality "event_class" 20 || FAILED=true
  31. check_label_cardinality "uri_group" 100 || FAILED=true
  32. check_label_cardinality "status" 10 || FAILED=true
  33. if [ "$FAILED" = true ]; then
  34. echo ""
  35. echo "❌ Cardinality check failed! Some labels exceed safe limits."
  36. echo " Review the Vector transform rules and reduce uri_group cardinality if needed."
  37. exit 1
  38. else
  39. echo ""
  40. echo "✅ All label cardinality checks passed!"
  41. echo " Safe to deploy."
  42. exit 0
  43. fi