mirror of
https://github.com/prometheus/statsd_exporter.git
synced 2024-11-22 15:30:59 +00:00
Merge pull request #107 from prometheus/mr/fix-and-test-readme
Fix and test examples in the README
This commit is contained in:
commit
0ceac67bc6
3 changed files with 97 additions and 5 deletions
|
@ -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:
|
||||
|
||||
|
@ -107,7 +107,7 @@ mappings:
|
|||
action: "$2"
|
||||
outcome: "$3"
|
||||
job: "test_dispatcher"
|
||||
- match: *.signup.*.*
|
||||
- match: "*.signup.*.*"
|
||||
labels:
|
||||
name: "signup_events_total"
|
||||
provider: "$2"
|
||||
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -189,6 +189,45 @@ mappings:
|
|||
config: ``,
|
||||
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: `---
|
||||
|
@ -219,13 +258,48 @@ mappings:
|
|||
{
|
||||
config: `---
|
||||
mappings:
|
||||
- match: *\.foo
|
||||
- match: "*\.foo"
|
||||
match_type: regex
|
||||
labels:
|
||||
name: "foo"
|
||||
`,
|
||||
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{}
|
||||
|
|
Loading…
Reference in a new issue