iron_runtime_analytics
Lock-free event-based analytics for Python LlmRouter.
Installation
[]
= { = "../iron_runtime_analytics" }
= { = "../iron_cost" } # For pricing
Features
- Lock-free storage - crossbeam ArrayQueue for bounded event buffer
- Atomic counters - O(1) stats access without locks
- Per-model/provider stats - DashMap for concurrent aggregation
- High-level recording API - automatic provider inference and cost calculation
- Protocol 012 compatible - field compatibility with analytics API
- Background sync - server sync with auto-flush on shutdown (feature:
sync)
Quick Start
High-Level API (Recommended)
The high-level API handles provider inference and cost calculation automatically:
use EventStore;
use PricingManager;
let store = new;
let pricing = new.unwrap;
// Record successful LLM request - provider inferred from model name
store.record_llm_completed;
// Record with agent attribution
store.record_llm_completed;
// Record failed request
store.record_llm_failed;
// Lifecycle events
store.record_router_started;
store.record_router_stopped; // Captures final stats automatically
Accessing Statistics
let stats = store.stats;
// Totals (O(1) access)
println!;
println!;
println!;
// Per-model breakdown
for in &stats.by_model
// Per-provider breakdown
for in &stats.by_provider
Pilot Strategy
Simple, predictable behavior:
- Fixed Memory: Bounded buffer (default 10,000 slots, ~2-5MB)
- Non-Blocking: Drop new events when buffer full (never block)
- Observability:
dropped_count()tracks lost events
Performance
- Fixed Memory: ~2-5MB for 10,000 events
- O(1) stats access: Atomic counters, no lock contention
- Non-blocking: Never waits for locks or I/O
When enabled with the sync feature, events can be automatically synced to Control API:
use ;
use Arc;
let store = new;
let config = new
.with_interval // Sync every 30s
.with_batch_threshold; // Or when 10 events pending
// Start background sync (requires tokio runtime handle)
let handle = client.start;
// ... use store normally ...
// Stop and flush remaining events
handle.stop;
SyncConfig Options
| Option | Default | Description |
|---|---|---|
sync_interval |
30s | How often to sync events |
batch_threshold |
10 | Sync immediately when this many events pending |
timeout |
30s | HTTP request timeout |
Sync Behavior
- Events are synced in batches to
/api/v1/analytics/events - Only
llm_request_completedandllm_request_failedevents are synced - On shutdown, remaining events are flushed before stopping
- Failed syncs are retried on next interval (except 4xx errors)
For full control over event construction:
use ;
use ;
let store = new;
store.record;
// Check for dropped events (buffer overflow)
if store.dropped_count > 0
// Check unsynced events (pending server sync)
println!;
use thread;
// Create store with streaming channel
let = with_streaming;
// Spawn consumer thread
spawn;
// Events are automatically sent to channel when recorded
store.record_llm_completed;
src/
├── lib.rs # Re-exports
├── event.rs # AnalyticsEvent, EventPayload, LlmUsageData
├── event_storage.rs # EventStore (lock-free buffer + atomic counters)
├── stats.rs # AtomicModelStats, ModelStats, ComputedStats
├── recording.rs # High-level record_* methods
└── helpers.rs # Provider enum, infer_provider, current_time_ms
In Scope:
- Lock-free event buffer (crossbeam ArrayQueue)
- Atomic running totals (AtomicU64)
- Per-model/provider stats (DashMap)
- Dropped event counter for observability
- Event streaming via channels
- Protocol 012 field compatibility
Out of Scope:
- Server-side event persistence (see iron_control_api)
- Dashboard WebSocket streaming (see iron_control_api)
- Agent name/budget lookups (server-side enrichment)
- Min/max/median computation (server computes from synced events)
Source Files
| File | Responsibility |
|---|---|
| lib.rs | Lock-free event-based analytics for Iron Runtime LLM proxy. |
| event.rs | Analytics event types and payloads. |
| event_storage.rs | Lock-free event storage with atomic counters. |
| provider_utils.rs | Utility functions and types for analytics. |
| recording.rs | High-level recording API for EventStore. |
| stats.rs | Statistics types for analytics aggregation. |
| sync.rs | Analytics sync - background sync of events to Control API. |
Notes:
- Entries marked 'TBD' require manual documentation
- Entries marked '⚠️ ANTI-PATTERN' should be renamed to specific responsibilities
License
Apache-2.0