Repository navigation
feat(server_status): separate ServerStatusService into standalone Connect-RPC with CPU usage monitoring - #902
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces host server CPU usage monitoring and streams real-time server resource statistics (CPU and memory) to the frontend via a new Connect-RPC service, ServerStatusService. On the backend, ResourceMonitor is extended with a GetCPUUsage method using gopsutil, and a new ServerStatusServiceServer is implemented to handle snapshot and streaming requests. On the frontend, the HeaderComponent is updated to display the server's CPU usage alongside memory usage, and BackendSyncService is updated to subscribe to the real-time server status stream. Feedback on the changes highlights a concurrency and correctness issue with calling cpu.Percent(0, false) directly inside the on-demand GetCPUUsage() method, which can cause logical race conditions and return inaccurate or zero values. It is recommended to implement a background polling loop to periodically update a cached CPU usage value, initialized via a dedicated constructor function, and to import the time package to support this loop.
1cb9eda to
cf46dfc
Compare
cf46dfc to
89895ca
Compare
Summary
This PR separates the server status monitoring functionality into a dedicated Protobuf / Connect-RPC service (
ServerStatusService) and adds CPU usage monitoring support toResourceMonitorand the frontend UI header.Background & Motivation
GetInspectionResponse). Decoupling server status into its own service (ServerStatusService) establishes a clean separation of concerns and allows real-time server health monitoring independent of inspection execution.ServerStatupdates and gracefully closes connections after a 30s cycle for environments with connection duration constraints; the client immediately reconnects to maintain continuous monitoring.API Changes
/api.v1.ServerStatusService/WatchServerStatServerStatupdates (current/total memory and CPU usage percentage) with immediate snapshot and 30s cycle cutoff.Key Changes
1. Protobuf (
proto/api/v1/server_status.proto)ServerStatusServicewith server-streaming RPCWatchServerStat.ServerStatmessage containingcurrent_memory_usage,total_memory, andcpu_usage_percentage.make build-proto).2. Go Backend
pkg/server/resource_monitor.go:GetCPUUsage() float64calculating CPU utilization across sample intervals usingruntime.NumCPU()and user/system CPU counters (syscall.Getrusage).pkg/server/api/v1/server_status.go:ServerStatusServiceServerhandlingWatchServerStat.pkg/core/init/default/server_status.go:ServerStatusServiceHandlerin the default server initializer.pkg/server/resource_monitor_test.go,pkg/server/api/v1/server_status_test.go, andpkg/core/init/default/server_status_test.go.3. Angular Frontend
web/src/app/services/api/connect-client.service.ts:serverStatusClient: Client<typeof ServerStatusService>.web/src/app/services/api/backend-sync.service.ts:startWatchingServerStatstreamingServerStatvia Connect-RPC with automatic reconnection on stream completion or error.web/src/app/header:HeaderComponentandHeaderSmartComponentto display CPU usage percentage alongside memory usage.header.component.stories.ts.Verification
make test-gopassed (all packages).make lint-gopassed (0 issues).make test-webpassed (Header and BackendSync unit tests).make format-go,make format-web, andmake format-protoapplied.