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
|
|
|
|
}{
|
fix prometheus output
if i understand the prometheus data model correctly, the current output
for this plugin is unusable
prometheus only accepts a single value per measurement. prior to this change, the range loop
causes a measurement to end up w/ a random value
for instance:
net,dc=sjc1,grp_dashboard=1,grp_home=1,grp_hwy_fetcher=1,grp_web_admin=1,host=sjc1-b4-8,hw=app,interface=docker0,state=live
bytes_recv=477596i,bytes_sent=152963303i,drop_in=0i,drop_out=0i,err_in=0i,err_out=0i,packets_recv=7231i,packets_sent=11460i
1457121990003778992
this 'net' measurent would have all it's tags copied to prometheus
labels, but any of 152963303, or 0, or 7231 as a value for
'net' depending on which field is last in the map iteration
this change expands the fields into new measurements by appending
the field name to the influxdb measurement name.
ie, the above example results with 'net' dropped and new measurements
to take it's place:
net_bytes_recv
net_bytes_sent
net_drop_in
net_err_in
net_packets_recv
net_packets_sent
i hope this can be merged, i love telegraf's composability of tags and
filtering
2016-03-04 20:05:10 +00:00
|
|
|
{"test_point_1_value", 0.0, tags},
|
|
|
|
{"test_point_2_value", 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
|
|
|
|
}{
|
fix prometheus output
if i understand the prometheus data model correctly, the current output
for this plugin is unusable
prometheus only accepts a single value per measurement. prior to this change, the range loop
causes a measurement to end up w/ a random value
for instance:
net,dc=sjc1,grp_dashboard=1,grp_home=1,grp_hwy_fetcher=1,grp_web_admin=1,host=sjc1-b4-8,hw=app,interface=docker0,state=live
bytes_recv=477596i,bytes_sent=152963303i,drop_in=0i,drop_out=0i,err_in=0i,err_out=0i,packets_recv=7231i,packets_sent=11460i
1457121990003778992
this 'net' measurent would have all it's tags copied to prometheus
labels, but any of 152963303, or 0, or 7231 as a value for
'net' depending on which field is last in the map iteration
this change expands the fields into new measurements by appending
the field name to the influxdb measurement name.
ie, the above example results with 'net' dropped and new measurements
to take it's place:
net_bytes_recv
net_bytes_sent
net_drop_in
net_err_in
net_packets_recv
net_packets_sent
i hope this can be merged, i love telegraf's composability of tags and
filtering
2016-03-04 20:05:10 +00:00
|
|
|
{"test_point_3_value", 0.0},
|
|
|
|
{"test_point_4_value", 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
|
|
|
}
|
|
|
|
}
|