Merge pull request #169 from Kong/fix-backtracking-captures-being-overwritten

fix: don't let captured fields being overwritten by backtracking
This commit is contained in:
Matthias Rampke 2018-12-05 21:48:52 +00:00 committed by GitHub
commit e019c01fb5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 48 additions and 2 deletions

View file

@ -138,6 +138,7 @@ func (f *FSM) GetMapping(statsdMetric string, statsdMetricType string) (*mapping
var finalState *mappingState
captures := make([]string, len(matchFields))
finalCaptures := make([]string, len(matchFields))
// keep track of captured group so we don't need to do append() on captures
captureIdx := 0
filedsCount := len(matchFields)
@ -193,6 +194,8 @@ func (f *FSM) GetMapping(statsdMetric string, statsdMetricType string) (*mapping
} else if finalState == nil || finalState.ResultPriority > state.ResultPriority {
// if we care about ordering, try to find a result with highest prioity
finalState = state
// do a deep copy to preserve current captures
copy(finalCaptures, captures)
}
break
}
@ -224,8 +227,7 @@ func (f *FSM) GetMapping(statsdMetric string, statsdMetricType string) (*mapping
resumeFromBacktrack = true
}
}
return finalState, captures
return finalState, finalCaptures
}
// TestIfNeedBacktracking tests if backtrack is needed for given list of mappings

View file

@ -167,6 +167,50 @@ mappings:
"label": "justatest_foo",
},
},
"backtrack.justatest.aaa": {
name: "testa",
labels: map[string]string{
"label": "_foo",
},
},
},
},
//Config with backtracking, the non-matched rule has star(s)
// A metric like full.name.anothertest will first match full.name.* and then tries
// to match *.dummy.* and then failed.
// This test case makes sure the captures in the non-matched later rule
// doesn't affect the captures in the first matched rule.
{
config: `
defaults:
glob_disable_ordering: false
mappings:
- match: '*.dummy.*'
name: metric_one
labels:
system: $1
attribute: $2
- match: 'full.name.*'
name: metric_two
labels:
system: static
attribute: $1
`,
mappings: mappings{
"whatever.dummy.test": {
name: "metric_one",
labels: map[string]string{
"system": "whatever",
"attribute": "test",
},
},
"full.name.anothertest": {
name: "metric_two",
labels: map[string]string{
"system": "static",
"attribute": "anothertest",
},
},
},
},
//Config with super sets, disables ordering