#!/usr/bin/env bash # Observability checks (spec NFR-004, FR-019): correlation ids (a caller's X-Request-Id is echoed and logged, an # unsafe one is replaced), one structured access-log line per API response without secrets, request counters and a # latency histogram, installation-wide gauges, and the metrics endpoint's protection (token, method, disabled state). # # Needs the container from docker-compose.yml running (name "axonasp", http://localhost:8801). The metrics checks # need AMK_METRICS_TOKEN set for the container (git-ignored .env, then docker compose up -d); without it only the # "disabled -> 404" check runs. Restarts the container once; creates and purges a throw-away tenant. # bash tests/observability.sh set -u BASE=${BASE:-http://localhost:8801/api/v1} PASS=0; FAIL=0; FAILED=(); TENANT="" prov() { MSYS_NO_PATHCONV=1 docker exec -u axonasp -e SERVER_WEB_ROOT=/opt/axonasp/site \ -e "PROV_ACTION=$1" -e "PROV_ARG1=${2:-}" -e "PROV_ARG2=${3:-}" -e "PROV_ARG3=${4:-}" -e "PROV_ARG4=${5:-}" \ axonasp ./axonasp-cli -r /opt/axonasp/site/scripts/provision.asp 2>&1 | tr -d '\015' } field() { sed -n "s/^$1: *\([^ ]*\).*/\1/p" | head -1; } check() { if [ "$2" = "$3" ]; then PASS=$((PASS+1)); else FAIL=$((FAIL+1)); FAILED+=("$1 (expected $2, got $3)"); fi; } cleanup() { [ -n "$TENANT" ] && prov purge "$TENANT" >/dev/null; } trap cleanup EXIT 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; } # access-log JSON lines written since the container started logs() { docker logs axonasp 2>&1 | grep -o '{"log":"access".*'; } TOKEN=$(MSYS_NO_PATHCONV=1 docker exec axonasp printenv AMK_METRICS_TOKEN 2>/dev/null | tr -d '\015') metrics() { curl -s -H "Authorization: Bearer $TOKEN" "$BASE/metrics"; } # value of one sample line, e.g. metric 'amk_http_requests_total{action="ApiHealthController.Live",method="GET",status="200"}' sample() { grep -F "$1 " | grep -v "^#" | head -1 | awk '{print $NF}'; } TENANT=$(prov tenant "obs" | field tenant_id) OUT=$(prov principal "$TENANT" obs-agent agent); PID=$(echo "$OUT" | field principal_id); KEY=$(echo "$OUT" | field api_key) prov grant "$PID" /obs "read,write" 1 >/dev/null NS=$(prov namespace "$TENANT" /obs | field namespace_id) [ -n "$NS" ] && [ -n "$KEY" ] || { echo "setup failed"; exit 2; } restart # ---- correlation ids and access log ------------------------------------------------------------------------------ RID="obs-test-$RANDOM-$RANDOM" H=$(curl -s -D - -o /dev/null -H "X-Request-Id: $RID" -H "Authorization: Bearer $KEY" -X POST "$BASE/remember" \ -d "{\"namespace_id\":\"$NS\",\"type\":\"semantic\",\"title\":\"Obs\",\"content\":\"secret-ish body text 4711\"}" | tr -d '\015') check "cold: write with caller request id -> 201" "201" "$(echo "$H" | sed -n 's/^HTTP\/[0-9.]* \([0-9]*\).*/\1/p' | head -1)" check "caller X-Request-Id echoed" "$RID" "$(echo "$H" | sed -n 's/^[Xx]-[Rr]equest-[Ii]d: *//p')" 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') check "unsafe X-Request-Id replaced" "yes" "$([[ "$BADH" == req-* ]] && echo yes || echo no)" 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') check "over-long X-Request-Id replaced" "yes" "$([[ "$LONGH" == req-* ]] && echo yes || echo no)" ERR=$(curl -s -H "X-Request-Id: $RID-404" -H "Authorization: Bearer $KEY" "$BASE/memories/00000000-0000-0000-0000-000000000000") check "error body carries the request id" "yes" "$(grep -q "\"request_id\":\"$RID-404\"" <<<"$ERR" && echo yes || echo no)" LINE=$(logs | grep -F "\"request_id\":\"$RID\"" | head -1) check "access log line for the write" "201 ApiMemoriesController.Remember POST $TENANT $PID" \ "$(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)" 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)" 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)" ALL=$(docker logs axonasp 2>&1) check "no API key in the logs" "no" "$(grep -qF "$KEY" <<<"$ALL" && echo yes || echo no)" check "no request body in the logs" "no" "$(grep -qF "secret-ish body text 4711" <<<"$ALL" && echo yes || echo no)" 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)" # ---- metrics ----------------------------------------------------------------------------------------------------- if [ ${#TOKEN} -lt 16 ]; then check "metrics disabled without a token -> 404" "404" "$(curl -s -o /dev/null -w '%{http_code}' "$BASE/metrics")" echo "AMK_METRICS_TOKEN is not set for the container: metrics checks skipped" else check "metrics without token -> 401" "401" "$(curl -s -o /dev/null -w '%{http_code}' "$BASE/metrics")" check "metrics with an API key -> 401" "401" "$(curl -s -o /dev/null -w '%{http_code}' -H "Authorization: Bearer $KEY" "$BASE/metrics")" check "metrics wrong method -> 405" "405" "$(curl -s -o /dev/null -w '%{http_code}' -X POST -H "Authorization: Bearer $TOKEN" "$BASE/metrics")" check "metrics content type" "text/plain; version=0.0.4; charset=utf-8" \ "$(curl -s -D - -o /dev/null -H "Authorization: Bearer $TOKEN" "$BASE/metrics" | tr -d '\015' | sed -n 's/^[Cc]ontent-[Tt]ype: *//p')" LIVE='amk_http_requests_total{action="ApiHealthController.Live",method="GET",status="200"}' B1=$(metrics | sample "$LIVE"); B1=${B1:-0} for i in 1 2 3 4 5; do curl -s -o /dev/null "$BASE/health/live"; done M=$(metrics) check "request counter counts exactly" "$((B1 + 5))" "$(echo "$M" | sample "$LIVE")" check "401s are counted" "yes" "$(echo "$M" | grep -q 'action="ApiHealthController.Metrics",method="GET",status="401"' && echo yes || echo no)" check "write is counted" "1" "$(echo "$M" | sample 'amk_http_requests_total{action="ApiMemoriesController.Remember",method="POST",status="201"}')" check "histogram +Inf bucket equals count" "yes" "$(echo "$M" | python -I -c " import sys,re t=sys.stdin.read() inf=re.search(r'amk_http_request_duration_ms_bucket\{action=\"ApiHealthController.Live\",le=\"\+Inf\"\} (\d+)',t).group(1) cnt=re.search(r'amk_http_request_duration_ms_count\{action=\"ApiHealthController.Live\"\} (\d+)',t).group(1) b=[int(x) for x in re.findall(r'amk_http_request_duration_ms_bucket\{action=\"ApiHealthController.Live\",le=\"[0-9]+\"\} (\d+)',t)] print('yes' if inf==cnt and b==sorted(b) and b[-1]<=int(inf) else 'no')")" check "gauges: memories" "yes" "$(echo "$M" | grep -q '^amk_memories{status="active"} [0-9]' && echo yes || echo no)" curl -s -o /dev/null -H "Authorization: Bearer $KEY" -X POST "$BASE/recall" -d '{"query":"zzqx unmatched term"}' M2=$(metrics) R1=$(echo "$M" | sample 'amk_retrieval_empty_total{operation="recall"}'); R2=$(echo "$M2" | sample 'amk_retrieval_empty_total{operation="recall"}') check "retrieval metrics: empty recall counted" "$(( ${R1:-0} + 1 ))" "${R2:-0}" check "metrics carry no tenant ids" "no" "$(grep -qF "$TENANT" <<<"$M2" && echo yes || echo no)" check "metrics every line well-formed" "0" "$(echo "$M2" | grep -v '^# \(HELP\|TYPE\) ' | grep -cvE '^[a-z_]+(\{[a-z_]+="[^"]*"(,[a-z_]+="[^"]*")*\})? -?[0-9.e+]+$')" S1=$(echo "$M2" | sample amk_process_start_time_seconds) sleep 1; restart M3=$(metrics) check "restart resets counters" "1" "$(echo "$M3" | grep -c '^amk_http_requests_total')" check "restart moves the start time" "yes" "$([ "$(echo "$M3" | sample amk_process_start_time_seconds)" -gt "${S1:-0}" ] && echo yes || echo no)" fi echo "passed: $PASS failed: $FAIL" for f in "${FAILED[@]:-}"; do [ -n "$f" ] && echo " FAIL: $f"; done [ "$FAIL" -eq 0 ]