abort parsing tags if malformed signalfx format, add tests

Signed-off-by: glightfoot <glightfoot@rsglab.com>
This commit is contained in:
glightfoot 2020-06-19 14:39:47 -04:00
parent 5b863848dd
commit 9faa4898de
2 changed files with 28 additions and 1 deletions

View file

@ -220,6 +220,26 @@ func TestHandlePacket(t *testing.T) {
CLabels: map[string]string{}, CLabels: map[string]string{},
}, },
}, },
}, {
name: "SignalFx tag extension, missing closing bracket",
in: "[tag1=bar,tag2=bazfoo.test:100|c",
out: event.Events{
&event.CounterEvent{
CMetricName: "[tag1=bar,tag2=bazfoo.test",
CValue: 100,
CLabels: map[string]string{},
},
},
}, {
name: "SignalFx tag extension, missing opening bracket",
in: "tag1=bar,tag2=baz]foo.test:100|c",
out: event.Events{
&event.CounterEvent{
CMetricName: "tag1=bar,tag2=baz]foo.test",
CValue: 100,
CLabels: map[string]string{},
},
},
}, { }, {
name: "influxdb tag extension with tag keys unsupported by prometheus", name: "influxdb tag extension with tag keys unsupported by prometheus",
in: "foo,09digits=0,tag.with.dots=1:100|c", in: "foo,09digits=0,tag.with.dots=1:100|c",

View file

@ -139,9 +139,16 @@ func parseNameAndTags(name string, labels map[string]string, tagErrors prometheu
startIdx := strings.IndexRune(name, '[') startIdx := strings.IndexRune(name, '[')
endIdx := strings.IndexRune(name, ']') endIdx := strings.IndexRune(name, ']')
if startIdx != -1 && endIdx != -1 { switch {
case startIdx != -1 && endIdx != -1:
// good signalfx tags
parseNameTags(name[startIdx+1:endIdx], labels, tagErrors, logger) parseNameTags(name[startIdx+1:endIdx], labels, tagErrors, logger)
return name[:startIdx] + name[endIdx+1:] return name[:startIdx] + name[endIdx+1:]
case (startIdx != -1) != (endIdx != -1):
// only one bracket, return unparsed
level.Debug(logger).Log("msg", "invalid SignalFx tags, not parsing", "metric", name)
tagErrors.Inc()
return name
} }
for i, c := range name { for i, c := range name {