[SG-4917] feat(infracost): name the cost being measured and what it covers - #362
Merged
Merged
Conversation
Codecov Report❌ Patch coverage is
🚀 New features to boost your workflow:
|
A cost message was the comparison and nothing else:
`300.1` is not less than or equal to `20`
which says neither which cost was measured nor what it covered. A monthly and
an hourly figure of the same size read identically, and the reader has to go
and open the policy to find out which one they are looking at. Now:
[all resources (2 resources)] total_monthly_cost: `300.1` is not less than or equal to `20`
[aws_eks_cluster, aws_s3_bucket (1 resource)] total_monthly_cost: `100.1` is not less than or equal to `-1`
This uses the same provider `context` mechanism terraform_plan uses, so it is
also carried as structured fields — `operation_type`, `resource_type`,
`matched_resources`, `currency`.
The resource count is there for a specific trap. `resource_type` is matched
against the breakdown, so a typo produces a cost of 0, and `LessThan 20` passes
while measuring nothing at all. The verdict is unchanged — a policy that passed
still passes — but `[aws_instances (0 resources)]` makes the reason visible
rather than leaving a green check on an empty measurement.
Two smaller things come with it: `format_context_prefix` gained a `qualifier`
key, which renders in the same parenthetical as `action` for providers that have
no planned action, and the core's non-`ProviderError` error branch now gets the
context prefix like every other message.
refeed
force-pushed
the
feat/infracost-message-context
branch
from
September 4, 2026 15:24
8110b6f to
a8fb5fb
Compare
|
❌ The last analysis has failed. |
arunim2405
approved these changes
Sep 4, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
A cost message was the comparison and nothing else:
It says neither which cost was measured nor what it covered — a monthly and an hourly figure of the same size read identically, and you have to go open the policy to tell which one you are looking at. Now:
Same provider
contextmechanismterraform_planuses, so the detail is also structured:{ "operation_type": "total_monthly_cost", "label": "aws_s3_bucket", "attribute": "total_monthly_cost", "resource_type": ["aws_s3_bucket"], "matched_resources": 1, "qualifier": "1 resource", "currency": "USD" }The resource count is there for a specific trap
resource_typeis matched against the infracost breakdown, so a typo produces a cost of 0 — andLessThan 20passes, while measuring nothing at all:The verdict is deliberately unchanged — a policy that passed still passes, since changing that would turn existing green pipelines red. But the reason is now visible instead of a green check on an empty measurement.
test_a_resource_type_matching_nothing_says_so_instead_of_reporting_a_bare_zeropins both halves of that.Also in here
format_context_prefixgained aqualifierkey, rendering in the same parenthetical asaction, for providers that have no notion of a planned action.terraform_plankeeps usingaction; nothing about its output changes.ProviderErrorerror branch (added in 1.1.0,core.py:118) was the one message path still not getting the context prefix. Now it does, so[all resources] total_monthly_cost: 'projects not found in input_data'names the evaluator it belongs to._is_every_resource()collects the three spellings of "all resources" (None,"*",["*"]) thatprovidealready accepted, so the message agrees with what was actually measured on every path.Verification
pytest: 787 passed. The 11 failures are pre-existing and need aterraformbinary onPATH.tests/providers/infracost/test_infracost_message_context.py, including one asserting the two cost walks still return the same totals (they now return(total, matched_count)).black --checkclean under both 25.9.0 and 26.5.1, and this branch adds zero files to the setmainalready flags under the newer one.pydocstyleclean.terraformstep template runs infracost, so a real run in demo-org exercises this.Two things found while doing this, not fixed here
tests/providers/infracost/tests_infracost.pyis not collected and is vacuous. The filename istests_rather thantest_, so pytest skips it. Its 6 tests would pass regardless: they passcostTypeinstead ofoperation_type, soprovideraisesKeyError, returns{"value": None}, and every assertion isNone != 0. Left alone — renaming it would surface 6 failures unrelated to this PR.test_infracost_message_context.pyrather thantest_message_context.pybecause pytest cannot collect two same-named test modules without__init__.pyin the test dirs.Related: SG-4917, tirith#243