sanitize tag keys

- add tests for edge cases
 - fix typo
This commit is contained in:
Daniel Bonkowski 2016-03-07 15:33:41 +01:00 committed by Ilya Margolin
parent ab7c27ee77
commit 8f36baf045
2 changed files with 32 additions and 4 deletions

View file

@ -66,6 +66,26 @@ func TestHandlePacket(t *testing.T) {
labels: map[string]string{"tag1": "bar", "tag2": "baz", "tag3": ".", "tag4": "."},
},
},
}, {
name: "datadog tag extension with # in all keys (as sent by datadog php client)",
in: "foo:100|c|#tag1:bar,#tag2:baz,#tag3,#tag4",
out: Events{
&CounterEvent{
metricName: "foo",
value: 100,
labels: map[string]string{"tag1": "bar", "tag2": "baz", "tag3": ".", "tag4": "."},
},
},
}, {
name: "datadog tag extension with tags unsupported by prometheus",
in: "foo:100|c|#09digits:0,tag.with.dots,tag_with_empty_value:",
out: Events{
&CounterEvent{
metricName: "foo",
value: 100,
labels: map[string]string{"_09digits": "0", "tag_with_dots": ".", "tag_with_empty_value": "."},
},
},
}, {
name: "datadog tag extension with sampling",
in: "foo:100|c|@0.1|#tag1:bar,tag2,tag3:baz",

View file

@ -360,17 +360,25 @@ func (l *StatsDListener) handlePacket(packet []byte, e chan<- Events) {
}
value /= samplingFactor
case '#':
networkStats.WithLabelValues("dogstasd_tags").Inc()
tags := strings.Split(component[1:], ",")
networkStats.WithLabelValues("dogstatsd_tags").Inc()
tags := strings.Split(component, ",")
for _, t := range tags {
t = strings.TrimPrefix(t, "#")
kv := strings.Split(t, ":")
tag_key := kv[0]
tag_key = escapeMetricName(tag_key)
var tag_value string
if len(kv) == 2 {
if len(kv[1]) > 0 {
labels[kv[0]] = kv[1]
tag_value = kv[1]
} else {
tag_value = "."
}
} else if len(kv) == 1 {
labels[kv[0]] = "."
tag_value = "."
}
labels[tag_key] = tag_value
}
default:
log.Printf("Invalid sampling factor or tag section %s on line %s", components[2], line)