Skip to content

Commit

Permalink
fix: properly wrap Datadog API v2 request body (#2771) (#2775)
Browse files Browse the repository at this point in the history
* Datadog: properly wrap request body

Signed-off-by: Alex Eftimie <[email protected]>

* Use milliseconds in v2 calls to datadog

Signed-off-by: Alex Eftimie <[email protected]>

---------

Signed-off-by: Alex Eftimie <[email protected]>
  • Loading branch information
alexef authored and zachaller committed May 24, 2023
1 parent bb30c9e commit 0322914
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 19 deletions.
27 changes: 16 additions & 11 deletions metricproviders/datadog/datadog.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ type datadogQuery struct {
QueryType string `json:"type"`
}

type datadogRequest struct {
Data datadogQuery `json:"data"`
}

type datadogResponseV1 struct {
Series []struct {
Pointlist [][]float64 `json:"pointlist"`
Expand Down Expand Up @@ -182,17 +186,18 @@ func (p *Provider) createRequest(query string, apiVersion string, now int64, int

return &http.Request{Method: "GET"}, nil
} else if apiVersion == "v2" {
queryBody, err := json.Marshal(datadogQuery{
QueryType: "timeseries_request",
Attributes: datadogQueryAttributes{
From: now - interval,
To: now,
Queries: []map[string]string{{
"data_source": "metrics",
"query": query,
}},
},
})
queryBody, err := json.Marshal(datadogRequest{
Data: datadogQuery{
QueryType: "timeseries_request",
Attributes: datadogQueryAttributes{
From: (now - interval) * 1000,
To: now * 1000,
Queries: []map[string]string{{
"data_source": "metrics",
"query": query,
}},
},
}})
if err != nil {
return nil, fmt.Errorf("Could not parse your JSON request: %v", err)
}
Expand Down
16 changes: 8 additions & 8 deletions metricproviders/datadog/datadogV2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,28 +245,28 @@ func TestRunSuiteV2(t *testing.T) {
t.Errorf("\nreceived no bytes in request: %v", err)
}

var reqBody datadogQuery
var reqBody datadogRequest
err = json.Unmarshal(bodyBytes, &reqBody)
if err != nil {
t.Errorf("\nCould not parse JSON request body: %v", err)
}

actualQuery := reqBody.Attributes.Queries[0]["query"]
actualFrom := reqBody.Attributes.From
actualTo := reqBody.Attributes.To
actualQuery := reqBody.Data.Attributes.Queries[0]["query"]
actualFrom := reqBody.Data.Attributes.From
actualTo := reqBody.Data.Attributes.To

if actualQuery != "avg:kubernetes.cpu.user.total{*}" {
t.Errorf("\nquery expected avg:kubernetes.cpu.user.total{*} but got %s", actualQuery)
}

if actualFrom != unixNow()-test.expectedIntervalSeconds {
t.Errorf("\nfrom %d expected be equal to %d", actualFrom, unixNow()-test.expectedIntervalSeconds)
if actualFrom != (unixNow()-test.expectedIntervalSeconds)*1000 {
t.Errorf("\nfrom %d expected be equal to %d", actualFrom, (unixNow()-test.expectedIntervalSeconds)*1000)
} else if err != nil {
t.Errorf("\nfailed to parse from: %v", err)
}

if actualTo != unixNow() {
t.Errorf("\nto %d was expected be equal to %d", actualTo, unixNow())
if actualTo != unixNow()*1000 {
t.Errorf("\nto %d was expected be equal to %d", actualTo, unixNow()*1000)
} else if err != nil {
t.Errorf("\nfailed to parse to: %v", err)
}
Expand Down

0 comments on commit 0322914

Please sign in to comment.