Skip to content

Commit c39e0b3

Browse files
Justin IsoSlavek Kabrda
authored andcommitted
Support interval field on metrics POST (#288)
1 parent da95613 commit c39e0b3

File tree

5 files changed

+144
-6
lines changed

5 files changed

+144
-6
lines changed

datadog-accessors.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13158,6 +13158,37 @@ func (m *Metric) SetHost(v string) {
1315813158
m.Host = &v
1315913159
}
1316013160

13161+
// GetInterval returns the Interval field if non-nil, zero value otherwise.
13162+
func (m *Metric) GetInterval() int {
13163+
if m == nil || m.Interval == nil {
13164+
return 0
13165+
}
13166+
return *m.Interval
13167+
}
13168+
13169+
// GetIntervalOk returns a tuple with the Interval field if it's non-nil, zero value otherwise
13170+
// and a boolean to check if the value has been set.
13171+
func (m *Metric) GetIntervalOk() (int, bool) {
13172+
if m == nil || m.Interval == nil {
13173+
return 0, false
13174+
}
13175+
return *m.Interval, true
13176+
}
13177+
13178+
// HasInterval returns a boolean if a field has been set.
13179+
func (m *Metric) HasInterval() bool {
13180+
if m != nil && m.Interval != nil {
13181+
return true
13182+
}
13183+
13184+
return false
13185+
}
13186+
13187+
// SetInterval allocates a new m.Interval and returns the pointer to it.
13188+
func (m *Metric) SetInterval(v int) {
13189+
m.Interval = &v
13190+
}
13191+
1316113192
// GetMetric returns the Metric field if non-nil, zero value otherwise.
1316213193
func (m *Metric) GetMetric() string {
1316313194
if m == nil || m.Metric == nil {

series.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,13 @@ type DataPoint [2]*float64
2020
// Metric represents a collection of data points that we might send or receive
2121
// on one single metric line.
2222
type Metric struct {
23-
Metric *string `json:"metric,omitempty"`
24-
Points []DataPoint `json:"points,omitempty"`
25-
Type *string `json:"type,omitempty"`
26-
Host *string `json:"host,omitempty"`
27-
Tags []string `json:"tags,omitempty"`
28-
Unit *string `json:"unit,omitempty"`
23+
Metric *string `json:"metric,omitempty"`
24+
Points []DataPoint `json:"points,omitempty"`
25+
Type *string `json:"type,omitempty"`
26+
Host *string `json:"host,omitempty"`
27+
Tags []string `json:"tags,omitempty"`
28+
Unit *string `json:"unit,omitempty"`
29+
Interval *int `json:"interval,omitempty"`
2930
}
3031

3132
// Unit represents a unit definition that we might receive when query for timeseries data.

series_test.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package datadog
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"io/ioutil"
7+
"net/http"
8+
"net/http/httptest"
9+
"strings"
10+
"testing"
11+
12+
"github.com/stretchr/testify/assert"
13+
)
14+
15+
func removeWhitespace(s string) string {
16+
s = strings.Replace(s, " ", "", -1)
17+
s = strings.Replace(s, "\n", "", -1)
18+
return s
19+
}
20+
21+
// TestPostMetrics tests submitting series sends correct
22+
// payloads to the Datadog API for the /v1/series endpoint
23+
func TestPostMetrics(t *testing.T) {
24+
reqs := make(chan string, 1)
25+
26+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
27+
buf := new(bytes.Buffer)
28+
buf.ReadFrom(r.Body)
29+
reqs <- buf.String()
30+
w.WriteHeader(200)
31+
w.Write([]byte("{\"status\": \"ok\"}"))
32+
return
33+
}))
34+
defer ts.Close()
35+
36+
client := Client{
37+
baseUrl: ts.URL,
38+
HttpClient: http.DefaultClient,
39+
}
40+
41+
tcs := []string{
42+
"./tests/fixtures/series/post_series_mixed.json",
43+
"./tests/fixtures/series/post_series_valid.json",
44+
}
45+
46+
for _, tc := range tcs {
47+
b, err := ioutil.ReadFile(tc)
48+
if err != nil {
49+
t.Fatal(err)
50+
}
51+
52+
var post reqPostSeries
53+
json.Unmarshal(b, &post)
54+
if err != nil {
55+
t.Fatal(err)
56+
}
57+
58+
err = client.PostMetrics(post.Series)
59+
if err != nil {
60+
t.Fatal(err)
61+
}
62+
63+
assert.Equal(t, nil, err)
64+
65+
payload := <-reqs
66+
assert.Equal(t, removeWhitespace(string(b)), payload)
67+
}
68+
69+
// Empty slice metrics test case
70+
71+
err := client.PostMetrics([]Metric{})
72+
if err != nil {
73+
t.Fatal(err)
74+
}
75+
76+
payload := <-reqs
77+
assert.Equal(t, "{}", payload)
78+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{"series" :
2+
[
3+
{"metric":"test.metric",
4+
"points":[[1575591864, 20]],
5+
"type":"rate",
6+
"host":"test.example.com",
7+
"tags":["environment:test"],
8+
"interval": 20},
9+
{"metric":"test.metric.duration",
10+
"points":[[1575591810.01, 73.45]],
11+
"type":"gauge"},
12+
{"metric":"test.metric.hits",
13+
"points":[
14+
[1575591810.5, 54],
15+
[1575591899.6, 73],
16+
[1575591810.9, 73]
17+
]}
18+
]
19+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{"series" :
2+
[{"metric":"test.metric",
3+
"points":[[1575591864, 20]],
4+
"type":"rate",
5+
"host":"test.example.com",
6+
"tags":["environment:test"],
7+
"interval": 20}
8+
]
9+
}

0 commit comments

Comments
 (0)