Skip to content

Commit 54dc7ed

Browse files
authored
Merge pull request #1233 from planetscale/tc-warning-threshold
Support configuring traffic budget `warning_threshold`
2 parents 1eb473f + a5a40ac commit 54dc7ed

File tree

5 files changed

+57
-41
lines changed

5 files changed

+57
-41
lines changed

internal/cmd/trafficcontrol/budget_create.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@ import (
1111

1212
func BudgetCreateCmd(ch *cmdutil.Helper) *cobra.Command {
1313
var flags struct {
14-
name string
15-
mode string
16-
capacity int
17-
rate int
18-
burst int
19-
concurrency int
14+
name string
15+
mode string
16+
capacity int
17+
rate int
18+
burst int
19+
concurrency int
20+
warningThreshold int
2021
}
2122

2223
cmd := &cobra.Command{
@@ -57,6 +58,9 @@ func BudgetCreateCmd(ch *cmdutil.Helper) *cobra.Command {
5758
if cmd.Flags().Changed("concurrency") {
5859
req.Concurrency = &flags.concurrency
5960
}
61+
if cmd.Flags().Changed("warning-threshold") {
62+
req.WarningThreshold = &flags.warningThreshold
63+
}
6064

6165
budget, err := client.TrafficBudgets.Create(ctx, req)
6266
if err != nil {
@@ -86,6 +90,7 @@ func BudgetCreateCmd(ch *cmdutil.Helper) *cobra.Command {
8690
cmd.Flags().IntVar(&flags.rate, "rate", 0, "Rate at which capacity refills, as a percentage of server resources (0-100). Unlimited when not set.")
8791
cmd.Flags().IntVar(&flags.burst, "burst", 0, "Maximum capacity a single query can consume (0-6000). Unlimited when not set.")
8892
cmd.Flags().IntVar(&flags.concurrency, "concurrency", 0, "Percentage of available worker processes (0-100). Unlimited when not set.")
93+
cmd.Flags().IntVar(&flags.warningThreshold, "warning-threshold", 0, "Percentage (0-100) of capacity, burst, or concurrency at which to emit warnings for enforced budgets.")
8994

9095
cmd.MarkFlagRequired("name") // nolint:errcheck
9196

internal/cmd/trafficcontrol/budget_create_test.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,19 @@ func TestBudgetCreateCmd(t *testing.T) {
2222
p := printer.NewPrinter(&format)
2323
p.SetResourceOutput(&buf)
2424

25-
cap, rate, burst, conc := 80, 50, 60, 40
25+
cap, rate, burst, conc, warnTh := 80, 50, 60, 40, 30
2626

2727
created := &ps.TrafficBudget{
28-
ID: budgetID,
29-
Name: "CPU Limiter",
30-
Mode: "enforce",
31-
Capacity: &cap,
32-
Rate: &rate,
33-
Burst: &burst,
34-
Concurrency: &conc,
35-
CreatedAt: time.Date(2025, 6, 15, 10, 0, 0, 0, time.UTC),
36-
UpdatedAt: time.Date(2025, 6, 15, 10, 0, 0, 0, time.UTC),
28+
ID: budgetID,
29+
Name: "CPU Limiter",
30+
Mode: "enforce",
31+
Capacity: &cap,
32+
Rate: &rate,
33+
Burst: &burst,
34+
Concurrency: &conc,
35+
WarningThreshold: &warnTh,
36+
CreatedAt: time.Date(2025, 6, 15, 10, 0, 0, 0, time.UTC),
37+
UpdatedAt: time.Date(2025, 6, 15, 10, 0, 0, 0, time.UTC),
3738
}
3839

3940
svc := &mock.TrafficBudgetsService{
@@ -47,6 +48,7 @@ func TestBudgetCreateCmd(t *testing.T) {
4748
c.Assert(*req.Rate, qt.Equals, 50)
4849
c.Assert(*req.Burst, qt.Equals, 60)
4950
c.Assert(*req.Concurrency, qt.Equals, 40)
51+
c.Assert(*req.WarningThreshold, qt.Equals, 30)
5052
return created, nil
5153
},
5254
}
@@ -67,6 +69,7 @@ func TestBudgetCreateCmd(t *testing.T) {
6769
"--rate", "50",
6870
"--burst", "60",
6971
"--concurrency", "40",
72+
"--warning-threshold", "30",
7073
})
7174
err := cmd.Execute()
7275

internal/cmd/trafficcontrol/budget_update.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@ import (
1111

1212
func BudgetUpdateCmd(ch *cmdutil.Helper) *cobra.Command {
1313
var flags struct {
14-
name string
15-
mode string
16-
capacity int
17-
rate int
18-
burst int
19-
concurrency int
14+
name string
15+
mode string
16+
capacity int
17+
rate int
18+
burst int
19+
concurrency int
20+
warningThreshold int
2021
}
2122

2223
cmd := &cobra.Command{
@@ -59,6 +60,9 @@ func BudgetUpdateCmd(ch *cmdutil.Helper) *cobra.Command {
5960
if cmd.Flags().Changed("concurrency") {
6061
req.Concurrency = &flags.concurrency
6162
}
63+
if cmd.Flags().Changed("warning-threshold") {
64+
req.WarningThreshold = &flags.warningThreshold
65+
}
6266

6367
end := ch.Printer.PrintProgress(fmt.Sprintf("Updating traffic budget %s in %s/%s",
6468
printer.BoldBlue(budgetID), printer.BoldBlue(database), printer.BoldBlue(branch)))
@@ -91,6 +95,7 @@ func BudgetUpdateCmd(ch *cmdutil.Helper) *cobra.Command {
9195
cmd.Flags().IntVar(&flags.rate, "rate", 0, "Rate at which capacity refills, as a percentage of server resources (0-100). Unlimited when not set.")
9296
cmd.Flags().IntVar(&flags.burst, "burst", 0, "Maximum capacity a single query can consume (0-6000). Unlimited when not set.")
9397
cmd.Flags().IntVar(&flags.concurrency, "concurrency", 0, "Percentage of available worker processes (0-100). Unlimited when not set.")
98+
cmd.Flags().IntVar(&flags.warningThreshold, "warning-threshold", 0, "Percentage (0-100) of capacity, burst, or concurrency at which to emit warnings for enforced budgets.")
9499

95100
return cmd
96101
}

internal/cmd/trafficcontrol/budget_update_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ func TestBudgetUpdateCmd(t *testing.T) {
4545
c.Assert(req.Rate, qt.IsNil)
4646
c.Assert(req.Burst, qt.IsNil)
4747
c.Assert(req.Concurrency, qt.IsNil)
48+
c.Assert(req.WarningThreshold, qt.IsNil)
4849
c.Assert(req.Rules, qt.IsNil)
4950
return updated, nil
5051
},

internal/cmd/trafficcontrol/traffic_control.go

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,16 @@ func TrafficCmd(ch *cmdutil.Helper) *cobra.Command {
4949
}
5050

5151
type TrafficBudget struct {
52-
ID string `header:"id" json:"id"`
53-
Name string `header:"name" json:"name"`
54-
Mode string `header:"mode" json:"mode"`
55-
Capacity string `header:"capacity" json:"capacity"`
56-
Rate string `header:"rate" json:"rate"`
57-
Burst string `header:"burst" json:"burst"`
58-
Concurrency string `header:"concurrency" json:"concurrency"`
59-
CreatedAt int64 `header:"created_at,timestamp(ms|utc|human)" json:"created_at"`
60-
UpdatedAt int64 `header:"updated_at,timestamp(ms|utc|human)" json:"updated_at"`
52+
ID string `header:"id" json:"id"`
53+
Name string `header:"name" json:"name"`
54+
Mode string `header:"mode" json:"mode"`
55+
Capacity string `header:"capacity" json:"capacity"`
56+
Rate string `header:"rate" json:"rate"`
57+
Burst string `header:"burst" json:"burst"`
58+
Concurrency string `header:"concurrency" json:"concurrency"`
59+
WarningThreshold string `header:"warning_threshold" json:"warning_threshold"`
60+
CreatedAt int64 `header:"created_at,timestamp(ms|utc|human)" json:"created_at"`
61+
UpdatedAt int64 `header:"updated_at,timestamp(ms|utc|human)" json:"updated_at"`
6162

6263
orig *ps.TrafficBudget
6364
}
@@ -72,16 +73,17 @@ func (b *TrafficBudget) MarshalCSVValue() any {
7273

7374
func toTrafficBudget(b *ps.TrafficBudget) *TrafficBudget {
7475
return &TrafficBudget{
75-
ID: b.ID,
76-
Name: b.Name,
77-
Mode: b.Mode,
78-
Capacity: formatOptionalInt(b.Capacity),
79-
Rate: formatOptionalInt(b.Rate),
80-
Burst: formatOptionalInt(b.Burst),
81-
Concurrency: formatOptionalInt(b.Concurrency),
82-
CreatedAt: printer.GetMilliseconds(b.CreatedAt),
83-
UpdatedAt: printer.GetMilliseconds(b.UpdatedAt),
84-
orig: b,
76+
ID: b.ID,
77+
Name: b.Name,
78+
Mode: b.Mode,
79+
Capacity: formatOptionalInt(b.Capacity),
80+
Rate: formatOptionalInt(b.Rate),
81+
Burst: formatOptionalInt(b.Burst),
82+
Concurrency: formatOptionalInt(b.Concurrency),
83+
WarningThreshold: formatOptionalInt(b.WarningThreshold),
84+
CreatedAt: printer.GetMilliseconds(b.CreatedAt),
85+
UpdatedAt: printer.GetMilliseconds(b.UpdatedAt),
86+
orig: b,
8587
}
8688
}
8789

0 commit comments

Comments
 (0)