telegraf/plugins/serializers/splunkmetric/splunkmetric_test.go

266 lines
6.2 KiB
Go
Raw Normal View History

2018-09-11 20:01:08 +00:00
package splunkmetric
import (
"testing"
"time"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/metric"
"github.com/stretchr/testify/assert"
2018-09-11 20:01:08 +00:00
)
func MustMetric(v telegraf.Metric, err error) telegraf.Metric {
if err != nil {
panic(err)
}
return v
}
func TestSerializeMetricFloat(t *testing.T) {
// Test sub-second time
now := time.Unix(1529875740, 819000000)
tags := map[string]string{
"cpu": "cpu0",
}
fields := map[string]interface{}{
"usage_idle": float64(91.5),
}
m, err := metric.New("cpu", tags, fields, now)
assert.NoError(t, err)
2019-11-18 20:38:34 +00:00
s, _ := NewSerializer(false, false)
2018-09-11 20:01:08 +00:00
var buf []byte
buf, err = s.Serialize(m)
assert.NoError(t, err)
expS := `{"_value":91.5,"cpu":"cpu0","metric_name":"cpu.usage_idle","time":1529875740.819}`
2018-09-11 20:01:08 +00:00
assert.Equal(t, string(expS), string(buf))
}
func TestSerializeMetricFloatHec(t *testing.T) {
// Test sub-second time
now := time.Unix(1529875740, 819000000)
tags := map[string]string{
"cpu": "cpu0",
}
fields := map[string]interface{}{
"usage_idle": float64(91.5),
}
m, err := metric.New("cpu", tags, fields, now)
assert.NoError(t, err)
2019-11-18 20:38:34 +00:00
s, _ := NewSerializer(true, false)
2018-09-11 20:01:08 +00:00
var buf []byte
buf, err = s.Serialize(m)
assert.NoError(t, err)
expS := `{"time":1529875740.819,"event":"metric","fields":{"_value":91.5,"cpu":"cpu0","metric_name":"cpu.usage_idle"}}`
2018-09-11 20:01:08 +00:00
assert.Equal(t, string(expS), string(buf))
}
func TestSerializeMetricInt(t *testing.T) {
now := time.Unix(0, 0)
tags := map[string]string{
"cpu": "cpu0",
}
fields := map[string]interface{}{
"usage_idle": int64(90),
}
m, err := metric.New("cpu", tags, fields, now)
assert.NoError(t, err)
2019-11-18 20:38:34 +00:00
s, _ := NewSerializer(false, false)
2018-09-11 20:01:08 +00:00
var buf []byte
buf, err = s.Serialize(m)
assert.NoError(t, err)
expS := `{"_value":90,"cpu":"cpu0","metric_name":"cpu.usage_idle","time":0}`
2018-09-11 20:01:08 +00:00
assert.Equal(t, string(expS), string(buf))
}
func TestSerializeMetricIntHec(t *testing.T) {
now := time.Unix(0, 0)
tags := map[string]string{
"cpu": "cpu0",
}
fields := map[string]interface{}{
"usage_idle": int64(90),
}
m, err := metric.New("cpu", tags, fields, now)
assert.NoError(t, err)
2019-11-18 20:38:34 +00:00
s, _ := NewSerializer(true, false)
2018-09-11 20:01:08 +00:00
var buf []byte
buf, err = s.Serialize(m)
assert.NoError(t, err)
expS := `{"time":0,"event":"metric","fields":{"_value":90,"cpu":"cpu0","metric_name":"cpu.usage_idle"}}`
2018-09-11 20:01:08 +00:00
assert.Equal(t, string(expS), string(buf))
}
func TestSerializeMetricBool(t *testing.T) {
now := time.Unix(0, 0)
tags := map[string]string{
"container-name": "telegraf-test",
}
fields := map[string]interface{}{
"oomkiller": bool(true),
}
m, err := metric.New("docker", tags, fields, now)
assert.NoError(t, err)
2019-11-18 20:38:34 +00:00
s, _ := NewSerializer(false, false)
var buf []byte
buf, err = s.Serialize(m)
assert.NoError(t, err)
expS := `{"_value":1,"container-name":"telegraf-test","metric_name":"docker.oomkiller","time":0}`
assert.Equal(t, string(expS), string(buf))
}
func TestSerializeMetricBoolHec(t *testing.T) {
now := time.Unix(0, 0)
tags := map[string]string{
"container-name": "telegraf-test",
}
fields := map[string]interface{}{
"oomkiller": bool(false),
}
m, err := metric.New("docker", tags, fields, now)
assert.NoError(t, err)
2019-11-18 20:38:34 +00:00
s, _ := NewSerializer(true, false)
var buf []byte
buf, err = s.Serialize(m)
assert.NoError(t, err)
expS := `{"time":0,"event":"metric","fields":{"_value":0,"container-name":"telegraf-test","metric_name":"docker.oomkiller"}}`
assert.Equal(t, string(expS), string(buf))
}
2018-09-11 20:01:08 +00:00
func TestSerializeMetricString(t *testing.T) {
now := time.Unix(0, 0)
tags := map[string]string{
"cpu": "cpu0",
}
fields := map[string]interface{}{
"processorType": "ARMv7 Processor rev 4 (v7l)",
"usage_idle": int64(5),
}
m, err := metric.New("cpu", tags, fields, now)
assert.NoError(t, err)
2019-11-18 20:38:34 +00:00
s, _ := NewSerializer(false, false)
2018-09-11 20:01:08 +00:00
var buf []byte
buf, err = s.Serialize(m)
assert.NoError(t, err)
expS := `{"_value":5,"cpu":"cpu0","metric_name":"cpu.usage_idle","time":0}`
2018-09-11 20:01:08 +00:00
assert.Equal(t, string(expS), string(buf))
assert.NoError(t, err)
}
func TestSerializeBatch(t *testing.T) {
m := MustMetric(
metric.New(
"cpu",
map[string]string{},
map[string]interface{}{
"value": 42.0,
},
time.Unix(0, 0),
),
)
n := MustMetric(
metric.New(
"cpu",
map[string]string{},
map[string]interface{}{
"value": 92.0,
},
time.Unix(0, 0),
),
)
metrics := []telegraf.Metric{m, n}
2019-11-18 20:38:34 +00:00
s, _ := NewSerializer(false, false)
2018-09-11 20:01:08 +00:00
buf, err := s.SerializeBatch(metrics)
assert.NoError(t, err)
expS := `{"_value":42,"metric_name":"cpu.value","time":0}{"_value":92,"metric_name":"cpu.value","time":0}`
2019-11-18 20:38:34 +00:00
assert.Equal(t, string(expS), string(buf))
}
func TestSerializeMulti(t *testing.T) {
m := MustMetric(
metric.New(
"cpu",
map[string]string{},
map[string]interface{}{
"user": 42.0,
"system": 8.0,
},
time.Unix(0, 0),
),
)
metrics := []telegraf.Metric{m}
s, _ := NewSerializer(false, true)
buf, err := s.SerializeBatch(metrics)
assert.NoError(t, err)
expS := `{"metric_name:cpu.system":8,"metric_name:cpu.user":42,"time":0}`
2018-09-11 20:01:08 +00:00
assert.Equal(t, string(expS), string(buf))
}
func TestSerializeBatchHec(t *testing.T) {
m := MustMetric(
metric.New(
"cpu",
map[string]string{},
map[string]interface{}{
"value": 42.0,
},
time.Unix(0, 0),
),
)
n := MustMetric(
metric.New(
"cpu",
map[string]string{},
map[string]interface{}{
"value": 92.0,
},
time.Unix(0, 0),
),
)
metrics := []telegraf.Metric{m, n}
2019-11-18 20:38:34 +00:00
s, _ := NewSerializer(true, false)
buf, err := s.SerializeBatch(metrics)
assert.NoError(t, err)
expS := `{"time":0,"event":"metric","fields":{"_value":42,"metric_name":"cpu.value"}}{"time":0,"event":"metric","fields":{"_value":92,"metric_name":"cpu.value"}}`
2019-11-18 20:38:34 +00:00
assert.Equal(t, string(expS), string(buf))
}
func TestSerializeMultiHec(t *testing.T) {
m := MustMetric(
metric.New(
"cpu",
map[string]string{},
map[string]interface{}{
"usage": 42.0,
"system": 8.0,
},
time.Unix(0, 0),
),
)
metrics := []telegraf.Metric{m}
s, _ := NewSerializer(true, true)
2018-09-11 20:01:08 +00:00
buf, err := s.SerializeBatch(metrics)
assert.NoError(t, err)
expS := `{"time":0,"event":"metric","fields":{"metric_name:cpu.system":8,"metric_name:cpu.usage":42}}`
2018-09-11 20:01:08 +00:00
assert.Equal(t, string(expS), string(buf))
}