feat(metrics): module counters for rate limits, cache, IdP, M-Pesa, check-ins and DB errors (#60) #170
No reviewers
Labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
primatekuntech/kws!170
Loading…
Reference in a new issue
No description provided.
Delete branch "feat/api-counters-60"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Step 2b of #60 (docs/05-observability.md): counters inside the API and worker, so the dashboards can show where a slowdown or a money problem is.
What is counted (only when
KWS_METRICS_ADDRis set)kws_rate_limited_totalclass: every 429 the limiter writes (not theauth_fail429, whichauthwrites)kws_cache_requests_total,kws_cache_version_poll_failures_totalresult=hit/misskws_auth_idp_unavailable_totalkws_mpesa_callback_totaloutcome:settled,replay,refund_due,no_payment,no_attempt,unverified,invalid,too_large,errorkws_mpesa_push_totaloutcome:accepted,rejected,maybe_sent,not_sentkws_checkin_totalaction,result(okor the fixed refusal code)kws_db_errors_totalkind:timeout,lock_wait,deadlock,serializationHow it is built
Limiter.OnDeniedCache.Observeauth.WithUnavailableHookCallbackHandler.OnOutcomecheckin.Handler.OnScanStkHandler'scountmetricshands out each callback, or nil when metrics are off. The fixed label values start at 0, so an alert on> 0has a series to watch.QueryTracerandBatchTracer, set throughpostgres.Config.Tracer. That means every query is seen once, and so is every failed batch (the seat locks and checkout run as batches).NewAPIHandler,registerJobsandmpesa.StkHandlereach take one more argument; tests pass nil.Also fixed
TestABrokenSnapshotKeepsTheLastGoodValuesAndIsCounted(from PR 165) failed about 2 times in 15. Prometheus collects metrics in parallel, so the snapshot error counter could be read before the failure in the same scrape was counted. The business collector now emits the counter itself, after it reads. It passed 30 of 30 under-race.Deviations from doc 05 (in ADR 0036)
kws_db_errors_total, with noconnkind (pool waits are already inkws_db_pool_*)kws_auth_idp_unavailable_totalnot_sentbypassresult, and the doc 05 §4.2 sales, payments and email counters.Checks
make cover: 91.5 % total (minimum 80 %). Touched packages:make lint: 0 issues. The touched packages pass under-race.Not done: alert rules, dashboards and the monitoring Compose files (steps 3 to 6 of doc 05).
🤖 Generated with Claude Code