Skip to content

Conversation

fengmk2
Copy link
Member

@fengmk2 fengmk2 commented Jul 11, 2025

close CLOUD-235

image

PR Dependency Tree

This tree was auto-generated by Charcoal

Summary by CodeRabbit

  • New Features

    • Introduced a new monitoring service that automatically collects and logs process memory usage statistics every minute.
    • Enhanced system monitoring capabilities by integrating a global monitoring module.
    • Added support for a new "process" scope in metric tracking.
  • Chores

    • Improved internal module organization by including the monitoring module in the core functionality set.

Copy link
Contributor

coderabbitai bot commented Jul 11, 2025

Walkthrough

A new MonitorModule and MonitorService have been introduced to the backend server. The service schedules a cron job to collect and log Node.js process memory usage every minute, recording the data into metrics gauges. The MonitorModule is globally registered and added to the application's core modules. The metric scope type union is updated to include 'process'.

Changes

File(s) Change Summary
.../server/src/app.module.ts Imported and registered MonitorModule in FunctionalityModules.
.../server/src/base/metrics/metrics.ts Extended KnownMetricScopes type union to include 'process'.
.../server/src/core/monitor/index.ts Added new global NestJS module MonitorModule providing MonitorService.
.../server/src/core/monitor/service.ts Added MonitorService with a cron job to log and record Node.js process memory usage every minute.

Sequence Diagram(s)

sequenceDiagram
    participant CronScheduler as Cron Scheduler
    participant MonitorService
    participant MetricsSystem
    participant Logger

    CronScheduler->>MonitorService: Trigger monitor() every minute
    MonitorService->>MonitorService: Collect process.memoryUsage()
    MonitorService->>Logger: Log memory usage
    MonitorService->>MetricsSystem: Record memory stats to gauges
Loading

Assessment against linked issues

Objective Addressed Explanation
Add process memory monitoring and metrics collection for leak investigation (CLOUD-235)
Integrate monitoring module into application core for stable environment (CLOUD-235)
Update metric scopes to support process-level monitoring (CLOUD-235)

Poem

In the server’s warren, deep and bright,
A Monitor hops in, every minute, day or night.
It sniffs out leaks, logs memory’s tale,
Gauging usage, leaving a metric trail.
With every cron hop, the backend’s in sight—
A rabbit’s work keeps memory light! 🐇✨

✨ Finishing Touches
  • 📝 Generate Docstrings

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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

perfsee bot commented Jul 11, 2025

affine-toeverything

Bundle main

diff ------------------- Bundle Size Diff -------------------------

@@                       EntryPoint: index                       @@
##                     canary …usage-metrics                 +/- ##
===================================================================
< Bundle              26.9 MB        26.9 MB    +3.54 kB(+0.01%)   
< Initial JS          11.6 MB        11.6 MB      +813 B(+0.01%)   
= Initial CSS          220 kB         220 kB                       
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
< Assets                   88             89                  +1   
< Chunks                   86             87                  +1   
= Packages                265            265                       
= Duplicates                5              5                       
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Warnings ~~~~~~~~~~~~~~~~~~~~~~~~~~~#
! Deduplicate versions of libraries                                
! Separate mixed content assets files                              
! Avoid cache wasting                                              

Copy link

codecov bot commented Jul 11, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 56.81%. Comparing base (8b579e3) to head (ee67957).
Report is 4 commits behind head on canary.

Additional details and impacted files
@@            Coverage Diff             @@
##           canary   #13148      +/-   ##
==========================================
- Coverage   56.82%   56.81%   -0.01%     
==========================================
  Files        2706     2708       +2     
  Lines      132147   132187      +40     
  Branches    20572    20570       -2     
==========================================
+ Hits        75088    75105      +17     
- Misses      54849    55408     +559     
+ Partials     2210     1674     -536     
Flag Coverage Δ
server-test 78.85% <100.00%> (-0.01%) ⬇️
unittest 32.21% <ø> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@fengmk2 fengmk2 requested review from Copilot, forehalo, darkskygit and a team July 11, 2025 03:48
@fengmk2 fengmk2 marked this pull request as ready for review July 11, 2025 03:48
@fengmk2 fengmk2 enabled auto-merge July 11, 2025 03:48
Copy link

@Copilot 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

This PR introduces automated monitoring of the Node.js process’s memory usage by adding a scheduled service, expands the metric scope, and wires the service into the application module.

  • Add MonitorService with a Cron job to log and emit process memory metrics every minute.
  • Extend the KnownMetricScopes union to include a new 'process' scope.
  • Register MonitorModule in AppModule to enable global monitoring.

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
packages/backend/server/src/core/monitor/service.ts New MonitorService logs and records Node.js memory usage metrics
packages/backend/server/src/core/monitor/index.ts Created MonitorModule as a global Nest module
packages/backend/server/src/base/metrics/metrics.ts Added 'process' to the KnownMetricScopes union
packages/backend/server/src/app.module.ts Imported and registered MonitorModule within application modules
Comments suppressed due to low confidence (1)

packages/backend/server/src/core/monitor/service.ts:11

  • There are no unit tests covering the new MonitorService.monitor method. Consider adding a test to verify that memory usage metrics are correctly recorded and that the log statement fires as expected.
  async monitor() {

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (2)
packages/backend/server/src/core/monitor/service.ts (2)

10-10: Consider adjusting the monitoring frequency.

Running memory monitoring every minute might be too frequent for production environments. Consider using a configurable interval or a less frequent schedule like every 5-10 minutes.

-  @Cron(CronExpression.EVERY_MINUTE)
+  @Cron(CronExpression.EVERY_5_MINUTES)

13-15: Consider making monitoring configurable by environment.

The monitoring runs unconditionally in all environments. Consider adding configuration to disable or adjust monitoring behavior based on environment (development, staging, production).

You could inject a configuration service to control monitoring behavior:

constructor(
  private readonly configService: ConfigService,
) {}

@Cron(CronExpression.EVERY_MINUTE)
async monitor() {
  if (!this.configService.get('ENABLE_MEMORY_MONITORING', true)) {
    return;
  }
  // ... rest of the method
}
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 8b579e3 and ee67957.

📒 Files selected for processing (4)
  • packages/backend/server/src/app.module.ts (2 hunks)
  • packages/backend/server/src/base/metrics/metrics.ts (1 hunks)
  • packages/backend/server/src/core/monitor/index.ts (1 hunks)
  • packages/backend/server/src/core/monitor/service.ts (1 hunks)
🔇 Additional comments (4)
packages/backend/server/src/base/metrics/metrics.ts (1)

63-64: LGTM! Proper extension of metric scopes.

The addition of 'process' to the KnownMetricScopes type union correctly supports the new process memory metrics functionality.

packages/backend/server/src/app.module.ts (2)

39-39: LGTM! Proper module import.

The import follows the established pattern for core modules.


116-116: LGTM! Correct module integration.

Adding MonitorModule to FunctionalityModules ensures the monitoring service is available application-wide.

packages/backend/server/src/core/monitor/index.ts (1)

1-9: LGTM! Well-structured global module.

The MonitorModule is properly implemented as a global NestJS module, making the MonitorService available throughout the application without explicit imports.

@fengmk2 fengmk2 added this pull request to the merge queue Jul 11, 2025
Merged via the queue into canary with commit af9c455 Jul 11, 2025
127 checks passed
@fengmk2 fengmk2 deleted the add-memory-usage-metrics branch July 11, 2025 04:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: No status
Development

Successfully merging this pull request may close these issues.

2 participants