Skip to content

Commit 1762de3

Browse files
amituclaude
andcommitted
docs: add automatic request tracking design to NEXT-metrics-and-data.md
- Add comprehensive time-windowed counter tracking for contexts that call persist() - Design automatic counters: since_start, last_day, last_hour, last_minute, last_second - Use full dotted context paths as keys for hierarchical aggregation - Zero manual tracking - persist() automatically updates all time window counters - Add sliding window implementation with efficient circular buffers - Include automatic rate calculation and trending capabilities - Show hierarchical aggregation: context path enables automatic rollups Example automatic tracking: - ctx.persist() on "global.p2p.alice@bv478gen.stream-123" - Auto-increments: requests_since_start, requests_last_hour, etc. - Hierarchical: "global.p2p.requests_last_hour" aggregates all P2P - Status shows: "1,247 total | 234 last hour | 45 last minute | 2/sec" This provides comprehensive operational analytics without manual counter management. Just persist contexts and get complete request tracking automatically. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 6d2e53c commit 1762de3

1 file changed

Lines changed: 54 additions & 1 deletion

File tree

fastn-context/NEXT-metrics-and-data.md

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,57 @@ ctx.child("remote-shell-handler")
6565
});
6666
```
6767

68-
**Implementation**: After basic Context tree structure is working.
68+
## Automatic Request Tracking
69+
70+
For contexts that call `.persist()`, automatic time-windowed counters will be maintained:
71+
72+
```rust
73+
// Automatic counters for persisted contexts (no manual tracking needed)
74+
// Uses full dotted context path as key
75+
76+
// When ctx.persist() is called on "global.p2p.alice@bv478gen.stream-123":
77+
// Auto-increments these counters:
78+
"global.p2p.alice@bv478gen.requests_since_start" // Total ever
79+
"global.p2p.alice@bv478gen.requests_last_day" // Last 24 hours
80+
"global.p2p.alice@bv478gen.requests_last_hour" // Last 60 minutes
81+
"global.p2p.alice@bv478gen.requests_last_minute" // Last 60 seconds
82+
"global.p2p.alice@bv478gen.requests_last_second" // Last 1 second
83+
84+
// Hierarchical aggregation automatically available:
85+
"global.p2p.requests_last_hour" // All P2P requests
86+
"global.requests_last_hour" // All application requests
87+
```
88+
89+
### Time Window Implementation
90+
91+
```rust
92+
// Sliding window counters with efficient circular buffers
93+
// Updated automatically when any context calls persist()
94+
95+
// Status display shows rates:
96+
global.p2p.alice@bv478gen (23m, active)
97+
Requests: 1,247 total | 234 last hour | 45 last minute | 2/sec current
98+
99+
// Automatic rate calculation and trending
100+
```
101+
102+
### Usage Pattern
103+
104+
```rust
105+
// P2P stream handler
106+
async fn handle_stream(ctx: Arc<Context>) {
107+
// Process stream...
108+
ctx.persist(); // Automatically increments all time window counters
109+
110+
// No manual counter management needed!
111+
// All metrics tracked automatically by dotted context path
112+
}
113+
114+
// HTTP request handler
115+
async fn handle_request(ctx: Arc<Context>) {
116+
// Process request...
117+
ctx.persist(); // Auto-tracks "global.http.endpoint-xyz.requests_*"
118+
}
119+
```
120+
121+
**Implementation**: After basic Context + counter storage foundation.

0 commit comments

Comments
 (0)