mirror of
https://github.com/VictoriaMetrics/VictoriaMetrics.git
synced 2024-11-23 12:31:07 +01:00
lib/promrelabel: optimize common regex mismatch cases for action: replace
and action: labelmap
This commit is contained in:
parent
4c6916f32a
commit
f49c9bb700
@ -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)
|
||||
|
@ -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 {
|
||||
|
@ -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{
|
||||
{
|
||||
|
@ -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)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user