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-27 23:15:14 +00:00
|
|
|
"github.com/influxdata/telegraf"
|
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"
|
2017-06-14 01:04:26 +00:00
|
|
|
prometheus_input "github.com/influxdata/telegraf/plugins/inputs/prometheus"
|
2016-01-20 18:57:35 +00:00
|
|
|
"github.com/influxdata/telegraf/testutil"
|
2017-06-14 01:04:26 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
"github.com/stretchr/testify/require"
|
2015-10-22 16:17:57 +00:00
|
|
|
)
|
|
|
|
|
2017-06-14 01:04:26 +00:00
|
|
|
func setUnixTime(client *PrometheusClient, sec int64) {
|
|
|
|
client.now = func() time.Time {
|
|
|
|
return time.Unix(sec, 0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewClient initializes a PrometheusClient.
|
|
|
|
func NewClient() *PrometheusClient {
|
|
|
|
return &PrometheusClient{
|
|
|
|
ExpirationInterval: internal.Duration{Duration: time.Second * 60},
|
|
|
|
fam: make(map[string]*MetricFamily),
|
|
|
|
now: time.Now,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWrite_Basic(t *testing.T) {
|
|
|
|
now := time.Now()
|
|
|
|
pt1, err := metric.New(
|
|
|
|
"foo",
|
|
|
|
make(map[string]string),
|
|
|
|
map[string]interface{}{"value": 0.0},
|
|
|
|
now)
|
|
|
|
var metrics = []telegraf.Metric{
|
|
|
|
pt1,
|
|
|
|
}
|
|
|
|
|
|
|
|
client := NewClient()
|
|
|
|
err = client.Write(metrics)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
fam, ok := client.fam["foo"]
|
|
|
|
require.True(t, ok)
|
|
|
|
require.Equal(t, prometheus.UntypedValue, fam.ValueType)
|
|
|
|
require.Equal(t, map[string]int{}, fam.LabelSet)
|
|
|
|
|
|
|
|
sample, ok := fam.Samples[CreateSampleID(pt1.Tags())]
|
|
|
|
require.True(t, ok)
|
|
|
|
|
|
|
|
require.Equal(t, 0.0, sample.Value)
|
|
|
|
require.True(t, now.Before(sample.Expiration))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWrite_IntField(t *testing.T) {
|
|
|
|
client := NewClient()
|
|
|
|
|
|
|
|
p1, err := metric.New(
|
|
|
|
"foo",
|
|
|
|
make(map[string]string),
|
|
|
|
map[string]interface{}{"value": 42},
|
|
|
|
time.Now())
|
|
|
|
err = client.Write([]telegraf.Metric{p1})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
fam, ok := client.fam["foo"]
|
|
|
|
require.True(t, ok)
|
|
|
|
for _, v := range fam.Samples {
|
|
|
|
require.Equal(t, 42.0, v.Value)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWrite_FieldNotValue(t *testing.T) {
|
|
|
|
client := NewClient()
|
|
|
|
|
|
|
|
p1, err := metric.New(
|
|
|
|
"foo",
|
|
|
|
make(map[string]string),
|
|
|
|
map[string]interface{}{"howdy": 0.0},
|
|
|
|
time.Now())
|
|
|
|
err = client.Write([]telegraf.Metric{p1})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
fam, ok := client.fam["foo_howdy"]
|
|
|
|
require.True(t, ok)
|
|
|
|
for _, v := range fam.Samples {
|
|
|
|
require.Equal(t, 0.0, v.Value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWrite_SkipNonNumberField(t *testing.T) {
|
|
|
|
client := NewClient()
|
|
|
|
|
|
|
|
p1, err := metric.New(
|
|
|
|
"foo",
|
|
|
|
make(map[string]string),
|
|
|
|
map[string]interface{}{"value": "howdy"},
|
|
|
|
time.Now())
|
|
|
|
err = client.Write([]telegraf.Metric{p1})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
_, ok := client.fam["foo"]
|
|
|
|
require.False(t, ok)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWrite_Counter(t *testing.T) {
|
|
|
|
client := NewClient()
|
|
|
|
|
|
|
|
p1, err := metric.New(
|
|
|
|
"foo",
|
|
|
|
make(map[string]string),
|
|
|
|
map[string]interface{}{"value": 42},
|
|
|
|
time.Now(),
|
|
|
|
telegraf.Counter)
|
|
|
|
err = client.Write([]telegraf.Metric{p1})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
fam, ok := client.fam["foo"]
|
|
|
|
require.True(t, ok)
|
|
|
|
require.Equal(t, prometheus.CounterValue, fam.ValueType)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWrite_Sanitize(t *testing.T) {
|
|
|
|
client := NewClient()
|
|
|
|
|
|
|
|
p1, err := metric.New(
|
|
|
|
"foo.bar",
|
|
|
|
map[string]string{"tag-with-dash": "localhost.local"},
|
|
|
|
map[string]interface{}{"field-with-dash": 42},
|
|
|
|
time.Now(),
|
|
|
|
telegraf.Counter)
|
|
|
|
err = client.Write([]telegraf.Metric{p1})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
fam, ok := client.fam["foo_bar_field_with_dash"]
|
|
|
|
require.True(t, ok)
|
|
|
|
require.Equal(t, map[string]int{"tag_with_dash": 1}, fam.LabelSet)
|
|
|
|
|
|
|
|
sample1, ok := fam.Samples[CreateSampleID(p1.Tags())]
|
|
|
|
require.True(t, ok)
|
|
|
|
|
|
|
|
require.Equal(t, map[string]string{
|
2017-06-21 19:36:29 +00:00
|
|
|
"tag_with_dash": "localhost.local"}, sample1.Labels)
|
2017-06-14 01:04:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestWrite_Gauge(t *testing.T) {
|
|
|
|
client := NewClient()
|
|
|
|
|
|
|
|
p1, err := metric.New(
|
|
|
|
"foo",
|
|
|
|
make(map[string]string),
|
|
|
|
map[string]interface{}{"value": 42},
|
|
|
|
time.Now(),
|
|
|
|
telegraf.Gauge)
|
|
|
|
err = client.Write([]telegraf.Metric{p1})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
fam, ok := client.fam["foo"]
|
|
|
|
require.True(t, ok)
|
|
|
|
require.Equal(t, prometheus.GaugeValue, fam.ValueType)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWrite_MixedValueType(t *testing.T) {
|
|
|
|
now := time.Now()
|
|
|
|
p1, err := metric.New(
|
|
|
|
"foo",
|
|
|
|
make(map[string]string),
|
|
|
|
map[string]interface{}{"value": 1.0},
|
|
|
|
now,
|
|
|
|
telegraf.Counter)
|
|
|
|
p2, err := metric.New(
|
|
|
|
"foo",
|
|
|
|
make(map[string]string),
|
|
|
|
map[string]interface{}{"value": 2.0},
|
|
|
|
now,
|
|
|
|
telegraf.Gauge)
|
|
|
|
var metrics = []telegraf.Metric{p1, p2}
|
|
|
|
|
|
|
|
client := NewClient()
|
|
|
|
err = client.Write(metrics)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
fam, ok := client.fam["foo"]
|
|
|
|
require.True(t, ok)
|
|
|
|
require.Equal(t, 1, len(fam.Samples))
|
|
|
|
}
|
|
|
|
|
2017-08-10 17:19:28 +00:00
|
|
|
func TestWrite_MixedValueTypeUpgrade(t *testing.T) {
|
|
|
|
now := time.Now()
|
|
|
|
p1, err := metric.New(
|
|
|
|
"foo",
|
|
|
|
map[string]string{"a": "x"},
|
|
|
|
map[string]interface{}{"value": 1.0},
|
|
|
|
now,
|
|
|
|
telegraf.Untyped)
|
|
|
|
p2, err := metric.New(
|
|
|
|
"foo",
|
|
|
|
map[string]string{"a": "y"},
|
|
|
|
map[string]interface{}{"value": 2.0},
|
|
|
|
now,
|
|
|
|
telegraf.Gauge)
|
|
|
|
var metrics = []telegraf.Metric{p1, p2}
|
|
|
|
|
|
|
|
client := NewClient()
|
|
|
|
err = client.Write(metrics)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
fam, ok := client.fam["foo"]
|
|
|
|
require.True(t, ok)
|
|
|
|
require.Equal(t, 2, len(fam.Samples))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWrite_MixedValueTypeDowngrade(t *testing.T) {
|
|
|
|
now := time.Now()
|
|
|
|
p1, err := metric.New(
|
|
|
|
"foo",
|
|
|
|
map[string]string{"a": "x"},
|
|
|
|
map[string]interface{}{"value": 1.0},
|
|
|
|
now,
|
|
|
|
telegraf.Gauge)
|
|
|
|
p2, err := metric.New(
|
|
|
|
"foo",
|
|
|
|
map[string]string{"a": "y"},
|
|
|
|
map[string]interface{}{"value": 2.0},
|
|
|
|
now,
|
|
|
|
telegraf.Untyped)
|
|
|
|
var metrics = []telegraf.Metric{p1, p2}
|
|
|
|
|
|
|
|
client := NewClient()
|
|
|
|
err = client.Write(metrics)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
fam, ok := client.fam["foo"]
|
|
|
|
require.True(t, ok)
|
|
|
|
require.Equal(t, 2, len(fam.Samples))
|
|
|
|
}
|
|
|
|
|
2017-06-14 01:04:26 +00:00
|
|
|
func TestWrite_Tags(t *testing.T) {
|
|
|
|
now := time.Now()
|
|
|
|
p1, err := metric.New(
|
|
|
|
"foo",
|
|
|
|
make(map[string]string),
|
|
|
|
map[string]interface{}{"value": 1.0},
|
|
|
|
now)
|
|
|
|
p2, err := metric.New(
|
|
|
|
"foo",
|
|
|
|
map[string]string{"host": "localhost"},
|
|
|
|
map[string]interface{}{"value": 2.0},
|
|
|
|
now)
|
|
|
|
var metrics = []telegraf.Metric{p1, p2}
|
|
|
|
|
|
|
|
client := NewClient()
|
|
|
|
err = client.Write(metrics)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
fam, ok := client.fam["foo"]
|
|
|
|
require.True(t, ok)
|
|
|
|
require.Equal(t, prometheus.UntypedValue, fam.ValueType)
|
|
|
|
|
|
|
|
require.Equal(t, map[string]int{"host": 1}, fam.LabelSet)
|
|
|
|
|
|
|
|
sample1, ok := fam.Samples[CreateSampleID(p1.Tags())]
|
|
|
|
require.True(t, ok)
|
|
|
|
|
|
|
|
require.Equal(t, 1.0, sample1.Value)
|
|
|
|
require.True(t, now.Before(sample1.Expiration))
|
|
|
|
|
|
|
|
sample2, ok := fam.Samples[CreateSampleID(p2.Tags())]
|
|
|
|
require.True(t, ok)
|
|
|
|
|
|
|
|
require.Equal(t, 2.0, sample2.Value)
|
|
|
|
require.True(t, now.Before(sample2.Expiration))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestExpire(t *testing.T) {
|
|
|
|
client := NewClient()
|
|
|
|
|
|
|
|
p1, err := metric.New(
|
|
|
|
"foo",
|
|
|
|
make(map[string]string),
|
|
|
|
map[string]interface{}{"value": 1.0},
|
|
|
|
time.Now())
|
|
|
|
setUnixTime(client, 0)
|
|
|
|
err = client.Write([]telegraf.Metric{p1})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
p2, err := metric.New(
|
|
|
|
"bar",
|
|
|
|
make(map[string]string),
|
|
|
|
map[string]interface{}{"value": 2.0},
|
|
|
|
time.Now())
|
|
|
|
setUnixTime(client, 1)
|
|
|
|
err = client.Write([]telegraf.Metric{p2})
|
|
|
|
|
|
|
|
setUnixTime(client, 61)
|
|
|
|
require.Equal(t, 2, len(client.fam))
|
|
|
|
client.Expire()
|
|
|
|
require.Equal(t, 1, len(client.fam))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestExpire_TagsNoDecrement(t *testing.T) {
|
|
|
|
client := NewClient()
|
|
|
|
|
|
|
|
p1, err := metric.New(
|
|
|
|
"foo",
|
|
|
|
make(map[string]string),
|
|
|
|
map[string]interface{}{"value": 1.0},
|
|
|
|
time.Now())
|
|
|
|
setUnixTime(client, 0)
|
|
|
|
err = client.Write([]telegraf.Metric{p1})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
p2, err := metric.New(
|
|
|
|
"foo",
|
|
|
|
map[string]string{"host": "localhost"},
|
|
|
|
map[string]interface{}{"value": 2.0},
|
|
|
|
time.Now())
|
|
|
|
setUnixTime(client, 1)
|
|
|
|
err = client.Write([]telegraf.Metric{p2})
|
|
|
|
|
|
|
|
setUnixTime(client, 61)
|
|
|
|
fam, ok := client.fam["foo"]
|
|
|
|
require.True(t, ok)
|
|
|
|
require.Equal(t, 2, len(fam.Samples))
|
|
|
|
client.Expire()
|
|
|
|
require.Equal(t, 1, len(fam.Samples))
|
|
|
|
|
|
|
|
require.Equal(t, map[string]int{"host": 1}, fam.LabelSet)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestExpire_TagsWithDecrement(t *testing.T) {
|
|
|
|
client := NewClient()
|
|
|
|
|
|
|
|
p1, err := metric.New(
|
|
|
|
"foo",
|
|
|
|
map[string]string{"host": "localhost"},
|
|
|
|
map[string]interface{}{"value": 1.0},
|
|
|
|
time.Now())
|
|
|
|
setUnixTime(client, 0)
|
|
|
|
err = client.Write([]telegraf.Metric{p1})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
p2, err := metric.New(
|
|
|
|
"foo",
|
|
|
|
make(map[string]string),
|
|
|
|
map[string]interface{}{"value": 2.0},
|
|
|
|
time.Now())
|
|
|
|
setUnixTime(client, 1)
|
|
|
|
err = client.Write([]telegraf.Metric{p2})
|
|
|
|
|
|
|
|
setUnixTime(client, 61)
|
|
|
|
fam, ok := client.fam["foo"]
|
|
|
|
require.True(t, ok)
|
|
|
|
require.Equal(t, 2, len(fam.Samples))
|
|
|
|
client.Expire()
|
|
|
|
require.Equal(t, 1, len(fam.Samples))
|
|
|
|
|
|
|
|
require.Equal(t, map[string]int{"host": 0}, fam.LabelSet)
|
|
|
|
}
|
|
|
|
|
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-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-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-01-29 21:58:38 +00:00
|
|
|
metrics = []telegraf.Metric{
|
|
|
|
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
|
|
|
|
2017-06-14 01:04:26 +00:00
|
|
|
func setupPrometheus() (*PrometheusClient, *prometheus_input.Prometheus, error) {
|
2016-11-15 11:33:39 +00:00
|
|
|
if pTesting == nil {
|
2017-06-14 01:04:26 +00:00
|
|
|
pTesting = NewClient()
|
|
|
|
pTesting.Listen = "localhost:9127"
|
2017-09-19 18:27:11 +00:00
|
|
|
pTesting.Path = "/metrics"
|
2016-11-15 11:33:39 +00:00
|
|
|
err := pTesting.Start()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
} else {
|
2017-06-14 01:04:26 +00:00
|
|
|
pTesting.fam = make(map[string]*MetricFamily)
|
2016-11-15 11:33:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(time.Millisecond * 200)
|
|
|
|
|
2017-06-14 01:04:26 +00:00
|
|
|
p := &prometheus_input.Prometheus{
|
2016-11-15 11:33:39 +00:00
|
|
|
Urls: []string{"http://localhost:9127/metrics"},
|
|
|
|
}
|
|
|
|
|
|
|
|
return pTesting, p, nil
|
|
|
|
}
|