Skip to content

Commit 7ce682b

Browse files
committed
fix the empty result when the value of an output field contains a dash/slash + trim prefix with the timestamp and priority for the output
Signed-off-by: Thomas Labarussias <issif+github@gadz.org>
1 parent e961285 commit 7ce682b

File tree

4 files changed

+23
-3
lines changed

4 files changed

+23
-3
lines changed

internal/api/api.go

+2
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ func AddEvent(c echo.Context) error {
6262

6363
models.GetOutputs().Update(payload.Outputs)
6464

65+
payload.Event.Output = utils.TrimPrefix(payload.Event.Output)
66+
6567
if err := events.Add(&payload.Event); err != nil {
6668
return err
6769
}

internal/database/redis/index.go

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ func CreateIndex(client *redisearch.Client) {
4444
AddField(redisearch.NewTextField("hostname")).
4545
AddField(redisearch.NewTextField("source")).
4646
AddField(redisearch.NewTextField("tags")).
47+
AddField(redisearch.NewTextField("outputfields")).
4748
AddField(redisearch.NewNumericField("timestamp")).
4849
AddField(redisearch.NewTextField("json"))
4950

internal/database/redis/set.go

+7
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ func SetKey(client *redisearch.Client, event *models.Event) error {
3030

3131
jsonString, _ := json.Marshal(event)
3232

33+
of := make([]string, 0, len(event.OutputFields))
34+
35+
for _, i := range event.OutputFields {
36+
of = append(of, fmt.Sprintf("%v", i))
37+
}
38+
3339
doc := redisearch.NewDocument(fmt.Sprintf("event:%v", event.UUID), 1.0).
3440
Set("rule", event.Rule).
3541
Set("priority", event.Priority).
@@ -38,6 +44,7 @@ func SetKey(client *redisearch.Client, event *models.Event) error {
3844
Set("timestamp", event.Time.UnixNano()/1e3).
3945
Set("tags", utils.Escape(strings.Join(event.Tags, ","))).
4046
Set("json", string(jsonString)).
47+
Set("outputfields", utils.Escape(strings.Join(of, ","))).
4148
Set("uuid", event.UUID).
4249
SetTTL(c.TTL)
4350
if event.Hostname != "" {

internal/utils/utils.go

+13-3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
const (
3030
extractNumber = "^[0-9]+"
3131
extractUnity = "[a-z-A-Z]+$"
32+
trimPrefix = "(?i)^\\d{2}:\\d{2}:\\d{2}\\.\\d{9}\\:\\ (Debug|Info|Informational|Notice|Warning|Error|Critical|Alert|Emergency)"
3233
)
3334

3435
const (
@@ -39,11 +40,12 @@ const (
3940
fatalLog
4041
)
4142

42-
var regExtractNumber, regExtractUnity *regexp.Regexp
43+
var regExtractNumber, regExtractUnity, regTrimPrefix *regexp.Regexp
4344

4445
func init() {
4546
regExtractNumber, _ = regexp.Compile(extractNumber)
4647
regExtractUnity, _ = regexp.Compile(extractUnity)
48+
regTrimPrefix, _ = regexp.Compile(trimPrefix)
4749
}
4850

4951
func CheckErr(e error) {
@@ -164,9 +166,17 @@ func GetPriortiyInt(prio string) int {
164166
}
165167

166168
func Escape(s string) string {
167-
return strings.ReplaceAll(s, "-", `\-`)
169+
s = strings.ReplaceAll(s, "-", `\-`)
170+
s = strings.ReplaceAll(s, "/", "\\/")
171+
return s
168172
}
169173

170174
func UnEscape(s string) string {
171-
return strings.ReplaceAll(s, `\-`, "-")
175+
s = strings.ReplaceAll(s, `\-`, "-")
176+
s = strings.ReplaceAll(s, `\\/`, "/")
177+
return s
178+
}
179+
180+
func TrimPrefix(s string) string {
181+
return regTrimPrefix.ReplaceAllString(s, "")
172182
}

0 commit comments

Comments
 (0)