Skip to content

feat(server_status): separate ServerStatusService into standalone Connect-RPC with CPU usage monitoring - #902

Merged
kyasbal merged 4 commits into
GoogleCloudPlatform:mainfrom
kyasbal:push-kkllnzssqlkz
Aug 26, 2026
Merged

kyasbal merged 4 commits into
GoogleCloudPlatform:mainfrom
kyasbal:push-kkllnzssqlkz

Conversation

@kyasbal

@kyasbal kyasbal commented Aug 26, 2026

Copy link
Copy Markdown
Member

Summary

This PR separates the server status monitoring functionality into a dedicated Protobuf / Connect-RPC service (ServerStatusService) and adds CPU usage monitoring support to ResourceMonitor and the frontend UI header.

Background & Motivation

  1. Decoupling Server Status from Inspection: Previously, server memory statistics were bundled into inspection responses (e.g. 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.
  2. CPU Usage Monitoring: In addition to existing memory usage metrics, monitoring host CPU utilization is critical for diagnosing backend performance and system resource constraints during heavy log processing.
  3. Resilient Long-Polling / Reconnect Lifecycle: The server streams ServerStat updates and gracefully closes connections after a 30s cycle for environments with connection duration constraints; the client immediately reconnects to maintain continuous monitoring.

API Changes

Change Protocol / Type Endpoint Description
➕ Added Connect-RPC (Server Streaming) /api.v1.ServerStatusService/WatchServerStat Streams real-time ServerStat updates (current/total memory and CPU usage percentage) with immediate snapshot and 30s cycle cutoff.

Key Changes

1. Protobuf (proto/api/v1/server_status.proto)

  • Defined ServerStatusService with server-streaming RPC WatchServerStat.
  • Defined ServerStat message containing current_memory_usage, total_memory, and cpu_usage_percentage.
  • Generated Go and TypeScript Connect-RPC code (make build-proto).

2. Go Backend

  • pkg/server/resource_monitor.go:
    • Added GetCPUUsage() float64 calculating CPU utilization across sample intervals using runtime.NumCPU() and user/system CPU counters (syscall.Getrusage).
  • pkg/server/api/v1/server_status.go:
    • Implemented ServerStatusServiceServer handling WatchServerStat.
    • Sends immediate snapshot on connection, pushes updates on resource changes or intervals, and disconnects after 30s cycle for runtime compatibility.
  • pkg/core/init/default/server_status.go:
    • Registered ServerStatusServiceHandler in the default server initializer.
  • Unit Tests:
    • Added tests in pkg/server/resource_monitor_test.go, pkg/server/api/v1/server_status_test.go, and pkg/core/init/default/server_status_test.go.

3. Angular Frontend

  • web/src/app/services/api/connect-client.service.ts:
    • Injected and initialized serverStatusClient: Client<typeof ServerStatusService>.
  • web/src/app/services/api/backend-sync.service.ts:
    • Implemented startWatchingServerStat streaming ServerStat via Connect-RPC with automatic reconnection on stream completion or error.
  • web/src/app/header:
    • Extended HeaderComponent and HeaderSmartComponent to display CPU usage percentage alongside memory usage.
    • Added CPU usage indicator stories in header.component.stories.ts.

Verification

  • Backend Tests: make test-go passed (all packages).
  • Backend Lint: make lint-go passed (0 issues).
  • Frontend Tests: make test-web passed (Header and BackendSync unit tests).
  • Formatting: make format-go, make format-web, and make format-proto applied.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread pkg/server/resource_monitor.go Resolved
Comment thread pkg/server/resource_monitor.go Resolved
@kyasbal kyasbal added the type:refactor Code refactoring without behavioral change label Aug 26, 2026
@kyasbal
kyasbal force-pushed the push-kkllnzssqlkz branch 2 times, most recently from 1cb9eda to cf46dfc Compare August 26, 2026 01:12
@kyasbal
kyasbal force-pushed the push-kkllnzssqlkz branch from cf46dfc to 89895ca Compare August 26, 2026 01:21
@kyasbal
kyasbal merged commit ddaca05 into GoogleCloudPlatform:main Aug 26, 2026
16 checks passed
@kyasbal
kyasbal deleted the push-kkllnzssqlkz branch August 26, 2026 01:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type:refactor Code refactoring without behavioral change

1 participant