Skip to content

Commit 46e01d7

Browse files
committed
fix(plugins/alerts): simplify deduplication logic and ensure consistent field handling
1 parent 311fcae commit 46e01d7

File tree

1 file changed

+16
-27
lines changed

1 file changed

+16
-27
lines changed

plugins/alerts/main.go

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package main
22

33
import (
44
"context"
5-
"fmt"
65
"os"
76
"regexp"
87
"strings"
@@ -86,22 +85,13 @@ func correlate(ctx context.Context,
8685
}
8786
}()
8887

89-
parentId := getPreviousAlertId(alert)
90-
91-
if parentId != nil {
92-
if isDuplicate(alert) {
93-
return nil, nil
94-
}
95-
return nil, newAlert(alert, parentId)
88+
if isDuplicate(alert) {
89+
return nil, nil
9690
}
9791

98-
if len(alert.DeduplicateBy) > 0 {
99-
if isDuplicate(alert) {
100-
return nil, nil
101-
}
102-
}
92+
parentId := getPreviousAlertId(alert)
10393

104-
return nil, newAlert(alert, nil)
94+
return nil, newAlert(alert, parentId)
10595
}
10696

10797
func isDuplicate(alert *plugins.Alert) bool {
@@ -116,6 +106,10 @@ func isDuplicate(alert *plugins.Alert) bool {
116106
}
117107
}()
118108

109+
if len(alert.DeduplicateBy) == 0 {
110+
return false
111+
}
112+
119113
alertString, err := utils.ProtoMessageToString(alert)
120114
if err != nil {
121115
_ = catcher.Error("cannot convert alert to string", err, map[string]any{"alert": alert.Name, "process": "plugin_com.utmstack.alerts"})
@@ -129,7 +123,7 @@ func isDuplicate(alert *plugins.Alert) bool {
129123
bb := sdkos.NewBoolBuilder(ctx, indices, "plugin_com.utmstack.alerts")
130124

131125
// 1. Filter by Name (always)
132-
bb.FilterTerm("name.keyword", alert.Name)
126+
bb.FilterTerm("name", alert.Name)
133127

134128
// Compile regex for array index stripping
135129
reArrayIndex := regexp.MustCompile(`\.[0-9]+(\.|$)`)
@@ -139,7 +133,7 @@ func isDuplicate(alert *plugins.Alert) bool {
139133

140134
value := gjson.Get(*alertString, d)
141135
if value.Type == gjson.Null {
142-
continue
136+
return false
143137
}
144138

145139
// Calculate OpenSearch field name by removing array indices
@@ -151,7 +145,7 @@ func isDuplicate(alert *plugins.Alert) bool {
151145
})
152146

153147
if value.Type == gjson.String {
154-
bb.FilterTerm(fmt.Sprintf("%s.keyword", searchField), value.String())
148+
bb.FilterTerm(searchField, value.String())
155149
} else if value.Type == gjson.Number {
156150
bb.FilterTerm(searchField, value.Float())
157151
} else if value.IsBool() {
@@ -196,12 +190,7 @@ func getPreviousAlertId(alert *plugins.Alert) *string {
196190
}
197191
}()
198192

199-
searchFields := alert.GroupBy
200-
if len(searchFields) == 0 {
201-
searchFields = alert.DeduplicateBy
202-
}
203-
204-
if len(searchFields) == 0 {
193+
if len(alert.GroupBy) == 0 {
205194
return nil
206195
}
207196

@@ -218,7 +207,7 @@ func getPreviousAlertId(alert *plugins.Alert) *string {
218207
bb := sdkos.NewBoolBuilder(ctx, indices, "plugin_com.utmstack.alerts")
219208

220209
// 1. Filter by Name (always)
221-
bb.FilterTerm("name.keyword", alert.Name)
210+
bb.FilterTerm("name", alert.Name)
222211

223212
// 2. Must NOT match existing ParentId (we want strictly the parent, or another orphan, not a child)
224213
// Original logic: MustNot exists field "parentId"
@@ -227,12 +216,12 @@ func getPreviousAlertId(alert *plugins.Alert) *string {
227216
// Compile regex for array index stripping
228217
reArrayIndex := regexp.MustCompile(`\.[0-9]+(\.|$)`)
229218

230-
for _, d := range searchFields {
219+
for _, d := range alert.GroupBy {
231220
d = strings.TrimSuffix(d, ".keyword")
232221

233222
value := gjson.Get(*alertString, d)
234223
if value.Type == gjson.Null {
235-
continue
224+
return nil
236225
}
237226

238227
// Calculate OpenSearch field name by removing array indices
@@ -244,7 +233,7 @@ func getPreviousAlertId(alert *plugins.Alert) *string {
244233
})
245234

246235
if value.Type == gjson.String {
247-
bb.FilterTerm(fmt.Sprintf("%s.keyword", searchField), value.String())
236+
bb.FilterTerm(searchField, value.String())
248237
} else if value.Type == gjson.Number {
249238
bb.FilterTerm(searchField, value.Float())
250239
} else if value.IsBool() {

0 commit comments

Comments
 (0)