Skip to content

Commit 7f09934

Browse files
authored
Merge pull request #3 from whiskeyjimbo/combine-certrules-and-rules
refactor: combine certrules and rules together and add a type
2 parents 368e74d + fa5fb41 commit 7f09934

File tree

5 files changed

+74
-31
lines changed

5 files changed

+74
-31
lines changed

examples/config.yaml

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -76,25 +76,41 @@ sites:
7676

7777
rules:
7878
- name: "api_high_latency"
79-
condition: "responseTime > 500" # milliseconds
79+
type: "standard"
80+
condition: "responseTime > 500"
8081
tags: ["http-api", "https-api"]
8182
notifications: ["log"]
8283

8384
- name: "api_critical_downtime"
84-
condition: "downtime > 30s" # seconds
85+
type: "standard"
86+
condition: "downtime > 30s"
8587
tags: ["service-api", "critical"]
8688
notifications: ["log"]
8789

8890
- name: "db_downtime_warning"
89-
condition: "downtime > 10s" # seconds
91+
type: "standard"
92+
condition: "downtime > 10s"
9093
tags: ["service-db"]
9194
notifications: ["log"]
9295

9396
- name: "prod_service_degraded"
94-
condition: "responseTime > 1000 || downtime > 0"
97+
type: "standard"
98+
condition: "responseTime > 1000 || downtime > 0"
9599
tags: ["prod", "critical"]
96100
notifications: ["log"]
97101

102+
- name: "cert_expiring_soon"
103+
type: "cert"
104+
minDaysValidity: 30
105+
tags: ["https-api"]
106+
notifications: ["log"]
107+
108+
- name: "cert_critical"
109+
type: "cert"
110+
minDaysValidity: 7
111+
tags: ["https-api", "critical"]
112+
notifications: ["log"]
113+
98114
notifications:
99115
- type: "log" # Currently the only implemented type
100116
# Future notification types:
@@ -105,14 +121,3 @@ notifications:
105121
# smtp_server: "smtp.example.com"
106122
# from: "alerts@example.com"
107123
# to: ["ops@example.com"]
108-
109-
certRules:
110-
- name: "cert_expiring_soon"
111-
minDaysValidity: 30
112-
tags: ["https-api"]
113-
notifications: ["log"]
114-
115-
- name: "cert_critical"
116-
minDaysValidity: 7
117-
tags: ["https-api", "critical"]
118-
notifications: ["log"]

internal/config/config.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ type Config struct {
2222
Sites []SiteConfig `yaml:"sites"`
2323
Rules []rules.Rule `yaml:"rules"`
2424
Notifications []NotificationConfig `yaml:"notifications"`
25-
CertRules []rules.CertRule `yaml:"certRules"`
2625
}
2726

2827
type SiteConfig struct {

internal/monitor/helpers.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,11 @@ func processRule(
106106
ruleModeResolver *config.RuleModeResolver,
107107
failingHosts []string,
108108
) {
109-
ruleResult := rules.EvaluateRule(rule, downtime, stats.AvgResponseTime)
109+
params := rules.EvaluationParams{
110+
Downtime: downtime,
111+
ResponseTime: stats.AvgResponseTime,
112+
}
113+
ruleResult := rules.EvaluateRule(rule, params)
110114
if !shouldSendNotification(ruleResult) {
111115
return
112116
}

internal/rules/cert_rules.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,14 @@ import (
55
"time"
66
)
77

8-
type CertRule struct {
9-
Name string `yaml:"name"`
10-
MinDaysValidity int `yaml:"minDaysValidity"`
11-
Tags []string `yaml:"tags"`
12-
Notifications []string `yaml:"notifications"`
13-
}
14-
15-
func EvaluateCertRule(rule CertRule, certExpiryTime time.Time) RuleResult {
8+
func evaluateCertRule(rule Rule, certExpiryTime time.Time) RuleResult {
169
daysUntilExpiry := time.Until(certExpiryTime).Hours() / 24
1710

1811
if daysUntilExpiry < float64(rule.MinDaysValidity) {
1912
return RuleResult{
2013
Satisfied: true,
21-
Message: fmt.Sprintf("Certificate expires in %.1f days (threshold: %d days)", daysUntilExpiry, rule.MinDaysValidity),
14+
Message: fmt.Sprintf("Certificate expires in %.1f days (threshold: %d days)",
15+
daysUntilExpiry, rule.MinDaysValidity),
2216
}
2317
}
2418

internal/rules/rules.go

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,20 @@ import (
99
"github.com/expr-lang/expr"
1010
)
1111

12+
type RuleType string
13+
14+
const (
15+
StandardRule RuleType = "standard"
16+
CertRule RuleType = "cert"
17+
)
18+
1219
type Rule struct {
13-
Name string `yaml:"name"`
14-
Condition string `yaml:"condition"`
15-
Tags []string `yaml:"tags"`
16-
Notifications []string `yaml:"notifications"`
20+
Name string `yaml:"name"`
21+
Type RuleType `yaml:"type"`
22+
Tags []string `yaml:"tags"`
23+
Notifications []string `yaml:"notifications"`
24+
Condition string `yaml:"condition,omitempty"`
25+
MinDaysValidity int `yaml:"minDaysValidity,omitempty"`
1726
}
1827

1928
type RuleResult struct {
@@ -27,7 +36,39 @@ var (
2736
ErrInvalidSyntax = errors.New("invalid rule syntax")
2837
)
2938

30-
func EvaluateRule(rule Rule, downtime, responseTime time.Duration) RuleResult {
39+
type EvaluationParams struct {
40+
Downtime time.Duration
41+
ResponseTime time.Duration
42+
CertExpiryTime time.Time
43+
}
44+
45+
func (r Rule) Validate() error {
46+
if r.Type == "" {
47+
return fmt.Errorf("rule type must be specified")
48+
}
49+
switch r.Type {
50+
case StandardRule, CertRule:
51+
return nil
52+
default:
53+
return fmt.Errorf("invalid rule type: %s", r.Type)
54+
}
55+
}
56+
57+
func EvaluateRule(rule Rule, params EvaluationParams) RuleResult {
58+
if err := rule.Validate(); err != nil {
59+
return RuleResult{Error: err}
60+
}
61+
62+
switch rule.Type {
63+
case StandardRule:
64+
return evaluateStandardRule(rule, params.Downtime, params.ResponseTime)
65+
case CertRule:
66+
return evaluateCertRule(rule, params.CertExpiryTime)
67+
}
68+
return RuleResult{Error: fmt.Errorf("unsupported rule type: %s", rule.Type)}
69+
}
70+
71+
func evaluateStandardRule(rule Rule, downtime, responseTime time.Duration) RuleResult {
3172
if rule.Condition == "" {
3273
return RuleResult{Error: ErrEmptyCondition}
3374
}

0 commit comments

Comments
 (0)