Skip to content

feat(obs): attribute runner cost from the CUR by instance, with five cost alarms (#667) - #1332

Merged
athvin merged 1 commit into
mainfrom
feat/667-cost-roll
Nov 28, 2026
Merged

athvin merged 1 commit into
mainfrom
feat/667-cost-roll

Conversation

@athvin

@athvin athvin commented Nov 28, 2026 •

Copy link
Copy Markdown
Owner

Closes #667

Post-merge is part of this pull request. The ledger below records whether
the follow-up work actually happened. /land-pr closes it.

src/cost_roll.rs (pure allocation + scalars + the roll), src/cost_aws.rs (Athena source, CloudWatch sink), entities run_cost and compute_rate, er-server cost-roll, the er-cost-roll CronJob, infra/terraform/envs/dev/cost-roll.tf (read-only cost policy on the er-api role + five alarms), and the as-built section in the er-engine-ops runbook.

#667: "understanding costs and being able to attribute" — the statement half was built; the dollars half was not. Its blocker (split cost allocation, an account opt-in) is still off, so this attributes by instance instead, which is exact for runner pods because each holds its node alone.

Proof

  • Probes (2026-11-27): the November CUR has 149,479 rows and 0 split rows; kubectl get nodes names every node i-… (= spec.providerID); the CUR's BoxUsage, SpotUsage and EKS-Auto:*-management-hours lines all carry that instance id in line_item_resource_id.
  • cost_roll unit tests: an instance's cost splits by wait + run; an uncovered instance's attempt is allocated at the prior rate and not billed; rates per class; each of the five scalars computed on a worked example; periods and instance-id validation.
  • pg_cost_roll: two tenants, five attempts across two periods against Postgres with a fake CUR — billed shares, the not-yet-billed attempt allocated at the class rate, rates stored per (class, period), a re-roll that replaces rows in place once the CUR covers the last instance, and scalars reaching the sink with no tenant in them.
  • terraform validate (envs/dev); cargo deny check ok; make check exit 0; Postgres suite 162 passed. The terraform plan is in this PR's infra job: it adds one role policy and five alarms and destroys nothing.

Risk & rollback

Read-only billing access and one metric namespace; a daily CronJob that only writes its own two tables. git revert <merge-sha>.

Post-merge ledger

Closed by: agent · session Claude Code:24d16c2e-fec6-49d8-9797-060338df4848 on Mac · 2026-11-28T21:34:59Z

Specific to this change

Context — plan, discoveries, measurements, files

Plan

Discoveries

  • The ticket's pod→CUR join (split_line_item → pod_uid) is blocked on the opt-in, but runner pods are exclusive on their nodes, so the instance join is exact for them and needs no opt-in.
  • The CloudWatch SDK speaks CBOR; minicbor is BlueOak-1.0.0, which npm's allowlist already carried.
  • The ticket's tasks 2, 3 and 5 (Athena, probes, standalone alarms) were already done; this is task 4.

Measurements

Files

🤖 Generated with Claude Code

…cost alarms (#667)

The dollars half of the telemetry programme, without waiting on split
cost allocation (still 0 of 149,479 November CUR rows).

- The join: EKS Auto Mode names a node by its instance id, task_instances
  records it, and the CUR keys BoxUsage, SpotUsage and the Auto Mode fee
  by the same line_item_resource_id. A runner pod holds its node alone, so
  an instance's cost splits across the attempts it hosted by wait + run.
- run_costs (one per attempt): allocated_usd = hours × the class rate,
  billed_usd = its share of the instance's CUR cost (NULL until covered),
  billed_as_of. compute_rates per (class, YYYY-MM): self-calibrated
  usd_per_pod_hour. Each roll re-rolls the current and previous period in
  place; the previous rate prices the current period until billed.
- Five scalars to CloudWatch ER/Cost, no tenant dimension, alarmed:
  MaxTenantSpendShare, MaxTenantDisproportion (spend ÷ records share, per
  dataset), FailedRunSpendShare, MaxCostPerMillionMoMChange (per dataset),
  CurStalenessHours.
- er-server cost-roll, the er-cost-roll CronJob (dev ci only), and a
  read-only cost policy on the er-api Pod Identity role: the er-cost
  Athena workgroup (10 GiB scan cap), the er_cur Glue database, the CUR
  bucket, the results prefix, and PutMetricData limited to ER/Cost.
- aws-sdk-athena and aws-sdk-cloudwatch (default-features = false);
  BlueOak-1.0.0 joins the Rust licence allowlist, as npm's already has it
  (minicbor, via the CloudWatch SDK's CBOR protocol).

Follow-ups: per-statement shares #1330, prod #1331.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions

Copy link
Copy Markdown
Contributor

terraform plan

156face · run log and full plans

env add change destroy
state-backend (plan only) 0 0 0
core 0 0 0
dev 6 0 0
prod 0 0 0

Planned with -refresh=false, so this is the config-versus-state diff and
not out-of-band drift. That is a security choice, not a speed one: a refreshing
plan reads secret values, and the plan role is assumable from any PR branch —
which can edit the workflow it runs under. Drift is caught at apply time, which
refreshes fully.

dev — 6 to add, 0 to change
data.aws_iam_policy_document.efs_csi_trust: Reading...
data.aws_iam_policy_document.er_api_cost_roll: Reading...
data.aws_iam_policy_document.efs_csi_trust: Read complete after 0s [id=819195744]
data.aws_iam_policy_document.er_api_cost_roll: Read complete after 0s [id=3548580374]
module.network.data.aws_region.current: Reading...
data.aws_region.current: Reading...
data.aws_iam_policy_document.obs_pod_trust: Reading...
data.aws_iam_openid_connect_provider.github: Reading...
data.aws_iam_policy_document.eso_trust: Reading...
module.network.data.aws_region.current: Read complete after 0s [id=us-east-2]
data.aws_iam_policy_document.obs_pod_trust: Read complete after 0s [id=819195744]
data.aws_iam_policy_document.eso_trust: Read complete after 0s [id=819195744]
data.aws_region.current: Read complete after 0s [id=us-east-2]
data.aws_iam_policy_document.eso: Reading...
module.network.data.aws_availability_zones.available: Reading...
module.eks.data.aws_iam_policy_document.node_trust: Reading...
data.aws_iam_policy_document.eso: Read complete after 0s [id=1725605485]
module.eks.data.aws_iam_policy_document.cluster_trust: Reading...
module.eks.data.aws_iam_policy_document.node_trust: Read complete after 0s [id=2851119427]
module.eks.data.aws_iam_policy_document.cluster_trust: Read complete after 0s [id=1029077455]
data.aws_rds_engine_version.aurora_pg: Reading...
data.aws_iam_policy_document.er_api_pod_trust: Reading...
data.aws_caller_identity.current: Reading...
data.aws_iam_policy_document.er_api_pod_trust: Read complete after 0s [id=819195744]
data.aws_iam_policy_document.fluent_bit: Reading...
data.aws_iam_policy_document.fluent_bit: Read complete after 0s [id=4006586444]
data.aws_caller_identity.current: Read complete after 0s [id=797781631727]
module.lake.data.aws_iam_policy_document.lake_tls_only: Reading...
module.lake.data.aws_iam_policy_document.lake_tls_only: Read complete after 0s [id=4017345831]
data.aws_iam_policy_document.otel_collector: Reading...
data.aws_iam_policy_document.otel_collector: Read complete after 0s [id=3111493560]
data.aws_iam_policy_document.bedrock_invoke: Reading...
data.aws_iam_policy_document.lake_access: Reading...
data.aws_iam_policy_document.bedrock_invoke: Read complete after 0s [id=3601248236]
data.aws_iam_policy_document.lake_access: Read complete after 0s [id=2921565705]
data.aws_rds_engine_version.aurora_pg: Read complete after 0s [id=17.9]
module.network.data.aws_availability_zones.available: Read complete after 0s [id=us-east-2]
data.aws_iam_openid_connect_provider.github: Read complete after 0s [id=arn:aws:iam::797781631727:oidc-provider/token.actions.githubusercontent.com]
data.aws_iam_policy_document.ci_deploy_dev_trust: Reading...
data.aws_iam_policy_document.ci_deploy_dev_trust: Read complete after 0s [id=4060172364]
data.aws_iam_policy_document.ci_deploy_dev: Reading...
data.aws_iam_policy_document.ci_deploy_dev: Read complete after 0s [id=1754478413]

Terraform used the selected providers to generate the following execution
plan. Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # aws_cloudwatch_metric_alarm.cost["cost-per-million-mom"] will be created
  + resource "aws_cloudwatch_metric_alarm" "cost" {
      + actions_enabled                       = true
      + alarm_actions                         = [
          + "arn:aws:sns:us-east-2:797781631727:er-alerts",
        ]
      + alarm_description                     = "A dataset's cost per million records rose more than 50% month over month on two consecutive rolls. Per dataset, not per org: an org's blend hides the degrading dataset."
      + alarm_name                            = "er-dev-cost-cost-per-million-mom"
      + arn                                   = (known after apply)
      + comparison_operator                   = "GreaterThanThreshold"
      + datapoints_to_alarm                   = 2
      + dimensions                            = {
          + "Environment" = "dev"
        }
      + evaluate_low_sample_count_percentiles = (known after apply)
      + evaluation_periods                    = 2
      + id                                    = (known after apply)
      + metric_name                           = "MaxCostPerMillionMoMChange"
      + namespace                             = "ER/Cost"
      + ok_actions                            = [
          + "arn:aws:sns:us-east-2:797781631727:er-alerts",
        ]
      + period                                = 86400
      + region                                = "us-east-2"
      + statistic                             = "Maximum"
      + tags_all                              = {
          + "Environment" = "dev"
          + "ManagedBy"   = "terraform"
          + "Project"     = "entity-resolution"
        }
      + threshold                             = 0.5
      + treat_missing_data                    = "notBreaching"
    }

  # aws_cloudwatch_metric_alarm.cost["cur-staleness"] will be created
  + resource "aws_cloudwatch_metric_alarm" "cost" {
      + actions_enabled                       = true
      + alarm_actions                         = [
          + "arn:aws:sns:us-east-2:797781631727:er-alerts",
        ]
      + alarm_description                     = "The CUR's newest line is more than 48h old: billed_usd is going stale and attribution is falling back to allocated. Check the core/cur.tf export."
      + alarm_name                            = "er-dev-cost-cur-staleness"
      + arn                                   = (known after apply)
      + comparison_operator                   = "GreaterThanThreshold"
      + datapoints_to_alarm                   = 1
      + dimensions                            = {
          + "Environment" = "dev"
        }
      + evaluate_low_sample_count_percentiles = (known after apply)
      + evaluation_periods                    = 1
      + id                                    = (known after apply)
      + metric_name                           = "CurStalenessHours"
      + namespace                             = "ER/Cost"
      + ok_actions                            = [
          + "arn:aws:sns:us-east-2:797781631727:er-alerts",
        ]
      + period                                = 86400
      + region                                = "us-east-2"
      + statistic                             = "Maximum"
      + tags_all                              = {
          + "Environment" = "dev"
          + "ManagedBy"   = "terraform"
          + "Project"     = "entity-resolution"
        }
      + threshold                             = 48
      + treat_missing_data                    = "notBreaching"
    }

  # aws_cloudwatch_metric_alarm.cost["failed-run-spend-share"] will be created
  + resource "aws_cloudwatch_metric_alarm" "cost" {
      + actions_enabled                       = true
      + alarm_actions                         = [
          + "arn:aws:sns:us-east-2:797781631727:er-alerts",
        ]
      + alarm_description                     = "More than 20% of runner spend went to attempts that failed, timed out or were retried. Check the failing task class and its error_class."
      + alarm_name                            = "er-dev-cost-failed-run-spend-share"
      + arn                                   = (known after apply)
      + comparison_operator                   = "GreaterThanThreshold"
      + datapoints_to_alarm                   = 1
      + dimensions                            = {
          + "Environment" = "dev"
        }
      + evaluate_low_sample_count_percentiles = (known after apply)
      + evaluation_periods                    = 1
      + id                                    = (known after apply)
      + metric_name                           = "FailedRunSpendShare"
      + namespace                             = "ER/Cost"
      + ok_actions                            = [
          + "arn:aws:sns:us-east-2:797781631727:er-alerts",
        ]
      + period                                = 86400
      + region                                = "us-east-2"
      + statistic                             = "Maximum"
      + tags_all                              = {
          + "Environment" = "dev"
          + "ManagedBy"   = "terraform"
          + "Project"     = "entity-resolution"
        }
      + threshold                             = 0.2
      + treat_missing_data                    = "notBreaching"
    }

  # aws_cloudwatch_metric_alarm.cost["max-tenant-disproportion"] will be created
  + resource "aws_cloudwatch_metric_alarm" "cost" {
      + actions_enabled                       = true
      + alarm_actions                         = [
          + "arn:aws:sns:us-east-2:797781631727:er-alerts",
        ]
      + alarm_description                     = "A dataset's share of spend is at least 3x its share of records on 2 of the last 3 days: a small tenant costing like a big one. The report names the (org, dataset)."
      + alarm_name                            = "er-dev-cost-max-tenant-disproportion"
      + arn                                   = (known after apply)
      + comparison_operator                   = "GreaterThanOrEqualToThreshold"
      + datapoints_to_alarm                   = 2
      + dimensions                            = {
          + "Environment" = "dev"
        }
      + evaluate_low_sample_count_percentiles = (known after apply)
      + evaluation_periods                    = 3
      + id                                    = (known after apply)
      + metric_name                           = "MaxTenantDisproportion"
      + namespace                             = "ER/Cost"
      + ok_actions                            = [
          + "arn:aws:sns:us-east-2:797781631727:er-alerts",
        ]
      + period                                = 86400
      + region                                = "us-east-2"
      + statistic                             = "Maximum"
      + tags_all                              = {
          + "Environment" = "dev"
          + "ManagedBy"   = "terraform"
          + "Project"     = "entity-resolution"
        }
      + threshold                             = 3
      + treat_missing_data                    = "notBreaching"
    }

  # aws_cloudwatch_metric_alarm.cost["max-tenant-spend-share"] will be created
  + resource "aws_cloudwatch_metric_alarm" "cost" {
      + actions_enabled                       = true
      + alarm_actions                         = [
          + "arn:aws:sns:us-east-2:797781631727:er-alerts",
        ]
      + alarm_description                     = "One org is more than half of the period's runner spend. Read the er-cost-roll job's report for which org; a single tenant dominating cost is a pricing or runaway-job signal."
      + alarm_name                            = "er-dev-cost-max-tenant-spend-share"
      + arn                                   = (known after apply)
      + comparison_operator                   = "GreaterThanThreshold"
      + datapoints_to_alarm                   = 1
      + dimensions                            = {
          + "Environment" = "dev"
        }
      + evaluate_low_sample_count_percentiles = (known after apply)
      + evaluation_periods                    = 1
      + id                                    = (known after apply)
      + metric_name                           = "MaxTenantSpendShare"
      + namespace                             = "ER/Cost"
      + ok_actions                            = [
          + "arn:aws:sns:us-east-2:797781631727:er-alerts",
        ]
      + period                                = 86400
      + region                                = "us-east-2"
      + statistic                             = "Maximum"
      + tags_all                              = {
          + "Environment" = "dev"
          + "ManagedBy"   = "terraform"
          + "Project"     = "entity-resolution"
        }
      + threshold                             = 0.5
      + treat_missing_data                    = "notBreaching"
    }

  # aws_iam_role_policy.er_api_cost_roll will be created
  + resource "aws_iam_role_policy" "er_api_cost_roll" {
      + id          = (known after apply)
      + name        = "er-dev-api-cost-roll"
      + name_prefix = (known after apply)
      + policy      = jsonencode(
            {
              + Statement = [
                  + {
                      + Action   = [
                          + "athena:StartQueryExecution",
                          + "athena:GetWorkGroup",
                          + "athena:GetQueryResults",
                          + "athena:GetQueryExecution",
                        ]
                      + Effect   = "Allow"
                      + Resource = "arn:aws:athena:us-east-2:797781631727:workgroup/er-cost"
                      + Sid      = "AthenaCostWorkgroup"
                    },
                  + {
                      + Action   = [
                          + "glue:GetTables",
                          + "glue:GetTable",
                          + "glue:GetPartitions",
                          + "glue:GetPartition",
                          + "glue:GetDatabase",
                        ]
                      + Effect   = "Allow"
                      + Resource = [
                          + "arn:aws:glue:us-east-2:797781631727:table/er_cur/*",
                          + "arn:aws:glue:us-east-2:797781631727:database/er_cur",
                          + "arn:aws:glue:us-east-2:797781631727:catalog",
                        ]
                      + Sid      = "GlueCurCatalog"
                    },
                  + {
                      + Action   = [
                          + "s3:ListBucket",
                          + "s3:GetObject",
                          + "s3:GetBucketLocation",
                        ]
                      + Effect   = "Allow"
                      + Resource = [
                          + "arn:aws:s3:::er-cur-797781631727/*",
                          + "arn:aws:s3:::er-cur-797781631727",
                        ]
                      + Sid      = "CurRead"
                    },
                  + {
                      + Action   = [
                          + "s3:PutObject",
                          + "s3:ListBucket",
                          + "s3:GetObject",
                          + "s3:GetBucketLocation",
                          + "s3:AbortMultipartUpload",
                        ]
                      + Effect   = "Allow"
                      + Resource = [
                          + "arn:aws:s3:::er-athena-results-797781631727/q/*",
                          + "arn:aws:s3:::er-athena-results-797781631727",
                        ]
                      + Sid      = "AthenaResults"
                    },
                  + {
                      + Action    = "cloudwatch:PutMetricData"
                      + Condition = {
                          + StringEquals = {
                              + "cloudwatch:namespace" = "ER/Cost"
                            }
                        }
                      + Effect    = "Allow"
                      + Resource  = "*"
                      + Sid       = "CostMetrics"
                    },
                ]
              + Version   = "2012-10-17"
            }
        )
      + role        = "er-dev-api-pod"
    }

Plan: 6 to add, 0 to change, 0 to destroy.

This branch was previously deployed

1 inactive deployment
marketing-preview — 50de19f6 Deployed Nov 28, 2026 by athvin via remove closed PR previews #112
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cost attribution: the unbuilt half of the telemetry programme (P16/P17)

2 participants