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-11-15 11:33:39 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2016-01-07 17:52:46 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2016-12-23 15:18:27 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins"
|
2016-11-15 11:33:39 +00:00
|
|
|
"github.com/influxdata/telegraf/internal"
|
2016-11-22 12:51:57 +00:00
|
|
|
"github.com/influxdata/telegraf/metric"
|
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-11-15 11:33:39 +00:00
|
|
|
|
|
|
|
pClient, p, err := setupPrometheus()
|
2016-02-03 19:59:34 +00:00
|
|
|
require.NoError(t, err)
|
2016-11-15 11:33:39 +00:00
|
|
|
defer pClient.Stop()
|
2015-10-28 22:19:13 +00:00
|
|
|
|
2016-11-15 11:33:39 +00:00
|
|
|
now := time.Now()
|
2015-10-22 16:17:57 +00:00
|
|
|
tags := make(map[string]string)
|
2016-11-22 12:51:57 +00:00
|
|
|
pt1, _ := metric.New(
|
2015-11-11 00:05:28 +00:00
|
|
|
"test_point_1",
|
|
|
|
tags,
|
2016-06-13 14:21:11 +00:00
|
|
|
map[string]interface{}{"value": 0.0},
|
|
|
|
now)
|
2016-11-22 12:51:57 +00:00
|
|
|
pt2, _ := metric.New(
|
2015-11-11 00:05:28 +00:00
|
|
|
"test_point_2",
|
|
|
|
tags,
|
2016-06-13 14:21:11 +00:00
|
|
|
map[string]interface{}{"value": 1.0},
|
|
|
|
now)
|
2016-12-23 15:18:27 +00:00
|
|
|
var metrics = []plugins.Metric{
|
2015-11-11 00:05:28 +00:00
|
|
|
pt1,
|
|
|
|
pt2,
|
2015-10-22 16:17:57 +00:00
|
|
|
}
|
2016-11-15 11:33:39 +00:00
|
|
|
require.NoError(t, pClient.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-03-01 16:12:23 +00:00
|
|
|
acc.AssertContainsFields(t, e.name,
|
2016-01-07 17:52:46 +00:00
|
|
|
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-11-22 12:51:57 +00:00
|
|
|
pt3, _ := metric.New(
|
2015-11-11 00:05:28 +00:00
|
|
|
"test_point_3",
|
|
|
|
tags,
|
2016-06-13 14:21:11 +00:00
|
|
|
map[string]interface{}{"value": 0.0},
|
|
|
|
now)
|
2016-11-22 12:51:57 +00:00
|
|
|
pt4, _ := metric.New(
|
2015-11-11 00:05:28 +00:00
|
|
|
"test_point_4",
|
|
|
|
tags,
|
2016-06-13 14:21:11 +00:00
|
|
|
map[string]interface{}{"value": 1.0},
|
|
|
|
now)
|
2016-12-23 15:18:27 +00:00
|
|
|
metrics = []plugins.Metric{
|
2016-01-29 21:58:38 +00:00
|
|
|
pt3,
|
|
|
|
pt4,
|
2015-10-22 16:17:57 +00:00
|
|
|
}
|
2016-11-15 11:33:39 +00:00
|
|
|
require.NoError(t, pClient.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-03-01 16:12:23 +00:00
|
|
|
acc.AssertContainsFields(t, e.name,
|
2016-01-07 17:52:46 +00:00
|
|
|
map[string]interface{}{"value": e.value})
|
2015-10-22 16:17:57 +00:00
|
|
|
}
|
|
|
|
}
|
2016-11-15 11:33:39 +00:00
|
|
|
|
|
|
|
func TestPrometheusExpireOldMetrics(t *testing.T) {
|
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("Skipping integration test in short mode")
|
|
|
|
}
|
|
|
|
|
|
|
|
pClient, p, err := setupPrometheus()
|
|
|
|
pClient.ExpirationInterval = internal.Duration{Duration: time.Second * 10}
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer pClient.Stop()
|
|
|
|
|
|
|
|
now := time.Now()
|
|
|
|
tags := make(map[string]string)
|
2016-11-22 12:51:57 +00:00
|
|
|
pt1, _ := metric.New(
|
2016-11-15 11:33:39 +00:00
|
|
|
"test_point_1",
|
|
|
|
tags,
|
|
|
|
map[string]interface{}{"value": 0.0},
|
|
|
|
now)
|
2016-12-23 15:18:27 +00:00
|
|
|
var metrics = []plugins.Metric{pt1}
|
2016-11-15 11:33:39 +00:00
|
|
|
require.NoError(t, pClient.Write(metrics))
|
|
|
|
|
|
|
|
for _, m := range pClient.metrics {
|
|
|
|
m.Expiration = now.Add(time.Duration(-15) * time.Second)
|
|
|
|
}
|
|
|
|
|
2016-11-22 12:51:57 +00:00
|
|
|
pt2, _ := metric.New(
|
2016-11-15 11:33:39 +00:00
|
|
|
"test_point_2",
|
|
|
|
tags,
|
|
|
|
map[string]interface{}{"value": 1.0},
|
|
|
|
now)
|
2016-12-23 15:18:27 +00:00
|
|
|
var metrics2 = []plugins.Metric{pt2}
|
2016-11-15 11:33:39 +00:00
|
|
|
require.NoError(t, pClient.Write(metrics2))
|
|
|
|
|
|
|
|
expected := []struct {
|
|
|
|
name string
|
|
|
|
value float64
|
|
|
|
tags map[string]string
|
|
|
|
}{
|
|
|
|
{"test_point_2", 1.0, tags},
|
|
|
|
}
|
|
|
|
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
|
|
|
|
require.NoError(t, p.Gather(&acc))
|
|
|
|
for _, e := range expected {
|
|
|
|
acc.AssertContainsFields(t, e.name,
|
|
|
|
map[string]interface{}{"value": e.value})
|
|
|
|
}
|
|
|
|
|
|
|
|
acc.AssertDoesNotContainMeasurement(t, "test_point_1")
|
|
|
|
|
|
|
|
// Confirm that it's not in the PrometheusClient map anymore
|
|
|
|
assert.Equal(t, 1, len(pClient.metrics))
|
|
|
|
}
|
|
|
|
|
|
|
|
func setupPrometheus() (*PrometheusClient, *prometheus.Prometheus, error) {
|
|
|
|
if pTesting == nil {
|
|
|
|
pTesting = &PrometheusClient{Listen: "localhost:9127"}
|
|
|
|
err := pTesting.Start()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
pTesting.metrics = make(map[string]*MetricWithExpiration)
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(time.Millisecond * 200)
|
|
|
|
|
|
|
|
p := &prometheus.Prometheus{
|
|
|
|
Urls: []string{"http://localhost:9127/metrics"},
|
|
|
|
}
|
|
|
|
|
|
|
|
return pTesting, p, nil
|
|
|
|
}
|