Sub topic 5
Tagging compute to attribute cost
This chapter assumes you can query system tables in SQL, know how clusters and jobs are created in a Databricks workspace, and have seen system.billing.usage from the previous chapter. The goal is to make every DBU traceable to a team, project, or environment.
Why tags matter
system.billing.usage tells you what was consumed. By itself it does not tell you who should pay for it. Attribution comes from custom tags, which are key-value pairs you attach to compute. Databricks copies them into the custom_tags column of each usage record, and they are also applied to the underlying cloud instances. This means the same tag appears in Databricks usage data and in your cloud provider's bill.
A tag only describes usage recorded after it was applied. Changing a tag does not rewrite past records.
How tags reach the usage table
Tags can come from several places. Databricks merges them, and the result lands in custom_tags.
- Tags set on a cluster, job cluster, or SQL warehouse.
- Tags set by a compute policy, which can fix a value so users cannot change it.
- Default tags that Databricks adds itself, such as cluster and job identifiers. These are separate from your custom tags.
- Workspace-level tags for serverless usage, which is covered in a later chapter. Serverless compute has no cluster for you to tag, so you attribute it differently.
Choosing a tag scheme
Keep the scheme small and consistent. A few keys cover most needs.
| Key | Example values | Answers the question |
|---|---|---|
| `team` | `data-eng`, `analytics` | Who owns this spend? |
| `env` | `dev`, `prod` | Is this production or experimentation? |
| `project` | `churn-model` | Which initiative does it support? |
| `cost_center` | `cc-1042` | Which budget is charged? |
Tag keys are case-sensitive in queries, so Team and team are different keys. Pick one spelling and enforce it.
Setting tags on a job cluster
Put tags in the job definition so every run is tagged without anyone remembering to do it.
{
"job_clusters": [
{
"job_cluster_key": "etl_cluster",
"new_cluster": {
"spark_version": "15.4.x-scala2.12",
"node_type_id": "i3.xlarge",
"autoscale": { "min_workers": 2, "max_workers": 6 },
"aws_attributes": { "availability": "SPOT_WITH_FALLBACK", "first_on_demand": 1 },
"custom_tags": { "team": "data-eng", "env": "prod" }
}
}
]
}Enforcing tags with a policy
Relying on users to tag by hand leads to gaps. A compute policy can fix a tag so it is always present. The custom_tags.team entry below forces the value data-eng onto every cluster created under the policy.
{
"custom_tags.team": { "type": "fixed", "value": "data-eng" }
}You would normally create one policy per team, each with that team's value. The next chapters cover policies in full.
Querying cost by tag
Join usage to list prices and group by the tag. This query estimates cost per workspace and team over the last 30 days.
SELECT
u.workspace_id,
u.custom_tags['team'] AS team,
SUM(u.usage_quantity * p.pricing.default) AS estimated_list_cost
FROM system.billing.usage u
JOIN system.billing.list_prices p
ON u.sku_name = p.sku_name
AND u.cloud = p.cloud
AND u.usage_unit = p.usage_unit
AND u.usage_end_time >= p.price_start_time
AND (p.price_end_time IS NULL OR u.usage_end_time < p.price_end_time)
WHERE u.usage_date >= date_sub(current_date(), 30)
GROUP BY u.workspace_id, u.custom_tags['team']
ORDER BY estimated_list_cost DESC;Rows where team is NULL are untagged usage. The size of that row is your best measure of how well tagging works. The result uses list prices, so it ignores negotiated discounts and cloud infrastructure charges.
Common errors
- Tagging only some compute types. Interactive clusters, job clusters, and warehouses each need tags. Check all three.
- Expecting retroactive changes. Fix tags going forward and report the old gap as "untagged".
- Free-form values. Users type
data-eng,DataEng, anddata eng. Use fixed or allowed-list values in policies. - Cloud tag limits. Cloud providers limit the number and format of tags. Keep keys and values short and simple.
Exercise
Your cost query shows 35% of estimated spend with a NULL team. Name two likely causes and one change that prevents it going forward.
Show answer
Likely causes: clusters or warehouses were created without a team tag, or usage comes from a compute type that was never tagged (for example, an interactive cluster or a SQL warehouse). Another cause is a differently spelled key such as Team. To prevent it, use compute policies that set custom_tags.team as a fixed value (or require it) so every cluster is tagged at creation.