Add timestamp and window metadata to live_stat metrics
- Dominant language
- Python
- Stars
- 670
- Forks
- 183
- Avg merge
- 17h 7m
- Merged PRs (30d)
- 358
Description
## Motivation
The Kubernetes Bridge needs timestamp and window metadata for metrics to support kubectl top pods and kubectl top nodes.
The live_stat field already exists on kernels and agents with CPU/memory metrics, but it lacks timing information that Kubernetes Metrics Server requires.
Currently the bridge has no way to know when a metric was collected or over what time window it was measured.
## Required Features
**Add timing metadata to live_stat serialization:**
- `timestamp` - Unix timestamp (or ISO8601) when the measurement was taken
- `window` - Duration in seconds over which the metric was measured (typically matches UTILIZATION_METRIC_INTERVAL = 5s)
**Files to modify:**
- `src/ai/backend/agent/stats.py` - Add timestamp/window to Metric.to_serializable_dict() and MovingStatistics.to_serializable_dict()
- `src/ai/backend/common/types.py` - Update MetricValue TypedDict to include new fields
## Impact
- Enables proper kubectl top support in Kubernetes Bridge
- Provides timing context for metrics in Backend.AI UI
- No breaking changes - adds new optional fields to existing data structure
- Minimal performance impact - just adds two fields to serialization
## Testing Scenarios
- Verify live_stat response includes timestamp and window fields for both kernels and agents
- Verify timestamp updates on each collection cycle (~5 seconds)
- Verify window matches the configured UTILIZATION_METRIC_INTERVAL
- Verify existing consumers of live_stat continue to work (backward compatibility)
JIRA Issue: BA-3873
Contributor guide
Assessment
This issue has not been assessed yet.