Update documentation of metric name escaping.

The escaping changed in 728bdc52ae, before
0.1.0. Add a test for the escape function, and test various cases,
including those mentioned in the README.

This reveals that the README is inaccurate, adjust it to what has been
the reality for many years. Fixes #97.
This commit is contained in:
Matthias Rampke 2017-11-10 20:34:10 +00:00
parent 6c04ec1d89
commit 0ac908603a
2 changed files with 21 additions and 3 deletions

View file

@ -83,8 +83,8 @@ starting at 1. Multiple matching definitions are separated by one or more empty
lines. The first mapping rule that matches a StatsD metric wins.
Metrics that don't match any mapping in the configuration file are translated
into Prometheus metrics without any labels and with certain characters escaped
(`_` -> `__`; `-` -> `__`; `.` -> `_`).
into Prometheus metrics without any labels and with any non-alphanumeric
characters, including periods, translated into underscores.
In general, the different metric types are translated as follows:
@ -125,7 +125,7 @@ follows:
=> signup_events_total{provider="facebook", outcome="failure", job="foo_product_server"}
test.web-server.foo.bar
=> test_web__server_foo_bar{}
=> test_web_server_foo_bar{}
If the default metric help text is insufficient for your needs you may use the YAML

View file

@ -153,3 +153,21 @@ func (ml *mockStatsDTCPListener) handlePacket(packet []byte, e chan<- Events) {
}
ml.handleConn(sc, e)
}
func TestEscapeMetricName(t *testing.T) {
scenarios := map[string]string{
"clean": "clean",
"0starts_with_digit": "_0starts_with_digit",
"with_underscore": "with_underscore",
"with.dot": "with_dot",
"with😱emoji": "with_emoji",
"with.*.multiple": "with___multiple",
"test.web-server.foo.bar": "test_web_server_foo_bar",
}
for in, want := range scenarios {
if got := escapeMetricName(in); want != got {
t.Errorf("expected `%s` to be escaped to `%s`, got `%s`", in, want, got)
}
}
}