feat: add runtime statistics to healthcheck endpoint #80
Reference in New Issue
Block a user
Delete Branch "feature/healthcheck-runtime-stats"
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?
Summary
Expands the
/.well-known/healthcheck.jsonendpoint with runtime statistics, giving operators visibility into server load and usage patterns.closes #74
New healthcheck fields
sessionsclientsqueuedLineschannelsconnectionsSinceBootsessionsSinceBootmessagesSinceBootImplementation
internal/statspackage — atomic counters for boot-scoped metrics (connectionsSinceBoot,sessionsSinceBoot,messagesSinceBoot). Thread-safe viasync/atomic.GetClientCount()andGetQueueEntryCount()for current snapshot counts.Healthcheck()now acceptscontext.Contextto query the database. Response struct extended with all 7 new fields. DB-derived stats populated with graceful error handling (logged, not fatal).handleCreateSession→IncrSessions+IncrConnectionshandleRegister→IncrSessions+IncrConnectionshandleLogin→IncrConnections(new client for existing session)handlePrivmsg→IncrMessages(covers both PRIVMSG and NOTICE)stats.Trackerprovided through Uber fx DI in both production and test setups.Tests
internal/stats/stats_test.go— 5 tests covering all counter operations (100% coverage)TestHealthcheckRuntimeStatsFields— verifies all 7 new fields are present in the responseTestHealthcheckRuntimeStatsValues— end-to-end: creates a session, joins a channel, sends a message, then verifies counts are nonzeroREADME
Updated healthcheck documentation with full response shape, field descriptions, and project structure listing for
internal/stats/.Review: PR #80 — feat: add runtime statistics to healthcheck endpoint
Closes: #74
Requirements Checklist (7/7 ✅)
All 7 counters from #74 are present and correctly sourced:
sessionsGetUserCount(ctx)→sessionstableclientsGetClientCount(ctx)→clientstablequeuedLinesGetQueueEntryCount(ctx)→client_queuestablechannelsGetChannelCount(ctx)→channelstableconnectionsSinceBootstats.TrackersessionsSinceBootstats.TrackermessagesSinceBootstats.TrackerCounter Instrumentation Accuracy ✅
handleCreateSession(after successful DB create)IncrSessions+IncrConnectionshandleRegister(after successful DB register)IncrSessions+IncrConnectionshandleLogin(after successful DB login)IncrConnectionsonlyhandlePrivmsg(after validation, before routing)IncrMessagesdispatchCommandpath)All counters are incremented after error checks, so only successful operations are counted. No missed paths —
handleCreateSession,handleRegister, andhandleLoginare the only three session/connection creation entry points.Policy Compliance ✅
statshas package comment;Tracker,New(), and all 6 methods have doc comments. ✅cmd/beyond bootstrapping: Onlystats.Newadded to fx providers. ✅populateDBStatsare logged viaslogand gracefully degraded (zero value returned). Non-fatal — healthcheck still responds. ✅if err != niluse plain assignment pattern. ✅stats_test.goachieves 100% statement coverage. Integration testsTestHealthcheckRuntimeStatsFieldsandTestHealthcheckRuntimeStatsValuesverify end-to-end correctness. ✅stats.Trackerproperly provided via fx in both production (cmd/neoircd/main.go) and test harness (api_test.go). ✅Code Quality Notes
sync/atomicis the right approach for boot-scoped counters — simple, zero-contention, no mutex overhead.populateDBStatscleanly separates DB-derived stats from in-memory counters. Graceful degradation on DB errors is correct for a healthcheck — you don't want the healthcheck itself to fail because a COUNT query timed out.GetClientCount,GetQueueEntryCount) follow the exact same pattern as existingGetUserCountandGetChannelCount.Build Result ✅
docker build .passes. All tests green:internal/stats— 5/5 tests pass, 100% coverageinternal/handlers— all tests pass including 2 new integration testsVerdict: PASS ✅
Clean implementation. All 7 required counters present and correctly sourced. Policy-compliant. Well-tested. Build green.