feat: accept, store and expose client diagnostics
- V11 client_logs table with per-user storage and retention/pruning - POST/GET/DELETE /logs with batch validation, rate limit and admin access - redact bearer tokens, Authorization headers and query/JSON secrets twice - add X-Request-Id to responses and include it in the server log pattern - report client error entries immediately in the server log - scripts/logs.sh for source-side retrieval and clearing - chart knobs CLIENT_LOG_RETENTION_DAYS and CLIENT_LOG_MAX_ROWS_PER_USER
This commit is contained in:
1 parent
91312faa1c
commit
44b75c4d4d
19 files changed
+981
-5
No files matched your search
Executable
+177
@@ -0,0 +1,177 @@
|
||||
#!/usr/bin/env bash
|
||||
#
|
||||
# Fetch and print client diagnostics stored by the server.
|
||||
#
|
||||
# The password is read from LOGS_PASSWORD or prompted, and is only used for the
|
||||
# login request. The token is revoked before the script exits.
|
||||
#
|
||||
# Usage:
|
||||
# scripts/logs.sh --server https://music.example.com --user alice
|
||||
# scripts/logs.sh --server ... --user admin --target bob --level error
|
||||
# scripts/logs.sh --server ... --user alice --since 7d --limit 500
|
||||
# scripts/logs.sh --server ... --user alice --clear
|
||||
set -euo pipefail
|
||||
|
||||
SERVER=""
|
||||
USER_NAME=""
|
||||
PASSWORD="${LOGS_PASSWORD:-}"
|
||||
TARGET=""
|
||||
LEVEL=""
|
||||
DEVICE=""
|
||||
LIMIT="200"
|
||||
SINCE=""
|
||||
CLEAR=0
|
||||
RAW=0
|
||||
|
||||
usage() {
|
||||
cat <<'EOF'
|
||||
Usage: scripts/logs.sh --server URL --user NAME [options]
|
||||
|
||||
Options:
|
||||
--password PW Password (otherwise LOGS_PASSWORD or a prompt)
|
||||
--target NAME Read another user's logs (administrators only)
|
||||
--level LEVEL debug, info, warn or error
|
||||
--device ID Only this install id
|
||||
--since VALUE Epoch milliseconds or a window such as 7d / 12h
|
||||
--limit N Rows to fetch (1..1000, default 200)
|
||||
--clear Delete the selected user's logs instead of printing them
|
||||
--raw Print the JSON response unchanged
|
||||
-h, --help Show this help
|
||||
EOF
|
||||
}
|
||||
|
||||
while [[ $# -gt 0 ]]; do
|
||||
case "$1" in
|
||||
--server) SERVER="${2:?--server needs a value}"; shift 2 ;;
|
||||
--user) USER_NAME="${2:?--user needs a value}"; shift 2 ;;
|
||||
--password) PASSWORD="${2:?--password needs a value}"; shift 2 ;;
|
||||
--target) TARGET="${2:?--target needs a value}"; shift 2 ;;
|
||||
--level) LEVEL="${2:?--level needs a value}"; shift 2 ;;
|
||||
--device) DEVICE="${2:?--device needs a value}"; shift 2 ;;
|
||||
--since) SINCE="${2:?--since needs a value}"; shift 2 ;;
|
||||
--limit) LIMIT="${2:?--limit needs a value}"; shift 2 ;;
|
||||
--clear) CLEAR=1; shift ;;
|
||||
--raw) RAW=1; shift ;;
|
||||
-h|--help) usage; exit 0 ;;
|
||||
*) echo "Unknown argument: $1" >&2; usage >&2; exit 2 ;;
|
||||
esac
|
||||
done
|
||||
|
||||
if [[ -z "$SERVER" || -z "$USER_NAME" ]]; then
|
||||
usage >&2
|
||||
exit 2
|
||||
fi
|
||||
SERVER="${SERVER%/}"
|
||||
|
||||
if [[ -z "$PASSWORD" ]]; then
|
||||
read -rsp "Password for $USER_NAME: " PASSWORD
|
||||
echo
|
||||
fi
|
||||
|
||||
since_millis() {
|
||||
local value="$1"
|
||||
if [[ "$value" =~ ^([0-9]+)([dh])$ ]]; then
|
||||
python3 - "$value" <<'PY'
|
||||
import re, sys, time
|
||||
value = sys.argv[1]
|
||||
amount = int(re.match(r"([0-9]+)", value).group(1))
|
||||
unit = value[-1]
|
||||
seconds = amount * (86400 if unit == "d" else 3600)
|
||||
print(int((time.time() - seconds) * 1000))
|
||||
PY
|
||||
else
|
||||
printf '%s' "$value"
|
||||
fi
|
||||
}
|
||||
|
||||
# Keep the password out of argv: it is piped to the JSON encoder as stdin.
|
||||
login_body="$(printf '%s\n%s\n' "$USER_NAME" "$PASSWORD" | python3 -c '
|
||||
import json, sys
|
||||
user = sys.stdin.readline().rstrip("\n")
|
||||
password = sys.stdin.readline().rstrip("\n")
|
||||
print(json.dumps({
|
||||
"username": user,
|
||||
"password": password,
|
||||
"deviceName": "diagnostics-cli",
|
||||
"platform": "cli",
|
||||
}))
|
||||
')"
|
||||
|
||||
login_response="$(printf '%s' "$login_body" | curl -sS -w '\n%{http_code}' -X POST \
|
||||
"$SERVER/api/v1/auth/login" \
|
||||
-H 'Content-Type: application/json' \
|
||||
--data-binary @-)"
|
||||
login_status="$(printf '%s' "$login_response" | tail -1)"
|
||||
login_body="$(printf '%s' "$login_response" | sed '$d')"
|
||||
if [[ "$login_status" != "200" ]]; then
|
||||
echo "Login failed (HTTP $login_status): $login_body" >&2
|
||||
exit 1
|
||||
fi
|
||||
TOKEN="$(printf '%s' "$login_body" | python3 -c 'import json,sys; print(json.load(sys.stdin)["token"])')"
|
||||
|
||||
# The token stays in a 0600 curl config instead of the process list.
|
||||
AUTH_CONFIG="$(mktemp "${TMPDIR:-/tmp}/audiomanager-logs.XXXXXX")"
|
||||
PAYLOAD_FILE=""
|
||||
chmod 600 "$AUTH_CONFIG"
|
||||
printf 'header = "Authorization: Bearer %s"\n' "$TOKEN" > "$AUTH_CONFIG"
|
||||
|
||||
cleanup() {
|
||||
curl -sS -o /dev/null -X POST "$SERVER/api/v1/auth/logout" \
|
||||
-K "$AUTH_CONFIG" 2>/dev/null || true
|
||||
rm -f "$AUTH_CONFIG" "$PAYLOAD_FILE"
|
||||
}
|
||||
trap cleanup EXIT
|
||||
|
||||
query=()
|
||||
[[ -n "$LEVEL" ]] && query+=(--data-urlencode "level=$LEVEL")
|
||||
[[ -n "$DEVICE" ]] && query+=(--data-urlencode "device=$DEVICE")
|
||||
[[ -n "$TARGET" ]] && query+=(--data-urlencode "user=$TARGET")
|
||||
[[ -n "$SINCE" ]] && query+=(--data-urlencode "since=$(since_millis "$SINCE")")
|
||||
|
||||
if [[ "$CLEAR" -eq 1 ]]; then
|
||||
response="$(curl -sS -G -X DELETE "$SERVER/api/v1/logs" \
|
||||
-K "$AUTH_CONFIG" "${query[@]}")"
|
||||
printf '%s' "$response" | python3 -c 'import json,sys; print("deleted", json.load(sys.stdin)["deleted"], "entries")'
|
||||
exit 0
|
||||
fi
|
||||
|
||||
if [[ "$LIMIT" != "200" ]]; then
|
||||
query+=(--data-urlencode "limit=$LIMIT")
|
||||
fi
|
||||
|
||||
response="$(curl -sS -G "$SERVER/api/v1/logs" \
|
||||
-K "$AUTH_CONFIG" "${query[@]}")"
|
||||
|
||||
PAYLOAD_FILE="$(mktemp "${TMPDIR:-/tmp}/audiomanager-logs-payload.XXXXXX")"
|
||||
printf '%s' "$response" > "$PAYLOAD_FILE"
|
||||
|
||||
python3 - "$PAYLOAD_FILE" "$RAW" <<'PY'
|
||||
import json
|
||||
import sys
|
||||
|
||||
with open(sys.argv[1], encoding="utf-8") as handle:
|
||||
data = json.load(handle)
|
||||
if sys.argv[2] == "1":
|
||||
print(json.dumps(data, indent=2))
|
||||
raise SystemExit(0)
|
||||
|
||||
logs = data.get("logs", [])
|
||||
if not logs:
|
||||
print("No diagnostics stored for this user.")
|
||||
raise SystemExit(0)
|
||||
|
||||
print(f"{len(logs)} entr{'y' if len(logs) == 1 else 'ies'} (newest first)")
|
||||
print()
|
||||
for row in logs:
|
||||
when = row.get("createdAt") or row.get("receivedAt") or "?"
|
||||
device = f"{row.get('platform', '?')} {row.get('appVersion', '?')} {row.get('deviceId', '?')}"
|
||||
session = row.get("sessionId")
|
||||
suffix = f" session={session}" if session else ""
|
||||
print(f"{when} {row.get('level', '?').upper():5} {row.get('tag', '?')} [{device}]{suffix}")
|
||||
print(f" {row.get('message', '')}")
|
||||
detail = row.get("detail")
|
||||
if detail:
|
||||
for line in detail.splitlines():
|
||||
print(f" | {line}")
|
||||
print()
|
||||
PY
|
||||
Reference in new issue
Block a user