This document provides guidance for AI assistants working with the OpenCost codebase.
OpenCost is an open source Kubernetes cost monitoring tool maintained by the Cloud Native Computing Foundation (CNCF). It provides real-time cost allocation, asset tracking, and cloud cost monitoring for Kubernetes clusters across multiple cloud providers.
Key Features:
opencost/
├── cmd/costmodel/ # Main entry point (main.go)
├── core/ # Core module (shared libraries)
│ └── pkg/
│ ├── clusters/ # Cluster management
│ ├── env/ # Environment variable utilities
│ ├── filter/ # Query filter implementations
│ ├── log/ # Structured logging
│ ├── model/ # Core data models
│ ├── opencost/ # OpenCost domain types (Allocation, Asset, CloudCost)
│ ├── storage/ # Storage abstractions
│ └── util/ # Utility packages
├── modules/
│ ├── collector-source/ # Custom metrics collector (alternative to Prometheus)
│ └── prometheus-source/ # Prometheus data source implementation
├── pkg/
│ ├── cloud/ # Cloud provider implementations
│ │ ├── aws/
│ │ ├── azure/
│ │ ├── gcp/
│ │ ├── alibaba/
│ │ ├── oracle/
│ │ ├── digitalocean/
│ │ ├── scaleway/
│ │ └── otc/ # Open Telekom Cloud
│ ├── cloudcost/ # Cloud cost processing pipeline
│ ├── clustercache/ # Kubernetes cluster caching
│ ├── cmd/costmodel/ # Cost model command implementation
│ ├── config/ # Configuration management
│ ├── costmodel/ # Core cost model logic and API handlers
│ ├── customcost/ # Custom cost plugin support
│ ├── env/ # Environment variable definitions
│ ├── mcp/ # MCP server implementation
│ └── metrics/ # Prometheus metrics
├── configs/ # Default pricing configurations
├── kubernetes/ # Kubernetes manifests (deprecated - use Helm)
├── protos/ # Protocol buffer definitions
├── spec/ # OpenCost specification
└── ui/ # UI components (main UI in opencost/opencost-ui repo)
buildx support# Run all unit tests
just test
# Run tests for specific module
just test-core
just test-opencost
just test-prometheus-source
just test-collector-source
# Build local binary
just build-local
# Run locally (requires Prometheus and optionally Kubernetes access)
PROMETHEUS_SERVER_ENDPOINT="http://127.0.0.1:9080" go run ./cmd/costmodel/main.go
# Start development environment with Tilt
tilt up
Set PROMETHEUS_SERVER_ENDPOINT to your Prometheus URL:
# Port-forward to Prometheus in your cluster
kubectl port-forward svc/prometheus-server 9080:80
# Run OpenCost
PROMETHEUS_SERVER_ENDPOINT="http://127.0.0.1:9080" go run ./cmd/costmodel/main.go
INTEGRATION=true just test-integration
# Build local binary
just build-local
# Build multi-arch binaries
just build-binary <version>
# Build and push Docker image
just build <image-tag> <release-version>
# Validate protobuf definitions
just validate-protobuf
| Variable | Default | Description |
|---|---|---|
PROMETHEUS_SERVER_ENDPOINT |
(required) | Prometheus server URL |
API_PORT |
9003 |
OpenCost API port |
CLUSTER_ID |
auto-detected | Cluster identifier |
CONFIG_PATH |
/var/configs |
Configuration directory |
| Variable | Default | Description |
|---|---|---|
MCP_SERVER_ENABLED |
false |
Enable MCP server |
MCP_HTTP_PORT |
8081 |
MCP server HTTP port |
| Variable | Description |
|---|---|
AWS_ACCESS_KEY_ID |
AWS authentication |
AWS_SECRET_ACCESS_KEY |
AWS authentication |
AZURE_OFFER_ID |
Azure pricing offer ID |
AZURE_BILLING_ACCOUNT |
Azure billing account |
CLOUD_PROVIDER |
Force cloud provider (aws, azure, gcp, etc.) |
USE_CSV_PROVIDER |
Enable CSV-based custom pricing |
CSV_PATH |
Path to CSV pricing file |
| Variable | Default | Description |
|---|---|---|
PROMETHEUS_QUERY_TIMEOUT |
120s |
Query timeout |
PROMETHEUS_QUERY_RESOLUTION_SECONDS |
300 |
Query resolution |
MAX_QUERY_CONCURRENCY |
5 |
Concurrent queries |
PROM_CLUSTER_ID_LABEL |
cluster_id |
Cluster ID label name |
| Variable | Default | Description |
|---|---|---|
CLOUD_COST_ENABLED |
false |
Enable cloud cost ingestion |
CARBON_ESTIMATES_ENABLED |
false |
Enable carbon estimation |
COLLECTOR_DATA_SOURCE_ENABLED |
false |
Use collector instead of Prometheus |
Main API runs on port 9003 by default:
| Endpoint | Description |
|---|---|
GET /allocation |
Cost allocation data |
GET /allocation/summary |
Summarized allocation |
GET /assets |
Asset cost data |
GET /assets/carbon |
Asset carbon estimates |
GET /cloudCost |
Cloud cost data |
GET /customCost/status |
Custom cost status |
GET /metrics |
Prometheus metrics |
github.com/opencost/opencost/core/pkg/logpkg/env or core/pkg/envBefore committing, always run:
go fmt ./...
go vet ./...
OpenCost uses Go workspace with multiple modules:
github.com/opencost/opencost - Main modulegithub.com/opencost/opencost/core - Core shared librarygithub.com/opencost/opencost/modules/prometheus-source - Prometheus integrationgithub.com/opencost/opencost/modules/collector-source - Metrics collectorWhen adding dependencies, ensure they're added to the correct module.
*_test.go files)INTEGRATION=true environment variableimport "github.com/opencost/opencost/core/pkg/log"
log.Infof("Processing allocation for window: %s", window)
log.Errorf("Failed to query Prometheus: %v", err)
log.Warnf("Missing pricing data, using defaults")
log.Debugf("Detailed debug information")
Fixes #123, Closes #456just test before submittingSigned-off-by header required)Allocation - Cost allocation for a workload over a time windowAsset - Infrastructure asset (node, disk, load balancer)CloudCost - Cloud service costs from billing APIsWindow - Time range for queriesOpenCost auto-detects the cloud provider from:
CLOUD_PROVIDER environment variable (explicit override)pkg/cloud/<provider>/models.Provider interfacepkg/env/costmodel.gopkg/cloud/provider/provider.goconfigs/pkg/costmodel/router.go or appropriate filepkg/cmd/costmodel/costmodel.go*_test.go file.proto files in protos/./generate.sh to regenerate Go codejust validate-protobuf to verifyCore formulas from the OpenCost Specification (spec/opencost-specv01.md):