2015-02-18 21:40:55 +00:00
|
|
|
// Copyright 2013 The Prometheus Authors
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
2013-07-05 17:31:53 +00:00
|
|
|
//
|
2015-02-18 21:40:55 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2013-07-05 17:31:53 +00:00
|
|
|
|
2018-08-10 12:28:38 +00:00
|
|
|
package mapper
|
2013-07-05 17:31:53 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2018-12-13 13:53:40 +00:00
|
|
|
"time"
|
2013-07-05 17:31:53 +00:00
|
|
|
)
|
|
|
|
|
2019-06-08 00:04:33 +00:00
|
|
|
type mappings []struct {
|
|
|
|
statsdMetric string
|
|
|
|
name string
|
|
|
|
labels map[string]string
|
|
|
|
quantiles []metricObjective
|
|
|
|
notPresent bool
|
|
|
|
ttl time.Duration
|
|
|
|
metricType MetricType
|
2017-11-13 12:21:29 +00:00
|
|
|
}
|
|
|
|
|
2017-07-27 20:58:10 +00:00
|
|
|
func TestMetricMapperYAML(t *testing.T) {
|
|
|
|
scenarios := []struct {
|
|
|
|
config string
|
|
|
|
configBad bool
|
2017-11-13 12:21:29 +00:00
|
|
|
mappings mappings
|
2017-07-27 20:58:10 +00:00
|
|
|
}{
|
|
|
|
// Empty config.
|
|
|
|
{},
|
|
|
|
// Config with several mapping definitions.
|
|
|
|
{
|
|
|
|
config: `---
|
|
|
|
mappings:
|
|
|
|
- match: test.dispatcher.*.*.*
|
2017-11-13 12:21:29 +00:00
|
|
|
name: "dispatch_events"
|
2017-07-27 20:58:10 +00:00
|
|
|
labels:
|
|
|
|
processor: "$1"
|
|
|
|
action: "$2"
|
|
|
|
result: "$3"
|
|
|
|
job: "test_dispatcher"
|
|
|
|
- match: test.my-dispatch-host01.name.dispatcher.*.*.*
|
2017-11-13 12:21:29 +00:00
|
|
|
name: "host_dispatch_events"
|
2017-07-27 20:58:10 +00:00
|
|
|
labels:
|
|
|
|
processor: "$1"
|
|
|
|
action: "$2"
|
|
|
|
result: "$3"
|
|
|
|
job: "test_dispatcher"
|
2017-09-20 10:27:12 +00:00
|
|
|
- match: request_time.*.*.*.*.*.*.*.*.*.*.*.*
|
2017-11-13 12:21:29 +00:00
|
|
|
name: "tyk_http_request"
|
2017-09-20 10:27:12 +00:00
|
|
|
labels:
|
|
|
|
method_and_path: "${1}"
|
|
|
|
response_code: "${2}"
|
|
|
|
apikey: "${3}"
|
|
|
|
apiversion: "${4}"
|
|
|
|
apiname: "${5}"
|
|
|
|
apiid: "${6}"
|
|
|
|
ipv4_t1: "${7}"
|
|
|
|
ipv4_t2: "${8}"
|
|
|
|
ipv4_t3: "${9}"
|
|
|
|
ipv4_t4: "${10}"
|
|
|
|
orgid: "${11}"
|
|
|
|
oauthid: "${12}"
|
2017-07-27 20:58:10 +00:00
|
|
|
- match: "*.*"
|
2017-11-13 12:21:29 +00:00
|
|
|
name: "catchall"
|
2017-07-27 20:58:10 +00:00
|
|
|
labels:
|
|
|
|
first: "$1"
|
|
|
|
second: "$2"
|
|
|
|
third: "$3"
|
|
|
|
job: "$1-$2-$3"
|
2017-09-29 07:57:17 +00:00
|
|
|
- match: (.*)\.(.*)-(.*)\.(.*)
|
|
|
|
match_type: regex
|
2017-11-13 12:21:29 +00:00
|
|
|
name: "proxy_requests_total"
|
2017-09-29 07:57:17 +00:00
|
|
|
labels:
|
|
|
|
job: "$1"
|
|
|
|
protocol: "$2"
|
|
|
|
endpoint: "$3"
|
|
|
|
result: "$4"
|
|
|
|
|
2017-07-27 20:58:10 +00:00
|
|
|
`,
|
2017-11-13 12:21:29 +00:00
|
|
|
mappings: mappings{
|
2019-06-08 00:04:33 +00:00
|
|
|
{
|
|
|
|
statsdMetric: "test.dispatcher.FooProcessor.send.succeeded",
|
|
|
|
name: "dispatch_events",
|
2017-11-13 12:21:29 +00:00
|
|
|
labels: map[string]string{
|
|
|
|
"processor": "FooProcessor",
|
|
|
|
"action": "send",
|
|
|
|
"result": "succeeded",
|
|
|
|
"job": "test_dispatcher",
|
|
|
|
},
|
2017-07-27 20:58:10 +00:00
|
|
|
},
|
2019-06-08 00:04:33 +00:00
|
|
|
{
|
|
|
|
statsdMetric: "test.my-dispatch-host01.name.dispatcher.FooProcessor.send.succeeded",
|
|
|
|
name: "host_dispatch_events",
|
2017-11-13 12:21:29 +00:00
|
|
|
labels: map[string]string{
|
|
|
|
"processor": "FooProcessor",
|
|
|
|
"action": "send",
|
|
|
|
"result": "succeeded",
|
|
|
|
"job": "test_dispatcher",
|
|
|
|
},
|
2017-07-27 20:58:10 +00:00
|
|
|
},
|
2019-06-08 00:04:33 +00:00
|
|
|
{
|
|
|
|
statsdMetric: "request_time.get/threads/1/posts.200.00000000.nonversioned.discussions.a11bbcdf0ac64ec243658dc64b7100fb.172.20.0.1.12ba97b7eaa1a50001000001.",
|
|
|
|
name: "tyk_http_request",
|
2017-11-13 12:21:29 +00:00
|
|
|
labels: map[string]string{
|
|
|
|
"method_and_path": "get/threads/1/posts",
|
|
|
|
"response_code": "200",
|
|
|
|
"apikey": "00000000",
|
|
|
|
"apiversion": "nonversioned",
|
|
|
|
"apiname": "discussions",
|
|
|
|
"apiid": "a11bbcdf0ac64ec243658dc64b7100fb",
|
|
|
|
"ipv4_t1": "172",
|
|
|
|
"ipv4_t2": "20",
|
|
|
|
"ipv4_t3": "0",
|
|
|
|
"ipv4_t4": "1",
|
|
|
|
"orgid": "12ba97b7eaa1a50001000001",
|
|
|
|
"oauthid": "",
|
|
|
|
},
|
2017-09-20 10:27:12 +00:00
|
|
|
},
|
2019-06-08 00:04:33 +00:00
|
|
|
{
|
|
|
|
statsdMetric: "foo.bar",
|
|
|
|
name: "catchall",
|
2017-11-13 12:21:29 +00:00
|
|
|
labels: map[string]string{
|
|
|
|
"first": "foo",
|
|
|
|
"second": "bar",
|
|
|
|
"third": "",
|
|
|
|
"job": "foo-bar-",
|
|
|
|
},
|
2017-07-27 20:58:10 +00:00
|
|
|
},
|
2019-06-08 00:04:33 +00:00
|
|
|
{
|
|
|
|
statsdMetric: "foo.bar.baz",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
statsdMetric: "proxy-1.http-goober.success",
|
|
|
|
name: "proxy_requests_total",
|
2017-11-13 12:21:29 +00:00
|
|
|
labels: map[string]string{
|
|
|
|
"job": "proxy-1",
|
|
|
|
"protocol": "http",
|
|
|
|
"endpoint": "goober",
|
|
|
|
"result": "success",
|
|
|
|
},
|
2017-09-29 07:57:17 +00:00
|
|
|
},
|
2017-07-27 20:58:10 +00:00
|
|
|
},
|
|
|
|
},
|
2018-09-13 01:18:12 +00:00
|
|
|
//Config with backtracking
|
|
|
|
{
|
|
|
|
config: `
|
2018-09-13 18:46:41 +00:00
|
|
|
defaults:
|
|
|
|
glob_disable_ordering: true
|
2018-09-13 01:18:12 +00:00
|
|
|
mappings:
|
2018-09-13 18:46:41 +00:00
|
|
|
- match: backtrack.*.bbb
|
2018-09-13 01:18:12 +00:00
|
|
|
name: "testb"
|
|
|
|
labels:
|
|
|
|
label: "${1}_foo"
|
2018-09-13 18:46:41 +00:00
|
|
|
- match: backtrack.justatest.aaa
|
2018-09-13 01:18:12 +00:00
|
|
|
name: "testa"
|
|
|
|
labels:
|
|
|
|
label: "${1}_foo"
|
|
|
|
`,
|
|
|
|
mappings: mappings{
|
2019-06-08 00:04:33 +00:00
|
|
|
{
|
|
|
|
statsdMetric: "backtrack.good.bbb",
|
|
|
|
name: "testb",
|
2018-09-13 01:18:12 +00:00
|
|
|
labels: map[string]string{
|
|
|
|
"label": "good_foo",
|
|
|
|
},
|
|
|
|
},
|
2019-06-08 00:04:33 +00:00
|
|
|
{
|
|
|
|
statsdMetric: "backtrack.justatest.bbb",
|
|
|
|
name: "testb",
|
2018-09-13 01:18:12 +00:00
|
|
|
labels: map[string]string{
|
|
|
|
"label": "justatest_foo",
|
|
|
|
},
|
|
|
|
},
|
2019-06-08 00:04:33 +00:00
|
|
|
{
|
|
|
|
statsdMetric: "backtrack.justatest.aaa",
|
|
|
|
name: "testa",
|
2018-12-05 19:25:43 +00:00
|
|
|
labels: map[string]string{
|
|
|
|
"label": "_foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
//Config with backtracking, the non-matched rule has star(s)
|
|
|
|
// A metric like full.name.anothertest will first match full.name.* and then tries
|
|
|
|
// to match *.dummy.* and then failed.
|
|
|
|
// This test case makes sure the captures in the non-matched later rule
|
|
|
|
// doesn't affect the captures in the first matched rule.
|
|
|
|
{
|
|
|
|
config: `
|
|
|
|
defaults:
|
|
|
|
glob_disable_ordering: false
|
|
|
|
mappings:
|
|
|
|
- match: '*.dummy.*'
|
|
|
|
name: metric_one
|
|
|
|
labels:
|
|
|
|
system: $1
|
|
|
|
attribute: $2
|
|
|
|
- match: 'full.name.*'
|
|
|
|
name: metric_two
|
|
|
|
labels:
|
|
|
|
system: static
|
|
|
|
attribute: $1
|
|
|
|
`,
|
|
|
|
mappings: mappings{
|
2019-06-08 00:04:33 +00:00
|
|
|
{
|
|
|
|
statsdMetric: "whatever.dummy.test",
|
|
|
|
name: "metric_one",
|
2018-12-05 19:25:43 +00:00
|
|
|
labels: map[string]string{
|
|
|
|
"system": "whatever",
|
|
|
|
"attribute": "test",
|
|
|
|
},
|
|
|
|
},
|
2019-06-08 00:04:33 +00:00
|
|
|
{
|
|
|
|
statsdMetric: "full.name.anothertest",
|
|
|
|
name: "metric_two",
|
2018-12-05 19:25:43 +00:00
|
|
|
labels: map[string]string{
|
|
|
|
"system": "static",
|
|
|
|
"attribute": "anothertest",
|
|
|
|
},
|
|
|
|
},
|
2018-09-13 01:18:12 +00:00
|
|
|
},
|
|
|
|
},
|
2018-09-13 18:46:41 +00:00
|
|
|
//Config with super sets, disables ordering
|
2018-09-13 01:18:12 +00:00
|
|
|
{
|
|
|
|
config: `
|
2018-09-13 18:46:41 +00:00
|
|
|
defaults:
|
|
|
|
glob_disable_ordering: true
|
2018-09-13 01:18:12 +00:00
|
|
|
mappings:
|
2018-09-13 18:46:41 +00:00
|
|
|
- match: noorder.*.*
|
|
|
|
name: "testa"
|
|
|
|
labels:
|
|
|
|
label: "${1}_foo"
|
|
|
|
- match: noorder.*.bbb
|
2018-09-13 01:18:12 +00:00
|
|
|
name: "testb"
|
|
|
|
labels:
|
|
|
|
label: "${1}_foo"
|
2018-09-13 18:46:41 +00:00
|
|
|
- match: noorder.ccc.bbb
|
|
|
|
name: "testc"
|
|
|
|
labels:
|
|
|
|
label: "ccc_foo"
|
|
|
|
`,
|
|
|
|
mappings: mappings{
|
2019-06-08 00:04:33 +00:00
|
|
|
{
|
|
|
|
statsdMetric: "noorder.good.bbb",
|
|
|
|
name: "testb",
|
2018-09-13 18:46:41 +00:00
|
|
|
labels: map[string]string{
|
|
|
|
"label": "good_foo",
|
|
|
|
},
|
|
|
|
},
|
2019-06-08 00:04:33 +00:00
|
|
|
{
|
|
|
|
statsdMetric: "noorder.ccc.bbb",
|
|
|
|
name: "testc",
|
2018-09-13 18:46:41 +00:00
|
|
|
labels: map[string]string{
|
|
|
|
"label": "ccc_foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
//Config with super sets, keeps ordering
|
|
|
|
{
|
|
|
|
config: `
|
|
|
|
defaults:
|
|
|
|
glob_disable_ordering: false
|
|
|
|
mappings:
|
|
|
|
- match: order.*.*
|
2018-09-13 01:18:12 +00:00
|
|
|
name: "testa"
|
|
|
|
labels:
|
|
|
|
label: "${1}_foo"
|
2018-09-13 18:46:41 +00:00
|
|
|
- match: order.*.bbb
|
|
|
|
name: "testb"
|
|
|
|
labels:
|
|
|
|
label: "${1}_foo"
|
2018-09-13 01:18:12 +00:00
|
|
|
`,
|
|
|
|
mappings: mappings{
|
2019-06-08 00:04:33 +00:00
|
|
|
{
|
|
|
|
statsdMetric: "order.good.bbb",
|
|
|
|
name: "testa",
|
2018-09-13 01:18:12 +00:00
|
|
|
labels: map[string]string{
|
|
|
|
"label": "good_foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2017-07-27 20:58:10 +00:00
|
|
|
// Config with bad regex reference.
|
|
|
|
{
|
|
|
|
config: `---
|
|
|
|
mappings:
|
|
|
|
- match: test.*
|
2017-11-13 12:21:29 +00:00
|
|
|
name: "name"
|
2017-07-27 20:58:10 +00:00
|
|
|
labels:
|
|
|
|
label: "$1_foo"
|
|
|
|
`,
|
2017-11-13 12:21:29 +00:00
|
|
|
mappings: mappings{
|
2019-06-08 00:04:33 +00:00
|
|
|
{
|
|
|
|
statsdMetric: "test.a",
|
|
|
|
name: "name",
|
2017-11-13 12:21:29 +00:00
|
|
|
labels: map[string]string{
|
|
|
|
"label": "",
|
|
|
|
},
|
2017-07-27 20:58:10 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
// Config with good regex reference.
|
|
|
|
{
|
|
|
|
config: `
|
|
|
|
mappings:
|
|
|
|
- match: test.*
|
2017-11-13 12:21:29 +00:00
|
|
|
name: "name"
|
2017-07-27 20:58:10 +00:00
|
|
|
labels:
|
|
|
|
label: "${1}_foo"
|
|
|
|
`,
|
2017-11-13 12:21:29 +00:00
|
|
|
mappings: mappings{
|
2019-06-08 00:04:33 +00:00
|
|
|
{
|
|
|
|
statsdMetric: "test.a",
|
|
|
|
name: "name",
|
2017-11-13 12:21:29 +00:00
|
|
|
labels: map[string]string{
|
|
|
|
"label": "a_foo",
|
|
|
|
},
|
2017-07-27 20:58:10 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
// Config with bad metric line.
|
|
|
|
{
|
|
|
|
config: `---
|
|
|
|
mappings:
|
|
|
|
- match: bad--metric-line.*.*
|
2017-11-13 12:21:29 +00:00
|
|
|
name: "foo"
|
|
|
|
labels: {}
|
2017-07-27 20:58:10 +00:00
|
|
|
`,
|
|
|
|
configBad: true,
|
|
|
|
},
|
2018-01-16 13:16:15 +00:00
|
|
|
// Config with dynamic metric name.
|
|
|
|
{
|
|
|
|
config: `---
|
|
|
|
mappings:
|
|
|
|
- match: test1.*.*
|
|
|
|
name: "$1"
|
|
|
|
labels: {}
|
|
|
|
- match: test2.*.*
|
|
|
|
name: "${1}_$2"
|
|
|
|
labels: {}
|
|
|
|
- match: test3\.(\w+)\.(\w+)
|
|
|
|
match_type: regex
|
|
|
|
name: "${2}_$1"
|
|
|
|
labels: {}
|
|
|
|
`,
|
|
|
|
mappings: mappings{
|
2019-06-08 00:04:33 +00:00
|
|
|
{
|
|
|
|
statsdMetric: "test1.total_requests.count",
|
|
|
|
name: "total_requests",
|
2018-01-16 13:16:15 +00:00
|
|
|
},
|
2019-06-08 00:04:33 +00:00
|
|
|
{
|
|
|
|
statsdMetric: "test2.total_requests.count",
|
|
|
|
name: "total_requests_count",
|
2018-01-16 13:16:15 +00:00
|
|
|
},
|
2019-06-08 00:04:33 +00:00
|
|
|
{
|
|
|
|
statsdMetric: "test3.total_requests.count",
|
|
|
|
name: "count_total_requests",
|
2018-01-16 13:16:15 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2017-07-27 20:58:10 +00:00
|
|
|
// Config with bad metric name.
|
|
|
|
{
|
|
|
|
config: `---
|
|
|
|
mappings:
|
|
|
|
- match: test.*.*
|
2017-11-13 12:21:29 +00:00
|
|
|
name: "0foo"
|
|
|
|
labels: {}
|
2017-07-27 20:58:10 +00:00
|
|
|
`,
|
|
|
|
configBad: true,
|
|
|
|
},
|
|
|
|
// Config with no metric name.
|
|
|
|
{
|
|
|
|
config: `---
|
|
|
|
mappings:
|
|
|
|
- match: test.*.*
|
|
|
|
labels:
|
|
|
|
this: "$1"
|
|
|
|
`,
|
|
|
|
configBad: true,
|
|
|
|
},
|
|
|
|
// Config with no mappings.
|
|
|
|
{
|
|
|
|
config: ``,
|
2017-11-13 12:21:29 +00:00
|
|
|
mappings: mappings{},
|
2017-07-27 20:58:10 +00:00
|
|
|
},
|
2017-11-10 20:01:15 +00:00
|
|
|
// Config without a trailing newline.
|
|
|
|
{
|
|
|
|
config: `mappings:
|
|
|
|
- match: test.*
|
2017-11-13 12:21:29 +00:00
|
|
|
name: "name"
|
2017-11-10 20:01:15 +00:00
|
|
|
labels:
|
|
|
|
label: "${1}_foo"`,
|
2017-11-13 12:21:29 +00:00
|
|
|
mappings: mappings{
|
2019-06-08 00:04:33 +00:00
|
|
|
{
|
|
|
|
statsdMetric: "test.a",
|
|
|
|
name: "name",
|
2017-11-13 12:21:29 +00:00
|
|
|
labels: map[string]string{
|
|
|
|
"label": "a_foo",
|
|
|
|
},
|
2017-11-10 20:01:15 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2017-11-10 20:01:57 +00:00
|
|
|
// Config with an improperly escaped *.
|
|
|
|
{
|
|
|
|
config: `
|
|
|
|
mappings:
|
|
|
|
- match: *.test.*
|
2017-11-13 12:21:29 +00:00
|
|
|
name: "name"
|
2017-11-10 20:01:57 +00:00
|
|
|
labels:
|
|
|
|
label: "${1}_foo"`,
|
|
|
|
configBad: true,
|
|
|
|
},
|
|
|
|
// Config with a properly escaped *.
|
|
|
|
{
|
|
|
|
config: `
|
|
|
|
mappings:
|
|
|
|
- match: "*.test.*"
|
2017-11-13 12:21:29 +00:00
|
|
|
name: "name"
|
2017-11-10 20:01:57 +00:00
|
|
|
labels:
|
|
|
|
label: "${2}_foo"`,
|
2017-11-13 12:21:29 +00:00
|
|
|
mappings: mappings{
|
2019-06-08 00:04:33 +00:00
|
|
|
{
|
|
|
|
statsdMetric: "foo.test.a",
|
|
|
|
name: "name",
|
2017-11-13 12:21:29 +00:00
|
|
|
labels: map[string]string{
|
|
|
|
"label": "a_foo",
|
|
|
|
},
|
2017-11-10 20:01:57 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2017-07-27 21:01:12 +00:00
|
|
|
// Config with good timer type.
|
|
|
|
{
|
|
|
|
config: `---
|
|
|
|
mappings:
|
|
|
|
- match: test.*.*
|
|
|
|
timer_type: summary
|
2017-11-13 12:21:29 +00:00
|
|
|
name: "foo"
|
|
|
|
labels: {}
|
2018-08-09 02:41:41 +00:00
|
|
|
quantiles:
|
|
|
|
- quantile: 0.42
|
|
|
|
error: 0.04
|
|
|
|
- quantile: 0.7
|
|
|
|
error: 0.002
|
2017-07-27 21:01:12 +00:00
|
|
|
`,
|
2017-11-13 12:21:29 +00:00
|
|
|
mappings: mappings{
|
2019-06-08 00:04:33 +00:00
|
|
|
{
|
|
|
|
statsdMetric: "test.*.*",
|
|
|
|
name: "foo",
|
|
|
|
labels: map[string]string{},
|
2018-08-09 02:41:41 +00:00
|
|
|
quantiles: []metricObjective{
|
|
|
|
{Quantile: 0.42, Error: 0.04},
|
|
|
|
{Quantile: 0.7, Error: 0.002},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
config: `---
|
|
|
|
mappings:
|
|
|
|
- match: test1.*.*
|
|
|
|
timer_type: summary
|
|
|
|
name: "foo"
|
|
|
|
labels: {}
|
|
|
|
`,
|
|
|
|
mappings: mappings{
|
2019-06-08 00:04:33 +00:00
|
|
|
{
|
|
|
|
statsdMetric: "test1.*.*",
|
|
|
|
name: "foo",
|
|
|
|
labels: map[string]string{},
|
2018-08-09 02:41:41 +00:00
|
|
|
quantiles: []metricObjective{
|
|
|
|
{Quantile: 0.5, Error: 0.05},
|
|
|
|
{Quantile: 0.9, Error: 0.01},
|
|
|
|
{Quantile: 0.99, Error: 0.001},
|
|
|
|
},
|
2017-07-27 21:01:12 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
// Config with bad timer type.
|
|
|
|
{
|
|
|
|
config: `---
|
|
|
|
mappings:
|
|
|
|
- match: test.*.*
|
|
|
|
timer_type: wrong
|
2017-11-13 12:21:29 +00:00
|
|
|
name: "foo"
|
2018-06-14 12:27:28 +00:00
|
|
|
labels: {}
|
|
|
|
`,
|
|
|
|
configBad: true,
|
|
|
|
},
|
|
|
|
// Config with good metric type.
|
|
|
|
{
|
|
|
|
config: `---
|
|
|
|
mappings:
|
|
|
|
- match: test.*.*
|
|
|
|
match_metric_type: counter
|
|
|
|
name: "foo"
|
|
|
|
labels: {}
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
// Config with bad metric type matcher.
|
|
|
|
{
|
|
|
|
config: `---
|
|
|
|
mappings:
|
|
|
|
- match: test.*.*
|
|
|
|
match_metric_type: wrong
|
|
|
|
name: "foo"
|
2017-11-13 12:21:29 +00:00
|
|
|
labels: {}
|
2017-09-29 07:57:17 +00:00
|
|
|
`,
|
|
|
|
configBad: true,
|
|
|
|
},
|
2019-06-08 00:04:33 +00:00
|
|
|
// 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,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2017-09-29 07:57:17 +00:00
|
|
|
//Config with uncompilable regex.
|
|
|
|
{
|
|
|
|
config: `---
|
|
|
|
mappings:
|
2018-02-22 22:44:09 +00:00
|
|
|
- match: "*\\.foo"
|
2017-11-13 12:21:29 +00:00
|
|
|
match_type: regex
|
|
|
|
name: "foo"
|
|
|
|
labels: {}
|
|
|
|
`,
|
|
|
|
configBad: true,
|
|
|
|
},
|
|
|
|
//Config with non-matched metric.
|
|
|
|
{
|
|
|
|
config: `---
|
|
|
|
mappings:
|
|
|
|
- match: foo.*.*
|
|
|
|
timer_type: summary
|
|
|
|
name: "foo"
|
|
|
|
labels: {}
|
|
|
|
`,
|
|
|
|
mappings: mappings{
|
2019-06-08 00:04:33 +00:00
|
|
|
{
|
|
|
|
statsdMetric: "test.1.2",
|
|
|
|
name: "test_1_2",
|
|
|
|
labels: map[string]string{},
|
|
|
|
notPresent: true,
|
2017-11-13 12:21:29 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
//Config with no name.
|
|
|
|
{
|
|
|
|
config: `---
|
|
|
|
mappings:
|
|
|
|
- match: *\.foo
|
2017-09-29 07:57:17 +00:00
|
|
|
match_type: regex
|
2017-07-27 21:01:12 +00:00
|
|
|
labels:
|
2017-11-13 12:21:29 +00:00
|
|
|
bar: "foo"
|
2017-07-27 21:01:12 +00:00
|
|
|
`,
|
|
|
|
configBad: true,
|
|
|
|
},
|
2017-11-10 20:18:57 +00:00
|
|
|
// Example from the README.
|
|
|
|
{
|
|
|
|
config: `
|
|
|
|
mappings:
|
|
|
|
- match: test.dispatcher.*.*.*
|
2017-11-13 12:21:29 +00:00
|
|
|
name: "dispatcher_events_total"
|
2017-11-10 20:18:57 +00:00
|
|
|
labels:
|
|
|
|
processor: "$1"
|
|
|
|
action: "$2"
|
|
|
|
outcome: "$3"
|
|
|
|
job: "test_dispatcher"
|
|
|
|
- match: "*.signup.*.*"
|
2017-11-13 12:21:29 +00:00
|
|
|
name: "signup_events_total"
|
2017-11-10 20:18:57 +00:00
|
|
|
labels:
|
|
|
|
provider: "$2"
|
|
|
|
outcome: "$3"
|
|
|
|
job: "${1}_server"
|
|
|
|
`,
|
2017-11-13 12:21:29 +00:00
|
|
|
mappings: mappings{
|
2019-06-08 00:04:33 +00:00
|
|
|
{
|
|
|
|
statsdMetric: "test.dispatcher.FooProcessor.send.success",
|
|
|
|
name: "dispatcher_events_total",
|
2017-11-13 12:21:29 +00:00
|
|
|
labels: map[string]string{
|
|
|
|
"processor": "FooProcessor",
|
|
|
|
"action": "send",
|
|
|
|
"outcome": "success",
|
|
|
|
"job": "test_dispatcher",
|
|
|
|
},
|
2017-11-10 20:18:57 +00:00
|
|
|
},
|
2019-06-08 00:04:33 +00:00
|
|
|
{
|
|
|
|
statsdMetric: "foo_product.signup.facebook.failure",
|
|
|
|
name: "signup_events_total",
|
2017-11-13 12:21:29 +00:00
|
|
|
labels: map[string]string{
|
|
|
|
"provider": "facebook",
|
|
|
|
"outcome": "failure",
|
|
|
|
"job": "foo_product_server",
|
|
|
|
},
|
|
|
|
},
|
2019-06-08 00:04:33 +00:00
|
|
|
{
|
|
|
|
statsdMetric: "test.web-server.foo.bar",
|
|
|
|
name: "test_web_server_foo_bar",
|
|
|
|
labels: map[string]string{},
|
2017-11-10 20:18:57 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2018-01-02 22:21:50 +00:00
|
|
|
// Config that drops all.
|
|
|
|
{
|
|
|
|
config: `mappings:
|
|
|
|
- match: .
|
|
|
|
match_type: regex
|
|
|
|
name: "drop"
|
|
|
|
action: drop`,
|
|
|
|
mappings: mappings{
|
2019-06-08 00:04:33 +00:00
|
|
|
{
|
|
|
|
statsdMetric: "test.a",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
statsdMetric: "abc",
|
|
|
|
},
|
2018-01-02 22:21:50 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
// Config that has a catch-all to drop all.
|
|
|
|
{
|
|
|
|
config: `mappings:
|
|
|
|
- match: web.*
|
|
|
|
name: "web"
|
|
|
|
labels:
|
|
|
|
site: "$1"
|
|
|
|
- match: .
|
|
|
|
match_type: regex
|
|
|
|
name: "drop"
|
|
|
|
action: drop`,
|
|
|
|
mappings: mappings{
|
2019-06-08 00:04:33 +00:00
|
|
|
{
|
|
|
|
statsdMetric: "test.a",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
statsdMetric: "web.localhost",
|
|
|
|
name: "web",
|
2018-01-02 22:21:50 +00:00
|
|
|
labels: map[string]string{
|
|
|
|
"site": "localhost",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2018-12-13 13:53:40 +00:00
|
|
|
// Config that has a ttl.
|
|
|
|
{
|
|
|
|
config: `mappings:
|
|
|
|
- match: web.*
|
|
|
|
name: "web"
|
|
|
|
ttl: 10s
|
|
|
|
labels:
|
|
|
|
site: "$1"`,
|
|
|
|
mappings: mappings{
|
2019-06-08 00:04:33 +00:00
|
|
|
{
|
|
|
|
statsdMetric: "test.a",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
statsdMetric: "web.localhost",
|
|
|
|
name: "web",
|
2018-12-13 13:53:40 +00:00
|
|
|
labels: map[string]string{
|
|
|
|
"site": "localhost",
|
|
|
|
},
|
|
|
|
ttl: time.Second * 10,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
// Config that has a default ttl.
|
|
|
|
{
|
|
|
|
config: `defaults:
|
|
|
|
ttl: 1m2s
|
|
|
|
mappings:
|
|
|
|
- match: web.*
|
|
|
|
name: "web"
|
|
|
|
labels:
|
|
|
|
site: "$1"`,
|
|
|
|
mappings: mappings{
|
2019-06-08 00:04:33 +00:00
|
|
|
{
|
|
|
|
statsdMetric: "test.a",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
statsdMetric: "web.localhost",
|
|
|
|
name: "web",
|
2018-12-13 13:53:40 +00:00
|
|
|
labels: map[string]string{
|
|
|
|
"site": "localhost",
|
|
|
|
},
|
|
|
|
ttl: time.Minute + time.Second*2,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
// Config that override a default ttl.
|
|
|
|
{
|
|
|
|
config: `defaults:
|
|
|
|
ttl: 1m2s
|
|
|
|
mappings:
|
|
|
|
- match: web.*
|
|
|
|
name: "web"
|
|
|
|
ttl: 5s
|
|
|
|
labels:
|
|
|
|
site: "$1"`,
|
|
|
|
mappings: mappings{
|
2019-06-08 00:04:33 +00:00
|
|
|
{
|
|
|
|
statsdMetric: "test.a",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
statsdMetric: "web.localhost",
|
|
|
|
name: "web",
|
2018-12-13 13:53:40 +00:00
|
|
|
labels: map[string]string{
|
|
|
|
"site": "localhost",
|
|
|
|
},
|
|
|
|
ttl: time.Second * 5,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2017-07-27 20:58:10 +00:00
|
|
|
}
|
|
|
|
|
2018-08-10 12:28:38 +00:00
|
|
|
mapper := MetricMapper{}
|
2017-07-27 20:58:10 +00:00
|
|
|
for i, scenario := range scenarios {
|
2019-06-08 00:04:33 +00:00
|
|
|
err := mapper.InitFromYAMLString(scenario.config, 1000)
|
2017-07-27 20:58:10 +00:00
|
|
|
if err != nil && !scenario.configBad {
|
|
|
|
t.Fatalf("%d. Config load error: %s %s", i, scenario.config, err)
|
|
|
|
}
|
|
|
|
if err == nil && scenario.configBad {
|
|
|
|
t.Fatalf("%d. Expected bad config, but loaded ok: %s", i, scenario.config)
|
|
|
|
}
|
|
|
|
|
|
|
|
for metric, mapping := range scenario.mappings {
|
2018-10-03 23:30:01 +00:00
|
|
|
// exporter will call mapper.GetMapping with valid MetricType
|
2019-06-08 00:04:33 +00:00
|
|
|
// so we also pass a sane MetricType in testing if it's not specified
|
|
|
|
mapType := mapping.metricType
|
|
|
|
if mapType == "" {
|
|
|
|
mapType = MetricTypeCounter
|
|
|
|
}
|
|
|
|
m, labels, present := mapper.GetMapping(mapping.statsdMetric, mapType)
|
2018-01-02 22:21:50 +00:00
|
|
|
if present && mapping.name != "" && m.Name != mapping.name {
|
2017-11-13 12:21:29 +00:00
|
|
|
t.Fatalf("%d.%q: Expected name %v, got %v", i, metric, m.Name, mapping.name)
|
|
|
|
}
|
|
|
|
if mapping.notPresent && present {
|
2017-07-27 20:58:10 +00:00
|
|
|
t.Fatalf("%d.%q: Expected metric to not be present", i, metric)
|
|
|
|
}
|
2017-11-13 12:21:29 +00:00
|
|
|
if len(labels) != len(mapping.labels) {
|
|
|
|
t.Fatalf("%d.%q: Expected %d labels, got %d", i, metric, len(mapping.labels), len(labels))
|
2017-07-27 20:58:10 +00:00
|
|
|
}
|
|
|
|
for label, value := range labels {
|
2017-11-13 12:21:29 +00:00
|
|
|
if mapping.labels[label] != value {
|
2017-07-27 20:58:10 +00:00
|
|
|
t.Fatalf("%d.%q: Expected labels %v, got %v", i, metric, mapping, labels)
|
|
|
|
}
|
|
|
|
}
|
2018-12-13 13:53:40 +00:00
|
|
|
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())
|
|
|
|
}
|
2019-06-08 00:04:33 +00:00
|
|
|
if mapping.metricType != "" && mapType != m.MatchMetricType {
|
|
|
|
t.Fatalf("%d.%q: Expected match metric of %s, got %s", i, metric, mapType, m.MatchMetricType)
|
|
|
|
}
|
2017-11-13 12:21:29 +00:00
|
|
|
|
2018-08-09 02:41:41 +00:00
|
|
|
if len(mapping.quantiles) != 0 {
|
|
|
|
if len(mapping.quantiles) != len(m.Quantiles) {
|
|
|
|
t.Fatalf("%d.%q: Expected %d quantiles, got %d", i, metric, len(mapping.quantiles), len(m.Quantiles))
|
|
|
|
}
|
|
|
|
for i, quantile := range mapping.quantiles {
|
|
|
|
if quantile.Quantile != m.Quantiles[i].Quantile {
|
|
|
|
t.Fatalf("%d.%q: Expected quantile %v, got %v", i, metric, m.Quantiles[i].Quantile, quantile.Quantile)
|
|
|
|
}
|
|
|
|
if quantile.Error != m.Quantiles[i].Error {
|
|
|
|
t.Fatalf("%d.%q: Expected Error margin %v, got %v", i, metric, m.Quantiles[i].Error, quantile.Error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-07-27 20:58:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-01-02 22:21:50 +00:00
|
|
|
|
|
|
|
func TestAction(t *testing.T) {
|
|
|
|
scenarios := []struct {
|
|
|
|
config string
|
|
|
|
configBad bool
|
2018-08-10 12:28:38 +00:00
|
|
|
expectedAction ActionType
|
2018-01-02 22:21:50 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
// no action set
|
|
|
|
config: `---
|
|
|
|
mappings:
|
|
|
|
- match: test.*.*
|
|
|
|
name: "foo"
|
|
|
|
`,
|
|
|
|
configBad: false,
|
2018-08-10 12:28:38 +00:00
|
|
|
expectedAction: ActionTypeMap,
|
2018-01-02 22:21:50 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
// map action set
|
|
|
|
config: `---
|
|
|
|
mappings:
|
|
|
|
- match: test.*.*
|
|
|
|
name: "foo"
|
|
|
|
action: map
|
|
|
|
`,
|
|
|
|
configBad: false,
|
2018-08-10 12:28:38 +00:00
|
|
|
expectedAction: ActionTypeMap,
|
2018-01-02 22:21:50 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
// drop action set
|
|
|
|
config: `---
|
|
|
|
mappings:
|
|
|
|
- match: test.*.*
|
|
|
|
name: "foo"
|
|
|
|
action: drop
|
|
|
|
`,
|
|
|
|
configBad: false,
|
2018-08-10 12:28:38 +00:00
|
|
|
expectedAction: ActionTypeDrop,
|
2018-01-02 22:21:50 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
// invalid action set
|
|
|
|
config: `---
|
|
|
|
mappings:
|
|
|
|
- match: test.*.*
|
|
|
|
name: "foo"
|
|
|
|
action: xyz
|
|
|
|
`,
|
|
|
|
configBad: true,
|
2018-08-10 12:28:38 +00:00
|
|
|
expectedAction: ActionTypeDrop,
|
2018-01-02 22:21:50 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, scenario := range scenarios {
|
2018-08-10 12:28:38 +00:00
|
|
|
mapper := MetricMapper{}
|
2019-04-03 15:36:34 +00:00
|
|
|
err := mapper.InitFromYAMLString(scenario.config, 0)
|
2018-01-02 22:21:50 +00:00
|
|
|
if err != nil && !scenario.configBad {
|
|
|
|
t.Fatalf("%d. Config load error: %s %s", i, scenario.config, err)
|
|
|
|
}
|
|
|
|
if err == nil && scenario.configBad {
|
|
|
|
t.Fatalf("%d. Expected bad config, but loaded ok: %s", i, scenario.config)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !scenario.configBad {
|
|
|
|
a := mapper.Mappings[0].Action
|
|
|
|
if scenario.expectedAction != a {
|
|
|
|
t.Fatalf("%d: Expected action %v, got %v", i, scenario.expectedAction, a)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|