Skip to content

feat: bot performance monitoring#218

Merged
BillChirico merged 6 commits intomainfrom
feat/issue-144
Mar 2, 2026
Merged

feat: bot performance monitoring#218
BillChirico merged 6 commits intomainfrom
feat/issue-144

Conversation

@BillChirico
Copy link
Collaborator

Summary

Adds real-time performance monitoring and alerting for the bot with a dashboard UI.

Features

  • Memory usage tracking — heap used, heap total, RSS, external; sampled every 30s into a circular buffer (last 60 min)
  • CPU utilization monitoring — process CPU delta sampled every 30s
  • Response time metrics — tracks command execution time and API endpoint latency; p50/p95/p99/max
  • Alert thresholds — configurable per-metric thresholds with 5-minute cooldown; fires registered callbacks + warns in logs
  • Dashboard graphs — recharts area charts for memory/CPU time-series, bar chart histogram for response times, recent samples table

Architecture

Backend

  • src/modules/performanceMonitor.js — singleton with circular buffer time-series storage (no DB, in-memory)
  • src/api/routes/performance.js — GET /api/v1/performance (snapshot), GET/PUT /api/v1/performance/thresholds
  • Response time middleware on all /api/v1 routes
  • Command execution timing in slash command handler
  • Starts/stops with bot lifecycle (ClientReady / graceful shutdown)

Frontend

  • web/src/app/dashboard/performance/page.tsx — dashboard page
  • web/src/components/dashboard/performance-dashboard.tsx — recharts charts + threshold editor
  • web/src/app/api/performance/route.ts — Next.js proxy (GET)
  • web/src/app/api/performance/thresholds/route.ts — Next.js proxy (GET/PUT)
  • Sidebar: Performance nav link (Activity icon)

Tests

  • 24 unit tests for PerformanceMonitor and CircularBuffer
  • 12 integration tests for the performance API routes
  • 36 tests total, all passing

Files Changed

  • src/modules/performanceMonitor.js — new: monitor singleton
  • src/api/routes/performance.js — new: API routes
  • src/api/index.js — mount /performance router
  • src/api/server.js — response time middleware
  • src/index.js — lifecycle wiring + command timing
  • web/src/app/api/performance/route.ts — new: proxy GET
  • web/src/app/api/performance/thresholds/route.ts — new: proxy GET/PUT
  • web/src/app/dashboard/performance/page.tsx — new: page
  • web/src/components/dashboard/performance-dashboard.tsx — new: charts + UI
  • web/src/components/layout/sidebar.tsx — add Performance nav link
  • tests/modules/performanceMonitor.test.js — new: 24 tests
  • tests/api/routes/performance.test.js — new: 12 tests

- PerformanceMonitor singleton with circular buffer time-series storage
- Samples memory (heap/RSS) and CPU every 30s
- Records command and API response times
- Configurable alert thresholds with callback hooks (5min cooldown)
- GET /api/v1/performance — full snapshot (auth required)
- GET/PUT /api/v1/performance/thresholds — threshold management
- Wired into bot startup/shutdown lifecycle
- API response time middleware on all /api/v1 routes
- Command execution timing in slash command handler
- /dashboard/performance page with recharts area/bar charts
- Memory usage (heap + RSS) over time — AreaChart
- CPU utilization over time — AreaChart
- Response time histogram (500ms buckets) — BarChart
- Recent response times table with type badges
- Alert threshold editor (save via PUT /api/v1/performance/thresholds)
- KPI cards for heap, RSS, CPU, uptime, and percentile response times
- Auto-refreshes every 30s; manual refresh button
- Sidebar: Activity icon + Performance nav link
- Next.js proxy routes: GET/PUT /api/performance and /api/performance/thresholds
Copilot AI review requested due to automatic review settings March 2, 2026 04:44
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 2, 2026

Warning

Rate limit exceeded

@BillChirico has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 3 minutes and 22 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between c6633d1 and 9f9e921.

📒 Files selected for processing (15)
  • src/api/index.js
  • src/api/routes/performance.js
  • src/api/server.js
  • src/index.js
  • src/modules/performanceMonitor.js
  • tests/api/routes/performance.test.js
  • tests/api/utils/ssrfProtection.test.js
  • tests/modules/events-uncaught-exit.test.js
  • tests/modules/performanceMonitor.test.js
  • web/src/app/api/performance/route.ts
  • web/src/app/api/performance/thresholds/route.ts
  • web/src/app/dashboard/performance/page.tsx
  • web/src/components/dashboard/config-editor.tsx
  • web/src/components/dashboard/performance-dashboard.tsx
  • web/src/components/layout/sidebar.tsx
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feat/issue-144

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds real-time performance monitoring (memory/CPU/response times), alert thresholds, and a dashboard UI to visualize and configure metrics.

Changes:

  • Introduces a backend PerformanceMonitor singleton with circular-buffer time series and threshold-based alerting.
  • Adds /api/v1/performance + /api/v1/performance/thresholds endpoints and wires response-time sampling into the API + command handler.
  • Adds a Next.js dashboard page with charts, recent samples table, and a threshold editor (plus sidebar nav link).

Reviewed changes

Copilot reviewed 12 out of 12 changed files in this pull request and generated 7 comments.

Show a summary per file
File Description
web/src/components/layout/sidebar.tsx Adds a “Performance” nav item pointing to the new dashboard page
web/src/components/dashboard/performance-dashboard.tsx Implements dashboard UI: charts, recent samples table, fetch/auto-refresh, and threshold editor
web/src/app/dashboard/performance/page.tsx Provides the dashboard route entry that renders the new performance dashboard component
web/src/app/api/performance/route.ts Adds authenticated Next.js proxy endpoint for performance snapshot
web/src/app/api/performance/thresholds/route.ts Adds authenticated Next.js proxy endpoints for reading/updating thresholds
src/modules/performanceMonitor.js Adds monitor singleton, circular buffers, sampling, summary stats, and alerting
src/index.js Wires monitor startup/shutdown and records command execution response times
src/api/server.js Adds API middleware to record per-request response times
src/api/routes/performance.js Adds performance snapshot + thresholds endpoints (x-api-secret auth)
src/api/index.js Mounts the new performance router
tests/modules/performanceMonitor.test.js Adds unit tests for CircularBuffer and PerformanceMonitor behavior
tests/api/routes/performance.test.js Adds integration tests for the performance endpoints

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@greptile-apps
Copy link

greptile-apps bot commented Mar 2, 2026

Greptile Summary

Implemented comprehensive performance monitoring system with backend tracking module, REST API endpoints, and interactive dashboard UI. The solution follows project conventions (Winston logging, ESM modules, proper error handling) and includes excellent test coverage (36 tests across unit and integration layers).

Key Changes:

  • PerformanceMonitor singleton tracks memory (heap/RSS), CPU utilization, and response times using circular buffers (in-memory, 60-minute rolling window)
  • Configurable alert thresholds with 5-minute cooldown and callback support
  • Middleware automatically tracks API endpoint response times; slash command handler records command execution times
  • Dashboard displays real-time charts (Recharts area/bar charts), KPI cards, percentile metrics (p50/p95/p99), and threshold editor with auto-refresh
  • All endpoints require authentication (x-api-secret validation inline rather than via requireAuth() middleware)
  • Monitor lifecycle properly integrated (starts on ClientReady, stops on graceful shutdown)

Also included:

  • Fixed config-editor.tsx useEffect dependency array to prevent infinite loop
  • Minor test cleanup (removed unused imports, improved readability)

Confidence Score: 5/5

  • This PR is safe to merge with no blocking issues
  • Code follows all project conventions (Winston logging, ESM, proper error handling), has comprehensive test coverage (36 tests), uses secure patterns (authentication, input validation, circular buffers prevent memory leaks), and integrates cleanly with existing lifecycle hooks
  • No files require special attention

Important Files Changed

Filename Overview
src/modules/performanceMonitor.js implemented circular buffer-based performance monitoring with memory, CPU, and response time tracking; clean singleton pattern with proper error handling and Winston logging
src/api/routes/performance.js added three performance API endpoints with proper authentication, input validation, and OpenAPI documentation
web/src/components/dashboard/performance-dashboard.tsx comprehensive performance dashboard with real-time charts, metrics cards, threshold editor, and auto-refresh; proper error handling and loading states
tests/modules/performanceMonitor.test.js comprehensive test coverage for CircularBuffer and PerformanceMonitor including edge cases, alerts, cooldowns, and time-series sampling
tests/api/routes/performance.test.js thorough API endpoint tests covering authentication, authorization, input validation, and response structure

Sequence Diagram

sequenceDiagram
    participant Bot as Discord Bot
    participant PM as PerformanceMonitor
    participant API as Express API
    participant Next as Next.js Dashboard
    participant User as Browser

    Note over Bot,PM: Startup
    Bot->>PM: start() on ClientReady
    PM->>PM: Start 30s interval timer
    
    Note over PM: Every 30 seconds
    PM->>PM: Sample memory (heap, RSS)
    PM->>PM: Sample CPU (process.cpuUsage)
    PM->>PM: Store in CircularBuffer (120 samples)
    PM->>PM: Check thresholds & fire alerts
    
    Note over Bot,PM: Command Execution
    User->>Bot: /command
    Bot->>Bot: Start timer
    Bot->>Bot: Execute command
    Bot->>PM: recordResponseTime(name, duration)
    PM->>PM: Store in CircularBuffer (480 samples)
    PM->>PM: Check threshold
    
    Note over API,PM: API Request
    User->>API: GET /api/v1/endpoint
    API->>API: Start timer (middleware)
    API->>API: Handle request
    API->>PM: recordResponseTime(label, duration)
    
    Note over User,Next: Dashboard View
    User->>Next: Open /dashboard/performance
    Next->>Next: Verify NextAuth token
    Next->>API: GET /api/v1/performance (proxy)
    API->>API: Validate x-api-secret
    API->>PM: getSnapshot()
    PM-->>API: {current, timeSeries, responseTimes, summary}
    API-->>Next: JSON response
    Next-->>User: Render charts & metrics
    
    Note over User,Next: Update Thresholds
    User->>Next: PUT /api/performance/thresholds
    Next->>Next: Verify NextAuth token
    Next->>API: PUT /api/v1/performance/thresholds
    API->>API: Validate input
    API->>PM: setThresholds(values)
    PM-->>API: Updated thresholds
    API-->>Next: JSON response
    Next-->>User: Show success message
    
    Note over Bot,PM: Shutdown
    Bot->>PM: stop()
    PM->>PM: clearInterval(timer)
Loading

Last reviewed commit: 9f9e921

coderabbitai[bot]
coderabbitai bot previously approved these changes Mar 2, 2026
Copilot AI review requested due to automatic review settings March 2, 2026 11:46
@BillChirico BillChirico merged commit 20abb7e into main Mar 2, 2026
7 of 12 checks passed
@BillChirico BillChirico deleted the feat/issue-144 branch March 2, 2026 11:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants