2015-10-22 16:17:57 +00:00
|
|
|
package prometheus_client
|
|
|
|
|
|
|
|
import (
|
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-07 17:52:46 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2016-01-27 23:15:14 +00:00
|
|
|
"github.com/influxdata/telegraf"
|
2016-01-20 18:57:35 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins/inputs/prometheus"
|
|
|
|
"github.com/influxdata/telegraf/testutil"
|
2015-10-22 16:17:57 +00:00
|
|
|
)
|
|
|
|
|
2015-10-28 22:19:13 +00:00
|
|
|
var pTesting *PrometheusClient
|
2015-10-22 16:17:57 +00:00
|
|
|
|
|
|
|
func TestPrometheusWritePointEmptyTag(t *testing.T) {
|
2015-12-01 16:45:25 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("Skipping integration test in short mode")
|
|
|
|
}
|
2016-01-29 21:58:38 +00:00
|
|
|
pTesting = &PrometheusClient{Listen: "localhost:9127"}
|
2016-02-03 19:59:34 +00:00
|
|
|
err := pTesting.Start()
|
2016-02-13 18:50:43 +00:00
|
|
|
time.Sleep(time.Millisecond * 200)
|
2016-02-03 19:59:34 +00:00
|
|
|
require.NoError(t, err)
|
2016-01-29 21:58:38 +00:00
|
|
|
defer pTesting.Stop()
|
2015-10-28 22:19:13 +00:00
|
|
|
|
2015-10-22 16:17:57 +00:00
|
|
|
p := &prometheus.Prometheus{
|
2016-01-29 21:58:38 +00:00
|
|
|
Urls: []string{"http://localhost:9127/metrics"},
|
2015-10-22 16:17:57 +00:00
|
|
|
}
|
|
|
|
tags := make(map[string]string)
|
2016-01-27 23:15:14 +00:00
|
|
|
pt1, _ := telegraf.NewMetric(
|
2015-11-11 00:05:28 +00:00
|
|
|
"test_point_1",
|
|
|
|
tags,
|
|
|
|
map[string]interface{}{"value": 0.0})
|
2016-01-27 23:15:14 +00:00
|
|
|
pt2, _ := telegraf.NewMetric(
|
2015-11-11 00:05:28 +00:00
|
|
|
"test_point_2",
|
|
|
|
tags,
|
|
|
|
map[string]interface{}{"value": 1.0})
|
2016-01-27 23:15:14 +00:00
|
|
|
var metrics = []telegraf.Metric{
|
2015-11-11 00:05:28 +00:00
|
|
|
pt1,
|
|
|
|
pt2,
|
2015-10-22 16:17:57 +00:00
|
|
|
}
|
2016-01-27 23:15:14 +00:00
|
|
|
require.NoError(t, pTesting.Write(metrics))
|
2015-10-22 16:17:57 +00:00
|
|
|
|
|
|
|
expected := []struct {
|
|
|
|
name string
|
|
|
|
value float64
|
|
|
|
tags map[string]string
|
|
|
|
}{
|
2016-03-08 18:33:57 +00:00
|
|
|
{"test_point_1", 0.0, tags},
|
|
|
|
{"test_point_2", 1.0, tags},
|
2015-10-22 16:17:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
|
|
|
|
require.NoError(t, p.Gather(&acc))
|
|
|
|
for _, e := range expected {
|
2016-01-07 17:52:46 +00:00
|
|
|
acc.AssertContainsFields(t, "prometheus_"+e.name,
|
|
|
|
map[string]interface{}{"value": e.value})
|
2015-10-22 16:17:57 +00:00
|
|
|
}
|
2015-10-28 22:19:13 +00:00
|
|
|
|
2016-01-29 21:58:38 +00:00
|
|
|
tags = make(map[string]string)
|
2015-10-22 16:17:57 +00:00
|
|
|
tags["testtag"] = "testvalue"
|
2016-01-29 21:58:38 +00:00
|
|
|
pt3, _ := telegraf.NewMetric(
|
2015-11-11 00:05:28 +00:00
|
|
|
"test_point_3",
|
|
|
|
tags,
|
|
|
|
map[string]interface{}{"value": 0.0})
|
2016-01-29 21:58:38 +00:00
|
|
|
pt4, _ := telegraf.NewMetric(
|
2015-11-11 00:05:28 +00:00
|
|
|
"test_point_4",
|
|
|
|
tags,
|
|
|
|
map[string]interface{}{"value": 1.0})
|
2016-01-29 21:58:38 +00:00
|
|
|
metrics = []telegraf.Metric{
|
|
|
|
pt3,
|
|
|
|
pt4,
|
2015-10-22 16:17:57 +00:00
|
|
|
}
|
2016-01-27 23:15:14 +00:00
|
|
|
require.NoError(t, pTesting.Write(metrics))
|
2015-10-22 16:17:57 +00:00
|
|
|
|
2016-01-29 21:58:38 +00:00
|
|
|
expected2 := []struct {
|
2015-10-22 16:17:57 +00:00
|
|
|
name string
|
|
|
|
value float64
|
|
|
|
}{
|
2016-03-08 18:33:57 +00:00
|
|
|
{"test_point_3", 0.0},
|
|
|
|
{"test_point_4", 1.0},
|
2015-10-22 16:17:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
require.NoError(t, p.Gather(&acc))
|
2016-01-29 21:58:38 +00:00
|
|
|
for _, e := range expected2 {
|
2016-01-07 17:52:46 +00:00
|
|
|
acc.AssertContainsFields(t, "prometheus_"+e.name,
|
|
|
|
map[string]interface{}{"value": e.value})
|
2015-10-22 16:17:57 +00:00
|
|
|
}
|
|
|
|
}
|