-
chore(core): restructure metrics collector for performance and maintainability (#33483)
发布于
2025-11-14 16:56:51 +00:00 Current Behavior
The metrics collector has several inefficiencies and architectural
issues:- Complex hierarchical data structure (
ProcessTreeMetrics) that
doesn't align with how the data is consumed - Separate
MetadataStorestruct with unnecessary indirection - Full metadata resent to all subscribers on every collection cycle
- Mutable parameters passed through collection functions instead of
functional return values - Repeated allocations and clones across collection cycles
CollectionRunnertightly coupled to NAPI, making it untestable in
pure Rust
Expected Behavior
This PR restructures the metrics collector for better performance,
testability, and maintainability:Architectural Changes
-
Flat Process Model: Replaced hierarchical
ProcessTreeMetrics
with a flatVec<ProcessMetrics>, simplifying data flow -
Group-Based Organization: Introduced
GroupInfoandGroupType
to logically organize processes:MainCLI- Nx CLI process and its subprocessesDaemon- Nx daemon and its childrenTask- Individual task execution processesBatch- Batch execution with multiple tasks
-
Incremental Metadata Updates:
- Track which groups and processes have been sent using
Arc<DashMap<String, GroupInfo>>andArc<DashMap<String, ProcessMetadata>> - Only send new metadata to subscribers instead of full state every
cycle - New subscribers receive full metadata on first update via
needs_full_metadataflag- Automatic cleanup of dead process/group metadata
- Shared State with Arc:
- Metadata maps shared between
ProcessMetricsCollectorand
CollectionRunnerusingArc<DashMap>- Eliminated duplicate metadata storage
- Single source of truth for all metadata
- Functional Programming Pattern:
- Collection functions now return
Result<MetricsCollectionResult>
instead of mutating parameters- Cleaner error handling with
inspect_errandmap - Easier to reason about data flow
- Removed ~100 lines of code by consolidating logic
- Cleaner error handling with
- Channel-Based Communication:
- Decoupled
CollectionRunnerfrom NAPI usingcrossbeam_channel - Collection thread sends metrics via channel to listener thread
- Listener thread receives metrics and notifies NAPI subscribers
CollectionRunneris now NAPI-free and fully testable in pure Rust
- Decoupled
- Non-blocking collection (subscriber callbacks don't block metrics
collection)
Performance Optimizations
- Pre-allocated
Veccapacity when combining metrics from different
sources - Eliminated unnecessary
HashMapclones during metadata updates - Single-pass insertion into
DashMapduring string key conversion - Reduced memory allocations in hot paths
- Collection thread never blocks on JavaScript callbacks
Code Quality Improvements
- Testability: Added 7 pure Rust unit tests for
CollectionRunner:- Group creation with different registration types
- Incremental metadata updates
- Dead group cleanup
- All tests pass without requiring NAPI/Node.js runtime
- Clearer separation of concerns between collection and notification
- Better comments explaining incremental update strategy
- More idiomatic Rust patterns throughout
- Updated TypeScript type exports to match new structure
Threading Model
Before:
CollectionRunner (mixed collection + NAPI notification)After:
CollectionRunner (pure Rust, testable) └─> Channel └─> Listener Thread └─> NAPI ThreadsafeFunction └─> SubscribersTesting
- ✅ 241 Rust tests passing (including 7 new
CollectionRunnertests) - ✅ Native module builds successfully
- ✅ TypeScript types updated and exports verified
Related Issue(s)
Part of ongoing metrics collector optimization work.
下载附件
- Complex hierarchical data structure (