Skip to content

Commit

Permalink
rules: fix silly mistakes in the rules API
Browse files Browse the repository at this point in the history
Fixes thanos-io#2938 and is a follow up
to thanos-io#2925.

Changes:
* Copying all fields properly to the group-level as well
* Set TZ to UTC explicitly in other `time.Time` fields to avoid a panic
* Make sure that `RuleGroup` always has an empty array in the `rules`
field because that's customary and that is what the new React UI
expects.

Manual testing for now.

Signed-off-by: Giedrius Statkevičius <giedriuswork@gmail.com>
  • Loading branch information
GiedriusS committed Jul 29, 2020
1 parent 6b4bfa8 commit 83068f1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
18 changes: 12 additions & 6 deletions pkg/rules/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,14 @@ type Group struct {

func (g Group) toProto() *rulespb.RuleGroup {
ret := &rulespb.RuleGroup{
Name: g.Name(),
File: g.OriginalFile,
Interval: g.Interval().Seconds(),
PartialResponseStrategy: g.PartialResponseStrategy,
Name: g.Name(),
File: g.OriginalFile,
Interval: g.Interval().Seconds(),
PartialResponseStrategy: g.PartialResponseStrategy,
DeprecatedPartialResponseStrategy: g.PartialResponseStrategy,
// https://github.com/gogo/protobuf/issues/519
LastEvaluation: g.GetEvaluationTimestamp().UTC(),
EvaluationDurationSeconds: g.GetEvaluationDuration().Seconds(),
}

for _, r := range g.Rules() {
Expand All @@ -65,7 +69,8 @@ func (g Group) toProto() *rulespb.RuleGroup {
Health: string(rule.Health()),
LastError: lastError,
EvaluationDurationSeconds: rule.GetEvaluationDuration().Seconds(),
LastEvaluation: rule.GetEvaluationTimestamp(),
// https://github.com/gogo/protobuf/issues/519
LastEvaluation: rule.GetEvaluationTimestamp().UTC(),
}}})
case *rules.RecordingRule:
ret.Rules = append(ret.Rules, &rulespb.Rule{
Expand All @@ -76,7 +81,8 @@ func (g Group) toProto() *rulespb.RuleGroup {
Health: string(rule.Health()),
LastError: lastError,
EvaluationDurationSeconds: rule.GetEvaluationDuration().Seconds(),
LastEvaluation: rule.GetEvaluationTimestamp(),
// https://github.com/gogo/protobuf/issues/519
LastEvaluation: rule.GetEvaluationTimestamp().UTC(),
}}})
default:
// We cannot do much, let's panic, API will recover.
Expand Down
9 changes: 9 additions & 0 deletions pkg/rules/rulespb/custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,15 @@ func (m *Rule) MarshalJSON() ([]byte, error) {
})
}

func (r *RuleGroup) MarshalJSON() ([]byte, error) {
if r.Rules == nil {
// Ensure that empty slices are marshaled as '[]' and not 'null'.
r.Rules = make([]*Rule, 0)
}
type plain RuleGroup
return json.Marshal((*plain)(r))
}

func (x *AlertState) UnmarshalJSON(entry []byte) error {
fieldStr, err := strconv.Unquote(string(entry))
if err != nil {
Expand Down

0 comments on commit 83068f1

Please sign in to comment.