lib/promrelabel: optimize common regex mismatch cases for action: replace and action: labelmap

This commit is contained in:
Aliaksandr Valialkin 2022-08-26 15:35:16 +03:00
parent 4c6916f32a
commit f49c9bb700
No known key found for this signature in database
GPG key ID: A72BEC6CD3D0DED1
4 changed files with 14 additions and 2 deletions

View file

@ -218,6 +218,7 @@ func parseRelabelConfig(rc *RelabelConfig) (*parsedRelabelConfig, error) {
regexOrig := regex
if rc.Action != "replace_all" && rc.Action != "labelmap_all" {
regex = regexutil.RemoveStartEndAnchors(regex)
regexOrig = regex
regex = "^(?:" + regex + ")$"
}
re, err := regexp.Compile(regex)

View file

@ -203,6 +203,11 @@ func (prc *parsedRelabelConfig) apply(labels []prompbmarshal.Label, labelsOffset
return labels
}
}
if !prc.regex.MatchString(bytesutil.ToUnsafeString(bb.B)) {
// Fast path - regexp mismatch.
relabelBufPool.Put(bb)
return labels
}
match := prc.RegexAnchored.FindSubmatchIndex(bb.B)
if match == nil {
// Fast path - nothing to replace.
@ -388,6 +393,10 @@ func (prc *parsedRelabelConfig) replaceFullString(s, replacement string, hasCapt
}
}
}
if !prc.regex.MatchString(s) {
// Fast path - regex mismatch
return s, false
}
// Slow path - regexp processing
match := prc.RegexAnchored.FindStringSubmatchIndex(s)
if match == nil {

View file

@ -497,7 +497,7 @@ func BenchmarkApplyRelabelConfigs(b *testing.B) {
pcs := mustParseRelabelConfigs(`
- action: drop
source_labels: [id]
regex: yes
regex: "yes"
`)
labelsOrig := []prompbmarshal.Label{
{
@ -584,7 +584,7 @@ func BenchmarkApplyRelabelConfigs(b *testing.B) {
pcs := mustParseRelabelConfigs(`
- action: keep
source_labels: [id]
regex: yes
regex: "yes"
`)
labelsOrig := []prompbmarshal.Label{
{

View file

@ -87,4 +87,6 @@ func TestPromRegex(t *testing.T) {
f(".*(a|b).*", "xa", true)
f(".*(a|b).*", "xay", true)
f(".*(a|b).*", "xzy", false)
f("^(?:true)$", "true", true)
f("^(?:true)$", "false", false)
}