> ## Documentation Index
> Fetch the complete documentation index at: https://docs.costory.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Allocate the cost of a shared Cloud SQL database to the teams using it

> Use Costory's virtual dimensions and usage metrics to split a shared Cloud SQL database cost across consuming teams, based on actual storage or compute usage.

A shared Cloud SQL instance lands as one line on the bill, even when several services query it. A flat split assigns every consumer the same share. That is a misleading cost allocation (The process of assigning shared or untagged cloud costs to specific teams, products, or environments.).

This walkthrough splits Cloud SQL spend with a Datadog usage metric. You preview the formula in **Advanced Explorer**, then store the split as a virtual dimension (Rule-based dimensions that map cloud resources to business groupings like teams, products, or environments.) so reports and alerts can reuse it.

![Advanced Explorer bar chart with Cost #1, Datadog render.service.cpu.time series, and formula a \* b / d allocating Cloud SQL cost](https://mintcdn.com/costory/5tKsyyiZ-p6P_OHL/use-cases/allocate_shared_cloud_sql_database/13.png?fit=max\&auto=format\&n=5tKsyyiZ-p6P_OHL\&q=85\&s=9d9988bf4c589e965b9de2a408b5516c)

Advanced Explorer preview of shared Cloud SQL cost split with formula a \* b / d

## Prerequisites

1. Know what drove the last database cost increase. Use a storage-size metric if storage grew. Use a compute metric if CPU or memory was the constraint.

2. Know where that usage metric lives, for example Datadog or [Google Cloud Monitoring](https://docs.costory.io/setup/usage-metrics/gcp-cloud-monitoring.md).

3. Have a Datadog connection under **Integrations**. This walkthrough uses the demo connection **Docs Datadog**. To split on a native GCP series instead, connect [GCP Cloud Monitoring](https://docs.costory.io/setup/usage-metrics/gcp-cloud-monitoring.md) and pick a Cloud SQL metric such as `cloudsql.googleapis.com/database/postgresql/insights/aggregate/execution_time`.

## What you get

* Each service's share of the shared Cloud SQL cost.
* Recurring reports so stakeholders see the allocated split.
* Alerts when a service's share grows unexpectedly.

## Steps

**1. Choose the usage metric**

Select the metric that matches your cost driver. The tabs below are example queries for Datadog and Google Cloud SQL.

This walkthrough uses a connected Datadog **render** metric (`render.service.cpu.time`) as the compute-style driver, grouped by `service.name`.

Storage-size example queries:

> [tab] Datadog
>
> ```
> avg:postgresql.database_size{<yourWhereClause>} by {db}.rollup(avg, daily, 'UTC')
> ```

> [tab] Google Cloud SQL
>
> Not available. Use a Datadog-sourced metric instead.

Compute example queries:

> [tab] Datadog
>
> ```
> sum:postgresql.queries.time{<yourWhereClause>} by {user}.rollup(sum, daily, 'UTC')
> ```

> [tab] Google Cloud SQL
>
> ```
> sum by ("user")(
>   rate({
>     "__name__"="cloudsql.googleapis.com/database/postgresql/insights/aggregate/execution_time",
>     "monitored_resource"="cloudsql_instance_database"
>   }[${__interval}])
> )
> ```

**2. Connect the metric as an External metric**

Go to [Integrations](https://app.costory.io/integration), click **+ Add integration**, then under **External metric** choose **Datadog**. Do not use **Metrics** > **Import new metrics datasource**.&#x20;

![Add integration page showing Billing tiles and an External metric section with Datadog, AWS, GCP, Amplitude, Tsuga, and Google Sheets](https://mintcdn.com/costory/7N_qRRx_7ACcj7QT/images/external-metrics/add-integration-external-metrics.png?fit=max\&auto=format\&n=7N_qRRx_7ACcj7QT\&q=85\&s=9c59528a5772931faac45e405038bb02)

Add integration page with the External metric grid, including Datadog

In this walkthrough **Docs Datadog** is already connected, so you can skip the connect form and query the metric in the next step. If you still need to connect Datadog, follow [Datadog metrics](https://docs.costory.io/setup/usage-metrics/datadog.md), then click **Validate** and **Save connection**.

![Integrations Data sources list showing Docs Datadog with metrics and metrics\_ci badges](https://mintcdn.com/costory/7N_qRRx_7ACcj7QT/images/external-metrics/integrations-datasources-with-datadog.png?fit=max\&auto=format\&n=7N_qRRx_7ACcj7QT\&q=85\&s=bd65a15ff233474f18d0858164157474)

Integrations Data sources list with Docs Datadog already connected

**3. Add Cloud SQL cost and per-service usage**

Try the split in **Advanced Explorer** before you store it as a virtual dimension. Click **Explore your data**, then switch to **Advanced**. **Classic** only accepts cost metrics.

> [!NOTE]
>
> Query Datadog and other [external metrics](https://docs.costory.io/setup/usage-metrics) in **Advanced Explorer** only.

You will build four series. The formula editor does not support `SUM()`, so the all-services total is its own series. Add cost and per-service usage here. The next step adds the denominator and the formula.

| Series | Role | Settings |
| - | - | - |
| **Cost #1 (a)** | Shared Cloud SQL cost | **Metric** **Cost**, **Aggregate** **SUM**, **Group By** **Service Name**, **Where** **Service Name** **In** **Cloud SQL** |
| **Externalmetric #2 (b)** | This service's usage | **Docs Datadog**, `render.service.cpu.time`, **Aggregate** **SUM**, **Group by** `service.name` |
| **Formula #3 (c)** | Allocated cost | `a * b / d` |
| **Externalmetric #4 (d)** | Usage across all services | Same `render.service.cpu.time` metric, **Aggregate** **SUM**, **Group by** **None** |

`a * b / d` is shared Cloud SQL cost times this service's `cpu.time`, divided by all services' `cpu.time`.

Configure **Cost #1 (a)** as in the table. Open **Where** and set **Service Name** **In** **Cloud SQL**.

![Advanced Explorer Cost #1 row with Where popover set to Service Name In Cloud SQL and a Cloud SQL bar chart](https://mintcdn.com/costory/5tKsyyiZ-p6P_OHL/use-cases/allocate_shared_cloud_sql_database/03.png?fit=max\&auto=format\&n=5tKsyyiZ-p6P_OHL\&q=85\&s=6836d8b3e91d2a430745e94ebb45b919)

Cost #1 (a) in Advanced Explorer, Where Service Name In Cloud SQL

Click **+ Add Metric**, open **integrations**, and choose **Docs Datadog**. Search `render`, then pick `render.service.cpu.time`. Set **Aggregate** to **SUM** and **Group by** to `service.name`.

![Advanced Explorer Datadog metric picker searching render and listing render.service.cpu.time](https://mintcdn.com/costory/5tKsyyiZ-p6P_OHL/use-cases/allocate_shared_cloud_sql_database/07.png?fit=max\&auto=format\&n=5tKsyyiZ-p6P_OHL\&q=85\&s=158284ed253cefce3b68950c4a255392)

Docs Datadog metric picker searching render, with render.service.cpu.time in the list

![Advanced Explorer dual-axis chart of Cloud SQL cost and Datadog render.service.cpu.time grouped by service.name](https://mintcdn.com/costory/5tKsyyiZ-p6P_OHL/use-cases/allocate_shared_cloud_sql_database/10.png?fit=max\&auto=format\&n=5tKsyyiZ-p6P_OHL\&q=85\&s=687cad8cc65f54e4aac9a9e100bd8dab)

Cost next to render.service.cpu.time grouped by service.name

Use **SUM** when you later aggregate this metric at weekly or monthly levels. A maximum aggregation can over-allocate cost to the service that spiked on a single day.

**4. Add the usage total and the allocation formula**

Click **+ Add Metric** again. Pick the same **Docs Datadog** metric `render.service.cpu.time`, **Aggregate** **SUM**, **Group by** **None**. That series is **Externalmetric #4 (d)**, the denominator.

Set **Formula #3 (c)** to `a * b / d`. Toggle the checkboxes on each series so you can inspect the allocated series on its own.

![Advanced Explorer with Cost a, Datadog series b grouped by service.name, formula a \* b / d, and Datadog series d grouped by None](https://mintcdn.com/costory/5tKsyyiZ-p6P_OHL/use-cases/allocate_shared_cloud_sql_database/13.png?fit=max\&auto=format\&n=5tKsyyiZ-p6P_OHL\&q=85\&s=9d9988bf4c589e965b9de2a408b5516c)

Working allocation formula a \* b / d with Externalmetric #4 grouped by None

**5. Create a virtual dimension scoped to Cloud SQL**

Store the split so you can reuse it in group-bys, reports, and alerts.

Go to [Dimensions](https://app.costory.io/dimensions), click **+ Add new dimension**, then **Virtual dimension**.&#x20;

![Dimensions page Add new dimension menu with Dimension and Virtual dimension options](https://mintcdn.com/costory/5tKsyyiZ-p6P_OHL/use-cases/allocate_shared_cloud_sql_database/16.png?fit=max\&auto=format\&n=5tKsyyiZ-p6P_OHL\&q=85\&s=e1b0a11f89154804e51952fe4094b448)

Dimensions page with Add new dimension open on Virtual dimension

Name it **cloud sql reallocated**. The modal accepts lowercase letters, numbers, and underscores.

![Create a new dimension modal with Name set to cloud sql reallocated](https://mintcdn.com/costory/5tKsyyiZ-p6P_OHL/use-cases/allocate_shared_cloud_sql_database/18.png?fit=max\&auto=format\&n=5tKsyyiZ-p6P_OHL\&q=85\&s=82b224fb78ce95503c007488561579db)

Create a new dimension modal with name cloud sql reallocated

On **Rule 1**, set the condition **Where** **Service Name** **In** **Cloud SQL**. That scopes the shared cost this rule reallocates.

Open **Allocate costs by:** and select **Dynamic based on usage metric**. That is Dynamic Allocation (A virtual dimension option that splits shared costs proportionally based on actual usage data (e.g., storage or CPU usage per team) rather than fixed rules.): Costory splits the scoped cost in proportion to the usage series.

![Virtual dimension Rule 1 Allocate costs by menu listing Direct value, Split cost, Existing value, and Dynamic based on usage metric](https://mintcdn.com/costory/5tKsyyiZ-p6P_OHL/use-cases/allocate_shared_cloud_sql_database/22.png?fit=max\&auto=format\&n=5tKsyyiZ-p6P_OHL\&q=85\&s=9858f78b88bae1b047675417145af75c)

Allocate costs by dropdown with Dynamic based on usage metric

Set **Integration** to **Docs Datadog**, **Metric** to `render.service.cpu.time`, and **Split by** to `service.name`. **Aggregate** stays **SUM**.

![cloud sql reallocated Rule 1 with Dynamic based on usage metric and the Datadog metric list filtered to render](https://mintcdn.com/costory/5tKsyyiZ-p6P_OHL/use-cases/allocate_shared_cloud_sql_database/26.png?fit=max\&auto=format\&n=5tKsyyiZ-p6P_OHL\&q=85\&s=7704f607e6edeab2612710dd10dbdec1)

Rule metric picker on Docs Datadog searching render.service.cpu.time

**6. Map metric values and save a draft**

Map Datadog `service.name` values to dimension values. You can use identity pass-through, a regex, or a manual map.

| Mapping | How you set it | When to use |
| - | - | - |
| Identity (pass-through) | Click **Switch to identity**, or check **Set with Regex** and keep `[\s\S]*` | `service.name` values are the dimension values you want |
| Regex | Check **Set with Regex** and edit the pattern | You need to extract a team or service token from the metric value |
| Manual | Pick **Select a value** for each metric label | Labels have no relation to the dimension values |

![Rule 1 mapping list for costory-app-back and costory-app-back-prod with Select a value dropdowns and Mapping is required](https://mintcdn.com/costory/5tKsyyiZ-p6P_OHL/use-cases/allocate_shared_cloud_sql_database/29.png?fit=max\&auto=format\&n=5tKsyyiZ-p6P_OHL\&q=85\&s=5977d97f9608ffe063bc9eeb05720c63)

Manual mapping with Select a value for each Datadog service.name

This walkthrough uses identity pass-through: check **Set with Regex** and keep `[\s\S]*`. The helper text reads **Identity pass-through: metric values are used as dimension values (regex \[\s\S]\*).**

![Rule 1 with Set with Regex checked and the pattern that passes metric values through as dimension values](https://mintcdn.com/costory/5tKsyyiZ-p6P_OHL/use-cases/allocate_shared_cloud_sql_database/31.png?fit=max\&auto=format\&n=5tKsyyiZ-p6P_OHL\&q=85\&s=acd62e6d4c165c6f78f25ced46cd91dd)

Set with Regex checked, identity pass-through pattern

![Completed cloud sql reallocated Rule 1 with Docs Datadog render.service.cpu.time, Split by service.name, and identity regex mapping](https://mintcdn.com/costory/5tKsyyiZ-p6P_OHL/use-cases/allocate_shared_cloud_sql_database/32.png?fit=max\&auto=format\&n=5tKsyyiZ-p6P_OHL\&q=85\&s=69637c4e81542e2a5628cd5edcc77b0c)

Draft rule for cloud sql reallocated using Dynamic based on usage metric

> [!WARNING]
>
> If you choose manual mapping and a new service appears later, edit the virtual dimension to map that service.

Click **Save draft**. A draft does not appear in Explorer group-by until you **Publish**. This walkthrough stops at the draft.

> [!TIP]
>
> The [Costory MCP](https://docs.costory.io/features/mcp.md) can build this from chat: "draft a virtual dimension that allocates my shared Cloud SQL cost using render.service.cpu.time grouped by service.name, preview it, then save a draft."

**7. Publish and create a report**

After you **Publish**, go to [Explorer](https://app.costory.io/explorer) and create a new report grouped by **cloud sql reallocated**. You can also schedule the report to Slack on a weekly or monthly basis. See [Cost Reports](https://docs.costory.io/features/slack-reports.md).&#x20;

## Troubleshooting

> [accordion] The dimension does not appear in Explorer group-by
>
> A draft is not available as a group-by. Open the virtual dimension and click **Publish** after you finish editing.

> [accordion] The formula editor rejects SUM()
>
> The editor does not support `SUM()` inside a formula. Add the all-services total as its own series (**Externalmetric #4**) with **Group by** **None**, then divide by that series (`d`).

> [accordion] A new service appears after you save
>
> Identity pass-through picks up new `service.name` values on the next run. Manual mapping does not. Edit the virtual dimension and map the new label.

## Related

* To allocate a shared API instead of a database, see [Split Shared API Costs Across Teams](https://docs.costory.io/use-cases/reallocate_api_costs.md). That guide uses an external usage metric such as API call counts.
* Connect Datadog or another usage source from [usage metrics](https://docs.costory.io/setup/usage-metrics).
* See [Shared Cost Allocation](https://docs.costory.io/features/tagging/shared-cost-allocation.md) for mapping types and other shared infrastructure patterns.
* Virtual dimension rules live in [Dimensions](https://docs.costory.io/features/tagging/dimensions.md#virtual-dimensions).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.