Overview
TheServerMetrics class handles server metrics collection and reporting, including connection tracking, processing times, CPU usage, and memory consumption.
Location: backend/src/metrics.py:10-61
Class Definition
Constructor
Instance Attributes
Current number of active WebSocket connections. Incremented on connect, decremented on disconnect.
Rolling buffer of last 100 audio processing times in seconds. Used to calculate average processing time.
Number of active background processes. Currently unused, always 0.
Total number of requests received since server start. Incremented on each new connection.
Total number of errors encountered. Incremented on processing failures.
Total number of CPU cores available to the server.
Example
Methods
log_current_metrics
metrics.py:23-44
Parameters
Optional connection identifier to include in log output. If provided, logs are prefixed with
[connection_id].Implementation Details
Calculates and logs:- Average processing time from last 100 requests
- Current memory usage using
psutil.Process().memory_info().rss - Total CPU usage percentage using
psutil.cpu_percent() - Per-core CPU usage using
psutil.cpu_percent(percpu=True) - Effective cores used:
(cpu_percent / 100) * cpu_cores
Example Output
Example Usage
get_metrics_dict
metrics.py:46-61
Returns
Returns a dictionary with the following structure:Current number of active WebSocket connections
Number of active background processes (currently always 0)
Total requests since server start
Total errors since server start
Average processing time in seconds from last 100 requests. Returns 0 if no requests processed.
Current memory usage in megabytes (RSS)
Total CPU usage percentage (0-100)
Array of CPU usage percentages for each core
Total number of CPU cores available
Effective cores utilized, calculated as
(cpu_total_percent / 100) * total_cpu_coresExample Usage
Frommain.py:46-49:
Usage Patterns
Updating Connection Counts
Recording Processing Times
Recording Errors
Dependencies
External Dependencies
-
psutil: System and process monitoring
psutil.cpu_percent(): CPU usage measurementpsutil.Process().memory_info(): Memory consumption
-
collections.deque: Efficient rolling buffer for processing times
maxlen=100: Automatically discards old values when full
Performance Considerations
Processing Times Deque
Theprocessing_times deque has a maximum length of 100:
- Automatically maintains the last 100 processing times
- O(1) append and average calculation
- Memory efficient (bounded size)
- No manual cleanup required
CPU Metrics
CPU metrics are calculated on-demand whenget_metrics_dict() or log_current_metrics() is called:
psutil.cpu_percent(): Blocking call, may take ~0.1spsutil.cpu_percent(percpu=True): Returns array of per-core percentages- Consider caching if metrics endpoint is called frequently
Memory Metrics
Memory usage is calculated from RSS (Resident Set Size):- Includes all memory in RAM
- Does not include swapped memory
- Converted from bytes to MB:
rss / 1024 / 1024

