fixes checksum calculation (#928)

* fixes checksum calculation,
'for' rule param wasnt marshal properly during checksum calculation

* fixes error
This commit is contained in:
Nikolay 2020-11-29 10:48:42 +03:00 committed by GitHub
parent 357f886f97
commit 0463cb5550
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 19 deletions

View file

@ -95,7 +95,7 @@ type Rule struct {
Record string `yaml:"record,omitempty"` Record string `yaml:"record,omitempty"`
Alert string `yaml:"alert,omitempty"` Alert string `yaml:"alert,omitempty"`
Expr string `yaml:"expr"` Expr string `yaml:"expr"`
For PromDuration `yaml:"for,omitempty"` For PromDuration `yaml:"for"`
Labels map[string]string `yaml:"labels,omitempty"` Labels map[string]string `yaml:"labels,omitempty"`
Annotations map[string]string `yaml:"annotations,omitempty"` Annotations map[string]string `yaml:"annotations,omitempty"`
@ -115,6 +115,11 @@ func NewPromDuration(d time.Duration) PromDuration {
} }
} }
// MarshalYAML implements yaml.Marshaler interface.
func (pd PromDuration) MarshalYAML() (interface{}, error) {
return pd.Duration().String(), nil
}
// UnmarshalYAML implements yaml.Unmarshaler interface. // UnmarshalYAML implements yaml.Unmarshaler interface.
func (pd *PromDuration) UnmarshalYAML(unmarshal func(interface{}) error) error { func (pd *PromDuration) UnmarshalYAML(unmarshal func(interface{}) error) error {
var s string var s string

View file

@ -323,14 +323,8 @@ func TestHashRule(t *testing.T) {
} }
func TestGroupChecksum(t *testing.T) { func TestGroupChecksum(t *testing.T) {
data := ` f := func(t *testing.T, data, newData string) {
name: TestGroup t.Helper()
rules:
- alert: ExampleAlertAlwaysFiring
expr: sum by(job) (up == 1)
- record: handler:requests:rate5m
expr: sum(rate(prometheus_http_requests_total[5m])) by (handler)
`
var g Group var g Group
if err := yaml.Unmarshal([]byte(data), &g); err != nil { if err := yaml.Unmarshal([]byte(data), &g); err != nil {
t.Fatalf("failed to unmarshal: %s", err) t.Fatalf("failed to unmarshal: %s", err)
@ -338,19 +332,46 @@ rules:
if g.Checksum == "" { if g.Checksum == "" {
t.Fatalf("expected to get non-empty checksum") t.Fatalf("expected to get non-empty checksum")
} }
newData := `
var ng Group
if err := yaml.Unmarshal([]byte(newData), &ng); err != nil {
t.Fatalf("failed to unmarshal: %s", err)
}
if g.Checksum == ng.Checksum {
t.Fatalf("expected to get different checksums")
}
}
t.Run("Ok", func(t *testing.T) {
f(t, `
name: TestGroup
rules:
- alert: ExampleAlertAlwaysFiring
expr: sum by(job) (up == 1)
- record: handler:requests:rate5m
expr: sum(rate(prometheus_http_requests_total[5m])) by (handler)
`, `
name: TestGroup name: TestGroup
rules: rules:
- record: handler:requests:rate5m - record: handler:requests:rate5m
expr: sum(rate(prometheus_http_requests_total[5m])) by (handler) expr: sum(rate(prometheus_http_requests_total[5m])) by (handler)
- alert: ExampleAlertAlwaysFiring - alert: ExampleAlertAlwaysFiring
expr: sum by(job) (up == 1) expr: sum by(job) (up == 1)
` `)
var ng Group })
if err := yaml.Unmarshal([]byte(newData), &g); err != nil {
t.Fatalf("failed to unmarshal: %s", err) t.Run("Ok, `for` must change cs", func(t *testing.T) {
} f(t, `
if g.Checksum == ng.Checksum { name: TestGroup
t.Fatalf("expected to get different checksums") rules:
} - alert: ExampleAlertWithFor
expr: sum by(job) (up == 1)
for: 5m
`, `
name: TestGroup
rules:
- alert: ExampleAlertWithFor
expr: sum by(job) (up == 1)
`)
})
} }