Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: properly wrap Datadog API v2 request body (#2771) #2775

Merged
merged 2 commits into from
May 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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