diff --git a/api/docs/ERROR_MONITORING_PUBLIC_API.md b/api/docs/ERROR_MONITORING_PUBLIC_API.md index 39334dc..5ba1c88 100644 --- a/api/docs/ERROR_MONITORING_PUBLIC_API.md +++ b/api/docs/ERROR_MONITORING_PUBLIC_API.md @@ -51,6 +51,8 @@ Alias: `GET /v1/paperclip/recent-errors` Each row includes CRM-compatible fields. For non-resolved incidents, `status` is always `"open"` (including `investigating`) so Paperclip `error_loop` continues to treat rows as open. +**`occurred_at`:** ISO-8601 timestamp of the **latest** occurrence returned for that incident (same row as `unique_id`). Used by ops scripts to verify “no new events in the last *N* hours” after a deploy (see repo runbook `docs/RUNBOOK_REPORTER_CRM_TASKS_DUE_TIME.md` §6). + **`stack_trace`:** Non-empty for every row: missing client stacks are synthesized on ingest; legacy DB rows with null/blank `stack_trace` get the same synthesis when serializing this endpoint (BIT-1008). **Example** diff --git a/api/src/routes/public/recentErrors.ts b/api/src/routes/public/recentErrors.ts index ff15abc..d9be4b3 100644 --- a/api/src/routes/public/recentErrors.ts +++ b/api/src/routes/public/recentErrors.ts @@ -38,6 +38,24 @@ function parseJsonObject(raw: string | null): Record | null { } } +function toIsoOccurredAt(value: unknown): string { + if (value == null) { + return ''; + } + if (value instanceof Date) { + return value.toISOString(); + } + const s = String(value).trim(); + if (s === '') { + return ''; + } + const t = Date.parse(s); + if (Number.isNaN(t)) { + return s; + } + return new Date(t).toISOString(); +} + function extractFileLine(ctx: Record | null): { file_path: string; line_number: string } { if (!ctx) { return { file_path: '', line_number: '' }; @@ -205,6 +223,7 @@ async function handleRecentErrors(request: FastifyRequest, reply: FastifyReply): unique_id: String(row.occurrence_id ?? row.incident_id ?? ''), project_slug: String(row.project_slug ?? '').toLowerCase(), environment: String(row.environment ?? 'production'), + occurred_at: toIsoOccurredAt(row.occurred_at), }; }); diff --git a/docs/HTTP_CONTRACTS.md b/docs/HTTP_CONTRACTS.md index e1e92cb..b210f8d 100644 --- a/docs/HTTP_CONTRACTS.md +++ b/docs/HTTP_CONTRACTS.md @@ -64,12 +64,15 @@ Alias: `GET /v1/paperclip/recent-errors` (same handler). "severity": "high", "unique_id": "12345", "project_slug": "crm", - "environment": "production" + "environment": "production", + "occurred_at": "2026-04-05T12:34:56.789Z" } ] } ``` +**`occurred_at`:** ISO-8601 time of the latest occurrence represented in the row (same as `unique_id` when that id is an occurrence id). Exposed for ops automation (e.g. post-deploy “no new events in *N* hours” checks). + **`stack_trace`:** The hub returns a **non-empty** string for every row. Ingest stores a synthesized stack when the reporter omits one; for legacy occurrences whose column is still null/blank, `recent-errors` applies the same synthesis at read time using `message` and `context_json` (BIT-1008). ```bash diff --git a/docs/RUNBOOK_REPORTER_CRM_TASKS_DUE_TIME.md b/docs/RUNBOOK_REPORTER_CRM_TASKS_DUE_TIME.md index e43abbc..0c55b1c 100644 --- a/docs/RUNBOOK_REPORTER_CRM_TASKS_DUE_TIME.md +++ b/docs/RUNBOOK_REPORTER_CRM_TASKS_DUE_TIME.md @@ -114,6 +114,18 @@ Com token (`Authorization: token …`) com permissão de leitura sobre o reposit **Nota:** em algumas versões de Gitea, `GET …/hooks/{id}/deliveries` devolve **404**; nesses casos a evidência de *Recent Deliveries* (HTTP 2xx) continua a ser obtida na **UI** ou nos logs do autodeploy no destino. +### 6.2.2 Hub — recência sem UI (ERRSIG `90a47beec0d7`) + +Com `ERROR_RESOLVE_API_KEY` (ou `ERROR_REPORTING_API_KEY`) e `ER_API_BASE` apontando para produção: + +```bash +ERROR_RESOLVE_API_KEY=… ./scripts/crm/verify_pmx_bet_due_time_deploy.sh hub +``` + +- Por omissão, `HUB_DUE_TIME_STABILITY_SEC=7200` (2h) e `HUB_DUE_TIME_HUB_QUERY=90a47beec0d7` (fingerprint no título/mensagem). +- A resposta `GET …/recent-errors` inclui `occurred_at` (última ocorrência por incidente) para comparar com o relógio local do operador. +- Sem chave no ambiente, o modo `hub` **sai 0** e imprime aviso (runners CI sem vault). + ### 6.3 Confirmar deploy no guest CRM (VPN / SSH) **Sem VPN**, muitos runners Paperclip **não têm rota** para o LXC interno — ver nota em [CRM `crm.pmx.bet` — PHP deploy](https://git.bitreport.pt/BitReport.pt/paperclip/src/branch/main/docs/runbooks/CRM_PMX_BET_PHP_MIGRATE_DEPLOY.md). Com acesso a **`10.10.20.22`**: diff --git a/scripts/crm/verify_pmx_bet_due_time_deploy.sh b/scripts/crm/verify_pmx_bet_due_time_deploy.sh index e4d00c9..19c9d43 100755 --- a/scripts/crm/verify_pmx_bet_due_time_deploy.sh +++ b/scripts/crm/verify_pmx_bet_due_time_deploy.sh @@ -1,10 +1,11 @@ #!/usr/bin/env bash -# Gitea + runtime checks for CRM `due_time` normalization on crm.pmx.bet. +# Gitea + runtime + hub checks for CRM `due_time` normalization on crm.pmx.bet. # See docs/RUNBOOK_REPORTER_CRM_TASKS_DUE_TIME.md §6 (BIT-1048 / BIT-1049 / BIT-887). # # Usage: # ./scripts/crm/verify_pmx_bet_due_time_deploy.sh gitea # CRM_PMX_SSH=deploy@10.10.20.22 ./scripts/crm/verify_pmx_bet_due_time_deploy.sh runtime +# ERROR_RESOLVE_API_KEY=… ./scripts/crm/verify_pmx_bet_due_time_deploy.sh hub set -euo pipefail readonly CRM_REPO_URL="${CRM_REPO_URL:-https://git.bitreport.pt/BitReport.pt/CRM_Project.git}" @@ -43,11 +44,71 @@ cmd_runtime() { "set -euo pipefail; cd '${RUNTIME_GIT_DIR}' && git fetch -q origin && git rev-parse HEAD && git merge-base --is-ancestor ${SHA_E0} HEAD && git merge-base --is-ancestor ${SHA_30} HEAD && echo ancestors_ok" } +cmd_hub() { + command -v curl >/dev/null 2>&1 || { + echo "verify_pmx_bet_due_time_deploy: hub: curl is required" >&2 + exit 1 + } + command -v jq >/dev/null 2>&1 || { + echo "verify_pmx_bet_due_time_deploy: hub: jq is required" >&2 + exit 1 + } + + local api_key base resp data_count latest_ts latest_epoch now threshold + api_key="${ERROR_RESOLVE_API_KEY:-${ERROR_REPORTING_API_KEY:-}}" + base="${ER_API_BASE:-https://errors.bitreport.pt}" + readonly window_sec="${HUB_DUE_TIME_STABILITY_SEC:-7200}" + readonly hub_q="${HUB_DUE_TIME_HUB_QUERY:-90a47beec0d7}" + + if [[ -z "${api_key}" ]]; then + echo "verify_pmx_bet_due_time_deploy: hub: ERROR_RESOLVE_API_KEY (or ERROR_REPORTING_API_KEY) not set — skipping recency check" >&2 + exit 0 + fi + + resp="$(curl -sfG "${base}/api/error-monitoring/recent-errors" \ + --data-urlencode "api_key=${api_key}" \ + --data-urlencode "status=open,investigating" \ + --data-urlencode "limit=500" \ + --data-urlencode "project_slug=crm" \ + --data-urlencode "q=${hub_q}" \ + --max-time 30)" || { + echo "verify_pmx_bet_due_time_deploy: hub: recent-errors request failed" >&2 + exit 1 + } + + data_count="$(echo "${resp}" | jq '.data | length')" + echo "verify_pmx_bet_due_time_deploy: hub: open/investigating rows (project=crm, q=${hub_q}): ${data_count}" + + if [[ "${data_count}" -eq 0 ]]; then + echo "verify_pmx_bet_due_time_deploy: hub: OK — no matching open incidents" + return 0 + fi + + latest_ts="$(echo "${resp}" | jq -r '[.data[].occurred_at | select(. != null and . != "")] | max // empty')" + if [[ -z "${latest_ts}" ]]; then + echo "verify_pmx_bet_due_time_deploy: hub: warning: rows lack occurred_at (deploy hub API with recent-errors change, then retry)" >&2 + exit 0 + fi + + latest_epoch="$(echo "${resp}" | jq '[.data[].occurred_at | select(. != null and . != "")] | map(fromdateiso8601) | max')" + now="$(date +%s)" + threshold=$((now - window_sec)) + + if ((latest_epoch > threshold)); then + echo "verify_pmx_bet_due_time_deploy: hub: FAIL — latest occurrence ${latest_ts} is within the last ${window_sec}s (open incidents still receiving recent traffic)" >&2 + echo "${resp}" | jq -r '.data[] | "[\(.unique_id)] \(.occurred_at // "?") \(.message // .error_message // "")"' >&2 + exit 1 + fi + + echo "verify_pmx_bet_due_time_deploy: hub: OK — latest matching occurrence ${latest_ts} is older than ${window_sec}s (no recent recurrence by this filter)" +} + case "${1:-}" in gitea) cmd_gitea ;; runtime) cmd_runtime ;; + hub) cmd_hub ;; *) - echo "usage: $0 gitea|runtime" >&2 + echo "usage: $0 gitea|runtime|hub" >&2 exit 1 ;; esac