Realtime Cost
The RealtimeCostController and RealtimeCostTrackingService provide real-time cost monitoring and alerting. Cost metrics are updated continuously as usage events flow through the system, giving tenants immediate visibility into their current spending.
Real-Time Cost Metrics
Get Current Costs
Endpoint: GET /api/v1/billing/realtime-cost/tenants/:tenantId/current
Returns the current cost accumulation for the active billing period.
curl http://localhost:8087/api/v1/billing/realtime-cost/tenants/550e8400/current \
-H "Authorization: Bearer ${TOKEN}"Cost by Resource Type
Endpoint: GET /api/v1/billing/realtime-cost/tenants/:tenantId/by-resource
Returns real-time cost broken down by resource type (compute, storage, API calls, etc.).
Cost Timeline
Endpoint: GET /api/v1/billing/realtime-cost/tenants/:tenantId/timeline
Returns cost accumulation over time for the current billing period, suitable for chart rendering.
RealtimeCostMetric Entity
| Field | Type | Description |
|---|---|---|
id | UUID | Metric identifier |
tenantId | UUID | Owning tenant |
metricType | String | Cost category |
currentValue | BigDecimal | Current accumulated cost |
previousValue | BigDecimal | Previous period value for comparison |
threshold | BigDecimal | Alert threshold |
unit | String | Currency unit |
timestamp | LocalDateTime | Last update time |
Cost Alerts
Configure Alert
Endpoint: POST /api/v1/billing/realtime-cost/tenants/:tenantId/alerts
curl -X POST http://localhost:8087/api/v1/billing/realtime-cost/tenants/550e8400/alerts \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${TOKEN}" \
-d '{
"thresholdAmount": 5000.00,
"thresholdType": "ABSOLUTE",
"notifyEmail": "finance@acme.com",
"notifySlack": true
}'Alert Types
| Type | Description |
|---|---|
ABSOLUTE | Alert when total cost exceeds a fixed amount |
PERCENTAGE_OF_BUDGET | Alert when cost exceeds a percentage of the monthly budget |
DAILY_RATE | Alert when the daily cost rate exceeds a threshold |
ANOMALY | Alert when cost pattern deviates significantly from baseline |
List Active Alerts
Endpoint: GET /api/v1/billing/realtime-cost/tenants/:tenantId/alerts
Dismiss Alert
Endpoint: POST /api/v1/billing/realtime-cost/alerts/:alertId/dismiss
Cost Tracking Flow
Usage Events (Kafka)
|
v
UsageMeteringService --> RealtimeCostTrackingService
| |
v v
UsageRecord (DB) RealtimeCostMetric (DB)
|
v
Cost Alert Evaluation
|
v
Notification Service (email/slack)As usage events are recorded, the real-time cost tracking service calculates the cost impact based on the active pricing plan and updates the running totals. When thresholds are crossed, alerts are triggered through the Notification Service.