File size: 10,629 Bytes
866dfb3 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 | # Implementation Tasks
# Kiro Gateway: Metrics, Dashboard & Load Balancing
## Master Task List
---
## Phase 1: Metrics Foundation
### 1.1 Backend Metrics Module
- [ ] **TASK-1.1.1** Create `/home/kiro/metrics/` directory structure
- [ ] **TASK-1.1.2** Create `__init__.py` with module exports
- [ ] **TASK-1.1.3** Implement `prometheus.py`:
- Define Counter for `kiro_requests_total`
- Define Histogram for `kiro_request_duration_seconds`
- Define Counter for `kiro_tokens_input_total`
- Define Counter for `kiro_tokens_output_total`
- Define Gauge for `kiro_active_requests`
- Define Counter for `kiro_errors_total`
- [ ] **TASK-1.1.4** Implement `opentelemetry.py`:
- Configure TracerProvider
- Configure MeterProvider
- Set up OTLP exporter
- Auto-instrument FastAPI
- [ ] **TASK-1.1.5** Implement `middleware.py`:
- Create `MetricsMiddleware` class
- Track request start/end times
- Record status codes
- Handle streaming responses
- [ ] **TASK-1.1.6** Implement `collectors.py`:
- Custom collector for token usage
- Per-model metrics aggregation
### 1.2 Integration
- [ ] **TASK-1.2.1** Add metrics middleware to `main.py`
- [ ] **TASK-1.2.2** Add `/metrics` endpoint for Prometheus scraping
- [ ] **TASK-1.2.3** Integrate token counting in `streaming_openai.py`
- [ ] **TASK-1.2.4** Integrate token counting in `streaming_anthropic.py`
- [ ] **TASK-1.2.5** Add metrics config to `config.py`:
- `METRICS_ENABLED`
- `METRICS_PREFIX`
- `OTEL_EXPORTER_OTLP_ENDPOINT`
- [ ] **TASK-1.2.6** Update `requirements.txt` with metrics dependencies
### 1.3 Docker & Testing
- [ ] **TASK-1.3.1** Create `docker/prometheus/prometheus.yml`
- [ ] **TASK-1.3.2** Update `docker-compose.yml` with Prometheus service
- [ ] **TASK-1.3.3** Write unit tests for metrics collection
- [ ] **TASK-1.3.4** Test Prometheus scraping
---
## Phase 2: Dashboard MVP
### 2.1 Project Setup
- [ ] **TASK-2.1.1** Initialize Next.js 16 project:
```bash
npx create-next-app@latest dashboard --typescript --tailwind --app --src-dir
```
- [ ] **TASK-2.1.2** Configure `tailwind.config.js`
- [ ] **TASK-2.1.3** Install and configure shadcn/ui:
```bash
npx shadcn-ui@latest init
```
- [ ] **TASK-2.1.4** Install Recharts:
```bash
npm install recharts
```
- [ ] **TASK-2.1.5** Install dependencies:
```bash
npm install @tanstack/react-query zustand socket.io-client lucide-react
```
- [ ] **TASK-2.1.6** Set up project structure (folders)
### 2.2 Core Components
- [ ] **TASK-2.2.1** Create `components/ui/` with shadcn components:
- Button, Card, Input, Select, Table, Tabs, Badge, Avatar
- [ ] **TASK-2.2.2** Create `components/dashboard/Sidebar.tsx`
- [ ] **TASK-2.2.3** Create `components/dashboard/Header.tsx`
- [ ] **TASK-2.2.4** Create `components/dashboard/StatsCards.tsx`
- [ ] **TASK-2.2.5** Create `components/common/ThemeToggle.tsx`
- [ ] **TASK-2.2.6** Create `app/(dashboard)/layout.tsx`
### 2.3 Charts (Recharts)
- [ ] **TASK-2.3.1** Create `components/charts/RequestsChart.tsx` (AreaChart)
- [ ] **TASK-2.3.2** Create `components/charts/LatencyChart.tsx` (BarChart)
- [ ] **TASK-2.3.3** Create `components/charts/TokenUsageChart.tsx` (ComposedChart)
- [ ] **TASK-2.3.4** Create `components/charts/ErrorRateChart.tsx` (PieChart)
- [ ] **TASK-2.3.5** Create `components/charts/AccountHealthChart.tsx` (BarChart)
### 2.4 Pages
- [ ] **TASK-2.4.1** Implement `app/(dashboard)/overview/page.tsx`:
- Stats cards (requests, tokens, latency, uptime)
- Mini charts
- Recent activity feed
- [ ] **TASK-2.4.2** Implement `app/(dashboard)/metrics/page.tsx`:
- Time range selector
- Model filter
- Full-size interactive charts
- [ ] **TASK-2.4.3** Implement `app/(dashboard)/accounts/page.tsx`:
- Account list table
- Health status indicators
- Add/Edit modal
- Delete confirmation
- [ ] **TASK-2.4.4** Implement `app/(dashboard)/config/page.tsx`:
- Configuration viewer
- Edit form for editable settings
- Model aliases manager
- [ ] **TASK-2.4.5** Implement `app/(dashboard)/logs/page.tsx`:
- Log level filter
- Search input
- Virtual scrolling log list
- WebSocket connection
### 2.5 API & State
- [ ] **TASK-2.5.1** Create `lib/api.ts`:
- Axios/fetch client setup
- API base URL configuration
- Auth header injection
- [ ] **TASK-2.5.2** Create `lib/websocket.ts`:
- Socket.io connection
- Event handlers
- Reconnection logic
- [ ] **TASK-2.5.3** Create `hooks/useMetrics.ts` with TanStack Query
- [ ] **TASK-2.5.4** Create `hooks/useAccounts.ts` with mutations
- [ ] **TASK-2.5.5** Create Zustand store for global state
- [ ] **TASK-2.5.6** Create `types/` TypeScript definitions
---
## Phase 3: Load Balancer Core
### 3.1 Module Structure
- [ ] **TASK-3.1.1** Create `/home/kiro/loadbalancer/` directory
- [ ] **TASK-3.1.2** Create `__init__.py` with exports
- [ ] **TASK-3.1.3** Create `models.py`:
- `KiroAccount` Pydantic model
- `LoadBalancerConfig` model
- `AccountHealth` enum
### 3.2 Core Implementation
- [ ] **TASK-3.2.1** Implement `pool.py`:
- `AccountPool` class
- Account registration/removal
- Get next account method
- Account state tracking
- [ ] **TASK-3.2.2** Implement `strategies.py`:
- `LoadBalancingStrategy` abstract base
- `RoundRobinStrategy`
- `WeightedRoundRobinStrategy`
- `LeastConnectionsStrategy`
- `RandomStrategy`
- `FailoverStrategy`
- [ ] **TASK-3.2.3** Implement `health_checker.py`:
- Async health check loop
- Token refresh verification
- API ping test
- Error rate tracking
### 3.3 Integration
- [ ] **TASK-3.3.1** Modify `KiroAuthManager` to support multiple instances
- [ ] **TASK-3.3.2** Create account factory function
- [ ] **TASK-3.3.3** Update `main.py` lifespan to initialize pool
- [ ] **TASK-3.3.4** Update `routes_openai.py` to use load balancer:
- Get account from pool before request
- Return account to pool after request
- Handle account failures
- [ ] **TASK-3.3.5** Update `routes_anthropic.py` similarly
- [ ] **TASK-3.3.6** Add load balancer config to `config.py`:
- `LOAD_BALANCER_ENABLED`
- `LOAD_BALANCER_STRATEGY`
- `LOAD_BALANCER_ACCOUNTS_FILE`
- `LOAD_BALANCER_HEALTH_CHECK_INTERVAL`
### 3.4 Account Management
- [ ] **TASK-3.4.1** Create `accounts.json` schema
- [ ] **TASK-3.4.2** Implement account loading from JSON file
- [ ] **TASK-3.4.3** Implement account loading from environment variables
- [ ] **TASK-3.4.4** Implement runtime account CRUD API
---
## Phase 4: Dashboard Backend API
### 4.1 API Implementation
- [ ] **TASK-4.1.1** Create `/home/kiro/dashboard_api/` directory
- [ ] **TASK-4.1.2** Create `models.py`:
- `DashboardStats` response model
- `MetricsQuery` request model
- `AccountCreate/Update` models
- [ ] **TASK-4.1.3** Implement `routes.py`:
- `GET /api/dashboard/stats`
- `GET /api/dashboard/metrics`
- `GET /api/dashboard/accounts`
- `POST /api/dashboard/accounts`
- `PUT /api/dashboard/accounts/{id}`
- `DELETE /api/dashboard/accounts/{id}`
- `GET /api/dashboard/config`
- `PUT /api/dashboard/config`
- [ ] **TASK-4.1.4** Implement `websocket.py`:
- WebSocket endpoint for logs
- Real-time metrics streaming
- [ ] **TASK-4.1.5** Register routes in `main.py`
### 4.2 CORS & Security
- [ ] **TASK-4.2.1** Configure CORS for dashboard origin
- [ ] **TASK-4.2.2** Add dashboard API key verification
- [ ] **TASK-4.2.3** Mask sensitive data in responses
---
## Phase 5: Polish & Deployment
### 5.1 Dashboard Polish
- [ ] **TASK-5.1.1** Implement dark/light theme persistence
- [ ] **TASK-5.1.2** Add loading states to all components
- [ ] **TASK-5.1.3** Add error boundaries
- [ ] **TASK-5.1.4** Implement responsive design for mobile
- [ ] **TASK-5.1.5** Add keyboard shortcuts
- [ ] **TASK-5.1.6** Create empty states for no data
### 5.2 Testing
- [ ] **TASK-5.2.1** Write unit tests for load balancer strategies
- [ ] **TASK-5.2.2** Write unit tests for health checker
- [ ] **TASK-5.2.3** Write integration tests for dashboard API
- [ ] **TASK-5.2.4** Write Playwright E2E tests for dashboard
- [ ] **TASK-5.2.5** Performance testing for metrics overhead
### 5.3 Docker & Deployment
- [ ] **TASK-5.3.1** Create `docker/Dockerfile.dashboard`
- [ ] **TASK-5.3.2** Create `docker/docker-compose.full.yml`:
- Kiro Gateway service
- Dashboard service
- Prometheus service
- Grafana service (optional)
- [ ] **TASK-5.3.3** Create Grafana dashboard templates
- [ ] **TASK-5.3.4** Update main README with new features
- [ ] **TASK-5.3.5** Create deployment guide
---
## Quick Reference: File Locations
### Backend (Python)
```
/home/kiro/metrics/__init__.py
/home/kiro/metrics/prometheus.py
/home/kiro/metrics/opentelemetry.py
/home/kiro/metrics/middleware.py
/home/kiro/metrics/collectors.py
/home/kiro/loadbalancer/__init__.py
/home/kiro/loadbalancer/pool.py
/home/kiro/loadbalancer/strategies.py
/home/kiro/loadbalancer/health_checker.py
/home/kiro/loadbalancer/models.py
/home/kiro/dashboard_api/__init__.py
/home/kiro/dashboard_api/routes.py
/home/kiro/dashboard_api/models.py
/home/kiro/dashboard_api/websocket.py
```
### Dashboard (Next.js)
```
/home/dashboard/app/layout.tsx
/home/dashboard/app/(dashboard)/overview/page.tsx
/home/dashboard/app/(dashboard)/metrics/page.tsx
/home/dashboard/app/(dashboard)/accounts/page.tsx
/home/dashboard/app/(dashboard)/config/page.tsx
/home/dashboard/app/(dashboard)/logs/page.tsx
/home/dashboard/components/ui/*
/home/dashboard/components/charts/*
/home/dashboard/components/dashboard/*
/home/dashboard/lib/api.ts
/home/dashboard/lib/websocket.ts
/home/dashboard/hooks/*
/home/dashboard/types/*
```
### Docker
```
/home/docker/Dockerfile.dashboard
/home/docker/docker-compose.full.yml
/home/docker/prometheus/prometheus.yml
```
---
## Estimated Timeline
| Phase | Duration | Dependencies |
|-------|----------|--------------|
| Phase 1: Metrics | 1 week | None |
| Phase 2: Dashboard MVP | 2 weeks | Phase 1 partial |
| Phase 3: Load Balancer | 1.5 weeks | None |
| Phase 4: Dashboard API | 1 week | Phase 1, 3 |
| Phase 5: Polish | 1 week | All phases |
**Total: ~6-7 weeks**
---
## Priority Order (MCP - Most Critical Path)
1. **HIGH** - TASK-1.1.3 (Prometheus metrics) - Foundation for everything
2. **HIGH** - TASK-3.2.1 (Account pool) - Core load balancer
3. **HIGH** - TASK-2.1.1 (Next.js setup) - Dashboard foundation
4. **MEDIUM** - TASK-1.2.2 (/metrics endpoint) - Enables monitoring
5. **MEDIUM** - TASK-3.2.2 (LB strategies) - Load balancer features
6. **MEDIUM** - TASK-2.4.1 (Overview page) - Dashboard MVP
7. **LOW** - TASK-5.3.3 (Grafana templates) - Nice to have
|