Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

100 Zeilen
8.0KB

  1. #!/usr/bin/env bash
  2. # Observability checks (spec NFR-004, FR-019): correlation ids (a caller's X-Request-Id is echoed and logged, an
  3. # unsafe one is replaced), one structured access-log line per API response without secrets, request counters and a
  4. # latency histogram, installation-wide gauges, and the metrics endpoint's protection (token, method, disabled state).
  5. #
  6. # Needs the container from docker-compose.yml running (name "axonasp", http://localhost:8801). The metrics checks
  7. # need AMK_METRICS_TOKEN set for the container (git-ignored .env, then docker compose up -d); without it only the
  8. # "disabled -> 404" check runs. Restarts the container once; creates and purges a throw-away tenant.
  9. # bash tests/observability.sh
  10. set -u
  11. BASE=${BASE:-http://localhost:8801/api/v1}
  12. PASS=0; FAIL=0; FAILED=(); TENANT=""
  13. prov() {
  14. MSYS_NO_PATHCONV=1 docker exec -u axonasp -e SERVER_WEB_ROOT=/opt/axonasp/site \
  15. -e "PROV_ACTION=$1" -e "PROV_ARG1=${2:-}" -e "PROV_ARG2=${3:-}" -e "PROV_ARG3=${4:-}" -e "PROV_ARG4=${5:-}" \
  16. axonasp ./axonasp-cli -r /opt/axonasp/site/scripts/provision.asp 2>&1 | tr -d '\015'
  17. }
  18. field() { sed -n "s/^$1: *\([^ ]*\).*/\1/p" | head -1; }
  19. check() { if [ "$2" = "$3" ]; then PASS=$((PASS+1)); else FAIL=$((FAIL+1)); FAILED+=("$1 (expected $2, got $3)"); fi; }
  20. cleanup() { [ -n "$TENANT" ] && prov purge "$TENANT" >/dev/null; }
  21. trap cleanup EXIT
  22. restart() { docker restart axonasp >/dev/null; for i in $(seq 1 30); do curl -s -o /dev/null "$BASE/health/live" && break; sleep 1; done; }
  23. # access-log JSON lines written since the container started
  24. logs() { docker logs axonasp 2>&1 | grep -o '{"log":"access".*'; }
  25. TOKEN=$(MSYS_NO_PATHCONV=1 docker exec axonasp printenv AMK_METRICS_TOKEN 2>/dev/null | tr -d '\015')
  26. metrics() { curl -s -H "Authorization: Bearer $TOKEN" "$BASE/metrics"; }
  27. # value of one sample line, e.g. metric 'amk_http_requests_total{action="ApiHealthController.Live",method="GET",status="200"}'
  28. sample() { grep -F "$1 " | grep -v "^#" | head -1 | awk '{print $NF}'; }
  29. TENANT=$(prov tenant "obs" | field tenant_id)
  30. OUT=$(prov principal "$TENANT" obs-agent agent); PID=$(echo "$OUT" | field principal_id); KEY=$(echo "$OUT" | field api_key)
  31. prov grant "$PID" /obs "read,write" 1 >/dev/null
  32. NS=$(prov namespace "$TENANT" /obs | field namespace_id)
  33. [ -n "$NS" ] && [ -n "$KEY" ] || { echo "setup failed"; exit 2; }
  34. restart
  35. # ---- correlation ids and access log ------------------------------------------------------------------------------
  36. RID="obs-test-$RANDOM-$RANDOM"
  37. H=$(curl -s -D - -o /dev/null -H "X-Request-Id: $RID" -H "Authorization: Bearer $KEY" -X POST "$BASE/remember" \
  38. -d "{\"namespace_id\":\"$NS\",\"type\":\"semantic\",\"title\":\"Obs\",\"content\":\"secret-ish body text 4711\"}" | tr -d '\015')
  39. check "cold: write with caller request id -> 201" "201" "$(echo "$H" | sed -n 's/^HTTP\/[0-9.]* \([0-9]*\).*/\1/p' | head -1)"
  40. check "caller X-Request-Id echoed" "$RID" "$(echo "$H" | sed -n 's/^[Xx]-[Rr]equest-[Ii]d: *//p')"
  41. BADH=$(curl -s -D - -o /dev/null -H 'X-Request-Id: bad id with spaces' "$BASE/health/live" | tr -d '\015' | sed -n 's/^[Xx]-[Rr]equest-[Ii]d: *//p')
  42. check "unsafe X-Request-Id replaced" "yes" "$([[ "$BADH" == req-* ]] && echo yes || echo no)"
  43. LONGH=$(curl -s -D - -o /dev/null -H "X-Request-Id: $(printf 'a%.0s' $(seq 1 80))" "$BASE/health/live" | tr -d '\015' | sed -n 's/^[Xx]-[Rr]equest-[Ii]d: *//p')
  44. check "over-long X-Request-Id replaced" "yes" "$([[ "$LONGH" == req-* ]] && echo yes || echo no)"
  45. ERR=$(curl -s -H "X-Request-Id: $RID-404" -H "Authorization: Bearer $KEY" "$BASE/memories/00000000-0000-0000-0000-000000000000")
  46. check "error body carries the request id" "yes" "$(grep -q "\"request_id\":\"$RID-404\"" <<<"$ERR" && echo yes || echo no)"
  47. LINE=$(logs | grep -F "\"request_id\":\"$RID\"" | head -1)
  48. check "access log line for the write" "201 ApiMemoriesController.Remember POST $TENANT $PID" \
  49. "$(python -I -c "import sys,json;d=json.loads(sys.argv[1]);print(d['status'],d['action'],d['method'],d['tenant_id'],d['principal_id'])" "$LINE" 2>&1)"
  50. check "access log has a duration" "yes" "$(python -I -c "import sys,json;d=json.loads(sys.argv[1]);print('yes' if d['duration_ms']>=0 else 'no')" "$LINE" 2>&1)"
  51. check "404 logged with its request id" "404" "$(logs | grep -F "\"request_id\":\"$RID-404\"" | python -I -c "import sys,json;print(json.loads(sys.stdin.readline())['status'])" 2>&1)"
  52. ALL=$(docker logs axonasp 2>&1)
  53. check "no API key in the logs" "no" "$(grep -qF "$KEY" <<<"$ALL" && echo yes || echo no)"
  54. check "no request body in the logs" "no" "$(grep -qF "secret-ish body text 4711" <<<"$ALL" && echo yes || echo no)"
  55. check "unauthenticated request logged without identity" "null" "$(logs | grep -F '"action":"ApiHealthController.Live"' | tail -1 | python -I -c "import sys,json;print(json.dumps(json.loads(sys.stdin.readline())['tenant_id']))" 2>&1)"
  56. # ---- metrics -----------------------------------------------------------------------------------------------------
  57. if [ ${#TOKEN} -lt 16 ]; then
  58. check "metrics disabled without a token -> 404" "404" "$(curl -s -o /dev/null -w '%{http_code}' "$BASE/metrics")"
  59. echo "AMK_METRICS_TOKEN is not set for the container: metrics checks skipped"
  60. else
  61. check "metrics without token -> 401" "401" "$(curl -s -o /dev/null -w '%{http_code}' "$BASE/metrics")"
  62. check "metrics with an API key -> 401" "401" "$(curl -s -o /dev/null -w '%{http_code}' -H "Authorization: Bearer $KEY" "$BASE/metrics")"
  63. check "metrics wrong method -> 405" "405" "$(curl -s -o /dev/null -w '%{http_code}' -X POST -H "Authorization: Bearer $TOKEN" "$BASE/metrics")"
  64. check "metrics content type" "text/plain; version=0.0.4; charset=utf-8" \
  65. "$(curl -s -D - -o /dev/null -H "Authorization: Bearer $TOKEN" "$BASE/metrics" | tr -d '\015' | sed -n 's/^[Cc]ontent-[Tt]ype: *//p')"
  66. LIVE='amk_http_requests_total{action="ApiHealthController.Live",method="GET",status="200"}'
  67. B1=$(metrics | sample "$LIVE"); B1=${B1:-0}
  68. for i in 1 2 3 4 5; do curl -s -o /dev/null "$BASE/health/live"; done
  69. M=$(metrics)
  70. check "request counter counts exactly" "$((B1 + 5))" "$(echo "$M" | sample "$LIVE")"
  71. check "401s are counted" "yes" "$(echo "$M" | grep -q 'action="ApiHealthController.Metrics",method="GET",status="401"' && echo yes || echo no)"
  72. check "write is counted" "1" "$(echo "$M" | sample 'amk_http_requests_total{action="ApiMemoriesController.Remember",method="POST",status="201"}')"
  73. check "histogram +Inf bucket equals count" "yes" "$(echo "$M" | python -I -c "
  74. import sys,re
  75. t=sys.stdin.read()
  76. inf=re.search(r'amk_http_request_duration_ms_bucket\{action=\"ApiHealthController.Live\",le=\"\+Inf\"\} (\d+)',t).group(1)
  77. cnt=re.search(r'amk_http_request_duration_ms_count\{action=\"ApiHealthController.Live\"\} (\d+)',t).group(1)
  78. b=[int(x) for x in re.findall(r'amk_http_request_duration_ms_bucket\{action=\"ApiHealthController.Live\",le=\"[0-9]+\"\} (\d+)',t)]
  79. print('yes' if inf==cnt and b==sorted(b) and b[-1]<=int(inf) else 'no')")"
  80. check "gauges: memories" "yes" "$(echo "$M" | grep -q '^amk_memories{status="active"} [0-9]' && echo yes || echo no)"
  81. curl -s -o /dev/null -H "Authorization: Bearer $KEY" -X POST "$BASE/recall" -d '{"query":"zzqx unmatched term"}'
  82. M2=$(metrics)
  83. R1=$(echo "$M" | sample 'amk_retrieval_empty_total{operation="recall"}'); R2=$(echo "$M2" | sample 'amk_retrieval_empty_total{operation="recall"}')
  84. check "retrieval metrics: empty recall counted" "$(( ${R1:-0} + 1 ))" "${R2:-0}"
  85. check "metrics carry no tenant ids" "no" "$(grep -qF "$TENANT" <<<"$M2" && echo yes || echo no)"
  86. check "metrics every line well-formed" "0" "$(echo "$M2" | grep -v '^# \(HELP\|TYPE\) ' | grep -cvE '^[a-z_]+(\{[a-z_]+="[^"]*"(,[a-z_]+="[^"]*")*\})? -?[0-9.e+]+$')"
  87. S1=$(echo "$M2" | sample amk_process_start_time_seconds)
  88. sleep 1; restart
  89. M3=$(metrics)
  90. check "restart resets counters" "1" "$(echo "$M3" | grep -c '^amk_http_requests_total')"
  91. check "restart moves the start time" "yes" "$([ "$(echo "$M3" | sample amk_process_start_time_seconds)" -gt "${S1:-0}" ] && echo yes || echo no)"
  92. fi
  93. echo "passed: $PASS failed: $FAIL"
  94. for f in "${FAILED[@]:-}"; do [ -n "$f" ] && echo " FAIL: $f"; done
  95. [ "$FAIL" -eq 0 ]

Powered by TurnKey Linux.