mirror of
https://github.com/VictoriaMetrics/VictoriaMetrics.git
synced 2025-03-21 15:45:01 +00:00
lib/promrelabel: optimize common regex mismatch cases for action: replace
and action: labelmap
This commit is contained in:
parent
4c6916f32a
commit
f49c9bb700
4 changed files with 14 additions and 2 deletions
|
@ -218,6 +218,7 @@ func parseRelabelConfig(rc *RelabelConfig) (*parsedRelabelConfig, error) {
|
||||||
regexOrig := regex
|
regexOrig := regex
|
||||||
if rc.Action != "replace_all" && rc.Action != "labelmap_all" {
|
if rc.Action != "replace_all" && rc.Action != "labelmap_all" {
|
||||||
regex = regexutil.RemoveStartEndAnchors(regex)
|
regex = regexutil.RemoveStartEndAnchors(regex)
|
||||||
|
regexOrig = regex
|
||||||
regex = "^(?:" + regex + ")$"
|
regex = "^(?:" + regex + ")$"
|
||||||
}
|
}
|
||||||
re, err := regexp.Compile(regex)
|
re, err := regexp.Compile(regex)
|
||||||
|
|
|
@ -203,6 +203,11 @@ func (prc *parsedRelabelConfig) apply(labels []prompbmarshal.Label, labelsOffset
|
||||||
return labels
|
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)
|
match := prc.RegexAnchored.FindSubmatchIndex(bb.B)
|
||||||
if match == nil {
|
if match == nil {
|
||||||
// Fast path - nothing to replace.
|
// 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
|
// Slow path - regexp processing
|
||||||
match := prc.RegexAnchored.FindStringSubmatchIndex(s)
|
match := prc.RegexAnchored.FindStringSubmatchIndex(s)
|
||||||
if match == nil {
|
if match == nil {
|
||||||
|
|
|
@ -497,7 +497,7 @@ func BenchmarkApplyRelabelConfigs(b *testing.B) {
|
||||||
pcs := mustParseRelabelConfigs(`
|
pcs := mustParseRelabelConfigs(`
|
||||||
- action: drop
|
- action: drop
|
||||||
source_labels: [id]
|
source_labels: [id]
|
||||||
regex: yes
|
regex: "yes"
|
||||||
`)
|
`)
|
||||||
labelsOrig := []prompbmarshal.Label{
|
labelsOrig := []prompbmarshal.Label{
|
||||||
{
|
{
|
||||||
|
@ -584,7 +584,7 @@ func BenchmarkApplyRelabelConfigs(b *testing.B) {
|
||||||
pcs := mustParseRelabelConfigs(`
|
pcs := mustParseRelabelConfigs(`
|
||||||
- action: keep
|
- action: keep
|
||||||
source_labels: [id]
|
source_labels: [id]
|
||||||
regex: yes
|
regex: "yes"
|
||||||
`)
|
`)
|
||||||
labelsOrig := []prompbmarshal.Label{
|
labelsOrig := []prompbmarshal.Label{
|
||||||
{
|
{
|
||||||
|
|
|
@ -87,4 +87,6 @@ func TestPromRegex(t *testing.T) {
|
||||||
f(".*(a|b).*", "xa", true)
|
f(".*(a|b).*", "xa", true)
|
||||||
f(".*(a|b).*", "xay", true)
|
f(".*(a|b).*", "xay", true)
|
||||||
f(".*(a|b).*", "xzy", false)
|
f(".*(a|b).*", "xzy", false)
|
||||||
|
f("^(?:true)$", "true", true)
|
||||||
|
f("^(?:true)$", "false", false)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue