Cost Tracking Setup Guide

This guide explains how to set up and configure cost tracking for the Blueberry IDP.

Overview

The cost tracking feature provides:
- Automatic cost estimation for all environments
- Real-time cost dashboard with visualizations
- Integration with GCP Billing API for actual cost data
- Cost optimization recommendations
- Automated tracking throughout environment lifecycle

Prerequisites

  1. GCP project with billing enabled
  2. Appropriate IAM permissions:
  3. billing.accounts.get
  4. billing.resourceCosts.get (for Cloud Billing API)
  5. bigquery.jobs.create (if using BigQuery billing export)

Configuration

1. Install Dependencies

The google-cloud-billing package is already included in pyproject.toml:

pip install -e ".[dev]"

2. Configure Billing Account

Set the billing account ID in your environment or Helm values:

Environment Variable

export BILLING_ACCOUNT_ID="012345-678901-234567"

Helm Values

billing:
  enabled: true
  accountId: "012345-678901-234567"
  exportDataset: "billing_export"  # Optional: BigQuery dataset

3. Enable Automatic Cost Tracking

Cost tracking is automatically enabled for:
- New environment creation (manual and PR-based)
- Environment lifecycle events (via cleanup job)

4. Deploy the Cleanup CronJob

The cleanup job handles expired environments and updates final costs:

cleanup:
  enabled: true
  schedule: "0 */6 * * *"  # Every 6 hours

Usage

Cost Dashboard

Access the cost dashboard at:

https://your-domain.com/api/observability/costs/dashboard

Features:
- Total monthly costs
- Active environment count
- Average cost per environment
- Cost trends (7/30/90 days)
- Cost breakdown by service and environment type
- Optimization recommendations

API Endpoints

Get Cost Summary

curl -H "Authorization: Bearer $TOKEN" \
  https://your-domain.com/api/observability/costs/summary?days=30

Get Environment Cost

curl -H "Authorization: Bearer $TOKEN" \
  https://your-domain.com/api/observability/costs/environment/{env_id}

Update Environment Cost

curl -X POST -H "Authorization: Bearer $TOKEN" \
  https://your-domain.com/api/observability/costs/environment/{env_id}/update

Cost Calculation Details

Base Resources

  • CPU: 0.25 cores
  • Memory: 0.5 GB
  • Storage: 5 GB

Additional Resources

  • Database: +0.25 CPU, +0.5 GB memory, +10 GB storage
  • Each frontend: +0.125 CPU, +0.25 GB memory
  • Redis (if enabled): +0.125 CPU, +0.25 GB memory

Pricing Model

Based on GKE Autopilot pricing:
- CPU: $0.0445/core/hour
- Memory: $0.0049/GB/hour
- Storage: $0.00013/GB/hour
- 15% networking overhead

Additional Services

  • Firestore: Document operations and storage
  • GCS: Storage and operations
  • Load Balancer: Forwarding rules and data processing
  • Secret Manager: Active secrets and access operations

Troubleshooting

No Billing Data Available

If you see "billing_api_not_available" errors:
1. Verify the google-cloud-billing package is installed
2. Check service account permissions
3. Ensure billing is enabled for the project

Missing Billing Account

If billing account discovery fails:
1. Set BILLING_ACCOUNT_ID explicitly
2. Verify the service account has billing.accounts.get permission
3. Check that the project is linked to a billing account

Cost Data Not Updating

If costs aren't automatically tracked:
1. Check the cleanup CronJob is running: kubectl get cronjobs
2. View cleanup job logs: kubectl logs -l app.kubernetes.io/component=cleanup
3. Verify environment store connectivity

Best Practices

  1. Regular Monitoring: Check the cost dashboard weekly
  2. Act on Recommendations: Follow optimization suggestions
  3. Set TTL Appropriately: Shorter TTLs reduce costs
  4. Clean Up Regularly: Ensure the cleanup job runs successfully
  5. Review Long-Running Environments: Environments >72 hours should be reviewed

Advanced Configuration

BigQuery Billing Export

For detailed historical analysis, enable BigQuery billing export:

  1. Enable billing export in GCP Console
  2. Set the dataset in configuration:
    yaml billing: exportDataset: "your_project.billing_export"

Custom Pricing

To adjust pricing for your specific contract:

  1. Modify the pricing model in cost_tracker.py
  2. Update the pricing dictionary with your rates
  3. Rebuild and deploy the application

Security Notes

  • Cost data is only accessible to authenticated users
  • Billing account IDs are not exposed in logs
  • SQL queries use parameterization to prevent injection
  • Sensitive billing data is not stored locally
Document ID: guides/monitoring/cost-tracking-setup