guenther/internal/drain3/masking.go

32 lines
1 KiB
Go

// Package drain3 provides log stripping via regex-based masking templates which
// sits in front of Drain3 template mining.
package drain3
import (
"codeberg.org/pata1704/guenther/internal/config"
)
// ApplyMasking applies all MaskingPatterns sequentially to line.
//
// For each pattern with a non-empty Name, capture group 1 of the regex is
// stored in params before the match is replaced with mp.Replace.
// Patterns without a Name only mask; they never write to params.
//
// All patterns are pre-compiled via config.Compile at startup;
// no compilation happens in this hot-path function.
func ApplyMasking(line string, patterns []config.MaskingPattern) (masked string, params map[string]string) {
params = make(map[string]string, len(patterns))
masked = line
for _, mp := range patterns {
if mp.Re == nil {
continue
}
if mp.Name != "" {
if m := mp.Re.FindStringSubmatch(masked); len(m) > 1 {
params[mp.Name] = m[1]
}
}
masked = mp.Re.ReplaceAllString(masked, mp.Replace)
}
return masked, params
}