#!/bin/bash # Cardinality Gate - CI stage validation per Log.md # Run this script in CI before deploying Vector to ensure labels stay within cardinality limits set -euo pipefail LOKI_ENDPOINT="${LOKI_ENDPOINT:-http://loki:3100}" MAX_CARDINALITY=5000 echo "🔍 Checking label cardinality limits for Vector-Loki deployment..." # Helper function to get cardinality from Loki check_label_cardinality() { local label=$1 local max=$2 echo -n " Checking ${label}... " # Query Loki for unique values of a label in the last 24h # This requires logcli installed: https://github.com/grafana/loki/releases COUNT=$(logcli query '{job="vector"}' --since=24h -o raw 2>/dev/null | \ jq -r ".[] | .${label}" 2>/dev/null | sort | uniq | wc -l) || COUNT=0 if [ "$COUNT" -gt "$max" ]; then echo "❌ FAILED (${COUNT} > ${max})" return 1 else echo "✅ OK (${COUNT} <= ${max})" return 0 fi } # Check critical low-cardinality labels (should be <<5000) FAILED=false check_label_cardinality "env" 10 || FAILED=true check_label_cardinality "app" 50 || FAILED=true check_label_cardinality "level" 10 || FAILED=true check_label_cardinality "event_class" 20 || FAILED=true check_label_cardinality "uri_group" 100 || FAILED=true check_label_cardinality "status" 10 || FAILED=true if [ "$FAILED" = true ]; then echo "" echo "❌ Cardinality check failed! Some labels exceed safe limits." echo " Review the Vector transform rules and reduce uri_group cardinality if needed." exit 1 else echo "" echo "✅ All label cardinality checks passed!" echo " Safe to deploy." exit 0 fi