Utilizing new client and overhauling Accumulator interface
Fixes #280 Fixes #281 Fixes #289
This commit is contained in:
@@ -8,7 +8,7 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/influxdb/influxdb/client"
|
||||
"github.com/influxdb/influxdb/client/v2"
|
||||
"github.com/influxdb/telegraf/outputs"
|
||||
)
|
||||
|
||||
@@ -51,15 +51,15 @@ func (o *OpenTSDB) Connect() error {
|
||||
return fmt.Errorf("OpenTSDB: TCP address cannot be resolved")
|
||||
}
|
||||
connection, err := net.DialTCP("tcp", nil, tcpAddr)
|
||||
defer connection.Close()
|
||||
if err != nil {
|
||||
return fmt.Errorf("OpenTSDB: Telnet connect fail")
|
||||
}
|
||||
defer connection.Close()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (o *OpenTSDB) Write(bp client.BatchPoints) error {
|
||||
if len(bp.Points) == 0 {
|
||||
func (o *OpenTSDB) Write(points []*client.Point) error {
|
||||
if len(points) == 0 {
|
||||
return nil
|
||||
}
|
||||
var timeNow = time.Now()
|
||||
@@ -70,19 +70,20 @@ func (o *OpenTSDB) Write(bp client.BatchPoints) error {
|
||||
if err != nil {
|
||||
return fmt.Errorf("OpenTSDB: Telnet connect fail")
|
||||
}
|
||||
for _, pt := range bp.Points {
|
||||
for _, pt := range points {
|
||||
metric := &MetricLine{
|
||||
Metric: fmt.Sprintf("%s%s", o.Prefix, pt.Measurement),
|
||||
Metric: fmt.Sprintf("%s%s", o.Prefix, pt.Name()),
|
||||
Timestamp: timeNow.Unix(),
|
||||
}
|
||||
metricValue, buildError := buildValue(bp, pt)
|
||||
|
||||
metricValue, buildError := buildValue(pt)
|
||||
if buildError != nil {
|
||||
fmt.Printf("OpenTSDB: %s\n", buildError.Error())
|
||||
continue
|
||||
}
|
||||
metric.Value = metricValue
|
||||
|
||||
tagsSlice := buildTags(bp.Tags, pt.Tags)
|
||||
tagsSlice := buildTags(pt.Tags())
|
||||
metric.Tags = fmt.Sprint(strings.Join(tagsSlice, " "))
|
||||
|
||||
messageLine := fmt.Sprintf("put %s %v %s %s\n", metric.Metric, metric.Timestamp, metric.Value, metric.Tags)
|
||||
@@ -99,13 +100,9 @@ func (o *OpenTSDB) Write(bp client.BatchPoints) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func buildTags(bpTags map[string]string, ptTags map[string]string) []string {
|
||||
tags := make([]string, (len(bpTags) + len(ptTags)))
|
||||
func buildTags(ptTags map[string]string) []string {
|
||||
tags := make([]string, len(ptTags))
|
||||
index := 0
|
||||
for k, v := range bpTags {
|
||||
tags[index] = fmt.Sprintf("%s=%s", k, v)
|
||||
index += 1
|
||||
}
|
||||
for k, v := range ptTags {
|
||||
tags[index] = fmt.Sprintf("%s=%s", k, v)
|
||||
index += 1
|
||||
@@ -114,9 +111,9 @@ func buildTags(bpTags map[string]string, ptTags map[string]string) []string {
|
||||
return tags
|
||||
}
|
||||
|
||||
func buildValue(bp client.BatchPoints, pt client.Point) (string, error) {
|
||||
func buildValue(pt *client.Point) (string, error) {
|
||||
var retv string
|
||||
var v = pt.Fields["value"]
|
||||
var v = pt.Fields()["value"]
|
||||
switch p := v.(type) {
|
||||
case int64:
|
||||
retv = IntToString(int64(p))
|
||||
|
||||
@@ -3,47 +3,42 @@ package opentsdb
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/influxdb/influxdb/client"
|
||||
"github.com/influxdb/influxdb/client/v2"
|
||||
"github.com/influxdb/telegraf/testutil"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestBuildTagsTelnet(t *testing.T) {
|
||||
var tagtests = []struct {
|
||||
bpIn map[string]string
|
||||
ptIn map[string]string
|
||||
outTags []string
|
||||
}{
|
||||
{
|
||||
map[string]string{"one": "two"},
|
||||
map[string]string{"three": "four"},
|
||||
map[string]string{"one": "two", "three": "four"},
|
||||
[]string{"one=two", "three=four"},
|
||||
},
|
||||
{
|
||||
map[string]string{"aaa": "bbb"},
|
||||
map[string]string{},
|
||||
[]string{"aaa=bbb"},
|
||||
},
|
||||
{
|
||||
map[string]string{"one": "two"},
|
||||
map[string]string{"aaa": "bbb"},
|
||||
map[string]string{"one": "two", "aaa": "bbb"},
|
||||
[]string{"aaa=bbb", "one=two"},
|
||||
},
|
||||
{
|
||||
map[string]string{},
|
||||
map[string]string{},
|
||||
[]string{},
|
||||
},
|
||||
}
|
||||
for _, tt := range tagtests {
|
||||
tags := buildTags(tt.bpIn, tt.ptIn)
|
||||
tags := buildTags(tt.ptIn)
|
||||
if !reflect.DeepEqual(tags, tt.outTags) {
|
||||
t.Errorf("\nexpected %+v\ngot %+v\n", tt.outTags, tags)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestWrite(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("Skipping integration test in short mode")
|
||||
@@ -60,36 +55,24 @@ func TestWrite(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
|
||||
// Verify that we can successfully write data to OpenTSDB
|
||||
err = o.Write(testutil.MockBatchPoints())
|
||||
err = o.Write(testutil.MockBatchPoints().Points())
|
||||
require.NoError(t, err)
|
||||
|
||||
// Verify postive and negative test cases of writing data
|
||||
var bp client.BatchPoints
|
||||
bp.Time = time.Now()
|
||||
bp.Tags = map[string]string{"testkey": "testvalue"}
|
||||
bp.Points = []client.Point{
|
||||
{
|
||||
Measurement: "justametric.float",
|
||||
Fields: map[string]interface{}{"value": float64(1.0)},
|
||||
},
|
||||
{
|
||||
Measurement: "justametric.int",
|
||||
Fields: map[string]interface{}{"value": int64(123456789)},
|
||||
},
|
||||
{
|
||||
Measurement: "justametric.uint",
|
||||
Fields: map[string]interface{}{"value": uint64(123456789012345)},
|
||||
},
|
||||
{
|
||||
Measurement: "justametric.string",
|
||||
Fields: map[string]interface{}{"value": "Lorem Ipsum"},
|
||||
},
|
||||
{
|
||||
Measurement: "justametric.anotherfloat",
|
||||
Fields: map[string]interface{}{"value": float64(42.0)},
|
||||
},
|
||||
}
|
||||
err = o.Write(bp)
|
||||
bp := testutil.MockBatchPoints()
|
||||
tags := make(map[string]string)
|
||||
bp.AddPoint(client.NewPoint("justametric.float", tags,
|
||||
map[string]interface{}{"value": float64(1.0)}))
|
||||
bp.AddPoint(client.NewPoint("justametric.int", tags,
|
||||
map[string]interface{}{"value": int64(123456789)}))
|
||||
bp.AddPoint(client.NewPoint("justametric.uint", tags,
|
||||
map[string]interface{}{"value": uint64(123456789012345)}))
|
||||
bp.AddPoint(client.NewPoint("justametric.string", tags,
|
||||
map[string]interface{}{"value": "Lorem Ipsum"}))
|
||||
bp.AddPoint(client.NewPoint("justametric.anotherfloat", tags,
|
||||
map[string]interface{}{"value": float64(42.0)}))
|
||||
|
||||
err = o.Write(bp.Points())
|
||||
require.NoError(t, err)
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user