forked from mirrors/statsd_exporter
sanitize tag keys
- add tests for edge cases - fix typo
This commit is contained in:
parent
ab7c27ee77
commit
8f36baf045
2 changed files with 32 additions and 4 deletions
|
@ -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",
|
||||
|
|
16
exporter.go
16
exporter.go
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue