Cost Tracking Flow Architecture

This document illustrates how the Blueberry IDP tracks and analyzes costs for environments, resources, and usage patterns.

Cost Tracking System Overview

graph TB subgraph "Cost Data Sources" GCP_BILLING[GCP Billing
Export] K8S_METRICS[Kubernetes
Metrics] USAGE_METRICS[Usage
Metrics] CUSTOM_METRICS[Custom
Metrics] end subgraph "Data Collection" COST_COLLECTOR[Cost
Collector] METRICS_SCRAPER[Metrics
Scraper] BILLING_PROCESSOR[Billing
Processor] USAGE_AGGREGATOR[Usage
Aggregator] end subgraph "Data Processing" COST_CALCULATOR[Cost
Calculator] ALLOCATION_ENGINE[Allocation
Engine] TREND_ANALYZER[Trend
Analyzer] BUDGET_PROCESSOR[Budget
Processor] end subgraph "Data Storage" COST_METRICS[(Cost Metrics
Time Series)] BILLING_DATA[(Billing Data
Firestore)] USAGE_CACHE[(Usage Cache
Redis)] REPORTS_STORE[(Reports
GCS)] end subgraph "Analysis & Reporting" COST_DASHBOARD[Cost
Dashboard] BUDGET_ALERTS[Budget
Alerts] COST_REPORTS[Cost
Reports] OPTIMIZATION[Optimization
Recommendations] end subgraph "External Systems" SLACK_NOTIFICATIONS[Slack
Notifications] EMAIL_ALERTS[Email
Alerts] BILLING_SYSTEM[Billing
System] REPORTING_TOOLS[Reporting
Tools] end %% Data flow from sources GCP_BILLING --> COST_COLLECTOR K8S_METRICS --> METRICS_SCRAPER USAGE_METRICS --> USAGE_AGGREGATOR CUSTOM_METRICS --> BILLING_PROCESSOR %% Processing pipeline COST_COLLECTOR --> COST_CALCULATOR METRICS_SCRAPER --> ALLOCATION_ENGINE BILLING_PROCESSOR --> TREND_ANALYZER USAGE_AGGREGATOR --> BUDGET_PROCESSOR %% Storage operations COST_CALCULATOR --> COST_METRICS ALLOCATION_ENGINE --> BILLING_DATA TREND_ANALYZER --> USAGE_CACHE BUDGET_PROCESSOR --> REPORTS_STORE %% Analysis and reporting COST_METRICS --> COST_DASHBOARD BILLING_DATA --> BUDGET_ALERTS USAGE_CACHE --> COST_REPORTS REPORTS_STORE --> OPTIMIZATION %% External integrations BUDGET_ALERTS --> SLACK_NOTIFICATIONS BUDGET_ALERTS --> EMAIL_ALERTS COST_REPORTS --> BILLING_SYSTEM OPTIMIZATION --> REPORTING_TOOLS %% Feedback loops COST_DASHBOARD -.->|"User Actions"| COST_COLLECTOR OPTIMIZATION -.->|"Recommendations"| COST_CALCULATOR classDef source fill:#e3f2fd,stroke:#1976d2,stroke-width:2px classDef collect fill:#fff3e0,stroke:#f57c00,stroke-width:2px classDef process fill:#e8f5e9,stroke:#388e3c,stroke-width:2px classDef storage fill:#fce4ec,stroke:#c2185b,stroke-width:2px classDef analysis fill:#f3e5f5,stroke:#7b1fa2,stroke-width:2px classDef external fill:#e0f2f1,stroke:#00695c,stroke-width:2px class GCP_BILLING,K8S_METRICS,USAGE_METRICS,CUSTOM_METRICS source class COST_COLLECTOR,METRICS_SCRAPER,BILLING_PROCESSOR,USAGE_AGGREGATOR collect class COST_CALCULATOR,ALLOCATION_ENGINE,TREND_ANALYZER,BUDGET_PROCESSOR process class COST_METRICS,BILLING_DATA,USAGE_CACHE,REPORTS_STORE storage class COST_DASHBOARD,BUDGET_ALERTS,COST_REPORTS,OPTIMIZATION analysis class SLACK_NOTIFICATIONS,EMAIL_ALERTS,BILLING_SYSTEM,REPORTING_TOOLS external

Environment Cost Lifecycle

sequenceDiagram participant Environment Service participant Cost Tracker participant Metrics Collector participant GCP Billing participant Firestore participant Dashboard participant Budget Monitor participant Alert System Note over Environment Service,Alert System: Environment Cost Lifecycle %% Environment Creation Environment Service->>Cost Tracker: Environment created Cost Tracker->>Firestore: Initialize cost tracking Cost Tracker->>Metrics Collector: Start metrics collection %% Continuous Monitoring loop Every 5 minutes Metrics Collector->>GCP Billing: Query resource usage GCP Billing->>Metrics Collector: Usage data Metrics Collector->>Cost Tracker: Process usage metrics Cost Tracker->>Firestore: Update cost data end %% Budget Monitoring loop Every 15 minutes Budget Monitor->>Firestore: Check environment costs Budget Monitor->>Budget Monitor: Compare with budget alt Budget Exceeded Budget Monitor->>Alert System: Send budget alert Alert System->>Environment Service: Notify about cost end end %% Dashboard Updates loop Every 30 seconds Dashboard->>Firestore: Query cost data Firestore->>Dashboard: Return cost metrics Dashboard->>Dashboard: Update charts end %% Environment Termination Environment Service->>Cost Tracker: Environment terminated Cost Tracker->>Firestore: Finalize cost calculation Cost Tracker->>Metrics Collector: Stop metrics collection Cost Tracker->>Dashboard: Update final cost

Cost Allocation and Attribution

graph TB subgraph "Resource Mapping" KUBERNETES[Kubernetes
Resources] GCP_RESOURCES[GCP
Resources] NAMESPACE[Namespace
Mapping] LABELS[Resource
Labels] end subgraph "Cost Categories" COMPUTE[Compute
Costs] STORAGE[Storage
Costs] NETWORK[Network
Costs] SERVICES[Service
Costs] end subgraph "Allocation Methods" DIRECT[Direct
Attribution] PROPORTIONAL[Proportional
Allocation] USAGE_BASED[Usage-Based
Allocation] FIXED[Fixed
Allocation] end subgraph "Cost Dimensions" ENVIRONMENT[Environment
Level] USER[User
Level] PROJECT[Project
Level] ORGANIZATION[Organization
Level] end %% Resource to cost mapping KUBERNETES --> COMPUTE GCP_RESOURCES --> STORAGE NAMESPACE --> NETWORK LABELS --> SERVICES %% Allocation methods COMPUTE --> DIRECT STORAGE --> PROPORTIONAL NETWORK --> USAGE_BASED SERVICES --> FIXED %% Cost dimensions DIRECT --> ENVIRONMENT PROPORTIONAL --> USER USAGE_BASED --> PROJECT FIXED --> ORGANIZATION classDef resource fill:#e3f2fd,stroke:#1976d2,stroke-width:2px classDef cost fill:#fff3e0,stroke:#f57c00,stroke-width:2px classDef allocation fill:#e8f5e9,stroke:#388e3c,stroke-width:2px classDef dimension fill:#fce4ec,stroke:#c2185b,stroke-width:2px class KUBERNETES,GCP_RESOURCES,NAMESPACE,LABELS resource class COMPUTE,STORAGE,NETWORK,SERVICES cost class DIRECT,PROPORTIONAL,USAGE_BASED,FIXED allocation class ENVIRONMENT,USER,PROJECT,ORGANIZATION dimension

Cost Metrics and KPIs

graph LR subgraph "Raw Metrics" CPU_USAGE[CPU
Usage] MEMORY_USAGE[Memory
Usage] STORAGE_USAGE[Storage
Usage] NETWORK_USAGE[Network
Usage] end subgraph "Cost Metrics" COST_PER_HOUR[Cost
per Hour] COST_PER_ENV[Cost
per Environment] COST_PER_USER[Cost
per User] TOTAL_COST[Total
Cost] end subgraph "Efficiency Metrics" UTILIZATION[Resource
Utilization] COST_EFFICIENCY[Cost
Efficiency] WASTE_RATIO[Waste
Ratio] OPTIMIZATION_SCORE[Optimization
Score] end subgraph "Business Metrics" COST_TREND[Cost
Trend] BUDGET_VARIANCE[Budget
Variance] FORECAST[Cost
Forecast] ROI[Return on
Investment] end %% Raw to cost metrics CPU_USAGE --> COST_PER_HOUR MEMORY_USAGE --> COST_PER_ENV STORAGE_USAGE --> COST_PER_USER NETWORK_USAGE --> TOTAL_COST %% Cost to efficiency metrics COST_PER_HOUR --> UTILIZATION COST_PER_ENV --> COST_EFFICIENCY COST_PER_USER --> WASTE_RATIO TOTAL_COST --> OPTIMIZATION_SCORE %% Efficiency to business metrics UTILIZATION --> COST_TREND COST_EFFICIENCY --> BUDGET_VARIANCE WASTE_RATIO --> FORECAST OPTIMIZATION_SCORE --> ROI classDef raw fill:#e3f2fd,stroke:#1976d2,stroke-width:2px classDef cost fill:#fff3e0,stroke:#f57c00,stroke-width:2px classDef efficiency fill:#e8f5e9,stroke:#388e3c,stroke-width:2px classDef business fill:#fce4ec,stroke:#c2185b,stroke-width:2px class CPU_USAGE,MEMORY_USAGE,STORAGE_USAGE,NETWORK_USAGE raw class COST_PER_HOUR,COST_PER_ENV,COST_PER_USER,TOTAL_COST cost class UTILIZATION,COST_EFFICIENCY,WASTE_RATIO,OPTIMIZATION_SCORE efficiency class COST_TREND,BUDGET_VARIANCE,FORECAST,ROI business

Budget Management System

graph TB subgraph "Budget Planning" BUDGET_CREATE[Budget
Creation] BUDGET_APPROVAL[Budget
Approval] BUDGET_ALLOCATION[Budget
Allocation] BUDGET_MONITORING[Budget
Monitoring] end subgraph "Budget Types" MONTHLY[Monthly
Budget] PROJECT[Project
Budget] USER[User
Budget] ENVIRONMENT[Environment
Budget] end subgraph "Alert Thresholds" THRESHOLD_50[50%
Warning] THRESHOLD_80[80%
Alert] THRESHOLD_95[95%
Critical] THRESHOLD_100[100%
Limit] end subgraph "Actions" NOTIFY[Send
Notification] THROTTLE[Throttle
Resources] BLOCK[Block
Creation] ESCALATE[Escalate
to Admin] end %% Budget planning flow BUDGET_CREATE --> BUDGET_APPROVAL BUDGET_APPROVAL --> BUDGET_ALLOCATION BUDGET_ALLOCATION --> BUDGET_MONITORING %% Budget types BUDGET_ALLOCATION --> MONTHLY BUDGET_ALLOCATION --> PROJECT BUDGET_ALLOCATION --> USER BUDGET_ALLOCATION --> ENVIRONMENT %% Monitoring to thresholds BUDGET_MONITORING --> THRESHOLD_50 BUDGET_MONITORING --> THRESHOLD_80 BUDGET_MONITORING --> THRESHOLD_95 BUDGET_MONITORING --> THRESHOLD_100 %% Threshold actions THRESHOLD_50 --> NOTIFY THRESHOLD_80 --> THROTTLE THRESHOLD_95 --> BLOCK THRESHOLD_100 --> ESCALATE classDef planning fill:#e3f2fd,stroke:#1976d2,stroke-width:2px classDef budget fill:#fff3e0,stroke:#f57c00,stroke-width:2px classDef threshold fill:#e8f5e9,stroke:#388e3c,stroke-width:2px classDef action fill:#fce4ec,stroke:#c2185b,stroke-width:2px class BUDGET_CREATE,BUDGET_APPROVAL,BUDGET_ALLOCATION,BUDGET_MONITORING planning class MONTHLY,PROJECT,USER,ENVIRONMENT budget class THRESHOLD_50,THRESHOLD_80,THRESHOLD_95,THRESHOLD_100 threshold class NOTIFY,THROTTLE,BLOCK,ESCALATE action

Cost Optimization Recommendations

graph TB subgraph "Analysis Engine" USAGE_ANALYSIS[Usage
Analysis] PATTERN_DETECTION[Pattern
Detection] ANOMALY_DETECTION[Anomaly
Detection] BENCHMARK_COMPARISON[Benchmark
Comparison] end subgraph "Optimization Areas" RESOURCE_SIZING[Resource
Sizing] SCHEDULING[Scheduling
Optimization] CLEANUP[Cleanup
Automation] RESERVED_INSTANCES[Reserved
Instances] end subgraph "Recommendations" RIGHTSIZING[Right-size
Resources] SCHEDULE_SHUTDOWN[Schedule
Shutdown] REMOVE_UNUSED[Remove
Unused] UPGRADE_INSTANCES[Upgrade
Instances] end subgraph "Implementation" AUTOMATED[Automated
Actions] MANUAL[Manual
Actions] APPROVAL_REQUIRED[Approval
Required] MONITORING[Monitoring
Impact] end %% Analysis to optimization USAGE_ANALYSIS --> RESOURCE_SIZING PATTERN_DETECTION --> SCHEDULING ANOMALY_DETECTION --> CLEANUP BENCHMARK_COMPARISON --> RESERVED_INSTANCES %% Optimization to recommendations RESOURCE_SIZING --> RIGHTSIZING SCHEDULING --> SCHEDULE_SHUTDOWN CLEANUP --> REMOVE_UNUSED RESERVED_INSTANCES --> UPGRADE_INSTANCES %% Recommendations to implementation RIGHTSIZING --> AUTOMATED SCHEDULE_SHUTDOWN --> MANUAL REMOVE_UNUSED --> APPROVAL_REQUIRED UPGRADE_INSTANCES --> MONITORING classDef analysis fill:#e3f2fd,stroke:#1976d2,stroke-width:2px classDef optimization fill:#fff3e0,stroke:#f57c00,stroke-width:2px classDef recommendation fill:#e8f5e9,stroke:#388e3c,stroke-width:2px classDef implementation fill:#fce4ec,stroke:#c2185b,stroke-width:2px class USAGE_ANALYSIS,PATTERN_DETECTION,ANOMALY_DETECTION,BENCHMARK_COMPARISON analysis class RESOURCE_SIZING,SCHEDULING,CLEANUP,RESERVED_INSTANCES optimization class RIGHTSIZING,SCHEDULE_SHUTDOWN,REMOVE_UNUSED,UPGRADE_INSTANCES recommendation class AUTOMATED,MANUAL,APPROVAL_REQUIRED,MONITORING implementation

Cost Tracking Configuration

Cost Collection Settings

# Cost tracking configuration
cost_tracking:
  enabled: true
  collection_interval: 300  # 5 minutes
  storage_retention: 90     # days

  metrics:
    - name: cpu_cost
      type: resource
      unit: core-hours
      rate: 0.031  # per core-hour

    - name: memory_cost
      type: resource
      unit: gb-hours
      rate: 0.0034  # per GB-hour

    - name: storage_cost
      type: resource
      unit: gb-months
      rate: 0.04  # per GB-month

    - name: network_cost
      type: usage
      unit: gb-egress
      rate: 0.085  # per GB egress

  allocation:
    method: proportional
    dimensions:
      - environment
      - user
      - project

  budgets:
    default_monthly: 1000  # USD
    alert_thresholds:
      warning: 50   # 50%
      critical: 80  # 80%
      limit: 95     # 95%

Cost Dashboard Configuration

# Dashboard configuration
dashboard:
  refresh_interval: 30  # seconds

  widgets:
    - type: cost_trend
      title: "Cost Trend (Last 30 Days)"
      metrics: [total_cost]
      period: 30d

    - type: environment_costs
      title: "Top 10 Environment Costs"
      metrics: [cost_per_environment]
      limit: 10

    - type: budget_status
      title: "Budget Status"
      metrics: [budget_used, budget_remaining]

    - type: optimization_savings
      title: "Optimization Savings"
      metrics: [potential_savings]

  alerts:
    - type: budget_exceeded
      threshold: 80
      severity: warning

    - type: cost_spike
      threshold: 200  # 200% of average
      severity: critical

Cost Reporting and Analytics

Cost Report Types

Report Type Frequency Audience Format
Daily Cost Summary Daily Operations Email
Weekly Cost Report Weekly Team Leads PDF
Monthly Budget Report Monthly Finance Excel
Quarterly Cost Analysis Quarterly Management Dashboard
Annual Cost Forecast Annually Executive Presentation

Cost Analytics Queries

-- Example cost analytics queries
-- Top 5 most expensive environments
SELECT
  environment_id,
  SUM(cost_usd) as total_cost,
  AVG(cost_usd) as avg_daily_cost
FROM cost_metrics
WHERE date >= DATE_SUB(CURRENT_DATE, INTERVAL 30 DAY)
GROUP BY environment_id
ORDER BY total_cost DESC
LIMIT 5;

-- Cost trend by user
SELECT
  user_id,
  DATE_TRUNC(date, WEEK) as week,
  SUM(cost_usd) as weekly_cost
FROM cost_metrics
WHERE date >= DATE_SUB(CURRENT_DATE, INTERVAL 12 WEEK)
GROUP BY user_id, week
ORDER BY week, weekly_cost DESC;

-- Budget utilization
SELECT
  budget_category,
  budget_amount,
  SUM(actual_cost) as used_amount,
  (SUM(actual_cost) / budget_amount) * 100 as utilization_percent
FROM budgets b
JOIN cost_metrics c ON b.category = c.category
WHERE c.date >= DATE_TRUNC(CURRENT_DATE, MONTH)
GROUP BY budget_category, budget_amount;

Cost Optimization Strategies

Strategy Impact Effort Savings
Auto-scaling High Medium 30-50%
Scheduled Shutdown Medium Low 15-30%
Resource Right-sizing High High 20-40%
Unused Resource Cleanup Low Low 5-15%
Reserved Instances Medium Medium 20-30%

Cost Monitoring Alerts

# Cost monitoring alerts
alerts:
  - name: "Environment Cost Spike"
    condition: "cost_per_hour > 2 * avg_cost_per_hour"
    duration: "10m"
    severity: "warning"

  - name: "Daily Budget Exceeded"
    condition: "daily_cost > daily_budget * 1.2"
    duration: "5m"
    severity: "critical"

  - name: "Unused Environment"
    condition: "environment_inactive_hours > 24"
    duration: "1h"
    severity: "info"

  - name: "Cost Anomaly"
    condition: "cost_deviation > 3 * standard_deviation"
    duration: "15m"
    severity: "warning"

Cost Data Export and Integration

Export Formats

# Cost data export formats
def export_cost_data(format_type, date_range, filters=None):
    """Export cost data in various formats"""

    formats = {
        'json': export_to_json,
        'csv': export_to_csv,
        'excel': export_to_excel,
        'pdf': export_to_pdf,
        'api': export_to_api
    }

    if format_type not in formats:
        raise ValueError(f"Unsupported format: {format_type}")

    return formats[format_type](/developer-docs/docs/architecture/diagrams/date_range, filters)

# API endpoint for cost data
@app.get("/api/costs/export")
async def export_costs(
    format: str = "json",
    start_date: str = None,
    end_date: str = None,
    environment_id: str = None,
    user_id: str = None
):
    """Export cost data with filters"""
    pass

Integration with External Systems

# External system integrations
integrations:
  billing_system:
    enabled: true
    endpoint: "https://billing.example.com/api/costs"
    authentication: "api_key"
    sync_frequency: "daily"

  monitoring_system:
    enabled: true
    type: "prometheus"
    metrics_endpoint: "/metrics"

  slack_notifications:
    enabled: true
    webhook_url: "${SLACK_WEBHOOK_URL}"
    channels:
      - "#cost-alerts"
      - "#finance-team"

Last Updated: January 2024

Document ID: architecture/diagrams/cost-tracking-flow