From c9726434ab2002dc05a3f1f5c317961a1c8075c0 Mon Sep 17 00:00:00 2001 From: Sergiusz Urbaniak Date: Fri, 3 Jul 2020 10:19:32 +0200 Subject: [PATCH] pkg/rules/rulespb.AlertState: marshal JSON as lowercase (#2834) Currently, alert state is rendered as upper case. In Prometheus it is lower case. This fixes it. Signed-off-by: Sergiusz Urbaniak --- CHANGELOG.md | 6 ++++++ pkg/query/api/v1_test.go | 8 ++++---- pkg/rules/rulespb/custom.go | 2 +- pkg/rules/rulespb/custom_test.go | 6 +++--- 4 files changed, 14 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a68b5e7349..5ff571018d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,12 @@ We use *breaking* word for marking changes that are not backward compatible (rel ## Unreleased +### Fixed + +- [#2834](https://github.com/thanos-io/thanos/pull/2834) Query: Fix rendered JSON state value for rules and alerts should be in lowercase + +### Changed + ## [v0.14.0-rc.1](https://github.com/thanos-io/thanos/releases/tag/v0.14.0-rc.1) - 2020.07.07 ### Fixed diff --git a/pkg/query/api/v1_test.go b/pkg/query/api/v1_test.go index 0254cf9c05..aae3753706 100644 --- a/pkg/query/api/v1_test.go +++ b/pkg/query/api/v1_test.go @@ -1318,7 +1318,7 @@ func TestRulesHandler(t *testing.T) { Type: "recording", }, testpromcompatibility.AlertingRule{ - State: all[2].GetAlert().State.String(), + State: strings.ToLower(all[2].GetAlert().State.String()), Name: all[2].GetAlert().Name, Query: all[2].GetAlert().Query, Labels: storepb.LabelsToPromLabels(all[2].GetAlert().Labels.Labels), @@ -1332,7 +1332,7 @@ func TestRulesHandler(t *testing.T) { { Labels: storepb.LabelsToPromLabels(all[2].GetAlert().Alerts[0].Labels.Labels), Annotations: storepb.LabelsToPromLabels(all[2].GetAlert().Alerts[0].Annotations.Labels), - State: all[2].GetAlert().Alerts[0].State.String(), + State: strings.ToLower(all[2].GetAlert().Alerts[0].State.String()), ActiveAt: all[2].GetAlert().Alerts[0].ActiveAt, Value: all[2].GetAlert().Alerts[0].Value, PartialResponseStrategy: all[2].GetAlert().Alerts[0].PartialResponseStrategy.String(), @@ -1340,7 +1340,7 @@ func TestRulesHandler(t *testing.T) { { Labels: storepb.LabelsToPromLabels(all[2].GetAlert().Alerts[1].Labels.Labels), Annotations: storepb.LabelsToPromLabels(all[2].GetAlert().Alerts[1].Annotations.Labels), - State: all[2].GetAlert().Alerts[1].State.String(), + State: strings.ToLower(all[2].GetAlert().Alerts[1].State.String()), ActiveAt: all[2].GetAlert().Alerts[1].ActiveAt, Value: all[2].GetAlert().Alerts[1].Value, PartialResponseStrategy: all[2].GetAlert().Alerts[1].PartialResponseStrategy.String(), @@ -1349,7 +1349,7 @@ func TestRulesHandler(t *testing.T) { Type: "alerting", }, testpromcompatibility.AlertingRule{ - State: all[3].GetAlert().State.String(), + State: strings.ToLower(all[3].GetAlert().State.String()), Name: all[3].GetAlert().Name, Query: all[3].GetAlert().Query, Labels: storepb.LabelsToPromLabels(all[3].GetAlert().Labels.Labels), diff --git a/pkg/rules/rulespb/custom.go b/pkg/rules/rulespb/custom.go index 98205960e4..582d055bff 100644 --- a/pkg/rules/rulespb/custom.go +++ b/pkg/rules/rulespb/custom.go @@ -247,7 +247,7 @@ func (x *AlertState) UnmarshalJSON(entry []byte) error { } func (x *AlertState) MarshalJSON() ([]byte, error) { - return []byte(strconv.Quote(x.String())), nil + return []byte(strconv.Quote(strings.ToLower(x.String()))), nil } // Compare compares alert state x and y and returns: diff --git a/pkg/rules/rulespb/custom_test.go b/pkg/rules/rulespb/custom_test.go index ac2d865b02..a2ba74f45a 100644 --- a/pkg/rules/rulespb/custom_test.go +++ b/pkg/rules/rulespb/custom_test.go @@ -207,7 +207,7 @@ func TestJSONUnmarshalMarshal(t *testing.T) { Annotations: labels.Labels{ {Name: "annotation1", Value: "2"}, }, - State: "INACTIVE", + State: "inactive", ActiveAt: nil, Value: "1", PartialResponseStrategy: "WARN", @@ -215,7 +215,7 @@ func TestJSONUnmarshalMarshal(t *testing.T) { { Labels: nil, Annotations: nil, - State: "FIRING", + State: "firing", ActiveAt: &twoHoursAgo, Value: "2143", PartialResponseStrategy: "ABORT", @@ -223,7 +223,7 @@ func TestJSONUnmarshalMarshal(t *testing.T) { }, LastError: "1", Duration: 60, - State: "PENDING", + State: "pending", LastEvaluation: now.Add(-1 * time.Minute), EvaluationTime: 1.1, },