-
Notifications
You must be signed in to change notification settings - Fork 156
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support
interval
field on metrics POST (#288)
- Loading branch information
Showing
5 changed files
with
144 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package datadog | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"io/ioutil" | ||
"net/http" | ||
"net/http/httptest" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func removeWhitespace(s string) string { | ||
s = strings.Replace(s, " ", "", -1) | ||
s = strings.Replace(s, "\n", "", -1) | ||
return s | ||
} | ||
|
||
// TestPostMetrics tests submitting series sends correct | ||
// payloads to the Datadog API for the /v1/series endpoint | ||
func TestPostMetrics(t *testing.T) { | ||
reqs := make(chan string, 1) | ||
|
||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
buf := new(bytes.Buffer) | ||
buf.ReadFrom(r.Body) | ||
reqs <- buf.String() | ||
w.WriteHeader(200) | ||
w.Write([]byte("{\"status\": \"ok\"}")) | ||
return | ||
})) | ||
defer ts.Close() | ||
|
||
client := Client{ | ||
baseUrl: ts.URL, | ||
HttpClient: http.DefaultClient, | ||
} | ||
|
||
tcs := []string{ | ||
"./tests/fixtures/series/post_series_mixed.json", | ||
"./tests/fixtures/series/post_series_valid.json", | ||
} | ||
|
||
for _, tc := range tcs { | ||
b, err := ioutil.ReadFile(tc) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
var post reqPostSeries | ||
json.Unmarshal(b, &post) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
err = client.PostMetrics(post.Series) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
assert.Equal(t, nil, err) | ||
|
||
payload := <-reqs | ||
assert.Equal(t, removeWhitespace(string(b)), payload) | ||
} | ||
|
||
// Empty slice metrics test case | ||
|
||
err := client.PostMetrics([]Metric{}) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
payload := <-reqs | ||
assert.Equal(t, "{}", payload) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{"series" : | ||
[ | ||
{"metric":"test.metric", | ||
"points":[[1575591864, 20]], | ||
"type":"rate", | ||
"host":"test.example.com", | ||
"tags":["environment:test"], | ||
"interval": 20}, | ||
{"metric":"test.metric.duration", | ||
"points":[[1575591810.01, 73.45]], | ||
"type":"gauge"}, | ||
{"metric":"test.metric.hits", | ||
"points":[ | ||
[1575591810.5, 54], | ||
[1575591899.6, 73], | ||
[1575591810.9, 73] | ||
]} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{"series" : | ||
[{"metric":"test.metric", | ||
"points":[[1575591864, 20]], | ||
"type":"rate", | ||
"host":"test.example.com", | ||
"tags":["environment:test"], | ||
"interval": 20} | ||
] | ||
} |