2015-09-11 02:35:12 +08:00
|
|
|
|
package opentsdb
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
2016-07-19 03:01:36 +08:00
|
|
|
|
"fmt"
|
|
|
|
|
|
"net"
|
|
|
|
|
|
"net/http"
|
|
|
|
|
|
"net/http/httptest"
|
|
|
|
|
|
"net/url"
|
2015-09-11 02:35:12 +08:00
|
|
|
|
"reflect"
|
2016-07-19 03:01:36 +08:00
|
|
|
|
"strconv"
|
2015-09-11 02:35:12 +08:00
|
|
|
|
"testing"
|
2016-07-19 03:01:36 +08:00
|
|
|
|
|
2017-09-14 08:30:52 +08:00
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
2016-07-19 03:01:36 +08:00
|
|
|
|
"github.com/influxdata/telegraf"
|
|
|
|
|
|
"github.com/influxdata/telegraf/testutil"
|
2015-09-11 02:35:12 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
2016-07-19 03:01:36 +08:00
|
|
|
|
func TestCleanTags(t *testing.T) {
|
2015-09-11 02:35:12 +08:00
|
|
|
|
var tagtests = []struct {
|
|
|
|
|
|
ptIn map[string]string
|
2016-07-19 03:01:36 +08:00
|
|
|
|
outTags map[string]string
|
2015-09-11 02:35:12 +08:00
|
|
|
|
}{
|
|
|
|
|
|
{
|
2015-10-17 06:13:32 +08:00
|
|
|
|
map[string]string{"one": "two", "three": "four"},
|
2016-07-19 03:01:36 +08:00
|
|
|
|
map[string]string{"one": "two", "three": "four"},
|
2015-09-11 02:35:12 +08:00
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
map[string]string{"aaa": "bbb"},
|
2016-07-19 03:01:36 +08:00
|
|
|
|
map[string]string{"aaa": "bbb"},
|
2015-09-11 02:35:12 +08:00
|
|
|
|
},
|
|
|
|
|
|
{
|
2017-09-14 08:30:52 +08:00
|
|
|
|
map[string]string{"Sp%ci@l Chars[": "g$t repl#ce)d"},
|
|
|
|
|
|
map[string]string{"Sp-ci-l_Chars_": "g-t_repl-ce_d"},
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
map[string]string{"μnicodε_letters": "okαy"},
|
|
|
|
|
|
map[string]string{"μnicodε_letters": "okαy"},
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
map[string]string{"n☺": "emojies☠"},
|
|
|
|
|
|
map[string]string{"n_": "emojies_"},
|
2015-09-11 02:35:12 +08:00
|
|
|
|
},
|
2016-04-29 07:00:06 +08:00
|
|
|
|
{
|
2016-07-19 03:01:36 +08:00
|
|
|
|
map[string]string{},
|
|
|
|
|
|
map[string]string{},
|
|
|
|
|
|
},
|
|
|
|
|
|
}
|
|
|
|
|
|
for _, tt := range tagtests {
|
|
|
|
|
|
tags := cleanTags(tt.ptIn)
|
|
|
|
|
|
if !reflect.DeepEqual(tags, tt.outTags) {
|
|
|
|
|
|
t.Errorf("\nexpected %+v\ngot %+v\n", tt.outTags, tags)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func TestBuildTagsTelnet(t *testing.T) {
|
|
|
|
|
|
var tagtests = []struct {
|
|
|
|
|
|
ptIn map[string]string
|
|
|
|
|
|
outTags string
|
|
|
|
|
|
}{
|
|
|
|
|
|
{
|
|
|
|
|
|
map[string]string{"one": "two", "three": "four"},
|
|
|
|
|
|
"one=two three=four",
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
map[string]string{"aaa": "bbb"},
|
|
|
|
|
|
"aaa=bbb",
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
map[string]string{"one": "two", "aaa": "bbb"},
|
|
|
|
|
|
"aaa=bbb one=two",
|
2016-04-29 07:00:06 +08:00
|
|
|
|
},
|
2015-09-11 02:35:12 +08:00
|
|
|
|
{
|
|
|
|
|
|
map[string]string{},
|
2016-07-19 03:01:36 +08:00
|
|
|
|
"",
|
2015-09-11 02:35:12 +08:00
|
|
|
|
},
|
|
|
|
|
|
}
|
|
|
|
|
|
for _, tt := range tagtests {
|
2016-07-19 03:01:36 +08:00
|
|
|
|
tags := ToLineFormat(tt.ptIn)
|
2015-09-11 02:35:12 +08:00
|
|
|
|
if !reflect.DeepEqual(tags, tt.outTags) {
|
|
|
|
|
|
t.Errorf("\nexpected %+v\ngot %+v\n", tt.outTags, tags)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2015-10-17 06:13:32 +08:00
|
|
|
|
|
2017-09-14 08:30:52 +08:00
|
|
|
|
func TestSanitize(t *testing.T) {
|
|
|
|
|
|
tests := []struct {
|
|
|
|
|
|
name string
|
|
|
|
|
|
value string
|
|
|
|
|
|
expected string
|
|
|
|
|
|
}{
|
|
|
|
|
|
{
|
|
|
|
|
|
name: "Ascii letters and numbers allowed",
|
|
|
|
|
|
value: "ascii 123",
|
|
|
|
|
|
expected: "ascii_123",
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
name: "Allowed punct",
|
|
|
|
|
|
value: "-_./",
|
|
|
|
|
|
expected: "-_./",
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
name: "Special conversions to hyphen",
|
|
|
|
|
|
value: "@*%#$!",
|
|
|
|
|
|
expected: "-----_",
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
name: "Unicode Letters allowed",
|
|
|
|
|
|
value: "μnicodε_letters",
|
|
|
|
|
|
expected: "μnicodε_letters",
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
name: "Other Unicode not allowed",
|
|
|
|
|
|
value: "“☢”",
|
|
|
|
|
|
expected: "___",
|
|
|
|
|
|
},
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
|
|
actual := sanitize(tt.value)
|
|
|
|
|
|
require.Equal(t, tt.expected, actual)
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-07-19 03:01:36 +08:00
|
|
|
|
func BenchmarkHttpSend(b *testing.B) {
|
2022-08-03 20:20:51 +08:00
|
|
|
|
const batchSize = 50
|
|
|
|
|
|
const metricsCount = 4 * batchSize
|
2022-12-20 18:42:09 +08:00
|
|
|
|
metrics := make([]telegraf.Metric, 0, metricsCount)
|
2022-08-03 20:20:51 +08:00
|
|
|
|
for i := 0; i < metricsCount; i++ {
|
2022-12-20 18:42:09 +08:00
|
|
|
|
metrics = append(metrics, testutil.TestMetric(1.0))
|
2016-07-19 03:01:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-02-13 06:26:10 +08:00
|
|
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
2016-07-19 03:01:36 +08:00
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
2022-10-12 00:31:44 +08:00
|
|
|
|
fmt.Fprintln(w, "{}")
|
2016-07-19 03:01:36 +08:00
|
|
|
|
}))
|
|
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
|
|
|
|
u, err := url.Parse(ts.URL)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
panic(err)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-07-10 18:51:25 +08:00
|
|
|
|
_, p, err := net.SplitHostPort(u.Host)
|
|
|
|
|
|
require.NoError(b, err)
|
2016-07-19 03:01:36 +08:00
|
|
|
|
|
|
|
|
|
|
port, err := strconv.Atoi(p)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
panic(err)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
o := &OpenTSDB{
|
|
|
|
|
|
Host: ts.URL,
|
|
|
|
|
|
Port: port,
|
|
|
|
|
|
Prefix: "",
|
2022-08-03 20:20:51 +08:00
|
|
|
|
HTTPBatchSize: batchSize,
|
2021-03-02 05:04:35 +08:00
|
|
|
|
HTTPPath: "/api/put",
|
2016-07-19 03:01:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
2024-07-10 18:51:25 +08:00
|
|
|
|
//nolint:errcheck // skip error check for benchmarking
|
|
|
|
|
|
o.Write(metrics)
|
2016-07-19 03:01:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2021-01-27 02:06:12 +08:00
|
|
|
|
func TestWriteIntegration(t *testing.T) {
|
2022-06-17 04:10:11 +08:00
|
|
|
|
if testing.Short() {
|
|
|
|
|
|
t.Skip("Skipping integration test in short mode")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-01-27 02:06:12 +08:00
|
|
|
|
t.Skip("Skip as OpenTSDB not running")
|
2016-07-19 03:01:36 +08:00
|
|
|
|
|
2021-01-27 02:06:12 +08:00
|
|
|
|
o := &OpenTSDB{
|
|
|
|
|
|
Host: testutil.GetLocalHost(),
|
|
|
|
|
|
Port: 4242,
|
|
|
|
|
|
Prefix: "prefix.test.",
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Verify that we can connect to the OpenTSDB instance
|
|
|
|
|
|
err := o.Connect()
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
|
|
// Verify that we can successfully write data to OpenTSDB
|
|
|
|
|
|
err = o.Write(testutil.MockMetrics())
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
|
|
// Verify positive and negative test cases of writing data
|
|
|
|
|
|
metrics := testutil.MockMetrics()
|
2024-01-03 22:16:26 +08:00
|
|
|
|
metrics = append(metrics,
|
|
|
|
|
|
testutil.TestMetric(float64(1.0), "justametric.float"),
|
|
|
|
|
|
testutil.TestMetric(int64(123456789), "justametric.int"),
|
|
|
|
|
|
testutil.TestMetric(uint64(123456789012345), "justametric.uint"),
|
|
|
|
|
|
testutil.TestMetric("Lorem Ipsum", "justametric.string"),
|
|
|
|
|
|
testutil.TestMetric(float64(42.0), "justametric.anotherfloat"),
|
|
|
|
|
|
testutil.TestMetric(float64(42.0), "metric w/ specialchars"),
|
|
|
|
|
|
)
|
2021-01-27 02:06:12 +08:00
|
|
|
|
|
|
|
|
|
|
err = o.Write(metrics)
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
}
|