Chargeback & Financial Tracking
Relevant source files
The following files were used as context for generating this wiki page:
The Chargeback and Financial Tracking system provides a robust framework for calculating the costs associated with IT resource consumption across virtual machines, containers, and storage. It translates raw technical metrics (such as CPU usage, memory allocation, and disk I/O) into monetary values based on highly configurable rate definitions and tiered pricing models.
This system is designed to support multi-tenancy and varied infrastructure providers, allowing administrators to assign specific rates to different parts of the enterprise hierarchy, including providers, clusters, or tagged resources. The data generated through this module powers financial reporting, enabling organizational transparency and resource optimization.
Core Data Model and Architecture
The architecture is built around three primary entities: the Chargeback report, the Rate definitions, and Consumption tracking.
Entity Relationships
The relationship between rates, their specific details, and the pricing tiers is hierarchical. A ChargebackRate acts as a container for multiple ChargebackRateDetail objects, each targeting a specific metric (e.g., cpu_allocated). Each detail contains one or more ChargebackTier objects defining the price points.
The system supports two primary rate types: Compute and Storage. Compute rates typically track CPU and Memory, while Storage rates track disk allocation and usage.
Sources: app/models/chargeback_rate_detail.rb:1-10, spec/models/chargeback_rate_spec.rb:19-25, spec/models/chargeback_rate_detail_spec.rb:25-50
Rate Calculation Logic
The fundamental unit of cost calculation is the hourly_cost. Even if a rate is defined monthly or yearly, the system decomposes it to an hourly rate based on the consumption interval.
Tiers and Contiguity
Rates are defined using tiers. For a rate detail to be valid, its tiers must be contiguous, meaning they must start at 0, end at infinity, and have no gaps between them.
| Tier Property | Requirement | Description |
|---|---|---|
| Start | 0.0 (First Tier) | The first tier must always begin at zero usage. |
| Finish | Infinity (Last Tier) | the final tier must cover all usage beyond its start point. |
| Continuity | Tier[n].finish == Tier[n+1].start | There can be no gaps or overlaps between tiers. |
Sources: app/models/chargeback_rate_detail.rb:190-210, spec/models/chargeback_rate_detail_spec.rb:228-260
Mathematical Calculation
The cost for a specific metric is calculated as follows:
- Metric Measurement: The raw value is retrieved from consumption data.
- Rate Adjustment: Values are adjusted to match the rate unit (e.g., converting bytes from metrics into Gigabytes for the rate).
- Tier Selection: The system finds the tier where
Adjusted Valuefalls betweentier.startandtier.finish. - Hourly Decomposition: The fixed and variable rates of the tier are divided by the number of hours in the rate's time period (e.g., 24 for daily, 720 for monthly).
- Final Cost:
Hourly Fixed Rate + (Adjusted Value * Hourly Variable Rate).
Sources: app/models/chargeback_rate_detail.rb:147-165, spec/models/chargeback_rate_detail_spec.rb:120-140
Consumption Tracking
Chargeback calculations rely on Consumption objects, which represent the resource usage of a specific entity (like a VM) over a period.
Consumption Types
- ConsumptionWithRollups: Used when performance metrics (rollups) are available. It aggregates actual usage data over time.
- ConsumptionWithoutRollups: Used when metrics are missing. It relies on the last known configuration state (e.g., the number of CPUs allocated to a VM) to charge for the existence of the resource.
Report Generation and Assignment
Chargeback results are generated for reports through the build_results_for_report_chargeback method. This process involves identifying the applicable rates based on assignments and then calculating costs for every row in the report.
Assignment Hierarchy
Rates can be assigned at various levels. When multiple rates apply (cumulative chargeback), the system accumulates values from all relevant rate details. Descriptions for assignment types include:
- The Enterprise: Global default rates.
- Selected Providers: Rates specific to a Cloud/Infra provider.
- Tagged Resources: Rates assigned to entities with specific metadata (e.g.,
environment/production).
| Assignment Type | Description | Target |
|---|---|---|
object | The Enterprise | MiqEnterprise |
object | Selected Clusters | EmsCluster |
tag | Tagged VMs and Instances | Vm |
label | Labeled Container Images | ContainerImage |
Sources: app/models/chargeback.rb:45-70, spec/models/chargeback_rate_spec.rb:110-150
Dynamic Columns
The Chargeback model dynamically generates columns for reports based on the metrics being tracked. Each chargeable field creates a set of columns:
{metric}_rate: The human-readable rate string.{metric}_metric: The adjusted numerical value of usage.{metric}_cost: The calculated monetary cost.
Sources: app/models/chargeback.rb:30-43, app/models/chargeback_rate_detail.rb:120-130
Summary
The Chargeback and Financial Tracking system provides a highly granular approach to cloud cost management. By combining hardware configuration data with active performance metrics, it ensures that organizations can accurately bill for both allocated capacity and actual utilization. The flexible tiering and assignment models allow the system to adapt to complex enterprise environments where different departments or projects may operate under different financial constraints.
Sources: app/models/chargeback.rb, app/models/chargeback_rate_detail.rb, spec/models/chargeback_vm_spec.rb