Commit 35dea2d4 authored by Will Lahti's avatar Will Lahti Committed by Matthew Sykes
Browse files

Sanitize label values for statsd



Label values for certain metrics may contain characters that are not
allowed by the statsd wire protocol. This CR adds sanitizes label values
in the statsd namer by replacing invalid characters with an underscore.

FAB-12811 #done

Change-Id: I92fb4b0b37b2e8e0446c622a4b1ee043c11bb9de
Signed-off-by: default avatarWill Lahti <wtlahti@us.ibm.com>
Signed-off-by: default avatarMatthew Sykes <sykesmat@us.ibm.com>
parent a58bcd2b
......@@ -54,6 +54,7 @@ func (n *namer) labelsToMap(labelValues []string) map[string]string {
}
var formatRegexp = regexp.MustCompile(`%{([#?[:alnum:]_]+)}`)
var invalidLabelValueRegexp = regexp.MustCompile(`[.|:\s]`)
func (n *namer) Format(labelValues ...string) string {
labels := n.labelsToMap(labelValues)
......@@ -87,6 +88,7 @@ func (n *namer) Format(labelValues ...string) string {
if !ok {
panic(fmt.Sprintf("invalid label in name format: %s", key))
}
value = invalidLabelValueRegexp.ReplaceAllString(value, "_")
}
segments = append(segments, value)
......
......@@ -66,6 +66,34 @@ var _ = Describe("This is the thing", func() {
})
})
Context("when label values contain invalid characters", func() {
It("replaces them with underscores", func() {
name := n.Format("alpha", ":colon:colon:", "bravo", "|bar|bar|")
Expect(name).To(Equal("prefix.namespace.subsystem.name._colon_colon_.bravo._bar_bar_.suffix"))
})
})
Context("when label values contain new line, spaces, or tabs", func() {
It("replaces them with underscores", func() {
name := n.Format("alpha", "a\nb\tc", "bravo", "b c")
Expect(name).To(Equal("prefix.namespace.subsystem.name.a_b_c.bravo.b_c.suffix"))
})
})
Context("when label values contain periods", func() {
It("replaces them with underscores", func() {
name := n.Format("alpha", "period.period", "bravo", "...")
Expect(name).To(Equal("prefix.namespace.subsystem.name.period_period.bravo.___.suffix"))
})
})
Context("when label values contain multi-byte utf8 runes", func() {
It("leaves them alone", func() {
name := n.Format("alpha", "Ʊpsilon", "bravo", "b")
Expect(name).To(Equal("prefix.namespace.subsystem.name.Ʊpsilon.bravo.b.suffix"))
})
})
DescribeTable("#fqname",
func(n *namer, expectedName string) {
n.nameFormat = "%{#fqname}"
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment