From 0ac908603a60bf665233776ef30fb0eeefdfcb0a Mon Sep 17 00:00:00 2001 From: Matthias Rampke Date: Fri, 10 Nov 2017 20:34:10 +0000 Subject: [PATCH] Update documentation of metric name escaping. The escaping changed in 728bdc52ae71d9cb9bcbda7d93f47689e94eac4c, 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. --- README.md | 6 +++--- exporter_test.go | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 4370a9a..042dff3 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/exporter_test.go b/exporter_test.go index 7eeb4e1..9941f0e 100644 --- a/exporter_test.go +++ b/exporter_test.go @@ -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) + } + } +}