Merge pull request #107 from prometheus/mr/fix-and-test-readme

Fix and test examples in the README
This commit is contained in:
Matthias Rampke 2017-11-11 00:17:54 +01:00 committed by GitHub
commit 0ceac67bc6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 97 additions and 5 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. lines. The first mapping rule that matches a StatsD metric wins.
Metrics that don't match any mapping in the configuration file are translated 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: In general, the different metric types are translated as follows:
@ -107,7 +107,7 @@ mappings:
action: "$2" action: "$2"
outcome: "$3" outcome: "$3"
job: "test_dispatcher" job: "test_dispatcher"
- match: *.signup.*.* - match: "*.signup.*.*"
labels: labels:
name: "signup_events_total" name: "signup_events_total"
provider: "$2" provider: "$2"
@ -125,7 +125,7 @@ follows:
=> signup_events_total{provider="facebook", outcome="failure", job="foo_product_server"} => 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{} => test_web_server_foo_bar{}
If the default metric help text is insufficient for your needs you may use the YAML 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) 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)
}
}
}

View file

@ -189,6 +189,45 @@ mappings:
config: ``, config: ``,
mappings: map[string]map[string]string{}, mappings: map[string]map[string]string{},
}, },
// Config without a trailing newline.
{
config: `mappings:
- match: test.*
labels:
name: "name"
label: "${1}_foo"`,
mappings: map[string]map[string]string{
"test.a": map[string]string{
"name": "name",
"label": "a_foo",
},
},
},
// Config with an improperly escaped *.
{
config: `
mappings:
- match: *.test.*
labels:
name: "name"
label: "${1}_foo"`,
configBad: true,
},
// Config with a properly escaped *.
{
config: `
mappings:
- match: "*.test.*"
labels:
name: "name"
label: "${2}_foo"`,
mappings: map[string]map[string]string{
"foo.test.a": map[string]string{
"name": "name",
"label": "a_foo",
},
},
},
// Config with good timer type. // Config with good timer type.
{ {
config: `--- config: `---
@ -219,13 +258,48 @@ mappings:
{ {
config: `--- config: `---
mappings: mappings:
- match: *\.foo - match: "*\.foo"
match_type: regex match_type: regex
labels: labels:
name: "foo" name: "foo"
`, `,
configBad: true, configBad: true,
}, },
// Example from the README.
{
config: `
mappings:
- match: test.dispatcher.*.*.*
labels:
name: "dispatcher_events_total"
processor: "$1"
action: "$2"
outcome: "$3"
job: "test_dispatcher"
- match: "*.signup.*.*"
labels:
name: "signup_events_total"
provider: "$2"
outcome: "$3"
job: "${1}_server"
`,
mappings: map[string]map[string]string{
"test.dispatcher.FooProcessor.send.success": map[string]string{
"name": "dispatcher_events_total",
"processor": "FooProcessor",
"action": "send",
"outcome": "success",
"job": "test_dispatcher",
},
"foo_product.signup.facebook.failure": map[string]string{
"name": "signup_events_total",
"provider": "facebook",
"outcome": "failure",
"job": "foo_product_server",
},
"test.web-server.foo.bar": map[string]string{},
},
},
} }
mapper := metricMapper{} mapper := metricMapper{}