2015-08-13 04:15:34 +08:00
|
|
|
package datadog
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"fmt"
|
2019-08-06 05:50:29 +08:00
|
|
|
"math"
|
2015-08-13 04:15:34 +08:00
|
|
|
"net/http"
|
|
|
|
|
"net/http/httptest"
|
|
|
|
|
"reflect"
|
|
|
|
|
"testing"
|
|
|
|
|
"time"
|
|
|
|
|
|
2021-11-19 00:26:24 +08:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
2016-01-28 07:15:14 +08:00
|
|
|
"github.com/influxdata/telegraf"
|
2019-08-06 05:50:29 +08:00
|
|
|
"github.com/influxdata/telegraf/testutil"
|
2015-08-13 04:15:34 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var (
|
2021-03-02 05:04:35 +08:00
|
|
|
fakeURL = "http://test.datadog.com"
|
|
|
|
|
fakeAPIKey = "123456"
|
2015-08-13 04:15:34 +08:00
|
|
|
)
|
|
|
|
|
|
2018-10-06 04:51:16 +08:00
|
|
|
func NewDatadog(url string) *Datadog {
|
|
|
|
|
return &Datadog{
|
|
|
|
|
URL: url,
|
2022-11-22 04:54:24 +08:00
|
|
|
Log: testutil.Logger{},
|
2018-10-06 04:51:16 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-13 04:15:34 +08:00
|
|
|
func fakeDatadog() *Datadog {
|
2021-03-02 05:04:35 +08:00
|
|
|
d := NewDatadog(fakeURL)
|
|
|
|
|
d.Apikey = fakeAPIKey
|
2015-08-13 04:15:34 +08:00
|
|
|
return d
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestUriOverride(t *testing.T) {
|
|
|
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
w.WriteHeader(http.StatusOK)
|
2023-04-03 21:19:43 +08:00
|
|
|
json.NewEncoder(w).Encode(`{"status":"ok"}`) //nolint:errcheck // Ignore the returned error as the test will fail anyway
|
2015-08-13 04:15:34 +08:00
|
|
|
}))
|
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
|
|
d := NewDatadog(ts.URL)
|
2015-08-14 09:42:57 +08:00
|
|
|
d.Apikey = "123456"
|
|
|
|
|
err := d.Connect()
|
|
|
|
|
require.NoError(t, err)
|
2016-01-28 07:15:14 +08:00
|
|
|
err = d.Write(testutil.MockMetrics())
|
2015-08-13 04:15:34 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-08 00:38:19 +08:00
|
|
|
func TestCompressionOverride(t *testing.T) {
|
|
|
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
w.WriteHeader(http.StatusOK)
|
2023-04-03 21:19:43 +08:00
|
|
|
json.NewEncoder(w).Encode(`{"status":"ok"}`) //nolint:errcheck // Ignore the returned error as the test will fail anyway
|
2022-01-08 00:38:19 +08:00
|
|
|
}))
|
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
|
|
d := NewDatadog(ts.URL)
|
|
|
|
|
d.Apikey = "123456"
|
|
|
|
|
d.Compression = "zlib"
|
|
|
|
|
err := d.Connect()
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
err = d.Write(testutil.MockMetrics())
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-13 04:15:34 +08:00
|
|
|
func TestBadStatusCode(t *testing.T) {
|
2022-11-22 04:54:24 +08:00
|
|
|
errorString := `{"errors": ["Something bad happened to the server.", "Your query made the server very sad."]}`
|
2015-08-13 04:15:34 +08:00
|
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
2022-11-22 04:54:24 +08:00
|
|
|
fmt.Fprint(w, errorString)
|
2015-08-13 04:15:34 +08:00
|
|
|
}))
|
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
|
|
d := NewDatadog(ts.URL)
|
2015-08-14 09:42:57 +08:00
|
|
|
d.Apikey = "123456"
|
|
|
|
|
err := d.Connect()
|
|
|
|
|
require.NoError(t, err)
|
2016-01-28 07:15:14 +08:00
|
|
|
err = d.Write(testutil.MockMetrics())
|
2015-08-13 04:15:34 +08:00
|
|
|
if err == nil {
|
|
|
|
|
t.Errorf("error expected but none returned")
|
|
|
|
|
} else {
|
2022-11-22 04:54:24 +08:00
|
|
|
require.EqualError(t, err, fmt.Sprintf("received bad status code, %v: %s", http.StatusInternalServerError, errorString))
|
2015-08-13 04:15:34 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestAuthenticatedUrl(t *testing.T) {
|
|
|
|
|
d := fakeDatadog()
|
|
|
|
|
|
2021-03-02 05:04:35 +08:00
|
|
|
authURL := d.authenticatedURL()
|
2021-11-19 00:26:24 +08:00
|
|
|
require.EqualValues(t, fmt.Sprintf("%s?api_key=%s", fakeURL, fakeAPIKey), authURL)
|
2015-08-13 04:15:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestBuildTags(t *testing.T) {
|
|
|
|
|
var tagtests = []struct {
|
2018-10-06 04:48:18 +08:00
|
|
|
ptIn []*telegraf.Tag
|
2015-08-13 04:15:34 +08:00
|
|
|
outTags []string
|
|
|
|
|
}{
|
|
|
|
|
{
|
2018-10-06 04:48:18 +08:00
|
|
|
[]*telegraf.Tag{
|
2018-10-20 04:32:54 +08:00
|
|
|
{
|
2018-10-06 04:48:18 +08:00
|
|
|
Key: "one",
|
|
|
|
|
Value: "two",
|
|
|
|
|
},
|
2018-10-20 04:32:54 +08:00
|
|
|
{
|
2018-10-06 04:48:18 +08:00
|
|
|
Key: "three",
|
|
|
|
|
Value: "four",
|
|
|
|
|
},
|
|
|
|
|
},
|
2015-08-13 04:15:34 +08:00
|
|
|
[]string{"one:two", "three:four"},
|
|
|
|
|
},
|
|
|
|
|
{
|
2018-10-06 04:48:18 +08:00
|
|
|
[]*telegraf.Tag{
|
2018-10-20 04:32:54 +08:00
|
|
|
{
|
2018-10-06 04:48:18 +08:00
|
|
|
Key: "aaa",
|
|
|
|
|
Value: "bbb",
|
|
|
|
|
},
|
|
|
|
|
},
|
2015-08-13 04:15:34 +08:00
|
|
|
[]string{"aaa:bbb"},
|
|
|
|
|
},
|
|
|
|
|
{
|
2018-10-06 04:48:18 +08:00
|
|
|
[]*telegraf.Tag{},
|
2015-08-13 04:15:34 +08:00
|
|
|
[]string{},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
for _, tt := range tagtests {
|
2015-10-17 06:13:32 +08:00
|
|
|
tags := buildTags(tt.ptIn)
|
2015-08-13 04:15:34 +08:00
|
|
|
if !reflect.DeepEqual(tags, tt.outTags) {
|
|
|
|
|
t.Errorf("\nexpected %+v\ngot %+v\n", tt.outTags, tags)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestBuildPoint(t *testing.T) {
|
|
|
|
|
var tagtests = []struct {
|
2016-01-28 07:15:14 +08:00
|
|
|
ptIn telegraf.Metric
|
2015-08-13 04:15:34 +08:00
|
|
|
outPt Point
|
|
|
|
|
err error
|
|
|
|
|
}{
|
|
|
|
|
{
|
2016-01-28 07:15:14 +08:00
|
|
|
testutil.TestMetric(0.0, "test1"),
|
2015-10-17 06:13:32 +08:00
|
|
|
Point{
|
|
|
|
|
float64(time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix()),
|
|
|
|
|
0.0,
|
2015-08-13 04:15:34 +08:00
|
|
|
},
|
|
|
|
|
nil,
|
|
|
|
|
},
|
|
|
|
|
{
|
2016-01-28 07:15:14 +08:00
|
|
|
testutil.TestMetric(1.0, "test2"),
|
2015-10-17 06:13:32 +08:00
|
|
|
Point{
|
2015-11-11 08:05:28 +08:00
|
|
|
float64(time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix()),
|
2015-10-17 06:13:32 +08:00
|
|
|
1.0,
|
2015-08-13 04:15:34 +08:00
|
|
|
},
|
|
|
|
|
nil,
|
|
|
|
|
},
|
|
|
|
|
{
|
2016-01-28 07:15:14 +08:00
|
|
|
testutil.TestMetric(10, "test3"),
|
2015-10-17 06:13:32 +08:00
|
|
|
Point{
|
|
|
|
|
float64(time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix()),
|
|
|
|
|
10.0,
|
2015-08-13 04:15:34 +08:00
|
|
|
},
|
|
|
|
|
nil,
|
|
|
|
|
},
|
|
|
|
|
{
|
2016-01-28 07:15:14 +08:00
|
|
|
testutil.TestMetric(int32(112345), "test4"),
|
2015-10-17 06:13:32 +08:00
|
|
|
Point{
|
|
|
|
|
float64(time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix()),
|
|
|
|
|
112345.0,
|
2015-08-13 04:15:34 +08:00
|
|
|
},
|
|
|
|
|
nil,
|
|
|
|
|
},
|
|
|
|
|
{
|
2016-01-28 07:15:14 +08:00
|
|
|
testutil.TestMetric(int64(112345), "test5"),
|
2015-10-17 06:13:32 +08:00
|
|
|
Point{
|
|
|
|
|
float64(time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix()),
|
|
|
|
|
112345.0,
|
2015-08-13 04:15:34 +08:00
|
|
|
},
|
|
|
|
|
nil,
|
|
|
|
|
},
|
|
|
|
|
{
|
2016-01-28 07:15:14 +08:00
|
|
|
testutil.TestMetric(float32(11234.5), "test6"),
|
2015-10-17 06:13:32 +08:00
|
|
|
Point{
|
|
|
|
|
float64(time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix()),
|
|
|
|
|
11234.5,
|
2015-08-13 04:15:34 +08:00
|
|
|
},
|
|
|
|
|
nil,
|
|
|
|
|
},
|
2018-02-21 09:32:18 +08:00
|
|
|
{
|
2021-11-19 00:26:24 +08:00
|
|
|
testutil.TestMetric(true, "test7"),
|
2018-02-21 09:32:18 +08:00
|
|
|
Point{
|
|
|
|
|
float64(time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix()),
|
|
|
|
|
1.0,
|
|
|
|
|
},
|
|
|
|
|
nil,
|
|
|
|
|
},
|
|
|
|
|
{
|
2021-11-19 00:26:24 +08:00
|
|
|
testutil.TestMetric(false, "test8"),
|
2018-02-21 09:32:18 +08:00
|
|
|
Point{
|
|
|
|
|
float64(time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix()),
|
|
|
|
|
0.0,
|
|
|
|
|
},
|
|
|
|
|
nil,
|
|
|
|
|
},
|
2018-05-02 09:56:39 +08:00
|
|
|
{
|
|
|
|
|
testutil.TestMetric(int64(0), "test int64"),
|
|
|
|
|
Point{
|
|
|
|
|
float64(time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix()),
|
|
|
|
|
0.0,
|
|
|
|
|
},
|
|
|
|
|
nil,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
testutil.TestMetric(uint64(0), "test uint64"),
|
|
|
|
|
Point{
|
|
|
|
|
float64(time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix()),
|
|
|
|
|
0.0,
|
|
|
|
|
},
|
|
|
|
|
nil,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
testutil.TestMetric(true, "test bool"),
|
|
|
|
|
Point{
|
|
|
|
|
float64(time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix()),
|
|
|
|
|
1.0,
|
|
|
|
|
},
|
|
|
|
|
nil,
|
|
|
|
|
},
|
2015-08-13 04:15:34 +08:00
|
|
|
}
|
|
|
|
|
for _, tt := range tagtests {
|
2016-01-28 07:15:14 +08:00
|
|
|
pt, err := buildMetrics(tt.ptIn)
|
2015-08-13 04:15:34 +08:00
|
|
|
if err != nil && tt.err == nil {
|
2015-10-17 06:13:32 +08:00
|
|
|
t.Errorf("%s: unexpected error, %+v\n", tt.ptIn.Name(), err)
|
2015-08-13 04:15:34 +08:00
|
|
|
}
|
|
|
|
|
if tt.err != nil && err == nil {
|
2015-10-17 06:13:32 +08:00
|
|
|
t.Errorf("%s: expected an error (%s) but none returned", tt.ptIn.Name(), tt.err.Error())
|
2015-08-13 04:15:34 +08:00
|
|
|
}
|
2016-01-08 01:09:04 +08:00
|
|
|
if !reflect.DeepEqual(pt["value"], tt.outPt) && tt.err == nil {
|
|
|
|
|
t.Errorf("%s: \nexpected %+v\ngot %+v\n",
|
|
|
|
|
tt.ptIn.Name(), tt.outPt, pt["value"])
|
2015-08-13 04:15:34 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-03-22 23:07:01 +08:00
|
|
|
|
|
|
|
|
func TestVerifyValue(t *testing.T) {
|
|
|
|
|
var tagtests = []struct {
|
|
|
|
|
ptIn telegraf.Metric
|
|
|
|
|
validMetric bool
|
|
|
|
|
}{
|
|
|
|
|
{
|
|
|
|
|
testutil.TestMetric(float32(11234.5), "test1"),
|
|
|
|
|
true,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
testutil.TestMetric("11234.5", "test2"),
|
|
|
|
|
false,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
for _, tt := range tagtests {
|
|
|
|
|
ok := verifyValue(tt.ptIn.Fields()["value"])
|
|
|
|
|
if tt.validMetric != ok {
|
|
|
|
|
t.Errorf("%s: verification failed\n", tt.ptIn.Name())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-08-06 05:50:29 +08:00
|
|
|
|
|
|
|
|
func TestNaNIsSkipped(t *testing.T) {
|
|
|
|
|
plugin := &Datadog{
|
|
|
|
|
Apikey: "testing",
|
|
|
|
|
URL: "", // No request will be sent because all fields are skipped
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err := plugin.Connect()
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
err = plugin.Write([]telegraf.Metric{
|
|
|
|
|
testutil.MustMetric(
|
|
|
|
|
"cpu",
|
|
|
|
|
map[string]string{},
|
|
|
|
|
map[string]interface{}{
|
|
|
|
|
"time_idle": math.NaN(),
|
|
|
|
|
},
|
|
|
|
|
time.Now()),
|
|
|
|
|
})
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestInfIsSkipped(t *testing.T) {
|
|
|
|
|
plugin := &Datadog{
|
|
|
|
|
Apikey: "testing",
|
|
|
|
|
URL: "", // No request will be sent because all fields are skipped
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err := plugin.Connect()
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
err = plugin.Write([]telegraf.Metric{
|
|
|
|
|
testutil.MustMetric(
|
|
|
|
|
"cpu",
|
|
|
|
|
map[string]string{},
|
|
|
|
|
map[string]interface{}{
|
|
|
|
|
"time_idle": math.Inf(0),
|
|
|
|
|
},
|
|
|
|
|
time.Now()),
|
|
|
|
|
})
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
}
|