feat(ops): tooling CLI + runbook canónico de triagem

- scripts/er-cli.sh: CLI operacional sem UI com comandos list-open,
  export, resolve, resolve-by-pattern e purge; idempotente, dry-run
  em resolve-by-pattern e purge, autenticação via ERROR_RESOLVE_API_KEY
- docs/RUNBOOK_TRIAGE.md: runbook completo de triagem, resolução, deploy
  e fecho com checklist de validação pós-deploy e tabela de escalada

Paperclip-Issue: BIT-730
Paperclip-Run: f06292f3-5ab7-4715-9b61-6dee07a47ad7
This commit is contained in:
David Santos committed 2026-04-02 18:03:23 +00:00
1 parent de68f68103
commit 5c97326d11
2 files changed
+529

No files matched your search

+270
View File
@@ -0,0 +1,270 @@
#!/usr/bin/env bash
# er-cli.sh — BitReport Error Reporting operational CLI
# Usage: er-cli.sh <command> [options]
#
# Commands:
# list-open List open/investigating incidents
# export Export incidents to stdout (JSON or CSV)
# resolve Resolve a single incident by ID
# resolve-by-pattern Bulk-resolve incidents matching a message substring
# purge Purge all resolved incidents (destructive; use --dry-run first)
#
# Environment:
# ERROR_RESOLVE_API_KEY Required for mutations and reads. Also accepts ERROR_REPORTING_API_KEY.
# ER_API_BASE Base URL for the hub API (default: https://errors.bitreport.pt).
#
# Examples:
# er-cli.sh list-open
# er-cli.sh list-open --project crm --severity critical
# er-cli.sh export --state open --format csv > open-incidents.csv
# er-cli.sh resolve 12345 --note "Fixed in deploy abc"
# er-cli.sh resolve-by-pattern "Connection refused" --dry-run
# er-cli.sh purge --dry-run
# er-cli.sh purge
set -euo pipefail
# ── Config ────────────────────────────────────────────────────────────────────
_API_BASE="${ER_API_BASE:-https://errors.bitreport.pt}"
_API_KEY="${ERROR_RESOLVE_API_KEY:-${ERROR_REPORTING_API_KEY:-}}"
# ── Helpers ───────────────────────────────────────────────────────────────────
_die() { echo "error: $*" >&2; exit 1; }
_require_apikey() {
[[ -n "${_API_KEY}" ]] || _die "ERROR_RESOLVE_API_KEY (or ERROR_REPORTING_API_KEY) not set."
}
_require_cmd() { command -v "$1" >/dev/null 2>&1 || _die "'$1' is required but not found in PATH."; }
_require_cmd curl
_require_cmd jq
_curl_get() {
local url="$1"; shift
curl -sf --max-time 30 "$url" "$@"
}
_curl_post() {
local url="$1"; shift
local body="$1"; shift
curl -sf --max-time 30 -X POST "$url" \
-H "Content-Type: application/json" \
"$@" \
-d "$body"
}
_json_to_csv() {
jq -r '
if type == "array" then
(.[0] | keys_unsorted) as $keys |
($keys | @csv),
(.[] | [.[$keys[]]] | @csv)
else
"No data"
end
'
}
# ── Commands ──────────────────────────────────────────────────────────────────
cmd_list_open() {
_require_apikey
local project="" severity="" limit="100" q=""
while [[ $# -gt 0 ]]; do
case "$1" in
--project) project="$2"; shift 2 ;;
--severity) severity="$2"; shift 2 ;;
--limit) limit="$2"; shift 2 ;;
--q) q="$2"; shift 2 ;;
*) _die "Unknown option: $1" ;;
esac
done
local url="${_API_BASE}/api/error-monitoring/recent-errors?api_key=${_API_KEY}&status=open,investigating&limit=${limit}"
[[ -n "${project}" ]] && url+="&project_slug=${project}"
[[ -n "${severity}" ]] && url+="&severity=${severity}"
[[ -n "${q}" ]] && url+="&q=$(python3 -c "import urllib.parse,sys; print(urllib.parse.quote(sys.argv[1]))" "${q}" 2>/dev/null || printf '%s' "$q")"
local resp
resp="$(_curl_get "${url}")"
local rows
rows="$(echo "${resp}" | jq '.data // []')"
local count
count="$(echo "${rows}" | jq 'length')"
echo "=== Open incidents (${count}) ===" >&2
echo "${rows}" | jq -r '
.[] |
"[" + (.unique_id // .incident_id // "?") + "] " +
(.project_slug // "-") + " | " +
(.severity // "-") + " | " +
(.title // .message // "-")
'
}
cmd_export() {
_require_apikey
local state="open,investigating" project="" limit="10000" fmt="json"
while [[ $# -gt 0 ]]; do
case "$1" in
--state) state="$2"; shift 2 ;;
--project) project="$2"; shift 2 ;;
--limit) limit="$2"; shift 2 ;;
--format) fmt="$2"; shift 2 ;;
*) _die "Unknown option: $1" ;;
esac
done
# --state all → fetch open+investigating+resolved+ignored
if [[ "${state}" == "all" ]]; then
state="open,investigating,resolved,ignored"
fi
local url="${_API_BASE}/api/error-monitoring/recent-errors?api_key=${_API_KEY}&status=${state}&limit=${limit}"
[[ -n "${project}" ]] && url+="&project_slug=${project}"
local resp
resp="$(_curl_get "${url}")"
local rows
rows="$(echo "${resp}" | jq '.data // []')"
if [[ "${fmt}" == "csv" ]]; then
echo "${rows}" | _json_to_csv
else
echo "${rows}" | jq '.'
fi
}
cmd_resolve() {
_require_apikey
[[ $# -ge 1 ]] || _die "Usage: resolve <incident-id> [--note TEXT] [--assigned-to USER]"
local uid="$1"; shift
local note="Resolved via er-cli" assigned=""
while [[ $# -gt 0 ]]; do
case "$1" in
--note) note="$2"; shift 2 ;;
--assigned-to) assigned="$2"; shift 2 ;;
*) _die "Unknown option: $1" ;;
esac
done
[[ "${uid}" =~ ^[0-9]+$ ]] || _die "incident-id must be numeric: ${uid}"
local body
body="$(jq -cn \
--arg api_key "${_API_KEY}" \
--arg unique_id "${uid}" \
--arg resolution_notes "${note}" \
--arg assigned_to "${assigned}" \
'{api_key: $api_key, unique_id: $unique_id, resolution_notes: $resolution_notes} |
if $assigned_to != "" then . + {assigned_to: $assigned_to} else . end')"
local resp
resp="$(_curl_post "${_API_BASE}/api/error-monitoring/resolve" "${body}")"
echo "${resp}" | jq '.'
echo "${resp}" | jq -e '.success == true' >/dev/null && echo "✓ Resolved ${uid}" >&2 || true
}
cmd_resolve_by_pattern() {
_require_apikey
[[ $# -ge 1 ]] || _die "Usage: resolve-by-pattern <pattern> [--note TEXT] [--assigned-to USER] [--dry-run]"
local pattern="$1"; shift
local note="Bulk resolved by pattern via er-cli" assigned="" dry_run=false
while [[ $# -gt 0 ]]; do
case "$1" in
--note) note="$2"; shift 2 ;;
--assigned-to) assigned="$2"; shift 2 ;;
--dry-run) dry_run=true; shift ;;
*) _die "Unknown option: $1" ;;
esac
done
if "${dry_run}"; then
# Dry run: list matching open incidents without mutating
_require_apikey
local q_enc
q_enc="$(python3 -c "import urllib.parse,sys; print(urllib.parse.quote(sys.argv[1]))" "${pattern}" 2>/dev/null || printf '%s' "${pattern}")"
local url="${_API_BASE}/api/error-monitoring/recent-errors?api_key=${_API_KEY}&status=open,investigating&limit=10000&q=${q_enc}"
local resp rows count
resp="$(_curl_get "${url}")"
rows="$(echo "${resp}" | jq '.data // []')"
count="$(echo "${rows}" | jq 'length')"
echo "=== DRY RUN — incidents that would be resolved (${count}) ===" >&2
echo "${rows}" | jq -r '
.[] |
"[" + (.unique_id // .incident_id // "?") + "] " +
(.project_slug // "-") + " | " +
(.title // .message // "-")
'
return
fi
local body
body="$(jq -cn \
--arg api_key "${_API_KEY}" \
--arg message_pattern "${pattern}" \
--arg resolution_notes "${note}" \
--arg assigned_to "${assigned}" \
'{api_key: $api_key, message_pattern: $message_pattern, resolution_notes: $resolution_notes} |
if $assigned_to != "" then . + {assigned_to: $assigned_to} else . end')"
local resp
resp="$(_curl_post "${_API_BASE}/api/error-monitoring/bulk-resolve" "${body}")"
echo "${resp}" | jq '.'
echo "${resp}" | jq -r '"✓ Resolved " + (.data.resolved | tostring) + " incidents"' >&2 || true
}
cmd_purge() {
_require_apikey
local dry_run=false
while [[ $# -gt 0 ]]; do
case "$1" in
--dry-run) dry_run=true; shift ;;
*) _die "Unknown option: $1" ;;
esac
done
local body
body="$(jq -cn \
--arg api_key "${_API_KEY}" \
--argjson dry_run "${dry_run}" \
'{api_key: $api_key, dry_run: $dry_run}')"
local resp
resp="$(_curl_post "${_API_BASE}/api/error-monitoring/purge-resolved" "${body}")"
echo "${resp}" | jq '.'
if "${dry_run}"; then
echo "${resp}" | jq -r '"DRY RUN — would delete " + (.data.would_delete | tostring) + " resolved incidents"' >&2 || true
else
echo "${resp}" | jq -r '"✓ Purged " + (.data.deleted | tostring) + " incidents"' >&2 || true
fi
}
# ── Dispatch ──────────────────────────────────────────────────────────────────
usage() {
sed -n '/^# Usage:/,/^[^#]/p' "$0" | grep '^#' | sed 's/^# \?//'
exit 1
}
[[ $# -ge 1 ]] || usage
cmd="$1"; shift
case "${cmd}" in
list-open) cmd_list_open "$@" ;;
export) cmd_export "$@" ;;
resolve) cmd_resolve "$@" ;;
resolve-by-pattern) cmd_resolve_by_pattern "$@" ;;
purge) cmd_purge "$@" ;;
help|--help|-h) usage ;;
*) _die "Unknown command: ${cmd}. Run 'er-cli.sh help' for usage." ;;
esac