forked from mirrors/statsd_exporter
Merge pull request #231 from SpencerMalone/match_metric_type_tests
Add test for multiple explicit metric types.
This commit is contained in:
commit
c3dc7b4b96
1 changed files with 158 additions and 81 deletions
|
@ -18,12 +18,14 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type mappings map[string]struct {
|
type mappings []struct {
|
||||||
|
statsdMetric string
|
||||||
name string
|
name string
|
||||||
labels map[string]string
|
labels map[string]string
|
||||||
quantiles []metricObjective
|
quantiles []metricObjective
|
||||||
notPresent bool
|
notPresent bool
|
||||||
ttl time.Duration
|
ttl time.Duration
|
||||||
|
metricType MetricType
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMetricMapperYAML(t *testing.T) {
|
func TestMetricMapperYAML(t *testing.T) {
|
||||||
|
@ -85,7 +87,8 @@ mappings:
|
||||||
|
|
||||||
`,
|
`,
|
||||||
mappings: mappings{
|
mappings: mappings{
|
||||||
"test.dispatcher.FooProcessor.send.succeeded": {
|
{
|
||||||
|
statsdMetric: "test.dispatcher.FooProcessor.send.succeeded",
|
||||||
name: "dispatch_events",
|
name: "dispatch_events",
|
||||||
labels: map[string]string{
|
labels: map[string]string{
|
||||||
"processor": "FooProcessor",
|
"processor": "FooProcessor",
|
||||||
|
@ -94,7 +97,8 @@ mappings:
|
||||||
"job": "test_dispatcher",
|
"job": "test_dispatcher",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
"test.my-dispatch-host01.name.dispatcher.FooProcessor.send.succeeded": {
|
{
|
||||||
|
statsdMetric: "test.my-dispatch-host01.name.dispatcher.FooProcessor.send.succeeded",
|
||||||
name: "host_dispatch_events",
|
name: "host_dispatch_events",
|
||||||
labels: map[string]string{
|
labels: map[string]string{
|
||||||
"processor": "FooProcessor",
|
"processor": "FooProcessor",
|
||||||
|
@ -103,7 +107,8 @@ mappings:
|
||||||
"job": "test_dispatcher",
|
"job": "test_dispatcher",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
"request_time.get/threads/1/posts.200.00000000.nonversioned.discussions.a11bbcdf0ac64ec243658dc64b7100fb.172.20.0.1.12ba97b7eaa1a50001000001.": {
|
{
|
||||||
|
statsdMetric: "request_time.get/threads/1/posts.200.00000000.nonversioned.discussions.a11bbcdf0ac64ec243658dc64b7100fb.172.20.0.1.12ba97b7eaa1a50001000001.",
|
||||||
name: "tyk_http_request",
|
name: "tyk_http_request",
|
||||||
labels: map[string]string{
|
labels: map[string]string{
|
||||||
"method_and_path": "get/threads/1/posts",
|
"method_and_path": "get/threads/1/posts",
|
||||||
|
@ -120,7 +125,8 @@ mappings:
|
||||||
"oauthid": "",
|
"oauthid": "",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
"foo.bar": {
|
{
|
||||||
|
statsdMetric: "foo.bar",
|
||||||
name: "catchall",
|
name: "catchall",
|
||||||
labels: map[string]string{
|
labels: map[string]string{
|
||||||
"first": "foo",
|
"first": "foo",
|
||||||
|
@ -129,8 +135,11 @@ mappings:
|
||||||
"job": "foo-bar-",
|
"job": "foo-bar-",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
"foo.bar.baz": {},
|
{
|
||||||
"proxy-1.http-goober.success": {
|
statsdMetric: "foo.bar.baz",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
statsdMetric: "proxy-1.http-goober.success",
|
||||||
name: "proxy_requests_total",
|
name: "proxy_requests_total",
|
||||||
labels: map[string]string{
|
labels: map[string]string{
|
||||||
"job": "proxy-1",
|
"job": "proxy-1",
|
||||||
|
@ -157,19 +166,22 @@ mappings:
|
||||||
label: "${1}_foo"
|
label: "${1}_foo"
|
||||||
`,
|
`,
|
||||||
mappings: mappings{
|
mappings: mappings{
|
||||||
"backtrack.good.bbb": {
|
{
|
||||||
|
statsdMetric: "backtrack.good.bbb",
|
||||||
name: "testb",
|
name: "testb",
|
||||||
labels: map[string]string{
|
labels: map[string]string{
|
||||||
"label": "good_foo",
|
"label": "good_foo",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
"backtrack.justatest.bbb": {
|
{
|
||||||
|
statsdMetric: "backtrack.justatest.bbb",
|
||||||
name: "testb",
|
name: "testb",
|
||||||
labels: map[string]string{
|
labels: map[string]string{
|
||||||
"label": "justatest_foo",
|
"label": "justatest_foo",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
"backtrack.justatest.aaa": {
|
{
|
||||||
|
statsdMetric: "backtrack.justatest.aaa",
|
||||||
name: "testa",
|
name: "testa",
|
||||||
labels: map[string]string{
|
labels: map[string]string{
|
||||||
"label": "_foo",
|
"label": "_foo",
|
||||||
|
@ -199,14 +211,16 @@ mappings:
|
||||||
attribute: $1
|
attribute: $1
|
||||||
`,
|
`,
|
||||||
mappings: mappings{
|
mappings: mappings{
|
||||||
"whatever.dummy.test": {
|
{
|
||||||
|
statsdMetric: "whatever.dummy.test",
|
||||||
name: "metric_one",
|
name: "metric_one",
|
||||||
labels: map[string]string{
|
labels: map[string]string{
|
||||||
"system": "whatever",
|
"system": "whatever",
|
||||||
"attribute": "test",
|
"attribute": "test",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
"full.name.anothertest": {
|
{
|
||||||
|
statsdMetric: "full.name.anothertest",
|
||||||
name: "metric_two",
|
name: "metric_two",
|
||||||
labels: map[string]string{
|
labels: map[string]string{
|
||||||
"system": "static",
|
"system": "static",
|
||||||
|
@ -235,13 +249,15 @@ mappings:
|
||||||
label: "ccc_foo"
|
label: "ccc_foo"
|
||||||
`,
|
`,
|
||||||
mappings: mappings{
|
mappings: mappings{
|
||||||
"noorder.good.bbb": {
|
{
|
||||||
|
statsdMetric: "noorder.good.bbb",
|
||||||
name: "testb",
|
name: "testb",
|
||||||
labels: map[string]string{
|
labels: map[string]string{
|
||||||
"label": "good_foo",
|
"label": "good_foo",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
"noorder.ccc.bbb": {
|
{
|
||||||
|
statsdMetric: "noorder.ccc.bbb",
|
||||||
name: "testc",
|
name: "testc",
|
||||||
labels: map[string]string{
|
labels: map[string]string{
|
||||||
"label": "ccc_foo",
|
"label": "ccc_foo",
|
||||||
|
@ -265,7 +281,8 @@ mappings:
|
||||||
label: "${1}_foo"
|
label: "${1}_foo"
|
||||||
`,
|
`,
|
||||||
mappings: mappings{
|
mappings: mappings{
|
||||||
"order.good.bbb": {
|
{
|
||||||
|
statsdMetric: "order.good.bbb",
|
||||||
name: "testa",
|
name: "testa",
|
||||||
labels: map[string]string{
|
labels: map[string]string{
|
||||||
"label": "good_foo",
|
"label": "good_foo",
|
||||||
|
@ -283,7 +300,8 @@ mappings:
|
||||||
label: "$1_foo"
|
label: "$1_foo"
|
||||||
`,
|
`,
|
||||||
mappings: mappings{
|
mappings: mappings{
|
||||||
"test.a": {
|
{
|
||||||
|
statsdMetric: "test.a",
|
||||||
name: "name",
|
name: "name",
|
||||||
labels: map[string]string{
|
labels: map[string]string{
|
||||||
"label": "",
|
"label": "",
|
||||||
|
@ -301,7 +319,8 @@ mappings:
|
||||||
label: "${1}_foo"
|
label: "${1}_foo"
|
||||||
`,
|
`,
|
||||||
mappings: mappings{
|
mappings: mappings{
|
||||||
"test.a": {
|
{
|
||||||
|
statsdMetric: "test.a",
|
||||||
name: "name",
|
name: "name",
|
||||||
labels: map[string]string{
|
labels: map[string]string{
|
||||||
"label": "a_foo",
|
"label": "a_foo",
|
||||||
|
@ -335,13 +354,16 @@ mappings:
|
||||||
labels: {}
|
labels: {}
|
||||||
`,
|
`,
|
||||||
mappings: mappings{
|
mappings: mappings{
|
||||||
"test1.total_requests.count": {
|
{
|
||||||
|
statsdMetric: "test1.total_requests.count",
|
||||||
name: "total_requests",
|
name: "total_requests",
|
||||||
},
|
},
|
||||||
"test2.total_requests.count": {
|
{
|
||||||
|
statsdMetric: "test2.total_requests.count",
|
||||||
name: "total_requests_count",
|
name: "total_requests_count",
|
||||||
},
|
},
|
||||||
"test3.total_requests.count": {
|
{
|
||||||
|
statsdMetric: "test3.total_requests.count",
|
||||||
name: "count_total_requests",
|
name: "count_total_requests",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -379,7 +401,8 @@ mappings:
|
||||||
labels:
|
labels:
|
||||||
label: "${1}_foo"`,
|
label: "${1}_foo"`,
|
||||||
mappings: mappings{
|
mappings: mappings{
|
||||||
"test.a": {
|
{
|
||||||
|
statsdMetric: "test.a",
|
||||||
name: "name",
|
name: "name",
|
||||||
labels: map[string]string{
|
labels: map[string]string{
|
||||||
"label": "a_foo",
|
"label": "a_foo",
|
||||||
|
@ -406,7 +429,8 @@ mappings:
|
||||||
labels:
|
labels:
|
||||||
label: "${2}_foo"`,
|
label: "${2}_foo"`,
|
||||||
mappings: mappings{
|
mappings: mappings{
|
||||||
"foo.test.a": {
|
{
|
||||||
|
statsdMetric: "foo.test.a",
|
||||||
name: "name",
|
name: "name",
|
||||||
labels: map[string]string{
|
labels: map[string]string{
|
||||||
"label": "a_foo",
|
"label": "a_foo",
|
||||||
|
@ -429,7 +453,8 @@ mappings:
|
||||||
error: 0.002
|
error: 0.002
|
||||||
`,
|
`,
|
||||||
mappings: mappings{
|
mappings: mappings{
|
||||||
"test.*.*": {
|
{
|
||||||
|
statsdMetric: "test.*.*",
|
||||||
name: "foo",
|
name: "foo",
|
||||||
labels: map[string]string{},
|
labels: map[string]string{},
|
||||||
quantiles: []metricObjective{
|
quantiles: []metricObjective{
|
||||||
|
@ -448,7 +473,8 @@ mappings:
|
||||||
labels: {}
|
labels: {}
|
||||||
`,
|
`,
|
||||||
mappings: mappings{
|
mappings: mappings{
|
||||||
"test1.*.*": {
|
{
|
||||||
|
statsdMetric: "test1.*.*",
|
||||||
name: "foo",
|
name: "foo",
|
||||||
labels: map[string]string{},
|
labels: map[string]string{},
|
||||||
quantiles: []metricObjective{
|
quantiles: []metricObjective{
|
||||||
|
@ -491,6 +517,30 @@ mappings:
|
||||||
`,
|
`,
|
||||||
configBad: true,
|
configBad: true,
|
||||||
},
|
},
|
||||||
|
// Config with multiple explicit metric types
|
||||||
|
{
|
||||||
|
config: `---
|
||||||
|
mappings:
|
||||||
|
- match: test.foo.*
|
||||||
|
name: "test_foo_sum"
|
||||||
|
match_metric_type: counter
|
||||||
|
- match: test.foo.*
|
||||||
|
name: "test_foo_current"
|
||||||
|
match_metric_type: gauge
|
||||||
|
`,
|
||||||
|
mappings: mappings{
|
||||||
|
{
|
||||||
|
statsdMetric: "test.foo.test",
|
||||||
|
name: "test_foo_sum",
|
||||||
|
metricType: MetricTypeCounter,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
statsdMetric: "test.foo.test",
|
||||||
|
name: "test_foo_current",
|
||||||
|
metricType: MetricTypeGauge,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
//Config with uncompilable regex.
|
//Config with uncompilable regex.
|
||||||
{
|
{
|
||||||
config: `---
|
config: `---
|
||||||
|
@ -512,7 +562,8 @@ mappings:
|
||||||
labels: {}
|
labels: {}
|
||||||
`,
|
`,
|
||||||
mappings: mappings{
|
mappings: mappings{
|
||||||
"test.1.2": {
|
{
|
||||||
|
statsdMetric: "test.1.2",
|
||||||
name: "test_1_2",
|
name: "test_1_2",
|
||||||
labels: map[string]string{},
|
labels: map[string]string{},
|
||||||
notPresent: true,
|
notPresent: true,
|
||||||
|
@ -549,7 +600,8 @@ mappings:
|
||||||
job: "${1}_server"
|
job: "${1}_server"
|
||||||
`,
|
`,
|
||||||
mappings: mappings{
|
mappings: mappings{
|
||||||
"test.dispatcher.FooProcessor.send.success": {
|
{
|
||||||
|
statsdMetric: "test.dispatcher.FooProcessor.send.success",
|
||||||
name: "dispatcher_events_total",
|
name: "dispatcher_events_total",
|
||||||
labels: map[string]string{
|
labels: map[string]string{
|
||||||
"processor": "FooProcessor",
|
"processor": "FooProcessor",
|
||||||
|
@ -558,7 +610,8 @@ mappings:
|
||||||
"job": "test_dispatcher",
|
"job": "test_dispatcher",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
"foo_product.signup.facebook.failure": {
|
{
|
||||||
|
statsdMetric: "foo_product.signup.facebook.failure",
|
||||||
name: "signup_events_total",
|
name: "signup_events_total",
|
||||||
labels: map[string]string{
|
labels: map[string]string{
|
||||||
"provider": "facebook",
|
"provider": "facebook",
|
||||||
|
@ -566,7 +619,8 @@ mappings:
|
||||||
"job": "foo_product_server",
|
"job": "foo_product_server",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
"test.web-server.foo.bar": {
|
{
|
||||||
|
statsdMetric: "test.web-server.foo.bar",
|
||||||
name: "test_web_server_foo_bar",
|
name: "test_web_server_foo_bar",
|
||||||
labels: map[string]string{},
|
labels: map[string]string{},
|
||||||
},
|
},
|
||||||
|
@ -580,8 +634,12 @@ mappings:
|
||||||
name: "drop"
|
name: "drop"
|
||||||
action: drop`,
|
action: drop`,
|
||||||
mappings: mappings{
|
mappings: mappings{
|
||||||
"test.a": {},
|
{
|
||||||
"abc": {},
|
statsdMetric: "test.a",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
statsdMetric: "abc",
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
// Config that has a catch-all to drop all.
|
// Config that has a catch-all to drop all.
|
||||||
|
@ -596,8 +654,11 @@ mappings:
|
||||||
name: "drop"
|
name: "drop"
|
||||||
action: drop`,
|
action: drop`,
|
||||||
mappings: mappings{
|
mappings: mappings{
|
||||||
"test.a": {},
|
{
|
||||||
"web.localhost": {
|
statsdMetric: "test.a",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
statsdMetric: "web.localhost",
|
||||||
name: "web",
|
name: "web",
|
||||||
labels: map[string]string{
|
labels: map[string]string{
|
||||||
"site": "localhost",
|
"site": "localhost",
|
||||||
|
@ -614,8 +675,11 @@ mappings:
|
||||||
labels:
|
labels:
|
||||||
site: "$1"`,
|
site: "$1"`,
|
||||||
mappings: mappings{
|
mappings: mappings{
|
||||||
"test.a": {},
|
{
|
||||||
"web.localhost": {
|
statsdMetric: "test.a",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
statsdMetric: "web.localhost",
|
||||||
name: "web",
|
name: "web",
|
||||||
labels: map[string]string{
|
labels: map[string]string{
|
||||||
"site": "localhost",
|
"site": "localhost",
|
||||||
|
@ -634,8 +698,11 @@ mappings:
|
||||||
labels:
|
labels:
|
||||||
site: "$1"`,
|
site: "$1"`,
|
||||||
mappings: mappings{
|
mappings: mappings{
|
||||||
"test.a": {},
|
{
|
||||||
"web.localhost": {
|
statsdMetric: "test.a",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
statsdMetric: "web.localhost",
|
||||||
name: "web",
|
name: "web",
|
||||||
labels: map[string]string{
|
labels: map[string]string{
|
||||||
"site": "localhost",
|
"site": "localhost",
|
||||||
|
@ -655,8 +722,11 @@ mappings:
|
||||||
labels:
|
labels:
|
||||||
site: "$1"`,
|
site: "$1"`,
|
||||||
mappings: mappings{
|
mappings: mappings{
|
||||||
"test.a": {},
|
{
|
||||||
"web.localhost": {
|
statsdMetric: "test.a",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
statsdMetric: "web.localhost",
|
||||||
name: "web",
|
name: "web",
|
||||||
labels: map[string]string{
|
labels: map[string]string{
|
||||||
"site": "localhost",
|
"site": "localhost",
|
||||||
|
@ -669,7 +739,7 @@ mappings:
|
||||||
|
|
||||||
mapper := MetricMapper{}
|
mapper := MetricMapper{}
|
||||||
for i, scenario := range scenarios {
|
for i, scenario := range scenarios {
|
||||||
err := mapper.InitFromYAMLString(scenario.config, 0)
|
err := mapper.InitFromYAMLString(scenario.config, 1000)
|
||||||
if err != nil && !scenario.configBad {
|
if err != nil && !scenario.configBad {
|
||||||
t.Fatalf("%d. Config load error: %s %s", i, scenario.config, err)
|
t.Fatalf("%d. Config load error: %s %s", i, scenario.config, err)
|
||||||
}
|
}
|
||||||
|
@ -679,8 +749,12 @@ mappings:
|
||||||
|
|
||||||
for metric, mapping := range scenario.mappings {
|
for metric, mapping := range scenario.mappings {
|
||||||
// exporter will call mapper.GetMapping with valid MetricType
|
// exporter will call mapper.GetMapping with valid MetricType
|
||||||
// so we also pass a sane MetricType in testing
|
// so we also pass a sane MetricType in testing if it's not specified
|
||||||
m, labels, present := mapper.GetMapping(metric, MetricTypeCounter)
|
mapType := mapping.metricType
|
||||||
|
if mapType == "" {
|
||||||
|
mapType = MetricTypeCounter
|
||||||
|
}
|
||||||
|
m, labels, present := mapper.GetMapping(mapping.statsdMetric, mapType)
|
||||||
if present && mapping.name != "" && m.Name != mapping.name {
|
if present && mapping.name != "" && m.Name != mapping.name {
|
||||||
t.Fatalf("%d.%q: Expected name %v, got %v", i, metric, m.Name, mapping.name)
|
t.Fatalf("%d.%q: Expected name %v, got %v", i, metric, m.Name, mapping.name)
|
||||||
}
|
}
|
||||||
|
@ -698,6 +772,9 @@ mappings:
|
||||||
if mapping.ttl > 0 && mapping.ttl != m.Ttl {
|
if mapping.ttl > 0 && mapping.ttl != m.Ttl {
|
||||||
t.Fatalf("%d.%q: Expected ttl of %s, got %s", i, metric, mapping.ttl.String(), m.Ttl.String())
|
t.Fatalf("%d.%q: Expected ttl of %s, got %s", i, metric, mapping.ttl.String(), m.Ttl.String())
|
||||||
}
|
}
|
||||||
|
if mapping.metricType != "" && mapType != m.MatchMetricType {
|
||||||
|
t.Fatalf("%d.%q: Expected match metric of %s, got %s", i, metric, mapType, m.MatchMetricType)
|
||||||
|
}
|
||||||
|
|
||||||
if len(mapping.quantiles) != 0 {
|
if len(mapping.quantiles) != 0 {
|
||||||
if len(mapping.quantiles) != len(m.Quantiles) {
|
if len(mapping.quantiles) != len(m.Quantiles) {
|
||||||
|
|
Loading…
Reference in a new issue