2016-05-02 14:58:29 +00:00
|
|
|
package instrumental
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"net"
|
|
|
|
"net/textproto"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/influxdata/telegraf"
|
2016-11-22 12:51:57 +00:00
|
|
|
"github.com/influxdata/telegraf/metric"
|
2016-05-02 14:58:29 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestWrite(t *testing.T) {
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
|
|
|
go TCPServer(t, &wg)
|
|
|
|
// Give the fake TCP server some time to start:
|
|
|
|
time.Sleep(time.Millisecond * 100)
|
|
|
|
|
|
|
|
i := Instrumental{
|
|
|
|
Host: "127.0.0.1",
|
|
|
|
ApiToken: "abc123token",
|
|
|
|
Prefix: "my.prefix",
|
|
|
|
}
|
|
|
|
|
|
|
|
// Default to gauge
|
2016-11-22 12:51:57 +00:00
|
|
|
m1, _ := metric.New(
|
2016-05-02 14:58:29 +00:00
|
|
|
"mymeasurement",
|
|
|
|
map[string]string{"host": "192.168.0.1"},
|
|
|
|
map[string]interface{}{"myfield": float64(3.14)},
|
|
|
|
time.Date(2010, time.November, 10, 23, 0, 0, 0, time.UTC),
|
|
|
|
)
|
2016-11-22 12:51:57 +00:00
|
|
|
m2, _ := metric.New(
|
2016-05-02 14:58:29 +00:00
|
|
|
"mymeasurement",
|
|
|
|
map[string]string{"host": "192.168.0.1", "metric_type": "set"},
|
|
|
|
map[string]interface{}{"value": float64(3.14)},
|
|
|
|
time.Date(2010, time.November, 10, 23, 0, 0, 0, time.UTC),
|
|
|
|
)
|
|
|
|
|
|
|
|
metrics := []telegraf.Metric{m1, m2}
|
|
|
|
i.Write(metrics)
|
|
|
|
|
|
|
|
// Counter and Histogram are increments
|
2016-11-22 12:51:57 +00:00
|
|
|
m3, _ := metric.New(
|
2016-05-02 14:58:29 +00:00
|
|
|
"my_histogram",
|
|
|
|
map[string]string{"host": "192.168.0.1", "metric_type": "histogram"},
|
|
|
|
map[string]interface{}{"value": float64(3.14)},
|
|
|
|
time.Date(2010, time.November, 10, 23, 0, 0, 0, time.UTC),
|
|
|
|
)
|
2016-08-30 06:03:32 +00:00
|
|
|
// We will modify metric names that won't be accepted by Instrumental
|
2016-11-22 12:51:57 +00:00
|
|
|
m4, _ := metric.New(
|
2016-08-30 06:03:32 +00:00
|
|
|
"bad_metric_name",
|
|
|
|
map[string]string{"host": "192.168.0.1:8888::123", "metric_type": "counter"},
|
|
|
|
map[string]interface{}{"value": 1},
|
|
|
|
time.Date(2010, time.November, 10, 23, 0, 0, 0, time.UTC),
|
|
|
|
)
|
|
|
|
// We will drop metric values that won't be accepted by Instrumental
|
2016-11-22 12:51:57 +00:00
|
|
|
m5, _ := metric.New(
|
2016-05-02 14:58:29 +00:00
|
|
|
"bad_values",
|
|
|
|
map[string]string{"host": "192.168.0.1", "metric_type": "counter"},
|
|
|
|
map[string]interface{}{"value": "\" 3:30\""},
|
|
|
|
time.Date(2010, time.November, 10, 23, 0, 0, 0, time.UTC),
|
|
|
|
)
|
2016-11-22 12:51:57 +00:00
|
|
|
m6, _ := metric.New(
|
2016-05-02 14:58:29 +00:00
|
|
|
"my_counter",
|
|
|
|
map[string]string{"host": "192.168.0.1", "metric_type": "counter"},
|
|
|
|
map[string]interface{}{"value": float64(3.14)},
|
|
|
|
time.Date(2010, time.November, 10, 23, 0, 0, 0, time.UTC),
|
|
|
|
)
|
|
|
|
|
2016-08-30 06:03:32 +00:00
|
|
|
metrics = []telegraf.Metric{m3, m4, m5, m6}
|
2016-05-02 14:58:29 +00:00
|
|
|
i.Write(metrics)
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TCPServer(t *testing.T, wg *sync.WaitGroup) {
|
|
|
|
tcpServer, _ := net.Listen("tcp", "127.0.0.1:8000")
|
|
|
|
defer wg.Done()
|
|
|
|
conn, _ := tcpServer.Accept()
|
|
|
|
conn.SetDeadline(time.Now().Add(1 * time.Second))
|
|
|
|
reader := bufio.NewReader(conn)
|
|
|
|
tp := textproto.NewReader(reader)
|
|
|
|
|
|
|
|
hello, _ := tp.ReadLine()
|
2016-06-21 20:28:31 +00:00
|
|
|
assert.Equal(t, "hello version go/telegraf/1.1", hello)
|
2016-05-02 14:58:29 +00:00
|
|
|
auth, _ := tp.ReadLine()
|
|
|
|
assert.Equal(t, "authenticate abc123token", auth)
|
|
|
|
conn.Write([]byte("ok\nok\n"))
|
|
|
|
|
|
|
|
data1, _ := tp.ReadLine()
|
|
|
|
assert.Equal(t, "gauge my.prefix.192_168_0_1.mymeasurement.myfield 3.14 1289430000", data1)
|
|
|
|
data2, _ := tp.ReadLine()
|
|
|
|
assert.Equal(t, "gauge my.prefix.192_168_0_1.mymeasurement 3.14 1289430000", data2)
|
|
|
|
|
|
|
|
conn, _ = tcpServer.Accept()
|
|
|
|
conn.SetDeadline(time.Now().Add(1 * time.Second))
|
|
|
|
reader = bufio.NewReader(conn)
|
|
|
|
tp = textproto.NewReader(reader)
|
|
|
|
|
|
|
|
hello, _ = tp.ReadLine()
|
2016-06-21 20:28:31 +00:00
|
|
|
assert.Equal(t, "hello version go/telegraf/1.1", hello)
|
2016-05-02 14:58:29 +00:00
|
|
|
auth, _ = tp.ReadLine()
|
|
|
|
assert.Equal(t, "authenticate abc123token", auth)
|
|
|
|
conn.Write([]byte("ok\nok\n"))
|
|
|
|
|
|
|
|
data3, _ := tp.ReadLine()
|
|
|
|
assert.Equal(t, "increment my.prefix.192_168_0_1.my_histogram 3.14 1289430000", data3)
|
2016-08-30 06:03:32 +00:00
|
|
|
|
2016-05-02 14:58:29 +00:00
|
|
|
data4, _ := tp.ReadLine()
|
2016-08-30 06:03:32 +00:00
|
|
|
assert.Equal(t, "increment my.prefix.192_168_0_1_8888_123.bad_metric_name 1 1289430000", data4)
|
|
|
|
|
|
|
|
data5, _ := tp.ReadLine()
|
|
|
|
assert.Equal(t, "increment my.prefix.192_168_0_1.my_counter 3.14 1289430000", data5)
|
|
|
|
|
|
|
|
data6, _ := tp.ReadLine()
|
|
|
|
assert.Equal(t, "", data6)
|
2016-05-02 14:58:29 +00:00
|
|
|
|
|
|
|
conn.Close()
|
|
|
|
}
|