Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.25.2

require (
github.com/hashicorp/hcl/v2 v2.24.0
github.com/infracost/proto v1.19.0
github.com/infracost/proto v1.23.0
github.com/stretchr/testify v1.11.1
github.com/zclconf/go-cty v1.17.0
)
Expand Down
7 changes: 2 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,8 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/hashicorp/hcl/v2 v2.24.0 h1:2QJdZ454DSsYGoaE6QheQZjtKZSUs9Nh2izTWiwQxvE=
github.com/hashicorp/hcl/v2 v2.24.0/go.mod h1:oGoO1FIQYfn/AgyOhlg9qLC6/nOJPX3qGbkZpYAcqfM=
github.com/infracost/proto v1.17.0 h1:9OBYvA+/gfVDOzi0go4gTY2pOnTgqvov1OnwRBMDopA=
github.com/infracost/proto v1.17.0/go.mod h1:BO6ew0skDYqM8lxl3rKKMA7WRM7VG1dIUKErbTdfGPk=
github.com/infracost/proto v1.17.1-0.20260225103127-e390beafddfe/go.mod h1:BO6ew0skDYqM8lxl3rKKMA7WRM7VG1dIUKErbTdfGPk=
github.com/infracost/proto v1.19.0 h1:vLeQnM53e5A/+4zpU3d+ZXf55mPJLKzJUqLFMT9W5D8=
github.com/infracost/proto v1.19.0/go.mod h1:BO6ew0skDYqM8lxl3rKKMA7WRM7VG1dIUKErbTdfGPk=
github.com/infracost/proto v1.23.0 h1:zbjCxNdA+YKZlXzvVVN3drfJpDLFdLnctuDpDtmQ7r8=
github.com/infracost/proto v1.23.0/go.mod h1:BO6ew0skDYqM8lxl3rKKMA7WRM7VG1dIUKErbTdfGPk=
github.com/mitchellh/go-wordwrap v1.0.1 h1:TLuKupo69TCn6TQSyGxwI1EblZZEsQ0vMlAFQflz0v0=
github.com/mitchellh/go-wordwrap v1.0.1/go.mod h1:R62XHJLzvMFRBbcrT7m7WgmE1eOyTSsCt+hzestvNj0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
160 changes: 160 additions & 0 deletions pkg/event/guardrails.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
package event

import (
"github.com/infracost/go-proto/pkg/rat"
"github.com/infracost/proto/gen/go/infracost/parser/event"
)

// GuardrailResult is the result of evaluating a single applicable guardrail.
// It is serialized as JSON and sent back to the dashboard API via the addRun
// GraphQL mutation. Every applicable guardrail produces a result, whether or
// not it triggered.
type GuardrailResult struct {
GuardrailID string `json:"guardrailId"`
Triggered bool `json:"triggered"`
PRComment bool `json:"prComment"`
BlockPR bool `json:"blockPr"`
TriggeringProjectNames []string `json:"triggeringProjectNames"`
Increase *rat.Rat `json:"increase"`
PercentIncrease *rat.Rat `json:"percentIncrease"`
TotalMonthlyCost *rat.Rat `json:"totalMonthlyCost"`
}

// ProjectCostInfo holds the cost data for a single project, used as input
// to guardrail evaluation.
type ProjectCostInfo struct {
ProjectName string
TotalMonthlyCost *rat.Rat
PastTotalMonthlyCost *rat.Rat
}

// Guardrails wraps a list of guardrail proto configurations for evaluation.
type Guardrails []*event.Guardrail

// Evaluate checks guardrails against the provided run-level and project-level
// costs and returns a result for each applicable guardrail.
func (gs Guardrails) Evaluate(
totalMonthlyCost *rat.Rat,
pastTotalMonthlyCost *rat.Rat,
projects []ProjectCostInfo,
) []GuardrailResult {
var results []GuardrailResult

for _, g := range gs {
result := GuardrailResult{
GuardrailID: g.GetId(),
PRComment: g.GetPrComment(),
BlockPR: g.GetBlockPr(),
TriggeringProjectNames: []string{},
}

if !hasThresholds(g) {
results = append(results, result)
continue
}

switch g.GetScope() {
case event.Guardrail_REPO:
increase := calcIncrease(totalMonthlyCost, pastTotalMonthlyCost)
result.Increase = increase.increase
result.PercentIncrease = increase.percentIncrease
result.TotalMonthlyCost = totalMonthlyCost
result.Triggered = thresholdExceeded(g, totalMonthlyCost, pastTotalMonthlyCost)

case event.Guardrail_PROJECT:
projectFilter := StringFilterFromProto(g.GetProjectFilter())

var triggeringNames []string
var maxIncrease costIncrease

for _, p := range projects {
if !projectFilter.Matches(p.ProjectName) {
continue
}

inc := calcIncrease(p.TotalMonthlyCost, p.PastTotalMonthlyCost)
if thresholdExceeded(g, p.TotalMonthlyCost, p.PastTotalMonthlyCost) {
triggeringNames = append(triggeringNames, p.ProjectName)
}

if maxIncrease.increase == nil || inc.increase.GreaterThan(maxIncrease.increase) {
maxIncrease = inc
}
}

if maxIncrease.increase != nil {
result.Increase = maxIncrease.increase
result.PercentIncrease = maxIncrease.percentIncrease
} else {
result.Increase = rat.Zero
result.PercentIncrease = rat.Zero
}
result.TotalMonthlyCost = totalMonthlyCost
result.Triggered = len(triggeringNames) > 0
result.TriggeringProjectNames = triggeringNames
}

results = append(results, result)
}

return results
}

func hasThresholds(g *event.Guardrail) bool {
return g.IncreaseThreshold != nil || g.IncreasePercentThreshold != nil || g.TotalThreshold != nil
}

func thresholdExceeded(g *event.Guardrail, totalMonthlyCost, pastTotalMonthlyCost *rat.Rat) bool {
increase := calcIncrease(totalMonthlyCost, pastTotalMonthlyCost)

// Never trigger on cost decreases or no change.
if !increase.increase.GreaterThanZero() {
return false
}

// All configured thresholds must be exceeded (AND logic).
if g.IncreaseThreshold != nil {
if !increase.increase.GreaterThan(rat.FromProto(g.IncreaseThreshold)) {
return false
}
}

if g.IncreasePercentThreshold != nil {
if !increase.percentIncrease.GreaterThan(rat.FromProto(g.IncreasePercentThreshold)) {
return false
}
}

// Total threshold uses crossing logic: only triggers when crossing from
// at-or-below to above the threshold.
if g.TotalThreshold != nil {
threshold := rat.FromProto(g.TotalThreshold)
if !pastTotalMonthlyCost.LessThanOrEqual(threshold) || !totalMonthlyCost.GreaterThan(threshold) {
return false
}
}

return true
}

type costIncrease struct {
increase *rat.Rat
percentIncrease *rat.Rat
}

func calcIncrease(totalMonthlyCost, pastTotalMonthlyCost *rat.Rat) costIncrease {
increase := totalMonthlyCost.Sub(pastTotalMonthlyCost)

var percentIncrease *rat.Rat
if pastTotalMonthlyCost.IsZero() {
percentIncrease = rat.Zero
} else {
// (total / past - 1) * 100, rounded to 0 decimal places
percentIncrease = totalMonthlyCost.Div(pastTotalMonthlyCost).Sub(rat.New(1)).Mul(rat.New(100)).Round(0)
}

return costIncrease{
increase: increase,
percentIncrease: percentIncrease,
}
}
Loading
Loading