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/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},
|
2018-02-16 22:07:26 +00:00
|
|
|
StringAsLabel: true,
|
2017-06-14 01:04:26 +00:00
|
|
|
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)
|
2017-10-24 23:28:52 +00:00
|
|
|
require.Equal(t, telegraf.Untyped, fam.TelegrafValueType)
|
2017-06-14 01:04:26 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2017-10-18 21:51:08 +00:00
|
|
|
func TestWrite_Counters(t *testing.T) {
|
|
|
|
type args struct {
|
|
|
|
measurement string
|
|
|
|
tags map[string]string
|
|
|
|
fields map[string]interface{}
|
|
|
|
valueType telegraf.ValueType
|
|
|
|
}
|
|
|
|
var tests = []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
err error
|
|
|
|
metricName string
|
2017-10-24 23:28:52 +00:00
|
|
|
valueType telegraf.ValueType
|
2017-10-18 21:51:08 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "field named value is not added to metric name",
|
|
|
|
args: args{
|
|
|
|
measurement: "foo",
|
|
|
|
fields: map[string]interface{}{"value": 42},
|
|
|
|
valueType: telegraf.Counter,
|
|
|
|
},
|
|
|
|
metricName: "foo",
|
2017-10-24 23:28:52 +00:00
|
|
|
valueType: telegraf.Counter,
|
2017-10-18 21:51:08 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "field named counter is not added to metric name",
|
|
|
|
args: args{
|
|
|
|
measurement: "foo",
|
|
|
|
fields: map[string]interface{}{"counter": 42},
|
|
|
|
valueType: telegraf.Counter,
|
|
|
|
},
|
|
|
|
metricName: "foo",
|
2017-10-24 23:28:52 +00:00
|
|
|
valueType: telegraf.Counter,
|
2017-10-18 21:51:08 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "field with any other name is added to metric name",
|
|
|
|
args: args{
|
|
|
|
measurement: "foo",
|
|
|
|
fields: map[string]interface{}{"other": 42},
|
|
|
|
valueType: telegraf.Counter,
|
|
|
|
},
|
|
|
|
metricName: "foo_other",
|
2017-10-24 23:28:52 +00:00
|
|
|
valueType: telegraf.Counter,
|
2017-10-18 21:51:08 +00:00
|
|
|
},
|
2018-04-05 23:38:41 +00:00
|
|
|
{
|
|
|
|
name: "uint64 fields are output",
|
|
|
|
args: args{
|
|
|
|
measurement: "foo",
|
|
|
|
fields: map[string]interface{}{"value": uint64(42)},
|
|
|
|
valueType: telegraf.Counter,
|
|
|
|
},
|
|
|
|
metricName: "foo",
|
|
|
|
valueType: telegraf.Counter,
|
|
|
|
},
|
2017-10-18 21:51:08 +00:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
m, err := metric.New(
|
|
|
|
tt.args.measurement,
|
|
|
|
tt.args.tags,
|
|
|
|
tt.args.fields,
|
|
|
|
time.Now(),
|
|
|
|
tt.args.valueType,
|
|
|
|
)
|
|
|
|
client := NewClient()
|
|
|
|
err = client.Write([]telegraf.Metric{m})
|
|
|
|
require.Equal(t, tt.err, err)
|
|
|
|
|
|
|
|
fam, ok := client.fam[tt.metricName]
|
|
|
|
require.True(t, ok)
|
2017-10-24 23:28:52 +00:00
|
|
|
require.Equal(t, tt.valueType, fam.TelegrafValueType)
|
2017-10-18 21:51:08 +00:00
|
|
|
})
|
|
|
|
}
|
2017-06-14 01:04:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestWrite_Sanitize(t *testing.T) {
|
|
|
|
client := NewClient()
|
|
|
|
|
|
|
|
p1, err := metric.New(
|
2019-04-04 23:38:33 +00:00
|
|
|
"foo.bar:colon",
|
2017-06-14 01:04:26 +00:00
|
|
|
map[string]string{"tag-with-dash": "localhost.local"},
|
2019-04-04 23:38:33 +00:00
|
|
|
map[string]interface{}{"field-with-dash-and:colon": 42},
|
2017-06-14 01:04:26 +00:00
|
|
|
time.Now(),
|
|
|
|
telegraf.Counter)
|
|
|
|
err = client.Write([]telegraf.Metric{p1})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-04-04 23:38:33 +00:00
|
|
|
fam, ok := client.fam["foo_bar:colon_field_with_dash_and:colon"]
|
2017-06-14 01:04:26 +00:00
|
|
|
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) {
|
2017-10-24 23:28:52 +00:00
|
|
|
type args struct {
|
|
|
|
measurement string
|
|
|
|
tags map[string]string
|
|
|
|
fields map[string]interface{}
|
|
|
|
valueType telegraf.ValueType
|
|
|
|
}
|
|
|
|
var tests = []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
err error
|
|
|
|
metricName string
|
|
|
|
valueType telegraf.ValueType
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "field named value is not added to metric name",
|
|
|
|
args: args{
|
|
|
|
measurement: "foo",
|
|
|
|
fields: map[string]interface{}{"value": 42},
|
|
|
|
valueType: telegraf.Gauge,
|
|
|
|
},
|
|
|
|
metricName: "foo",
|
|
|
|
valueType: telegraf.Gauge,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "field named gauge is not added to metric name",
|
|
|
|
args: args{
|
|
|
|
measurement: "foo",
|
|
|
|
fields: map[string]interface{}{"gauge": 42},
|
|
|
|
valueType: telegraf.Gauge,
|
|
|
|
},
|
|
|
|
metricName: "foo",
|
|
|
|
valueType: telegraf.Gauge,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "field with any other name is added to metric name",
|
|
|
|
args: args{
|
|
|
|
measurement: "foo",
|
|
|
|
fields: map[string]interface{}{"other": 42},
|
|
|
|
valueType: telegraf.Gauge,
|
|
|
|
},
|
|
|
|
metricName: "foo_other",
|
|
|
|
valueType: telegraf.Gauge,
|
|
|
|
},
|
2018-04-05 23:38:41 +00:00
|
|
|
{
|
|
|
|
name: "uint64 fields are output",
|
|
|
|
args: args{
|
|
|
|
measurement: "foo",
|
|
|
|
fields: map[string]interface{}{"value": uint64(42)},
|
|
|
|
valueType: telegraf.Counter,
|
|
|
|
},
|
|
|
|
metricName: "foo",
|
|
|
|
valueType: telegraf.Counter,
|
|
|
|
},
|
2017-10-24 23:28:52 +00:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
m, err := metric.New(
|
|
|
|
tt.args.measurement,
|
|
|
|
tt.args.tags,
|
|
|
|
tt.args.fields,
|
|
|
|
time.Now(),
|
|
|
|
tt.args.valueType,
|
|
|
|
)
|
|
|
|
client := NewClient()
|
|
|
|
err = client.Write([]telegraf.Metric{m})
|
|
|
|
require.Equal(t, tt.err, err)
|
|
|
|
|
|
|
|
fam, ok := client.fam[tt.metricName]
|
|
|
|
require.True(t, ok)
|
|
|
|
require.Equal(t, tt.valueType, fam.TelegrafValueType)
|
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWrite_Summary(t *testing.T) {
|
2017-06-14 01:04:26 +00:00
|
|
|
client := NewClient()
|
|
|
|
|
|
|
|
p1, err := metric.New(
|
|
|
|
"foo",
|
|
|
|
make(map[string]string),
|
2017-10-24 23:28:52 +00:00
|
|
|
map[string]interface{}{"sum": 84, "count": 42, "0": 2, "0.5": 3, "1": 4},
|
2017-06-14 01:04:26 +00:00
|
|
|
time.Now(),
|
2017-10-24 23:28:52 +00:00
|
|
|
telegraf.Summary)
|
|
|
|
|
2017-06-14 01:04:26 +00:00
|
|
|
err = client.Write([]telegraf.Metric{p1})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
fam, ok := client.fam["foo"]
|
|
|
|
require.True(t, ok)
|
2017-10-24 23:28:52 +00:00
|
|
|
require.Equal(t, 1, len(fam.Samples))
|
|
|
|
|
|
|
|
sample1, ok := fam.Samples[CreateSampleID(p1.Tags())]
|
|
|
|
require.True(t, ok)
|
|
|
|
|
|
|
|
require.Equal(t, 84.0, sample1.Sum)
|
|
|
|
require.Equal(t, uint64(42), sample1.Count)
|
|
|
|
require.Equal(t, 3, len(sample1.SummaryValue))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWrite_Histogram(t *testing.T) {
|
|
|
|
client := NewClient()
|
|
|
|
|
|
|
|
p1, err := metric.New(
|
|
|
|
"foo",
|
|
|
|
make(map[string]string),
|
|
|
|
map[string]interface{}{"sum": 84, "count": 42, "0": 2, "0.5": 3, "1": 4},
|
|
|
|
time.Now(),
|
|
|
|
telegraf.Histogram)
|
|
|
|
|
|
|
|
err = client.Write([]telegraf.Metric{p1})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
fam, ok := client.fam["foo"]
|
|
|
|
require.True(t, ok)
|
|
|
|
require.Equal(t, 1, len(fam.Samples))
|
|
|
|
|
|
|
|
sample1, ok := fam.Samples[CreateSampleID(p1.Tags())]
|
|
|
|
require.True(t, ok)
|
|
|
|
|
|
|
|
require.Equal(t, 84.0, sample1.Sum)
|
|
|
|
require.Equal(t, uint64(42), sample1.Count)
|
|
|
|
require.Equal(t, 3, len(sample1.HistogramValue))
|
2017-06-14 01:04:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2017-10-24 23:28:52 +00:00
|
|
|
require.Equal(t, telegraf.Untyped, fam.TelegrafValueType)
|
2017-06-14 01:04:26 +00:00
|
|
|
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
2017-10-19 00:42:30 +00:00
|
|
|
func TestWrite_StringFields(t *testing.T) {
|
|
|
|
now := time.Now()
|
|
|
|
p1, err := metric.New(
|
|
|
|
"foo",
|
|
|
|
make(map[string]string),
|
|
|
|
map[string]interface{}{"value": 1.0, "status": "good"},
|
|
|
|
now,
|
|
|
|
telegraf.Counter)
|
|
|
|
p2, err := metric.New(
|
|
|
|
"bar",
|
|
|
|
make(map[string]string),
|
|
|
|
map[string]interface{}{"status": "needs numeric field"},
|
|
|
|
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, fam.LabelSet["status"])
|
|
|
|
|
|
|
|
fam, ok = client.fam["bar"]
|
|
|
|
require.False(t, ok)
|
|
|
|
}
|
|
|
|
|
2018-02-16 22:07:26 +00:00
|
|
|
func TestDoNotWrite_StringFields(t *testing.T) {
|
|
|
|
now := time.Now()
|
|
|
|
p1, err := metric.New(
|
|
|
|
"foo",
|
|
|
|
make(map[string]string),
|
|
|
|
map[string]interface{}{"value": 1.0, "status": "good"},
|
|
|
|
now,
|
|
|
|
telegraf.Counter)
|
|
|
|
p2, err := metric.New(
|
|
|
|
"bar",
|
|
|
|
make(map[string]string),
|
|
|
|
map[string]interface{}{"status": "needs numeric field"},
|
|
|
|
now,
|
|
|
|
telegraf.Gauge)
|
|
|
|
var metrics = []telegraf.Metric{p1, p2}
|
|
|
|
|
|
|
|
client := &PrometheusClient{
|
|
|
|
ExpirationInterval: internal.Duration{Duration: time.Second * 60},
|
|
|
|
StringAsLabel: false,
|
|
|
|
fam: make(map[string]*MetricFamily),
|
|
|
|
now: time.Now,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = client.Write(metrics)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
fam, ok := client.fam["foo"]
|
|
|
|
require.True(t, ok)
|
|
|
|
require.Equal(t, 0, fam.LabelSet["status"])
|
|
|
|
|
|
|
|
fam, ok = client.fam["bar"]
|
|
|
|
require.False(t, ok)
|
|
|
|
}
|
|
|
|
|
2017-06-14 01:04:26 +00:00
|
|
|
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)
|
2018-11-05 21:34:28 +00:00
|
|
|
defer pClient.Close()
|
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"
|
2018-11-05 21:34:28 +00:00
|
|
|
err := pTesting.Connect()
|
2016-11-15 11:33:39 +00:00
|
|
|
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{
|
2018-02-05 19:16:00 +00:00
|
|
|
URLs: []string{"http://localhost:9127/metrics"},
|
2016-11-15 11:33:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return pTesting, p, nil
|
|
|
|
}
|