mirror of
https://github.com/prometheus/statsd_exporter.git
synced 2025-01-25 21:58:08 +00:00
fix: don't let captured fields being overwritten by backtracking
Signed-off-by: Wangchong Zhou <fffonion@gmail.com>
This commit is contained in:
parent
275559585e
commit
d0ad532fa1
2 changed files with 48 additions and 2 deletions
|
@ -138,6 +138,7 @@ func (f *FSM) GetMapping(statsdMetric string, statsdMetricType string) (*mapping
|
||||||
var finalState *mappingState
|
var finalState *mappingState
|
||||||
|
|
||||||
captures := make([]string, len(matchFields))
|
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
|
// keep track of captured group so we don't need to do append() on captures
|
||||||
captureIdx := 0
|
captureIdx := 0
|
||||||
filedsCount := len(matchFields)
|
filedsCount := len(matchFields)
|
||||||
|
@ -193,6 +194,8 @@ func (f *FSM) GetMapping(statsdMetric string, statsdMetricType string) (*mapping
|
||||||
} else if finalState == nil || finalState.ResultPriority > state.ResultPriority {
|
} else if finalState == nil || finalState.ResultPriority > state.ResultPriority {
|
||||||
// if we care about ordering, try to find a result with highest prioity
|
// if we care about ordering, try to find a result with highest prioity
|
||||||
finalState = state
|
finalState = state
|
||||||
|
// do a deep copy to preserve current captures
|
||||||
|
copy(finalCaptures, captures)
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
@ -224,8 +227,7 @@ func (f *FSM) GetMapping(statsdMetric string, statsdMetricType string) (*mapping
|
||||||
resumeFromBacktrack = true
|
resumeFromBacktrack = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return finalState, finalCaptures
|
||||||
return finalState, captures
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestIfNeedBacktracking tests if backtrack is needed for given list of mappings
|
// TestIfNeedBacktracking tests if backtrack is needed for given list of mappings
|
||||||
|
|
|
@ -167,6 +167,50 @@ mappings:
|
||||||
"label": "justatest_foo",
|
"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
|
//Config with super sets, disables ordering
|
||||||
|
|
Loading…
Reference in a new issue