Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v10.0.x] Alerting: Sort NumberCaptureValues in EvaluationString #71931

Merged
merged 1 commit into from Jul 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 5 additions & 5 deletions pkg/services/ngalert/eval/extract_md.go
Expand Up @@ -2,6 +2,7 @@ package eval

import (
"fmt"
"sort"
"strings"

"github.com/grafana/grafana-plugin-sdk-go/data"
Expand Down Expand Up @@ -43,28 +44,27 @@ func extractEvalString(frame *data.Frame) (s string) {
}

if captures, ok := frame.Meta.Custom.([]NumberValueCapture); ok {
// Sort captures in ascending order of "Var" so we can assert in tests
sort.Slice(captures, func(i, j int) bool {
return captures[i].Var < captures[j].Var
})
sb := strings.Builder{}

for i, capture := range captures {
sb.WriteString("[ ")
sb.WriteString(fmt.Sprintf("var='%s' ", capture.Var))
sb.WriteString(fmt.Sprintf("labels={%s} ", capture.Labels))

valString := "null"
if capture.Value != nil {
valString = fmt.Sprintf("%v", *capture.Value)
}

sb.WriteString(fmt.Sprintf("value=%v ", valString))

sb.WriteString("]")
if i < len(captures)-1 {
sb.WriteString(", ")
}
}
return sb.String()
}

return ""
}

Expand Down
8 changes: 8 additions & 0 deletions pkg/services/ngalert/eval/extract_md_test.go
Expand Up @@ -40,6 +40,14 @@ func TestExtractEvalString(t *testing.T) {
}, util.Pointer(1.0), withRefID("A")),
outString: `[ var='A0' metric='Test' labels={host=foo} value=32.3 ], [ var='A1' metric='Test' labels={host=baz} value=10 ], [ var='A2' metric='TestA' labels={host=zip} value=11 ]`,
},
{
desc: "Captures are sorted in ascending order of var",
inFrame: newMetaFrame([]NumberValueCapture{
{Var: "B", Labels: data.Labels{"host": "foo"}, Value: util.Pointer(1.0)},
{Var: "A", Labels: data.Labels{"host": "foo"}, Value: util.Pointer(10.0)},
}, util.Pointer(1.0)),
outString: `[ var='A' labels={host=foo} value=10 ], [ var='B' labels={host=foo} value=1 ]`,
},
}
for _, tc := range cases {
t.Run(tc.desc, func(t *testing.T) {
Expand Down