2019-11-26 23:46:31 +00:00
|
|
|
package prometheus
|
2015-10-22 16:17:57 +00:00
|
|
|
|
|
|
|
import (
|
2019-11-26 23:46:31 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
2015-10-28 22:19:13 +00:00
|
|
|
"testing"
|
2016-02-13 18:50:43 +00:00
|
|
|
"time"
|
2015-10-28 22:19:13 +00:00
|
|
|
|
2016-01-27 23:15:14 +00:00
|
|
|
"github.com/influxdata/telegraf"
|
2016-01-20 18:57:35 +00:00
|
|
|
"github.com/influxdata/telegraf/testutil"
|
2017-06-14 01:04:26 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2015-10-22 16:17:57 +00:00
|
|
|
)
|
|
|
|
|
2019-11-26 23:46:31 +00:00
|
|
|
func TestMetricVersion1(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
output *PrometheusClient
|
|
|
|
metrics []telegraf.Metric
|
|
|
|
expected []byte
|
2017-10-18 21:51:08 +00:00
|
|
|
}{
|
|
|
|
{
|
2019-11-26 23:46:31 +00:00
|
|
|
name: "simple",
|
|
|
|
output: &PrometheusClient{
|
|
|
|
Listen: ":0",
|
|
|
|
MetricVersion: 1,
|
|
|
|
CollectorsExclude: []string{"gocollector", "process"},
|
|
|
|
Path: "/metrics",
|
|
|
|
Log: testutil.Logger{},
|
2017-10-18 21:51:08 +00:00
|
|
|
},
|
2019-11-26 23:46:31 +00:00
|
|
|
metrics: []telegraf.Metric{
|
|
|
|
testutil.MustMetric(
|
|
|
|
"cpu",
|
|
|
|
map[string]string{
|
|
|
|
"host": "example.org",
|
|
|
|
},
|
|
|
|
map[string]interface{}{
|
|
|
|
"time_idle": 42.0,
|
|
|
|
},
|
|
|
|
time.Unix(0, 0),
|
|
|
|
),
|
2017-10-18 21:51:08 +00:00
|
|
|
},
|
2019-11-26 23:46:31 +00:00
|
|
|
expected: []byte(`
|
|
|
|
# HELP cpu_time_idle Telegraf collected metric
|
|
|
|
# TYPE cpu_time_idle untyped
|
|
|
|
cpu_time_idle{host="example.org"} 42
|
|
|
|
`),
|
2017-10-18 21:51:08 +00:00
|
|
|
},
|
|
|
|
{
|
2019-11-26 23:46:31 +00:00
|
|
|
name: "prometheus untyped",
|
|
|
|
output: &PrometheusClient{
|
|
|
|
Listen: ":0",
|
|
|
|
MetricVersion: 1,
|
|
|
|
CollectorsExclude: []string{"gocollector", "process"},
|
|
|
|
Path: "/metrics",
|
|
|
|
Log: testutil.Logger{},
|
2017-10-18 21:51:08 +00:00
|
|
|
},
|
2019-11-26 23:46:31 +00:00
|
|
|
metrics: []telegraf.Metric{
|
|
|
|
testutil.MustMetric(
|
|
|
|
"cpu_time_idle",
|
|
|
|
map[string]string{
|
|
|
|
"host": "example.org",
|
|
|
|
},
|
|
|
|
map[string]interface{}{
|
|
|
|
"value": 42.0,
|
|
|
|
},
|
|
|
|
time.Unix(0, 0),
|
|
|
|
),
|
2018-04-05 23:38:41 +00:00
|
|
|
},
|
2019-11-26 23:46:31 +00:00
|
|
|
expected: []byte(`
|
|
|
|
# HELP cpu_time_idle Telegraf collected metric
|
|
|
|
# TYPE cpu_time_idle untyped
|
|
|
|
cpu_time_idle{host="example.org"} 42
|
|
|
|
`),
|
2018-04-05 23:38:41 +00:00
|
|
|
},
|
2017-10-24 23:28:52 +00:00
|
|
|
{
|
2019-11-26 23:46:31 +00:00
|
|
|
name: "prometheus counter",
|
|
|
|
output: &PrometheusClient{
|
|
|
|
Listen: ":0",
|
|
|
|
MetricVersion: 1,
|
|
|
|
CollectorsExclude: []string{"gocollector", "process"},
|
|
|
|
Path: "/metrics",
|
|
|
|
Log: testutil.Logger{},
|
|
|
|
},
|
|
|
|
metrics: []telegraf.Metric{
|
|
|
|
testutil.MustMetric(
|
|
|
|
"cpu_time_idle",
|
|
|
|
map[string]string{
|
|
|
|
"host": "example.org",
|
|
|
|
},
|
|
|
|
map[string]interface{}{
|
|
|
|
"counter": 42.0,
|
|
|
|
},
|
|
|
|
time.Unix(0, 0),
|
|
|
|
telegraf.Counter,
|
|
|
|
),
|
2017-10-24 23:28:52 +00:00
|
|
|
},
|
2019-11-26 23:46:31 +00:00
|
|
|
expected: []byte(`
|
|
|
|
# HELP cpu_time_idle Telegraf collected metric
|
|
|
|
# TYPE cpu_time_idle counter
|
|
|
|
cpu_time_idle{host="example.org"} 42
|
|
|
|
`),
|
2017-10-24 23:28:52 +00:00
|
|
|
},
|
|
|
|
{
|
2019-11-26 23:46:31 +00:00
|
|
|
name: "prometheus gauge",
|
|
|
|
output: &PrometheusClient{
|
|
|
|
Listen: ":0",
|
|
|
|
MetricVersion: 1,
|
|
|
|
CollectorsExclude: []string{"gocollector", "process"},
|
|
|
|
Path: "/metrics",
|
|
|
|
Log: testutil.Logger{},
|
2017-10-24 23:28:52 +00:00
|
|
|
},
|
2019-11-26 23:46:31 +00:00
|
|
|
metrics: []telegraf.Metric{
|
|
|
|
testutil.MustMetric(
|
|
|
|
"cpu_time_idle",
|
|
|
|
map[string]string{
|
|
|
|
"host": "example.org",
|
|
|
|
},
|
|
|
|
map[string]interface{}{
|
|
|
|
"gauge": 42.0,
|
|
|
|
},
|
|
|
|
time.Unix(0, 0),
|
|
|
|
telegraf.Gauge,
|
|
|
|
),
|
|
|
|
},
|
|
|
|
expected: []byte(`
|
|
|
|
# HELP cpu_time_idle Telegraf collected metric
|
|
|
|
# TYPE cpu_time_idle gauge
|
|
|
|
cpu_time_idle{host="example.org"} 42
|
|
|
|
`),
|
2017-10-24 23:28:52 +00:00
|
|
|
},
|
|
|
|
{
|
2019-11-26 23:46:31 +00:00
|
|
|
name: "prometheus histogram",
|
|
|
|
output: &PrometheusClient{
|
|
|
|
Listen: ":0",
|
|
|
|
MetricVersion: 1,
|
|
|
|
CollectorsExclude: []string{"gocollector", "process"},
|
|
|
|
Path: "/metrics",
|
|
|
|
Log: testutil.Logger{},
|
|
|
|
},
|
|
|
|
metrics: []telegraf.Metric{
|
|
|
|
testutil.MustMetric(
|
|
|
|
"http_request_duration_seconds",
|
|
|
|
map[string]string{},
|
|
|
|
map[string]interface{}{
|
|
|
|
"sum": 53423,
|
|
|
|
"0.05": 24054,
|
|
|
|
"0.1": 33444,
|
|
|
|
"0.2": 100392,
|
|
|
|
"0.5": 129389,
|
|
|
|
"1": 133988,
|
|
|
|
"+Inf": 144320,
|
|
|
|
"count": 144320,
|
|
|
|
},
|
|
|
|
time.Unix(0, 0),
|
|
|
|
telegraf.Histogram,
|
|
|
|
),
|
2017-10-24 23:28:52 +00:00
|
|
|
},
|
2019-11-26 23:46:31 +00:00
|
|
|
expected: []byte(`
|
|
|
|
# HELP http_request_duration_seconds Telegraf collected metric
|
|
|
|
# TYPE http_request_duration_seconds histogram
|
|
|
|
http_request_duration_seconds_bucket{le="0.05"} 24054
|
|
|
|
http_request_duration_seconds_bucket{le="0.1"} 33444
|
|
|
|
http_request_duration_seconds_bucket{le="0.2"} 100392
|
|
|
|
http_request_duration_seconds_bucket{le="0.5"} 129389
|
|
|
|
http_request_duration_seconds_bucket{le="1"} 133988
|
|
|
|
http_request_duration_seconds_bucket{le="+Inf"} 144320
|
|
|
|
http_request_duration_seconds_sum 53423
|
|
|
|
http_request_duration_seconds_count 144320
|
|
|
|
`),
|
2017-10-24 23:28:52 +00:00
|
|
|
},
|
2018-04-05 23:38:41 +00:00
|
|
|
{
|
2019-11-26 23:46:31 +00:00
|
|
|
name: "prometheus summary",
|
|
|
|
output: &PrometheusClient{
|
|
|
|
Listen: ":0",
|
|
|
|
MetricVersion: 1,
|
|
|
|
CollectorsExclude: []string{"gocollector", "process"},
|
|
|
|
Path: "/metrics",
|
|
|
|
Log: testutil.Logger{},
|
2018-04-05 23:38:41 +00:00
|
|
|
},
|
2019-11-26 23:46:31 +00:00
|
|
|
metrics: []telegraf.Metric{
|
|
|
|
testutil.MustMetric(
|
|
|
|
"rpc_duration_seconds",
|
|
|
|
map[string]string{},
|
|
|
|
map[string]interface{}{
|
|
|
|
"0.01": 3102,
|
|
|
|
"0.05": 3272,
|
|
|
|
"0.5": 4773,
|
|
|
|
"0.9": 9001,
|
|
|
|
"0.99": 76656,
|
|
|
|
"count": 2693,
|
|
|
|
"sum": 17560473,
|
|
|
|
},
|
|
|
|
time.Unix(0, 0),
|
|
|
|
telegraf.Summary,
|
|
|
|
),
|
|
|
|
},
|
|
|
|
expected: []byte(`
|
|
|
|
# HELP rpc_duration_seconds Telegraf collected metric
|
|
|
|
# TYPE rpc_duration_seconds summary
|
|
|
|
rpc_duration_seconds{quantile="0.01"} 3102
|
|
|
|
rpc_duration_seconds{quantile="0.05"} 3272
|
|
|
|
rpc_duration_seconds{quantile="0.5"} 4773
|
|
|
|
rpc_duration_seconds{quantile="0.9"} 9001
|
|
|
|
rpc_duration_seconds{quantile="0.99"} 76656
|
|
|
|
rpc_duration_seconds_sum 1.7560473e+07
|
|
|
|
rpc_duration_seconds_count 2693
|
|
|
|
`),
|
2018-04-05 23:38:41 +00:00
|
|
|
},
|
2017-10-24 23:28:52 +00:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2019-11-26 23:46:31 +00:00
|
|
|
err := tt.output.Init()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = tt.output.Connect()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
err := tt.output.Close()
|
|
|
|
require.NoError(t, err)
|
|
|
|
}()
|
|
|
|
|
|
|
|
err = tt.output.Write(tt.metrics)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
resp, err := http.Get(tt.output.URL())
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, http.StatusOK, resp.StatusCode)
|
|
|
|
defer resp.Body.Close()
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t,
|
|
|
|
strings.TrimSpace(string(tt.expected)),
|
|
|
|
strings.TrimSpace(string(body)))
|
2017-10-24 23:28:52 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-26 23:46:31 +00:00
|
|
|
func TestMetricVersion2(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
output *PrometheusClient
|
|
|
|
metrics []telegraf.Metric
|
|
|
|
expected []byte
|
2015-10-22 16:17:57 +00:00
|
|
|
}{
|
2019-11-26 23:46:31 +00:00
|
|
|
{
|
|
|
|
name: "simple",
|
|
|
|
output: &PrometheusClient{
|
|
|
|
Listen: ":0",
|
|
|
|
MetricVersion: 2,
|
|
|
|
CollectorsExclude: []string{"gocollector", "process"},
|
|
|
|
Path: "/metrics",
|
|
|
|
Log: testutil.Logger{},
|
|
|
|
},
|
|
|
|
metrics: []telegraf.Metric{
|
|
|
|
testutil.MustMetric(
|
|
|
|
"cpu",
|
|
|
|
map[string]string{
|
|
|
|
"host": "example.org",
|
|
|
|
},
|
|
|
|
map[string]interface{}{
|
|
|
|
"time_idle": 42.0,
|
|
|
|
},
|
|
|
|
time.Unix(0, 0),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
expected: []byte(`
|
|
|
|
# HELP cpu_time_idle Telegraf collected metric
|
|
|
|
# TYPE cpu_time_idle untyped
|
|
|
|
cpu_time_idle{host="example.org"} 42
|
|
|
|
`),
|
|
|
|
},
|
2016-11-15 11:33:39 +00:00
|
|
|
}
|
2019-11-26 23:46:31 +00:00
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
err := tt.output.Init()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = tt.output.Connect()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
err := tt.output.Close()
|
|
|
|
require.NoError(t, err)
|
|
|
|
}()
|
|
|
|
|
|
|
|
err = tt.output.Write(tt.metrics)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
resp, err := http.Get(tt.output.URL())
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, http.StatusOK, resp.StatusCode)
|
|
|
|
defer resp.Body.Close()
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t,
|
|
|
|
strings.TrimSpace(string(tt.expected)),
|
|
|
|
strings.TrimSpace(string(body)))
|
|
|
|
})
|
2016-11-15 11:33:39 +00:00
|
|
|
}
|
|
|
|
}
|